-
Notifications
You must be signed in to change notification settings - Fork 2
/
metric_sum.go
49 lines (42 loc) · 1.19 KB
/
metric_sum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package generator
import (
"encoding/json"
"fmt"
"time"
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
metricspb "go.opentelemetry.io/proto/otlp/metrics/v1"
)
type SumData struct {
Value int64 `json:"value"`
IsMonotonic bool `json:"isMonotonic"`
AggregationTemporality string `json:"aggregationTemporality"`
}
func parseSumData(rawData map[string]interface{}) (*SumData, error) {
dataBytes, err := json.Marshal(rawData)
if err != nil {
return nil, fmt.Errorf("failed to process provided sum data: %w", err)
}
var data SumData
err = json.Unmarshal(dataBytes, &data)
if err != nil {
return nil, fmt.Errorf("failed to parse JSON sum data: %w", err)
}
return &data, nil
}
func sum(attrs []*commonpb.KeyValue, data *SumData) *metricspb.Metric_Sum {
return &metricspb.Metric_Sum{
Sum: &metricspb.Sum{
IsMonotonic: data.IsMonotonic,
AggregationTemporality: getAggregationTemporality(data.AggregationTemporality),
DataPoints: []*metricspb.NumberDataPoint{
{
Attributes: attrs,
TimeUnixNano: uint64(time.Now().UnixNano()),
Value: &metricspb.NumberDataPoint_AsInt{
AsInt: data.Value,
},
},
},
},
}
}