@@ -5,18 +5,19 @@ import (
5
5
6
6
otlpcommon "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
7
7
otlpmetric "github.com/open-telemetry/opentelemetry-proto/gen/go/metrics/v1"
8
-
9
8
"github.com/tigrannajaryan/telemetry-schema/schema/types"
10
9
)
11
10
12
11
type MetricRenameAction map [types.MetricName ]types.MetricName
13
12
14
- func (act MetricRenameAction ) Apply (metric * otlpmetric.Metric ) error {
15
- newName , exists := act [types .MetricName (metric .MetricDescriptor .Name )]
16
- if exists {
17
- metric .MetricDescriptor .Name = string (newName )
13
+ func (act MetricRenameAction ) Apply (metrics []* otlpmetric.Metric ) ([]* otlpmetric.Metric , error ) {
14
+ for _ , metric := range metrics {
15
+ newName , exists := act [types .MetricName (metric .MetricDescriptor .Name )]
16
+ if exists {
17
+ metric .MetricDescriptor .Name = string (newName )
18
+ }
18
19
}
19
- return nil
20
+ return metrics , nil
20
21
}
21
22
22
23
type MetricLabelRenameAction struct {
@@ -26,27 +27,34 @@ type MetricLabelRenameAction struct {
26
27
LabelMap map [string ]string
27
28
}
28
29
29
- func (act MetricLabelRenameAction ) Apply (metric * otlpmetric.Metric ) error {
30
- if len (act .ApplyOnlyToMetrics ) > 0 {
31
- if _ , exists := act .ApplyOnlyToMetrics [types .MetricName (metric .MetricDescriptor .Name )]; ! exists {
32
- return nil
30
+ func (act MetricLabelRenameAction ) Apply (metrics []* otlpmetric.Metric ) (
31
+ []* otlpmetric.Metric , error ,
32
+ ) {
33
+ var retErr error
34
+ for _ , metric := range metrics {
35
+
36
+ if len (act .ApplyOnlyToMetrics ) > 0 {
37
+ if _ , exists := act .ApplyOnlyToMetrics [types .MetricName (metric .MetricDescriptor .Name )]; ! exists {
38
+ continue
39
+ }
33
40
}
34
- }
35
41
36
- dt := metric .MetricDescriptor .Type
37
- switch dt {
38
- case otlpmetric .MetricDescriptor_INT64 :
39
- dps := metric .Int64DataPoints
40
- for i := 0 ; i < len (dps ); i ++ {
41
- dp := dps [i ]
42
- err := renameLabels (dp .Labels , act .LabelMap )
43
- if err != nil {
44
- return err
42
+ dt := metric .MetricDescriptor .Type
43
+ switch dt {
44
+ case otlpmetric .MetricDescriptor_INT64 :
45
+ dps := metric .Int64DataPoints
46
+ for i := 0 ; i < len (dps ); i ++ {
47
+ dp := dps [i ]
48
+ err := renameLabels (dp .Labels , act .LabelMap )
49
+ if err != nil {
50
+ retErr = err
51
+ }
45
52
}
46
53
}
54
+
47
55
}
48
56
49
- return nil
57
+ return metrics , retErr
50
58
}
51
59
52
60
func renameLabels (labels []* otlpcommon.StringKeyValue , renameRules map [string ]string ) error {
@@ -74,3 +82,63 @@ func renameLabels(labels []*otlpcommon.StringKeyValue, renameRules map[string]st
74
82
}
75
83
return err
76
84
}
85
+
86
+ type MetricSplitAction struct {
87
+ // ApplyOnlyToMetrics limits which metrics this action should apply to. If empty then
88
+ // there is no limitation.
89
+ MetricName types.MetricName
90
+ AttributeName types.AttributeName
91
+ SplitMap map [types.AttributeValue ]types.MetricName
92
+ }
93
+
94
+ func (act MetricSplitAction ) Apply (metrics []* otlpmetric.Metric ) ([]* otlpmetric.Metric , error ) {
95
+ for i := 0 ; i < len (metrics ); i ++ {
96
+ metric := metrics [i ]
97
+ if act .MetricName != types .MetricName (metric .MetricDescriptor .Name ) {
98
+ continue
99
+ }
100
+
101
+ var outputMetrics []* otlpmetric.Metric
102
+ dt := metric .MetricDescriptor .Type
103
+ switch dt {
104
+ case otlpmetric .MetricDescriptor_INT64 :
105
+ dps := metric .Int64DataPoints
106
+ for j := 0 ; j < len (dps ); j ++ {
107
+ dp := dps [j ]
108
+ outputMetric := splitMetric (act .AttributeName , act .SplitMap , metric , dp )
109
+ outputMetrics = append (outputMetrics , outputMetric )
110
+ }
111
+ }
112
+
113
+ metrics = append (append (metrics [0 :i ], outputMetrics ... ), metrics [i + 1 :]... )
114
+ }
115
+
116
+ return metrics , nil
117
+ }
118
+
119
+ func splitMetric (
120
+ splitByAttr types.AttributeName ,
121
+ splitRules map [types.AttributeValue ]types.MetricName ,
122
+ input * otlpmetric.Metric ,
123
+ inputDp * otlpmetric.Int64DataPoint ,
124
+ ) * otlpmetric.Metric {
125
+ output := & otlpmetric.Metric {}
126
+ descr := * input .MetricDescriptor
127
+ output .MetricDescriptor = & descr
128
+
129
+ outputDp := * inputDp
130
+ outputDp .Labels = nil
131
+
132
+ for _ , label := range inputDp .Labels {
133
+ if label .Key == string (splitByAttr ) {
134
+ if convertTo , exists := splitRules [types .AttributeValue (label .Value )]; exists {
135
+ newMetricName := string (convertTo )
136
+ output .MetricDescriptor .Name = newMetricName
137
+ }
138
+ continue
139
+ }
140
+ outputDp .Labels = append (outputDp .Labels , label )
141
+ }
142
+ output .Int64DataPoints = []* otlpmetric.Int64DataPoint {& outputDp }
143
+ return output
144
+ }
0 commit comments