@@ -33,15 +33,14 @@ const (
33
33
scopeInfoMetricName = "otel_scope_info"
34
34
scopeInfoDescription = "Instrumentation Scope metadata"
35
35
36
+ scopeNameLabel = "otel_scope_name"
37
+ scopeVersionLabel = "otel_scope_version"
38
+
36
39
traceIDExemplarKey = "trace_id"
37
40
spanIDExemplarKey = "span_id"
38
41
)
39
42
40
- var (
41
- scopeInfoKeys = [2 ]string {"otel_scope_name" , "otel_scope_version" }
42
-
43
- errScopeInvalid = errors .New ("invalid scope" )
44
- )
43
+ var errScopeInvalid = errors .New ("invalid scope" )
45
44
46
45
// Exporter is a Prometheus Exporter that embeds the OTel metric.Reader
47
46
// interface for easy instantiation with a MeterProvider.
@@ -187,7 +186,11 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
187
186
}
188
187
189
188
for _ , scopeMetrics := range metrics .ScopeMetrics {
190
- var keys , values [2 ]string
189
+ n := len (c .resourceKeyVals .keys ) + 2 // resource attrs + scope name + scope version
190
+ kv := keyVals {
191
+ keys : make ([]string , 0 , n ),
192
+ vals : make ([]string , 0 , n ),
193
+ }
191
194
192
195
if ! c .disableScopeInfo {
193
196
scopeInfo , err := c .scopeInfo (scopeMetrics .Scope )
@@ -202,10 +205,13 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
202
205
203
206
ch <- scopeInfo
204
207
205
- keys = scopeInfoKeys
206
- values = [ 2 ] string { scopeMetrics .Scope .Name , scopeMetrics .Scope .Version }
208
+ kv . keys = append ( kv . keys , scopeNameLabel , scopeVersionLabel )
209
+ kv . vals = append ( kv . vals , scopeMetrics .Scope .Name , scopeMetrics .Scope .Version )
207
210
}
208
211
212
+ kv .keys = append (kv .keys , c .resourceKeyVals .keys ... )
213
+ kv .vals = append (kv .vals , c .resourceKeyVals .vals ... )
214
+
209
215
for _ , m := range scopeMetrics .Metrics {
210
216
typ := c .metricType (m )
211
217
if typ == nil {
@@ -224,25 +230,27 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
224
230
225
231
switch v := m .Data .(type ) {
226
232
case metricdata.Histogram [int64 ]:
227
- addHistogramMetric (ch , v , m , keys , values , name , c . resourceKeyVals )
233
+ addHistogramMetric (ch , v , m , name , kv )
228
234
case metricdata.Histogram [float64 ]:
229
- addHistogramMetric (ch , v , m , keys , values , name , c . resourceKeyVals )
235
+ addHistogramMetric (ch , v , m , name , kv )
230
236
case metricdata.Sum [int64 ]:
231
- addSumMetric (ch , v , m , keys , values , name , c . resourceKeyVals )
237
+ addSumMetric (ch , v , m , name , kv )
232
238
case metricdata.Sum [float64 ]:
233
- addSumMetric (ch , v , m , keys , values , name , c . resourceKeyVals )
239
+ addSumMetric (ch , v , m , name , kv )
234
240
case metricdata.Gauge [int64 ]:
235
- addGaugeMetric (ch , v , m , keys , values , name , c . resourceKeyVals )
241
+ addGaugeMetric (ch , v , m , name , kv )
236
242
case metricdata.Gauge [float64 ]:
237
- addGaugeMetric (ch , v , m , keys , values , name , c . resourceKeyVals )
243
+ addGaugeMetric (ch , v , m , name , kv )
238
244
}
239
245
}
240
246
}
241
247
}
242
248
243
- func addHistogramMetric [N int64 | float64 ](ch chan <- prometheus.Metric , histogram metricdata.Histogram [N ], m metricdata.Metrics , ks , vs [ 2 ] string , name string , resourceKV keyVals ) {
249
+ func addHistogramMetric [N int64 | float64 ](ch chan <- prometheus.Metric , histogram metricdata.Histogram [N ], m metricdata.Metrics , name string , kv keyVals ) {
244
250
for _ , dp := range histogram .DataPoints {
245
- keys , values := getAttrs (dp .Attributes , ks , vs , resourceKV )
251
+ keys , values := getAttrs (dp .Attributes )
252
+ keys = append (keys , kv .keys ... )
253
+ values = append (values , kv .vals ... )
246
254
247
255
desc := prometheus .NewDesc (name , m .Description , keys , nil )
248
256
buckets := make (map [float64 ]uint64 , len (dp .Bounds ))
@@ -262,14 +270,16 @@ func addHistogramMetric[N int64 | float64](ch chan<- prometheus.Metric, histogra
262
270
}
263
271
}
264
272
265
- func addSumMetric [N int64 | float64 ](ch chan <- prometheus.Metric , sum metricdata.Sum [N ], m metricdata.Metrics , ks , vs [ 2 ] string , name string , resourceKV keyVals ) {
273
+ func addSumMetric [N int64 | float64 ](ch chan <- prometheus.Metric , sum metricdata.Sum [N ], m metricdata.Metrics , name string , kv keyVals ) {
266
274
valueType := prometheus .CounterValue
267
275
if ! sum .IsMonotonic {
268
276
valueType = prometheus .GaugeValue
269
277
}
270
278
271
279
for _ , dp := range sum .DataPoints {
272
- keys , values := getAttrs (dp .Attributes , ks , vs , resourceKV )
280
+ keys , values := getAttrs (dp .Attributes )
281
+ keys = append (keys , kv .keys ... )
282
+ values = append (values , kv .vals ... )
273
283
274
284
desc := prometheus .NewDesc (name , m .Description , keys , nil )
275
285
m , err := prometheus .NewConstMetric (desc , valueType , float64 (dp .Value ), values ... )
@@ -286,9 +296,11 @@ func addSumMetric[N int64 | float64](ch chan<- prometheus.Metric, sum metricdata
286
296
}
287
297
}
288
298
289
- func addGaugeMetric [N int64 | float64 ](ch chan <- prometheus.Metric , gauge metricdata.Gauge [N ], m metricdata.Metrics , ks , vs [ 2 ] string , name string , resourceKV keyVals ) {
299
+ func addGaugeMetric [N int64 | float64 ](ch chan <- prometheus.Metric , gauge metricdata.Gauge [N ], m metricdata.Metrics , name string , kv keyVals ) {
290
300
for _ , dp := range gauge .DataPoints {
291
- keys , values := getAttrs (dp .Attributes , ks , vs , resourceKV )
301
+ keys , values := getAttrs (dp .Attributes )
302
+ keys = append (keys , kv .keys ... )
303
+ values = append (values , kv .vals ... )
292
304
293
305
desc := prometheus .NewDesc (name , m .Description , keys , nil )
294
306
m , err := prometheus .NewConstMetric (desc , prometheus .GaugeValue , float64 (dp .Value ), values ... )
@@ -300,9 +312,9 @@ func addGaugeMetric[N int64 | float64](ch chan<- prometheus.Metric, gauge metric
300
312
}
301
313
}
302
314
303
- // getAttrs parses the attribute.Set to two lists of matching Prometheus-style
315
+ // getAttrs converts the attribute.Set to two lists of matching Prometheus-style
304
316
// keys and values.
305
- func getAttrs (attrs attribute.Set , ks , vs [ 2 ] string , resourceKV keyVals ) ([]string , []string ) {
317
+ func getAttrs (attrs attribute.Set ) ([]string , []string ) {
306
318
keys := make ([]string , 0 , attrs .Len ())
307
319
values := make ([]string , 0 , attrs .Len ())
308
320
itr := attrs .Iter ()
@@ -334,29 +346,17 @@ func getAttrs(attrs attribute.Set, ks, vs [2]string, resourceKV keyVals) ([]stri
334
346
values = append (values , strings .Join (vals , ";" ))
335
347
}
336
348
}
337
-
338
- if ks [0 ] != "" {
339
- keys = append (keys , ks [:]... )
340
- values = append (values , vs [:]... )
341
- }
342
-
343
- for idx := range resourceKV .keys {
344
- keys = append (keys , resourceKV .keys [idx ])
345
- values = append (values , resourceKV .vals [idx ])
346
- }
347
-
348
349
return keys , values
349
350
}
350
351
351
352
func createInfoMetric (name , description string , res * resource.Resource ) (prometheus.Metric , error ) {
352
- keys , values := getAttrs (* res .Set (), [ 2 ] string {}, [ 2 ] string {}, keyVals {} )
353
+ keys , values := getAttrs (* res .Set ())
353
354
desc := prometheus .NewDesc (name , description , keys , nil )
354
355
return prometheus .NewConstMetric (desc , prometheus .GaugeValue , float64 (1 ), values ... )
355
356
}
356
357
357
358
func createScopeInfoMetric (scope instrumentation.Scope ) (prometheus.Metric , error ) {
358
- keys := scopeInfoKeys [:]
359
- desc := prometheus .NewDesc (scopeInfoMetricName , scopeInfoDescription , keys , nil )
359
+ desc := prometheus .NewDesc (scopeInfoMetricName , scopeInfoDescription , []string {scopeNameLabel , scopeVersionLabel }, nil )
360
360
return prometheus .NewConstMetric (desc , prometheus .GaugeValue , float64 (1 ), scope .Name , scope .Version )
361
361
}
362
362
@@ -446,7 +446,7 @@ func (c *collector) createResourceAttributes(res *resource.Resource) {
446
446
defer c .mu .Unlock ()
447
447
448
448
resourceAttrs , _ := res .Set ().Filter (c .resourceAttributesFilter )
449
- resourceKeys , resourceValues := getAttrs (resourceAttrs , [ 2 ] string {}, [ 2 ] string {}, keyVals {} )
449
+ resourceKeys , resourceValues := getAttrs (resourceAttrs )
450
450
c .resourceKeyVals = keyVals {keys : resourceKeys , vals : resourceValues }
451
451
}
452
452
0 commit comments