-
Notifications
You must be signed in to change notification settings - Fork 2
/
metric_gauge.go
45 lines (38 loc) · 981 Bytes
/
metric_gauge.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
package generator
import (
"encoding/json"
"fmt"
"time"
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
metricspb "go.opentelemetry.io/proto/otlp/metrics/v1"
)
type GaugeData struct {
Value int64 `json:"value"`
}
func parseGaugeData(rawData map[string]interface{}) (*GaugeData, error) {
dataBytes, err := json.Marshal(rawData)
if err != nil {
return nil, fmt.Errorf("failed to process provided gauge data: %w", err)
}
var data GaugeData
err = json.Unmarshal(dataBytes, &data)
if err != nil {
return nil, fmt.Errorf("failed to parse JSON gauge data: %w", err)
}
return &data, nil
}
func gauge(attrs []*commonpb.KeyValue, data *GaugeData) *metricspb.Metric_Gauge {
return &metricspb.Metric_Gauge{
Gauge: &metricspb.Gauge{
DataPoints: []*metricspb.NumberDataPoint{
{
Attributes: attrs,
TimeUnixNano: uint64(time.Now().UnixNano()),
Value: &metricspb.NumberDataPoint_AsInt{
AsInt: data.Value,
},
},
},
},
}
}