-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
82 lines (66 loc) · 1.91 KB
/
metrics.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package generator
import (
"log/slog"
"strings"
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
metricspb "go.opentelemetry.io/proto/otlp/metrics/v1"
)
type MetricConfig struct {
Type string `js:"type"`
Name string `js:"name"`
Unit string `js:"unit"`
Attributes map[string]interface{} `js:"attributes"`
Data map[string]interface{} `js:"data"`
}
func ResourceMetrics(resourceAttrs []*commonpb.KeyValue, config MetricConfig) *metricspb.ResourceMetrics {
metric := &metricspb.Metric{
Name: config.Name,
Unit: config.Unit,
}
attrs := ToAttributes(config.Attributes)
switch config.Type {
case "gauge":
data, err := parseGaugeData(config.Data)
if err != nil {
slog.Error("Failed to parse gauge data", "error", err)
return nil
}
metric.Data = gauge(attrs, data)
case "sum":
data, err := parseSumData(config.Data)
if err != nil {
slog.Error("Failed to parse sum data", "error", err)
return nil
}
metric.Data = sum(attrs, data)
case "histogram":
data, err := parseHistogramData(config.Data)
if err != nil {
slog.Error("Failed to parse histogram data", "error", err)
return nil
}
metric.Data = histogram(attrs, data)
default:
slog.Error("Unimplemented metric type %q, use one of [gauge, sum]", "type", config.Type)
return nil
}
return &metricspb.ResourceMetrics{
Resource: resource(resourceAttrs),
ScopeMetrics: []*metricspb.ScopeMetrics{
{
Metrics: []*metricspb.Metric{
metric,
},
},
},
}
}
func getAggregationTemporality(temporality string) metricspb.AggregationTemporality {
switch strings.ToLower(temporality) {
case "delta":
return metricspb.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA
case "cumulative":
return metricspb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE
}
return metricspb.AggregationTemporality_AGGREGATION_TEMPORALITY_UNSPECIFIED
}