@@ -21,9 +21,8 @@ import (
21
21
"strings"
22
22
"time"
23
23
24
- metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1 "
24
+ "go.opentelemetry.io/collector/consumer/pdata "
25
25
"go.opentelemetry.io/otel/attribute"
26
- "google.golang.org/protobuf/types/known/timestamppb"
27
26
)
28
27
29
28
var (
@@ -39,8 +38,8 @@ const TagMetricType = "metric_type"
39
38
40
39
// StatsDParser supports the Parse method for parsing StatsD messages with Tags.
41
40
type StatsDParser struct {
42
- gauges map [statsDMetricdescription ]* metricspb. Metric
43
- counters map [statsDMetricdescription ]* metricspb. Metric
41
+ gauges map [statsDMetricdescription ]pdata. InstrumentationLibraryMetrics
42
+ counters map [statsDMetricdescription ]pdata. InstrumentationLibraryMetrics
44
43
enableMetricType bool
45
44
}
46
45
@@ -51,10 +50,9 @@ type statsDMetric struct {
51
50
floatvalue float64
52
51
addition bool
53
52
unit string
54
- metricType metricspb.MetricDescriptor_Type
55
53
sampleRate float64
56
- labelKeys []* metricspb. LabelKey
57
- labelValues []* metricspb. LabelValue
54
+ labelKeys []string
55
+ labelValues []string
58
56
}
59
57
60
58
type statsDMetricdescription struct {
@@ -64,32 +62,34 @@ type statsDMetricdescription struct {
64
62
}
65
63
66
64
func (p * StatsDParser ) Initialize (enableMetricType bool ) error {
67
- p .gauges = make (map [statsDMetricdescription ]* metricspb. Metric )
68
- p .counters = make (map [statsDMetricdescription ]* metricspb. Metric )
65
+ p .gauges = make (map [statsDMetricdescription ]pdata. InstrumentationLibraryMetrics )
66
+ p .counters = make (map [statsDMetricdescription ]pdata. InstrumentationLibraryMetrics )
69
67
p .enableMetricType = enableMetricType
70
68
return nil
71
69
}
72
70
73
71
// get the metrics preparing for flushing and reset the state
74
- func (p * StatsDParser ) GetMetrics () []* metricspb.Metric {
75
- var metrics []* metricspb.Metric
72
+ func (p * StatsDParser ) GetMetrics () pdata.Metrics {
73
+ metrics := pdata .NewMetrics ()
74
+ metrics .ResourceMetrics ().Resize (1 )
75
+ metrics .ResourceMetrics ().At (0 ).InstrumentationLibraryMetrics ().Resize (0 )
76
76
77
77
for _ , metric := range p .gauges {
78
- metrics = append ( metrics , metric )
78
+ metrics . ResourceMetrics (). At ( 0 ). InstrumentationLibraryMetrics (). Append ( metric )
79
79
}
80
80
81
81
for _ , metric := range p .counters {
82
- metrics = append ( metrics , metric )
82
+ metrics . ResourceMetrics (). At ( 0 ). InstrumentationLibraryMetrics (). Append ( metric )
83
83
}
84
84
85
- p .gauges = make (map [statsDMetricdescription ]* metricspb. Metric )
86
- p .counters = make (map [statsDMetricdescription ]* metricspb. Metric )
85
+ p .gauges = make (map [statsDMetricdescription ]pdata. InstrumentationLibraryMetrics )
86
+ p .counters = make (map [statsDMetricdescription ]pdata. InstrumentationLibraryMetrics )
87
87
88
88
return metrics
89
89
}
90
90
91
- var timeNowFunc = func () int64 {
92
- return time .Now (). Unix ()
91
+ var timeNowFunc = func () time. Time {
92
+ return time .Now ()
93
93
}
94
94
95
95
//aggregate for each metric line
@@ -102,30 +102,25 @@ func (p *StatsDParser) Aggregate(line string) error {
102
102
case "g" :
103
103
_ , ok := p .gauges [parsedMetric .description ]
104
104
if ! ok {
105
- metricPoint := buildPoint (parsedMetric )
106
- p .gauges [parsedMetric .description ] = buildMetric (parsedMetric , metricPoint )
105
+ p .gauges [parsedMetric .description ] = buildGaugeMetric (parsedMetric , timeNowFunc ())
107
106
} else {
108
107
if parsedMetric .addition {
109
- savedValue := p .gauges [parsedMetric .description ].GetTimeseries ()[ 0 ]. Points [ 0 ]. GetDoubleValue ()
108
+ savedValue := p .gauges [parsedMetric .description ].Metrics (). At ( 0 ). DoubleGauge (). DataPoints (). At ( 0 ). Value ()
110
109
parsedMetric .floatvalue = parsedMetric .floatvalue + savedValue
111
- metricPoint := buildPoint (parsedMetric )
112
- p .gauges [parsedMetric .description ] = buildMetric (parsedMetric , metricPoint )
110
+ p .gauges [parsedMetric .description ] = buildGaugeMetric (parsedMetric , timeNowFunc ())
113
111
} else {
114
- metricPoint := buildPoint (parsedMetric )
115
- p .gauges [parsedMetric .description ] = buildMetric (parsedMetric , metricPoint )
112
+ p .gauges [parsedMetric .description ] = buildGaugeMetric (parsedMetric , timeNowFunc ())
116
113
}
117
114
}
118
115
119
116
case "c" :
120
117
_ , ok := p .counters [parsedMetric .description ]
121
118
if ! ok {
122
- metricPoint := buildPoint (parsedMetric )
123
- p .counters [parsedMetric .description ] = buildMetric (parsedMetric , metricPoint )
119
+ p .counters [parsedMetric .description ] = buildCounterMetric (parsedMetric , timeNowFunc ())
124
120
} else {
125
- savedValue := p .counters [parsedMetric .description ].GetTimeseries ()[ 0 ]. Points [ 0 ]. GetInt64Value ()
121
+ savedValue := p .counters [parsedMetric .description ].Metrics (). At ( 0 ). IntSum (). DataPoints (). At ( 0 ). Value ()
126
122
parsedMetric .intvalue = parsedMetric .intvalue + savedValue
127
- metricPoint := buildPoint (parsedMetric )
128
- p .counters [parsedMetric .description ] = buildMetric (parsedMetric , metricPoint )
123
+ p .counters [parsedMetric .description ] = buildCounterMetric (parsedMetric , timeNowFunc ())
129
124
}
130
125
}
131
126
@@ -188,11 +183,8 @@ func parseMessageToMetric(line string, enableMetricType bool) (statsDMetric, err
188
183
if len (tagParts ) != 2 {
189
184
return result , fmt .Errorf ("invalid tag format: %s" , tagParts )
190
185
}
191
- result .labelKeys = append (result .labelKeys , & metricspb.LabelKey {Key : tagParts [0 ]})
192
- result .labelValues = append (result .labelValues , & metricspb.LabelValue {
193
- Value : tagParts [1 ],
194
- HasValue : true ,
195
- })
186
+ result .labelKeys = append (result .labelKeys , tagParts [0 ])
187
+ result .labelValues = append (result .labelValues , tagParts [1 ])
196
188
kvs = append (kvs , attribute .String (tagParts [0 ], tagParts [1 ]))
197
189
}
198
190
@@ -207,7 +199,6 @@ func parseMessageToMetric(line string, enableMetricType bool) (statsDMetric, err
207
199
return result , fmt .Errorf ("gauge: parse metric value string: %s" , result .value )
208
200
}
209
201
result .floatvalue = f
210
- result .metricType = metricspb .MetricDescriptor_GAUGE_DOUBLE
211
202
case "c" :
212
203
f , err := strconv .ParseFloat (result .value , 64 )
213
204
if err != nil {
@@ -218,7 +209,6 @@ func parseMessageToMetric(line string, enableMetricType bool) (statsDMetric, err
218
209
i = int64 (f / result .sampleRate )
219
210
}
220
211
result .intvalue = i
221
- result .metricType = metricspb .MetricDescriptor_GAUGE_INT64
222
212
}
223
213
224
214
// add metric_type dimension for all metrics
@@ -231,11 +221,9 @@ func parseMessageToMetric(line string, enableMetricType bool) (statsDMetric, err
231
221
case "c" :
232
222
metricType = "counter"
233
223
}
234
- result .labelKeys = append (result .labelKeys , & metricspb.LabelKey {Key : TagMetricType })
235
- result .labelValues = append (result .labelValues , & metricspb.LabelValue {
236
- Value : metricType ,
237
- HasValue : true ,
238
- })
224
+ result .labelKeys = append (result .labelKeys , TagMetricType )
225
+ result .labelValues = append (result .labelValues , metricType )
226
+
239
227
kvs = append (kvs , attribute .String (TagMetricType , metricType ))
240
228
}
241
229
@@ -255,57 +243,3 @@ func contains(slice []string, element string) bool {
255
243
}
256
244
return false
257
245
}
258
-
259
- func buildMetric (metric statsDMetric , point * metricspb.Point ) * metricspb.Metric {
260
- return & metricspb.Metric {
261
- MetricDescriptor : & metricspb.MetricDescriptor {
262
- Name : metric .description .name ,
263
- Type : metric .metricType ,
264
- LabelKeys : metric .labelKeys ,
265
- Unit : metric .unit ,
266
- },
267
- Timeseries : []* metricspb.TimeSeries {
268
- {
269
- LabelValues : metric .labelValues ,
270
- Points : []* metricspb.Point {
271
- point ,
272
- },
273
- },
274
- },
275
- }
276
- }
277
-
278
- func buildPoint (parsedMetric statsDMetric ) * metricspb.Point {
279
- now := & timestamppb.Timestamp {
280
- Seconds : timeNowFunc (),
281
- }
282
-
283
- switch parsedMetric .description .statsdMetricType {
284
- case "c" :
285
- return buildCounterPoint (parsedMetric , now )
286
- case "g" :
287
- return buildGaugePoint (parsedMetric , now )
288
- }
289
-
290
- return nil
291
- }
292
-
293
- func buildCounterPoint (parsedMetric statsDMetric , now * timestamppb.Timestamp ) * metricspb.Point {
294
- point := & metricspb.Point {
295
- Timestamp : now ,
296
- Value : & metricspb.Point_Int64Value {
297
- Int64Value : parsedMetric .intvalue ,
298
- },
299
- }
300
- return point
301
- }
302
-
303
- func buildGaugePoint (parsedMetric statsDMetric , now * timestamppb.Timestamp ) * metricspb.Point {
304
- point := & metricspb.Point {
305
- Timestamp : now ,
306
- Value : & metricspb.Point_DoubleValue {
307
- DoubleValue : parsedMetric .floatvalue ,
308
- },
309
- }
310
- return point
311
- }
0 commit comments