Debian Codegraph @

exporters/prometheus/exporter.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package prometheus // import "go.opentelemetry.io/otel/exporters/prometheus"
5
6 import (
7 "context"
8 "encoding/hex"
9 "errors"
10 "fmt"
11 "math"
12 "slices"
13 "strings"
14 "sync"
15
16 "github.com/prometheus/client_golang/prometheus"
17 dto "github.com/prometheus/client_model/go"
18 "github.com/prometheus/otlptranslator"
19 "google.golang.org/protobuf/proto"
20
21 "go.opentelemetry.io/otel"
22 "go.opentelemetry.io/otel/attribute"
23 "go.opentelemetry.io/otel/exporters/prometheus/internal/counter"
24 "go.opentelemetry.io/otel/exporters/prometheus/internal/observ"
25 "go.opentelemetry.io/otel/internal/global"
26 "go.opentelemetry.io/otel/sdk/metric"
27 "go.opentelemetry.io/otel/sdk/metric/metricdata"
28 "go.opentelemetry.io/otel/sdk/resource"
29 )
30
31 const (
32 targetInfoDescription = "Target metadata"
33
34 scopeLabelPrefix = "otel_scope_"
35 scopeNameLabel = scopeLabelPrefix + "name"
36 scopeVersionLabel = scopeLabelPrefix + "version"
37 scopeSchemaLabel = scopeLabelPrefix + "schema_url"
38 // metrics from the prometehus bridge are ignored because this produces
39 // errors. Users should directly register prometheus metrics with the
40 // Registerer, rather than round-tripping them through the bridge and
41 // exporter.
42 bridgeScopeName = "go.opentelemetry.io/contrib/bridges/prometheus"
43 )
44
45 var metricsPool = sync.Pool{
46 New: func() any {
47 return &metricdata.ResourceMetrics{}
48 },
49 }
50
51 // Exporter is a Prometheus Exporter that embeds the OTel metric.Reader
52 // interface for easy instantiation with a MeterProvider.
53 type Exporter struct {
54 metric.Reader
55 }
56
57 // MarshalLog returns logging data about the Exporter.
58 func (e *Exporter) MarshalLog() any {
59 const t = "Prometheus exporter"
60
61 if r, ok := e.Reader.(*metric.ManualReader); ok {
62 under := r.MarshalLog()
63 if data, ok := under.(struct {
64 Type string
65 Registered bool
66 Shutdown bool
67 }); ok {
68 data.Type = t
69 return data
70 }
71 }
72
73 return struct{ Type string }{Type: t}
74 }
75
76 var _ metric.Reader = &Exporter{}
77
78 // keyVals is used to store resource attribute key value pairs.
79 type keyVals struct {
80 keys []string
81 vals []string
82 }
83
84 // collector is used to implement prometheus.Collector.
85 type collector struct {
86 reader metric.Reader
87
88 withoutUnits bool
89 withoutCounterSuffixes bool
90 disableScopeInfo bool
91 namespace string
92 resourceAttributesFilter attribute.Filter
93
94 mu sync.Mutex // mu protects all members below from the concurrent access.
95 disableTargetInfo bool
96 targetInfo prometheus.Metric
97 metricFamilies map[string]*dto.MetricFamily
98 resourceKeyVals keyVals
99 metricNamer otlptranslator.MetricNamer
100 labelNamer otlptranslator.LabelNamer
101 unitNamer otlptranslator.UnitNamer
102
103 inst *observ.Instrumentation
104
105 bridgeErrorOnce sync.Once
106 }
107
108 // New returns a Prometheus Exporter.
109 func New(opts ...Option) (*Exporter, error) {
110 cfg := newConfig(opts...)
111
112 // this assumes that the default temporality selector will always return cumulative.
113 // we only support cumulative temporality, so building our own reader enforces this.
114 // TODO (#3244): Enable some way to configure the reader, but not change temporality.
115 reader := metric.NewManualReader(cfg.readerOpts...)
116
117 labelNamer := otlptranslator.LabelNamer{UTF8Allowed: !cfg.translationStrategy.ShouldEscape()}
118 escapedNamespace := cfg.namespace
119 if escapedNamespace != "" {
120 var err error
121 // If the namespace needs to be escaped, do that now when creating the new
122 // Collector object. The escaping is not persisted in the Config itself.
123 escapedNamespace, err = labelNamer.Build(escapedNamespace)
124 if err != nil {
125 return nil, err
126 }
127 }
128
129 collector := &collector{
130 reader: reader,
131 disableTargetInfo: cfg.disableTargetInfo,
132 withoutUnits: cfg.withoutUnits,
133 withoutCounterSuffixes: cfg.withoutCounterSuffixes,
134 disableScopeInfo: cfg.disableScopeInfo,
135 metricFamilies: make(map[string]*dto.MetricFamily),
136 namespace: escapedNamespace,
137 resourceAttributesFilter: cfg.resourceAttributesFilter,
138 metricNamer: otlptranslator.NewMetricNamer(escapedNamespace, cfg.translationStrategy),
139 unitNamer: otlptranslator.UnitNamer{UTF8Allowed: !cfg.translationStrategy.ShouldEscape()},
140 labelNamer: labelNamer,
141 }
142
143 if err := cfg.registerer.Register(collector); err != nil {
144 return nil, fmt.Errorf("cannot register the collector: %w", err)
145 }
146
147 e := &Exporter{
148 Reader: reader,
149 }
150
151 var err error
152 collector.inst, err = observ.NewInstrumentation(counter.NextExporterID())
153
154 return e, err
155 }
156
157 // Describe implements prometheus.Collector.
158 func (*collector) Describe(chan<- *prometheus.Desc) {
159 // The Opentelemetry SDK doesn't have information on which will exist when the collector
160 // is registered. By returning nothing we are an "unchecked" collector in Prometheus,
161 // and assume responsibility for consistency of the metrics produced.
162 //
163 // See https://pkg.go.dev/github.com/prometheus/[email protected]/prometheus#hdr-Custom_Collectors_and_constant_Metrics
164 }
165
166 // Collect implements prometheus.Collector.
167 //
168 // This method is safe to call concurrently.
169 func (c *collector) Collect(ch chan<- prometheus.Metric) {
170 var err error
171 // Blocked by this issue: Propagate context.Context through Gather and Collect (#1538)
172 // https://github.com/prometheus/client_golang/issues/1538.
173 ctx := context.TODO()
174
175 if c.inst != nil {
176 timer := c.inst.RecordOperationDuration(ctx)
177 defer func() { timer.Stop(err) }()
178 }
179
180 metrics := metricsPool.Get().(*metricdata.ResourceMetrics)
181 defer metricsPool.Put(metrics)
182
183 endCollection := func(error) {}
184 if c.inst != nil {
185 endCollection = c.inst.RecordCollectionDuration(ctx).Stop
186 }
187 err = c.reader.Collect(ctx, metrics)
188 endCollection(err)
189
190 if err != nil {
191 if errors.Is(err, metric.ErrReaderShutdown) {
192 return
193 }
194 otel.Handle(err)
195 if errors.Is(err, metric.ErrReaderNotRegistered) {
196 return
197 }
198 }
199
200 global.Debug("Prometheus exporter export", "Data", metrics)
201
202 // Initialize (once) targetInfo and disableTargetInfo.
203 func() {
204 c.mu.Lock()
205 defer c.mu.Unlock()
206
207 if c.targetInfo == nil && !c.disableTargetInfo {
208 targetInfo, e := c.createInfoMetric(
209 otlptranslator.TargetInfoMetricName,
210 targetInfoDescription,
211 metrics.Resource,
212 )
213 if e != nil {
214 // If the target info metric is invalid, disable sending it.
215 c.disableTargetInfo = true
216 otel.Handle(e)
217 err = errors.Join(err, fmt.Errorf("failed to createInfoMetric: %w", e))
218 return
219 }
220
221 c.targetInfo = targetInfo
222 }
223 }()
224
225 if !c.disableTargetInfo {
226 ch <- c.targetInfo
227 }
228
229 if c.resourceAttributesFilter != nil && len(c.resourceKeyVals.keys) == 0 {
230 e := c.createResourceAttributes(metrics.Resource)
231 if e != nil {
232 otel.Handle(e)
233 err = errors.Join(err, fmt.Errorf("failed to createResourceAttributes: %w", e))
234 return
235 }
236 }
237
238 for j, scopeMetrics := range metrics.ScopeMetrics {
239 if scopeMetrics.Scope.Name == bridgeScopeName {
240 c.bridgeErrorOnce.Do(func() {
241 otel.Handle(errBridgeNotSupported)
242 })
243 continue
244 }
245 n := len(c.resourceKeyVals.keys) + 2 // resource attrs + scope name + scope version
246 kv := keyVals{
247 keys: make([]string, 0, n),
248 vals: make([]string, 0, n),
249 }
250
251 if !c.disableScopeInfo {
252 kv.keys = append(kv.keys, scopeNameLabel, scopeVersionLabel, scopeSchemaLabel)
253 kv.vals = append(kv.vals, scopeMetrics.Scope.Name, scopeMetrics.Scope.Version, scopeMetrics.Scope.SchemaURL)
254
255 attrKeys, attrVals, e := getAttrs(scopeMetrics.Scope.Attributes, c.labelNamer)
256 if e != nil {
257 reportError(ch, nil, e)
258 err = errors.Join(err, fmt.Errorf("failed to getAttrs for ScopeMetrics %d: %w", j, e))
259 continue
260 }
261 for i := range attrKeys {
262 attrKeys[i] = scopeLabelPrefix + attrKeys[i]
263 }
264 kv.keys = append(kv.keys, attrKeys...)
265 kv.vals = append(kv.vals, attrVals...)
266 }
267
268 kv.keys = append(kv.keys, c.resourceKeyVals.keys...)
269 kv.vals = append(kv.vals, c.resourceKeyVals.vals...)
270
271 for k, m := range scopeMetrics.Metrics {
272 typ := c.metricType(m)
273 if typ == nil {
274 reportError(ch, nil, errInvalidMetricType)
275 continue
276 }
277 name, e := c.getName(m)
278 if e != nil {
279 reportError(ch, nil, e)
280 err = errors.Join(err, fmt.Errorf("failed to getAttrs for ScopeMetrics %d, Metrics %d: %w", j, k, e))
281 continue
282 }
283
284 drop, help := c.validateMetrics(name, m.Description, typ)
285 if drop {
286 reportError(ch, nil, errInvalidMetric)
287 continue
288 }
289
290 if help != "" {
291 m.Description = help
292 }
293
294 switch v := m.Data.(type) {
295 case metricdata.Histogram[int64]:
296 addHistogramMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
297 case metricdata.Histogram[float64]:
298 addHistogramMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
299 case metricdata.ExponentialHistogram[int64]:
300 addExponentialHistogramMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
301 case metricdata.ExponentialHistogram[float64]:
302 addExponentialHistogramMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
303 case metricdata.Sum[int64]:
304 addSumMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
305 case metricdata.Sum[float64]:
306 addSumMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
307 case metricdata.Gauge[int64]:
308 addGaugeMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
309 case metricdata.Gauge[float64]:
310 addGaugeMetric(ch, v, m, name, kv, c.labelNamer, c.inst, ctx)
311 }
312 }
313 }
314 }
315
316 // downscaleExponentialBucket re-aggregates bucket counts when downscaling to a coarser resolution.
317 func downscaleExponentialBucket(bucket metricdata.ExponentialBucket, scaleDelta int32) metricdata.ExponentialBucket {
318 if len(bucket.Counts) == 0 || scaleDelta < 1 {
319 return metricdata.ExponentialBucket{
320 Offset: bucket.Offset >> scaleDelta,
321 Counts: append([]uint64(nil), bucket.Counts...), // copy slice
322 }
323 }
324
325 // The new offset is scaled down
326 newOffset := bucket.Offset >> scaleDelta
327
328 // Pre-calculate the new bucket count to avoid growing slice
329 // Each group of 2^scaleDelta buckets will merge into one bucket
330 //nolint:gosec // Length is bounded by slice allocation
331 lastBucketIdx := bucket.Offset + int32(len(bucket.Counts)) - 1
332 lastNewIdx := lastBucketIdx >> scaleDelta
333 newBucketCount := int(lastNewIdx - newOffset + 1)
334
335 if newBucketCount <= 0 {
336 return metricdata.ExponentialBucket{
337 Offset: newOffset,
338 Counts: []uint64{},
339 }
340 }
341
342 newCounts := make([]uint64, newBucketCount)
343
344 // Merge buckets according to the scale difference
345 for i, count := range bucket.Counts {
346 if count == 0 {
347 continue
348 }
349
350 // Calculate which new bucket this count belongs to
351 //nolint:gosec // Index is bounded by loop iteration
352 originalIdx := bucket.Offset + int32(i)
353 newIdx := originalIdx >> scaleDelta
354
355 // Calculate the position in the new counts array
356 position := newIdx - newOffset
357 //nolint:gosec // Length is bounded by allocation
358 if position >= 0 && position < int32(len(newCounts)) {
359 newCounts[position] += count
360 }
361 }
362
363 return metricdata.ExponentialBucket{
364 Offset: newOffset,
365 Counts: newCounts,
366 }
367 }
368
369 func addExponentialHistogramMetric[N int64 | float64](
370 ch chan<- prometheus.Metric,
371 histogram metricdata.ExponentialHistogram[N],
372 m metricdata.Metrics,
373 name string,
374 kv keyVals,
375 labelNamer otlptranslator.LabelNamer,
376 inst *observ.Instrumentation,
377 ctx context.Context,
378 ) {
379 var err error
380 var success int64
381 if inst != nil {
382 op := inst.ExportMetrics(ctx, int64(len(histogram.DataPoints)))
383 defer func() { op.End(success, err) }()
384 }
385
386 for j, dp := range histogram.DataPoints {
387 keys, values, e := getAttrs(dp.Attributes, labelNamer)
388 if e != nil {
389 reportError(ch, nil, e)
390 err = errors.Join(err, fmt.Errorf("failed to getAttrs for histogram.DataPoints %d: %w", j, e))
391 continue
392 }
393 keys = append(keys, kv.keys...)
394 values = append(values, kv.vals...)
395
396 desc := prometheus.NewDesc(name, m.Description, keys, nil)
397
398 // Prometheus native histograms support scales in the range [-4, 8]
399 scale := dp.Scale
400 if scale < -4 {
401 // Reject scales below -4 as they cannot be represented in Prometheus
402 reportError(
403 ch,
404 desc,
405 fmt.Errorf("%w: %d (min -4)", errEHScaleBelowMin, scale),
406 )
407 err = errors.Join(err, e)
408 continue
409 }
410
411 // If scale > 8, we need to downscale the buckets to match the clamped scale
412 positiveBucket := dp.PositiveBucket
413 negativeBucket := dp.NegativeBucket
414 if scale > 8 {
415 scaleDelta := scale - 8
416 positiveBucket = downscaleExponentialBucket(dp.PositiveBucket, scaleDelta)
417 negativeBucket = downscaleExponentialBucket(dp.NegativeBucket, scaleDelta)
418 scale = 8
419 }
420
421 // From spec: note that Prometheus Native Histograms buckets are indexed by upper boundary while Exponential Histograms are indexed by lower boundary, the result being that the Offset fields are different-by-one.
422 positiveBuckets := make(map[int]int64)
423 for i, c := range positiveBucket.Counts {
424 if c > math.MaxInt64 {
425 e := fmt.Errorf("positive count %d is too large to be represented as int64", c)
426 otel.Handle(e)
427 err = errors.Join(err, e)
428 continue
429 }
430 positiveBuckets[int(positiveBucket.Offset)+i+1] = int64(c) // nolint: gosec // Size check above.
431 }
432
433 negativeBuckets := make(map[int]int64)
434 for i, c := range negativeBucket.Counts {
435 if c > math.MaxInt64 {
436 e := fmt.Errorf("negative count %d is too large to be represented as int64", c)
437 otel.Handle(e)
438 err = errors.Join(err, e)
439 continue
440 }
441 negativeBuckets[int(negativeBucket.Offset)+i+1] = int64(c) // nolint: gosec // Size check above.
442 }
443
444 m, e := prometheus.NewConstNativeHistogram(
445 desc,
446 dp.Count,
447 float64(dp.Sum),
448 positiveBuckets,
449 negativeBuckets,
450 dp.ZeroCount,
451 scale,
452 dp.ZeroThreshold,
453 dp.StartTime,
454 values...)
455 if e != nil {
456 reportError(ch, desc, e)
457 err = errors.Join(
458 err,
459 fmt.Errorf("failed to NewConstNativeHistogram for histogram.DataPoints %d: %w", j, e),
460 )
461 continue
462 }
463 m = addExemplars(m, dp.Exemplars, labelNamer)
464 ch <- m
465
466 success++
467 }
468 }
469
470 func addHistogramMetric[N int64 | float64](
471 ch chan<- prometheus.Metric,
472 histogram metricdata.Histogram[N],
473 m metricdata.Metrics,
474 name string,
475 kv keyVals,
476 labelNamer otlptranslator.LabelNamer,
477 inst *observ.Instrumentation,
478 ctx context.Context,
479 ) {
480 var err error
481 var success int64
482 if inst != nil {
483 op := inst.ExportMetrics(ctx, int64(len(histogram.DataPoints)))
484 defer func() { op.End(success, err) }()
485 }
486
487 for j, dp := range histogram.DataPoints {
488 keys, values, e := getAttrs(dp.Attributes, labelNamer)
489 if e != nil {
490 reportError(ch, nil, e)
491 err = errors.Join(err, fmt.Errorf("failed to getAttrs for histogram.DataPoints %d: %w", j, e))
492 continue
493 }
494 keys = append(keys, kv.keys...)
495 values = append(values, kv.vals...)
496
497 desc := prometheus.NewDesc(name, m.Description, keys, nil)
498 buckets := make(map[float64]uint64, len(dp.Bounds))
499
500 cumulativeCount := uint64(0)
501 for i, bound := range dp.Bounds {
502 cumulativeCount += dp.BucketCounts[i]
503 buckets[bound] = cumulativeCount
504 }
505 m, e := prometheus.NewConstHistogram(desc, dp.Count, float64(dp.Sum), buckets, values...)
506 if e != nil {
507 reportError(ch, desc, e)
508 err = errors.Join(err, fmt.Errorf("failed to NewConstMetric for histogram.DataPoints %d: %w", j, e))
509 continue
510 }
511 m = addExemplars(m, dp.Exemplars, labelNamer)
512 ch <- m
513
514 success++
515 }
516 }
517
518 func addSumMetric[N int64 | float64](
519 ch chan<- prometheus.Metric,
520 sum metricdata.Sum[N],
521 m metricdata.Metrics,
522 name string,
523 kv keyVals,
524 labelNamer otlptranslator.LabelNamer,
525 inst *observ.Instrumentation,
526 ctx context.Context,
527 ) {
528 var err error
529 var success int64
530 if inst != nil {
531 op := inst.ExportMetrics(ctx, int64(len(sum.DataPoints)))
532 defer func() { op.End(success, err) }()
533 }
534
535 valueType := prometheus.CounterValue
536 if !sum.IsMonotonic {
537 valueType = prometheus.GaugeValue
538 }
539
540 for i, dp := range sum.DataPoints {
541 keys, values, e := getAttrs(dp.Attributes, labelNamer)
542 if e != nil {
543 reportError(ch, nil, e)
544 err = errors.Join(err, fmt.Errorf("failed to getAttrs for sum.DataPoints %d: %w", i, e))
545 continue
546 }
547 keys = append(keys, kv.keys...)
548 values = append(values, kv.vals...)
549
550 desc := prometheus.NewDesc(name, m.Description, keys, nil)
551 m, e := prometheus.NewConstMetric(desc, valueType, float64(dp.Value), values...)
552 if e != nil {
553 reportError(ch, desc, e)
554 err = errors.Join(err, fmt.Errorf("failed to NewConstMetric for sum.DataPoints %d: %w", i, e))
555 continue
556 }
557 // GaugeValues don't support Exemplars at this time
558 // https://github.com/prometheus/client_golang/blob/aef8aedb4b6e1fb8ac1c90790645169125594096/prometheus/metric.go#L199
559 if valueType != prometheus.GaugeValue {
560 m = addExemplars(m, dp.Exemplars, labelNamer)
561 }
562 ch <- m
563
564 success++
565 }
566 }
567
568 func addGaugeMetric[N int64 | float64](
569 ch chan<- prometheus.Metric,
570 gauge metricdata.Gauge[N],
571 m metricdata.Metrics,
572 name string,
573 kv keyVals,
574 labelNamer otlptranslator.LabelNamer,
575 inst *observ.Instrumentation,
576 ctx context.Context,
577 ) {
578 var err error
579 var success int64
580 if inst != nil {
581 op := inst.ExportMetrics(ctx, int64(len(gauge.DataPoints)))
582 defer func() { op.End(success, err) }()
583 }
584
585 for i, dp := range gauge.DataPoints {
586 keys, values, e := getAttrs(dp.Attributes, labelNamer)
587 if e != nil {
588 reportError(ch, nil, e)
589 err = errors.Join(err, fmt.Errorf("failed to getAttrs for gauge.DataPoints %d: %w", i, e))
590 continue
591 }
592 keys = append(keys, kv.keys...)
593 values = append(values, kv.vals...)
594
595 desc := prometheus.NewDesc(name, m.Description, keys, nil)
596 m, e := prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(dp.Value), values...)
597 if e != nil {
598 reportError(ch, desc, e)
599 err = errors.Join(err, fmt.Errorf("failed to NewConstMetric for gauge.DataPoints %d: %w", i, e))
600 continue
601 }
602 ch <- m
603
604 success++
605 }
606 }
607
608 // getAttrs converts the attribute.Set to two lists of matching Prometheus-style
609 // keys and values.
610 func getAttrs(attrs attribute.Set, labelNamer otlptranslator.LabelNamer) ([]string, []string, error) {
611 keys := make([]string, 0, attrs.Len())
612 values := make([]string, 0, attrs.Len())
613 itr := attrs.Iter()
614
615 if labelNamer.UTF8Allowed {
616 // Do not perform sanitization if prometheus supports UTF-8.
617 for itr.Next() {
618 kv := itr.Attribute()
619 keys = append(keys, string(kv.Key))
620 values = append(values, kv.Value.Emit())
621 }
622 } else {
623 // It sanitizes invalid characters and handles duplicate keys
624 // (due to sanitization) by sorting and concatenating the values following the spec.
625 keysMap := make(map[string][]string)
626 for itr.Next() {
627 kv := itr.Attribute()
628 key, err := labelNamer.Build(string(kv.Key))
629 if err != nil {
630 // TODO(#7066) Handle this error better.
631 return nil, nil, err
632 }
633 if _, ok := keysMap[key]; !ok {
634 keysMap[key] = []string{kv.Value.Emit()}
635 } else {
636 // if the sanitized key is a duplicate, append to the list of keys
637 keysMap[key] = append(keysMap[key], kv.Value.Emit())
638 }
639 }
640 for key, vals := range keysMap {
641 keys = append(keys, key)
642 slices.Sort(vals)
643 values = append(values, strings.Join(vals, ";"))
644 }
645 }
646 return keys, values, nil
647 }
648
649 func (c *collector) createInfoMetric(name, description string, res *resource.Resource) (prometheus.Metric, error) {
650 keys, values, err := getAttrs(*res.Set(), c.labelNamer)
651 if err != nil {
652 return nil, err
653 }
654 desc := prometheus.NewDesc(name, description, keys, nil)
655 return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
656 }
657
658 // getName returns the sanitized name, translated according to the selected
659 // TranslationStrategy and namespace option.
660 func (c *collector) getName(m metricdata.Metrics) (string, error) {
661 translatorMetric := otlptranslator.Metric{
662 Name: m.Name,
663 Type: c.namingMetricType(m),
664 }
665 if !c.withoutUnits {
666 translatorMetric.Unit = m.Unit
667 }
668 return c.metricNamer.Build(translatorMetric)
669 }
670
671 func (*collector) metricType(m metricdata.Metrics) *dto.MetricType {
672 switch v := m.Data.(type) {
673 case metricdata.ExponentialHistogram[int64], metricdata.ExponentialHistogram[float64]:
674 return dto.MetricType_HISTOGRAM.Enum()
675 case metricdata.Histogram[int64], metricdata.Histogram[float64]:
676 return dto.MetricType_HISTOGRAM.Enum()
677 case metricdata.Sum[float64]:
678 if v.IsMonotonic {
679 return dto.MetricType_COUNTER.Enum()
680 }
681 return dto.MetricType_GAUGE.Enum()
682 case metricdata.Sum[int64]:
683 if v.IsMonotonic {
684 return dto.MetricType_COUNTER.Enum()
685 }
686 return dto.MetricType_GAUGE.Enum()
687 case metricdata.Gauge[int64], metricdata.Gauge[float64]:
688 return dto.MetricType_GAUGE.Enum()
689 }
690 return nil
691 }
692
693 // namingMetricType provides the metric type for naming purposes.
694 func (c *collector) namingMetricType(m metricdata.Metrics) otlptranslator.MetricType {
695 switch v := m.Data.(type) {
696 case metricdata.ExponentialHistogram[int64], metricdata.ExponentialHistogram[float64]:
697 return otlptranslator.MetricTypeHistogram
698 case metricdata.Histogram[int64], metricdata.Histogram[float64]:
699 return otlptranslator.MetricTypeHistogram
700 case metricdata.Sum[float64]:
701 // If counter suffixes are disabled, treat them like non-monotonic
702 // suffixes for the purposes of naming.
703 if v.IsMonotonic && !c.withoutCounterSuffixes {
704 return otlptranslator.MetricTypeMonotonicCounter
705 }
706 return otlptranslator.MetricTypeNonMonotonicCounter
707 case metricdata.Sum[int64]:
708 // If counter suffixes are disabled, treat them like non-monotonic
709 // suffixes for the purposes of naming.
710 if v.IsMonotonic && !c.withoutCounterSuffixes {
711 return otlptranslator.MetricTypeMonotonicCounter
712 }
713 return otlptranslator.MetricTypeNonMonotonicCounter
714 case metricdata.Gauge[int64], metricdata.Gauge[float64]:
715 return otlptranslator.MetricTypeGauge
716 case metricdata.Summary:
717 return otlptranslator.MetricTypeSummary
718 }
719 return otlptranslator.MetricTypeUnknown
720 }
721
722 func (c *collector) createResourceAttributes(res *resource.Resource) error {
723 c.mu.Lock()
724 defer c.mu.Unlock()
725
726 resourceAttrs, _ := res.Set().Filter(c.resourceAttributesFilter)
727 resourceKeys, resourceValues, err := getAttrs(resourceAttrs, c.labelNamer)
728 if err != nil {
729 return err
730 }
731
732 c.resourceKeyVals = keyVals{keys: resourceKeys, vals: resourceValues}
733 return nil
734 }
735
736 func (c *collector) validateMetrics(name, description string, metricType *dto.MetricType) (drop bool, help string) {
737 c.mu.Lock()
738 defer c.mu.Unlock()
739
740 emf, exist := c.metricFamilies[name]
741
742 if !exist {
743 c.metricFamilies[name] = &dto.MetricFamily{
744 Name: proto.String(name),
745 Help: proto.String(description),
746 Type: metricType,
747 }
748 return false, ""
749 }
750
751 if emf.GetType() != *metricType {
752 global.Error(
753 errors.New("instrument type conflict"),
754 "Using existing type definition.",
755 "instrument", name,
756 "existing", emf.GetType(),
757 "dropped", *metricType,
758 )
759 return true, ""
760 }
761 if emf.GetHelp() != description {
762 global.Info(
763 "Instrument description conflict, using existing",
764 "instrument", name,
765 "existing", emf.GetHelp(),
766 "dropped", description,
767 )
768 return false, emf.GetHelp()
769 }
770
771 return false, ""
772 }
773
774 func addExemplars[N int64 | float64](
775 m prometheus.Metric,
776 exemplars []metricdata.Exemplar[N],
777 labelNamer otlptranslator.LabelNamer,
778 ) prometheus.Metric {
779 if len(exemplars) == 0 {
780 return m
781 }
782 promExemplars := make([]prometheus.Exemplar, len(exemplars))
783 for i, exemplar := range exemplars {
784 labels, err := attributesToLabels(exemplar.FilteredAttributes, labelNamer)
785 if err != nil {
786 otel.Handle(err)
787 return m
788 }
789 // Overwrite any existing trace ID or span ID attributes
790 labels[otlptranslator.ExemplarTraceIDKey] = hex.EncodeToString(exemplar.TraceID)
791 labels[otlptranslator.ExemplarSpanIDKey] = hex.EncodeToString(exemplar.SpanID)
792 promExemplars[i] = prometheus.Exemplar{
793 Value: float64(exemplar.Value),
794 Timestamp: exemplar.Time,
795 Labels: labels,
796 }
797 }
798 metricWithExemplar, err := prometheus.NewMetricWithExemplars(m, promExemplars...)
799 if err != nil {
800 // If there are errors creating the metric with exemplars, just warn
801 // and return the metric without exemplars.
802 otel.Handle(err)
803 return m
804 }
805 return metricWithExemplar
806 }
807
808 func attributesToLabels(attrs []attribute.KeyValue, labelNamer otlptranslator.LabelNamer) (prometheus.Labels, error) {
809 labels := make(map[string]string)
810 for _, attr := range attrs {
811 name, err := labelNamer.Build(string(attr.Key))
812 if err != nil {
813 return nil, err
814 }
815 labels[name] = attr.Value.Emit()
816 }
817 return labels, nil
818 }
819
820 func reportError(ch chan<- prometheus.Metric, desc *prometheus.Desc, err error) {
821 if desc == nil {
822 desc = prometheus.NewInvalidDesc(err)
823 }
824 ch <- prometheus.NewInvalidMetric(desc, err)
825 }