Debian Codegraph @

exporters/prometheus/exporter_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package prometheus
5
6 import (
7 "context"
8 "errors"
9 "fmt"
10 "math"
11 "net/http"
12 "net/http/httptest"
13 "os"
14 "sync"
15 "testing"
16 "time"
17
18 "github.com/prometheus/client_golang/prometheus"
19 "github.com/prometheus/client_golang/prometheus/promhttp"
20 "github.com/prometheus/client_golang/prometheus/testutil"
21 dto "github.com/prometheus/client_model/go"
22 "github.com/prometheus/otlptranslator"
23 "github.com/stretchr/testify/assert"
24 "github.com/stretchr/testify/require"
25
26 "go.opentelemetry.io/otel"
27 "go.opentelemetry.io/otel/attribute"
28 "go.opentelemetry.io/otel/exporters/prometheus/internal/observ"
29 otelmetric "go.opentelemetry.io/otel/metric"
30 "go.opentelemetry.io/otel/sdk/instrumentation"
31 "go.opentelemetry.io/otel/sdk/metric"
32 "go.opentelemetry.io/otel/sdk/metric/metricdata"
33 "go.opentelemetry.io/otel/sdk/resource"
34 semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
35 "go.opentelemetry.io/otel/trace"
36 )
37
38 // producerFunc adapts a function to implement metric.Producer.
39 type producerFunc func(context.Context) ([]metricdata.ScopeMetrics, error)
40
41 func (f producerFunc) Produce(ctx context.Context) ([]metricdata.ScopeMetrics, error) { return f(ctx) }
42
43 // Helper: scrape with ContinueOnError and return body + status.
44 func scrapeWithContinueOnError(ctx context.Context, reg *prometheus.Registry) (int, string) {
45 h := promhttp.HandlerFor(
46 reg,
47 promhttp.HandlerOpts{
48 ErrorHandling: promhttp.ContinueOnError,
49 },
50 )
51
52 rr := httptest.NewRecorder()
53 req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/metrics", http.NoBody)
54 h.ServeHTTP(rr, req)
55
56 return rr.Code, rr.Body.String()
57 }
58
59 func TestPrometheusExporter(t *testing.T) {
60 testCases := []struct {
61 name string
62 emptyResource bool
63 customResourceAttrs []attribute.KeyValue
64 recordMetrics func(ctx context.Context, meter otelmetric.Meter)
65 options []Option
66 expectedFile string
67 checkMetricFamilies func(t testing.TB, dtos []*dto.MetricFamily)
68 }{
69 {
70 name: "counter",
71 expectedFile: "testdata/counter.txt",
72 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
73 opt := otelmetric.WithAttributes(
74 attribute.Key("A").String("B"),
75 attribute.Key("C").String("D"),
76 attribute.Key("E").Bool(true),
77 attribute.Key("F").Int(42),
78 )
79 counter, err := meter.Float64Counter(
80 "foo",
81 otelmetric.WithDescription("a simple counter"),
82 otelmetric.WithUnit("s"),
83 )
84 require.NoError(t, err)
85 counter.Add(ctx, 5, opt)
86 counter.Add(ctx, 10.3, opt)
87 counter.Add(ctx, 9, opt)
88
89 attrs2 := attribute.NewSet(
90 attribute.Key("A").String("D"),
91 attribute.Key("C").String("B"),
92 attribute.Key("E").Bool(true),
93 attribute.Key("F").Int(42),
94 )
95 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
96 },
97 options: []Option{
98 WithNamespace("my.dotted.namespace"),
99 WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes),
100 },
101 },
102 {
103 name: "counter that already has the unit suffix",
104 expectedFile: "testdata/counter_noutf8_with_unit_suffix.txt",
105 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
106 opt := otelmetric.WithAttributes(
107 attribute.Key("A").String("B"),
108 attribute.Key("C").String("D"),
109 attribute.Key("E").Bool(true),
110 attribute.Key("F").Int(42),
111 )
112 counter, err := meter.Float64Counter(
113 "foo.seconds",
114 otelmetric.WithDescription("a simple counter"),
115 otelmetric.WithUnit("s"),
116 )
117 require.NoError(t, err)
118 counter.Add(ctx, 5, opt)
119 counter.Add(ctx, 10.3, opt)
120 counter.Add(ctx, 9, opt)
121
122 attrs2 := attribute.NewSet(
123 attribute.Key("A").String("D"),
124 attribute.Key("C").String("B"),
125 attribute.Key("E").Bool(true),
126 attribute.Key("F").Int(42),
127 )
128 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
129 },
130 options: []Option{WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes)},
131 },
132 {
133 name: "counter with custom unit not tracked by ucum standards",
134 expectedFile: "testdata/counter_with_custom_unit_suffix.txt",
135 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
136 opt := otelmetric.WithAttributes(
137 attribute.Key("A").String("B"),
138 attribute.Key("C").String("D"),
139 attribute.Key("E").Bool(true),
140 attribute.Key("F").Int(42),
141 )
142 counter, err := meter.Float64Counter(
143 "foo.dotted",
144 otelmetric.WithDescription("a simple counter"),
145 otelmetric.WithUnit("madeup"),
146 )
147 require.NoError(t, err)
148 counter.Add(ctx, 5, opt)
149 counter.Add(ctx, 10.3, opt)
150 counter.Add(ctx, 9, opt)
151
152 attrs2 := attribute.NewSet(
153 attribute.Key("A").String("D"),
154 attribute.Key("C").String("B"),
155 attribute.Key("E").Bool(true),
156 attribute.Key("F").Int(42),
157 )
158 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
159 },
160 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
161 },
162 {
163 name: "counter with bracketed unit",
164 expectedFile: "testdata/counter_no_unit.txt",
165 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
166 opt := otelmetric.WithAttributes(
167 attribute.Key("A").String("B"),
168 attribute.Key("C").String("D"),
169 attribute.Key("E").Bool(true),
170 attribute.Key("F").Int(42),
171 )
172 counter, err := meter.Float64Counter(
173 "foo",
174 otelmetric.WithDescription("a simple counter"),
175 otelmetric.WithUnit("{spans}"),
176 )
177 require.NoError(t, err)
178 counter.Add(ctx, 5, opt)
179 counter.Add(ctx, 10.3, opt)
180 counter.Add(ctx, 9, opt)
181
182 attrs2 := attribute.NewSet(
183 attribute.Key("A").String("D"),
184 attribute.Key("C").String("B"),
185 attribute.Key("E").Bool(true),
186 attribute.Key("F").Int(42),
187 )
188 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
189 },
190 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
191 },
192 {
193 name: "counter that already has a total suffix",
194 expectedFile: "testdata/counter.txt",
195 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
196 opt := otelmetric.WithAttributes(
197 attribute.Key("A").String("B"),
198 attribute.Key("C").String("D"),
199 attribute.Key("E").Bool(true),
200 attribute.Key("F").Int(42),
201 )
202 counter, err := meter.Float64Counter(
203 "foo.total",
204 otelmetric.WithDescription("a simple counter"),
205 otelmetric.WithUnit("s"),
206 )
207 require.NoError(t, err)
208 counter.Add(ctx, 5, opt)
209 counter.Add(ctx, 10.3, opt)
210 counter.Add(ctx, 9, opt)
211
212 attrs2 := attribute.NewSet(
213 attribute.Key("A").String("D"),
214 attribute.Key("C").String("B"),
215 attribute.Key("E").Bool(true),
216 attribute.Key("F").Int(42),
217 )
218 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
219 },
220 options: []Option{
221 WithNamespace("my.dotted.namespace"),
222 WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes),
223 },
224 },
225 {
226 name: "counter with suffixes disabled",
227 expectedFile: "testdata/counter_disabled_suffix.txt",
228 options: []Option{
229 WithoutCounterSuffixes(),
230 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
231 },
232 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
233 opt := otelmetric.WithAttributes(
234 attribute.Key("A").String("B"),
235 attribute.Key("C").String("D"),
236 attribute.Key("E").Bool(true),
237 attribute.Key("F").Int(42),
238 )
239 counter, err := meter.Float64Counter(
240 "foo",
241 otelmetric.WithDescription("a simple counter without a total suffix"),
242 otelmetric.WithUnit("s"),
243 )
244 require.NoError(t, err)
245 counter.Add(ctx, 5, opt)
246 counter.Add(ctx, 10.3, opt)
247 counter.Add(ctx, 9, opt)
248
249 attrs2 := attribute.NewSet(
250 attribute.Key("A").String("D"),
251 attribute.Key("C").String("B"),
252 attribute.Key("E").Bool(true),
253 attribute.Key("F").Int(42),
254 )
255 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
256 },
257 },
258 {
259 name: "gauge",
260 expectedFile: "testdata/gauge.txt",
261 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
262 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
263 opt := otelmetric.WithAttributes(
264 attribute.Key("A").String("B"),
265 attribute.Key("C").String("D"),
266 )
267 gauge, err := meter.Float64Gauge(
268 "bar",
269 otelmetric.WithDescription("a fun little gauge"),
270 otelmetric.WithUnit("1"),
271 )
272 require.NoError(t, err)
273 gauge.Record(ctx, .75, opt)
274 },
275 },
276 {
277 name: "exponential histogram",
278 expectedFile: "testdata/exponential_histogram.txt",
279 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
280 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
281 var hist *dto.MetricFamily
282
283 for _, mf := range mfs {
284 if *mf.Name == `exponential_histogram_baz_bytes` {
285 hist = mf
286 break
287 }
288 }
289
290 if hist == nil {
291 t.Fatal("expected to find histogram")
292 }
293
294 m := hist.GetMetric()[0].Histogram
295
296 require.Equal(t, 236.0, *m.SampleSum)
297 require.Equal(t, uint64(4), *m.SampleCount)
298 require.Equal(t, []int64{1, -1, 1, -1, 2}, m.PositiveDelta)
299 require.Equal(t, uint32(5), *m.PositiveSpan[0].Length)
300 require.Equal(t, int32(3), *m.PositiveSpan[0].Offset)
301 },
302 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
303 // NOTE(GiedriusS): there is no text format for exponential (native)
304 // histograms so we don't expect any output.
305 opt := otelmetric.WithAttributes(
306 attribute.Key("A").String("B"),
307 attribute.Key("C").String("D"),
308 )
309 histogram, err := meter.Float64Histogram(
310 "exponential_histogram_baz",
311 otelmetric.WithDescription("a very nice histogram"),
312 otelmetric.WithUnit("By"),
313 )
314 require.NoError(t, err)
315 histogram.Record(ctx, 23, opt)
316 histogram.Record(ctx, 7, opt)
317 histogram.Record(ctx, 101, opt)
318 histogram.Record(ctx, 105, opt)
319 },
320 },
321 {
322 name: "histogram",
323 expectedFile: "testdata/histogram.txt",
324 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
325 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
326 opt := otelmetric.WithAttributes(
327 attribute.Key("A").String("B"),
328 attribute.Key("C").String("D"),
329 )
330 histogram, err := meter.Float64Histogram(
331 "histogram_baz",
332 otelmetric.WithDescription("a very nice histogram"),
333 otelmetric.WithUnit("By"),
334 )
335 require.NoError(t, err)
336 histogram.Record(ctx, 23, opt)
337 histogram.Record(ctx, 7, opt)
338 histogram.Record(ctx, 101, opt)
339 histogram.Record(ctx, 105, opt)
340 },
341 },
342 {
343 name: "sanitized attributes to labels",
344 expectedFile: "testdata/sanitized_labels.txt",
345 options: []Option{
346 WithoutUnits(),
347 WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes),
348 },
349 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
350 opt := otelmetric.WithAttributes(
351 // exact match, value should be overwritten
352 attribute.Key("A.B").String("X"),
353 attribute.Key("A.B").String("Q"),
354
355 // unintended match due to sanitization, values should be concatenated
356 attribute.Key("C.D").String("Y"),
357 attribute.Key("C/D").String("Z"),
358 )
359 counter, err := meter.Float64Counter(
360 "foo",
361 otelmetric.WithDescription("a sanitary counter"),
362 // This unit is not added to
363 otelmetric.WithUnit("By"),
364 )
365 require.NoError(t, err)
366 counter.Add(ctx, 5, opt)
367 counter.Add(ctx, 10.3, opt)
368 counter.Add(ctx, 9, opt)
369 },
370 },
371 {
372 name: "invalid instruments are renamed",
373 expectedFile: "testdata/sanitized_names.txt",
374 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
375 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
376 opt := otelmetric.WithAttributes(
377 attribute.Key("A").String("B"),
378 attribute.Key("C").String("D"),
379 )
380 // Valid.
381 gauge, err := meter.Float64UpDownCounter("bar", otelmetric.WithDescription("a fun little gauge"))
382 require.NoError(t, err)
383 gauge.Add(ctx, 100, opt)
384 gauge.Add(ctx, -25, opt)
385
386 // Invalid, will be renamed.
387 gauge, err = meter.Float64UpDownCounter(
388 "invalid.gauge.name",
389 otelmetric.WithDescription("a gauge with an invalid name"),
390 )
391 require.NoError(t, err)
392 gauge.Add(ctx, 100, opt)
393
394 counter, err := meter.Float64Counter(
395 "0invalid.counter.name",
396 otelmetric.WithDescription("a counter with an invalid name"),
397 )
398 require.ErrorIs(t, err, metric.ErrInstrumentName)
399 counter.Add(ctx, 100, opt)
400
401 histogram, err := meter.Float64Histogram(
402 "invalid.hist.name",
403 otelmetric.WithDescription("a histogram with an invalid name"),
404 )
405 require.NoError(t, err)
406 histogram.Record(ctx, 23, opt)
407 },
408 },
409 {
410 name: "empty resource",
411 emptyResource: true,
412 expectedFile: "testdata/empty_resource.txt",
413 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
414 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
415 opt := otelmetric.WithAttributes(
416 attribute.Key("A").String("B"),
417 attribute.Key("C").String("D"),
418 attribute.Key("E").Bool(true),
419 attribute.Key("F").Int(42),
420 )
421 counter, err := meter.Float64Counter("foo", otelmetric.WithDescription("a simple counter"))
422 require.NoError(t, err)
423 counter.Add(ctx, 5, opt)
424 counter.Add(ctx, 10.3, opt)
425 counter.Add(ctx, 9, opt)
426 },
427 },
428 {
429 name: "custom resource",
430 customResourceAttrs: []attribute.KeyValue{
431 attribute.Key("A").String("B"),
432 attribute.Key("C").String("D"),
433 },
434 expectedFile: "testdata/custom_resource.txt",
435 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
436 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
437 opt := otelmetric.WithAttributes(
438 attribute.Key("A").String("B"),
439 attribute.Key("C").String("D"),
440 attribute.Key("E").Bool(true),
441 attribute.Key("F").Int(42),
442 )
443 counter, err := meter.Float64Counter("foo", otelmetric.WithDescription("a simple counter"))
444 require.NoError(t, err)
445 counter.Add(ctx, 5, opt)
446 counter.Add(ctx, 10.3, opt)
447 counter.Add(ctx, 9, opt)
448 },
449 },
450 {
451 name: "without target_info",
452 options: []Option{
453 WithoutTargetInfo(),
454 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
455 },
456 expectedFile: "testdata/without_target_info.txt",
457 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
458 opt := otelmetric.WithAttributes(
459 attribute.Key("A").String("B"),
460 attribute.Key("C").String("D"),
461 attribute.Key("E").Bool(true),
462 attribute.Key("F").Int(42),
463 )
464 counter, err := meter.Float64Counter("foo", otelmetric.WithDescription("a simple counter"))
465 require.NoError(t, err)
466 counter.Add(ctx, 5, opt)
467 counter.Add(ctx, 10.3, opt)
468 counter.Add(ctx, 9, opt)
469 },
470 },
471 {
472 name: "without scope_info",
473 options: []Option{
474 WithoutScopeInfo(),
475 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
476 },
477 expectedFile: "testdata/without_scope_info.txt",
478 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
479 opt := otelmetric.WithAttributes(
480 attribute.Key("A").String("B"),
481 attribute.Key("C").String("D"),
482 )
483 gauge, err := meter.Int64Gauge(
484 "bar",
485 otelmetric.WithDescription("a fun little gauge"),
486 otelmetric.WithUnit("1"),
487 )
488 require.NoError(t, err)
489 gauge.Record(ctx, 1, opt)
490 },
491 },
492 {
493 name: "without scope_info and target_info",
494 options: []Option{
495 WithoutScopeInfo(),
496 WithoutTargetInfo(),
497 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
498 },
499 expectedFile: "testdata/without_scope_and_target_info.txt",
500 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
501 opt := otelmetric.WithAttributes(
502 attribute.Key("A").String("B"),
503 attribute.Key("C").String("D"),
504 )
505 counter, err := meter.Int64Counter(
506 "bar",
507 otelmetric.WithDescription("a fun little counter"),
508 otelmetric.WithUnit("By"),
509 )
510 require.NoError(t, err)
511 counter.Add(ctx, 2, opt)
512 counter.Add(ctx, 1, opt)
513 },
514 },
515 {
516 name: "with namespace",
517 expectedFile: "testdata/with_namespace.txt",
518 options: []Option{
519 WithNamespace("test"),
520 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
521 },
522 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
523 opt := otelmetric.WithAttributes(
524 attribute.Key("A").String("B"),
525 attribute.Key("C").String("D"),
526 attribute.Key("E").Bool(true),
527 attribute.Key("F").Int(42),
528 )
529 counter, err := meter.Float64Counter("foo", otelmetric.WithDescription("a simple counter"))
530 require.NoError(t, err)
531 counter.Add(ctx, 5, opt)
532 counter.Add(ctx, 10.3, opt)
533 counter.Add(ctx, 9, opt)
534 },
535 },
536 {
537 name: "with resource attributes filter",
538 expectedFile: "testdata/with_resource_attributes_filter.txt",
539 options: []Option{
540 WithResourceAsConstantLabels(attribute.NewDenyKeysFilter()),
541 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
542 },
543 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
544 opt := otelmetric.WithAttributes(
545 attribute.Key("A").String("B"),
546 attribute.Key("C").String("D"),
547 attribute.Key("E").Bool(true),
548 attribute.Key("F").Int(42),
549 )
550 counter, err := meter.Float64Counter("foo", otelmetric.WithDescription("a simple counter"))
551 require.NoError(t, err)
552 counter.Add(ctx, 5, opt)
553 counter.Add(ctx, 10.1, opt)
554 counter.Add(ctx, 9.8, opt)
555 },
556 },
557 {
558 name: "with some resource attributes filter",
559 expectedFile: "testdata/with_allow_resource_attributes_filter.txt",
560 options: []Option{
561 WithResourceAsConstantLabels(attribute.NewAllowKeysFilter("service.name")),
562 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
563 },
564 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
565 opt := otelmetric.WithAttributes(
566 attribute.Key("A").String("B"),
567 attribute.Key("C").String("D"),
568 attribute.Key("E").Bool(true),
569 attribute.Key("F").Int(42),
570 )
571 counter, err := meter.Float64Counter("foo", otelmetric.WithDescription("a simple counter"))
572 require.NoError(t, err)
573 counter.Add(ctx, 5, opt)
574 counter.Add(ctx, 5.9, opt)
575 counter.Add(ctx, 5.3, opt)
576 },
577 },
578 {
579 name: "counter utf-8",
580 expectedFile: "testdata/counter_utf8.txt",
581 options: []Option{
582 WithNamespace("my.dotted.namespace"),
583 WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes),
584 },
585 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
586 opt := otelmetric.WithAttributes(
587 attribute.Key("A.G").String("B"),
588 attribute.Key("C.H").String("D"),
589 attribute.Key("E.I").Bool(true),
590 attribute.Key("F.J").Int(42),
591 )
592 counter, err := meter.Float64Counter(
593 "foo.things",
594 otelmetric.WithDescription("a simple counter"),
595 otelmetric.WithUnit("s"),
596 )
597 require.NoError(t, err)
598 counter.Add(ctx, 5, opt)
599 counter.Add(ctx, 10.3, opt)
600 counter.Add(ctx, 9, opt)
601
602 attrs2 := attribute.NewSet(
603 attribute.Key("A.G").String("D"),
604 attribute.Key("C.H").String("B"),
605 attribute.Key("E.I").Bool(true),
606 attribute.Key("F.J").Int(42),
607 )
608 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
609 },
610 },
611 {
612 name: "counter utf-8 notranslation",
613 expectedFile: "testdata/counter_utf8_notranslation.txt",
614 options: []Option{
615 WithNamespace("my.dotted.namespace"),
616 WithTranslationStrategy(otlptranslator.NoTranslation),
617 },
618 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
619 opt := otelmetric.WithAttributes(
620 attribute.Key("A.G").String("B"),
621 attribute.Key("C.H").String("D"),
622 attribute.Key("E.I").Bool(true),
623 attribute.Key("F.J").Int(42),
624 )
625 counter, err := meter.Float64Counter(
626 "foo.things",
627 otelmetric.WithDescription("a simple counter"),
628 otelmetric.WithUnit("s"),
629 )
630 require.NoError(t, err)
631 counter.Add(ctx, 5, opt)
632 counter.Add(ctx, 10.3, opt)
633 counter.Add(ctx, 9, opt)
634
635 attrs2 := attribute.NewSet(
636 attribute.Key("A.G").String("D"),
637 attribute.Key("C.H").String("B"),
638 attribute.Key("E.I").Bool(true),
639 attribute.Key("F.J").Int(42),
640 )
641 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
642 },
643 },
644 {
645 name: "non-monotonic sum does not add exemplars",
646 expectedFile: "testdata/non_monotonic_sum_does_not_add_exemplars.txt",
647 options: []Option{WithTranslationStrategy(otlptranslator.NoUTF8EscapingWithSuffixes)},
648 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
649 sc := trace.NewSpanContext(trace.SpanContextConfig{
650 SpanID: trace.SpanID{0o1},
651 TraceID: trace.TraceID{0o1},
652 TraceFlags: trace.FlagsSampled,
653 })
654 ctx = trace.ContextWithSpanContext(ctx, sc)
655 opt := otelmetric.WithAttributes(
656 attribute.Key("A").String("B"),
657 attribute.Key("C").String("D"),
658 attribute.Key("E").Bool(true),
659 attribute.Key("F").Int(42),
660 )
661 counter, err := meter.Float64UpDownCounter(
662 "foo",
663 otelmetric.WithDescription("a simple up down counter"),
664 otelmetric.WithUnit("s"),
665 )
666 require.NoError(t, err)
667 counter.Add(ctx, 5, opt)
668 counter.Add(ctx, 10.3, opt)
669 counter.Add(ctx, 9, opt)
670 counter.Add(ctx, -1, opt)
671
672 attrs2 := attribute.NewSet(
673 attribute.Key("A").String("D"),
674 attribute.Key("C").String("B"),
675 attribute.Key("E").Bool(true),
676 attribute.Key("F").Int(42),
677 )
678 counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
679 },
680 },
681 }
682
683 for _, tc := range testCases {
684 t.Run(tc.name, func(t *testing.T) {
685 ctx := t.Context()
686 registry := prometheus.NewRegistry()
687 opts := append(tc.options, WithRegisterer(registry))
688 exporter, err := New(opts...)
689 require.NoError(t, err)
690
691 var res *resource.Resource
692 if tc.emptyResource {
693 res = resource.Empty()
694 } else {
695 res, err = resource.New(ctx,
696 // always specify service.name because the default depends on the running OS
697 resource.WithAttributes(semconv.ServiceName("prometheus_test")),
698 // Overwrite the semconv.TelemetrySDKVersionKey value so we don't need to update every version
699 resource.WithAttributes(semconv.TelemetrySDKVersion("latest")),
700 resource.WithAttributes(tc.customResourceAttrs...),
701 )
702 require.NoError(t, err)
703
704 res, err = resource.Merge(resource.Default(), res)
705 require.NoError(t, err)
706 }
707
708 provider := metric.NewMeterProvider(
709 metric.WithResource(res),
710 metric.WithReader(exporter),
711 metric.WithView(metric.NewView(
712 metric.Instrument{Name: "histogram_*"},
713 metric.Stream{Aggregation: metric.AggregationExplicitBucketHistogram{
714 Boundaries: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 1000},
715 }},
716 ),
717 metric.NewView(
718 metric.Instrument{Name: "exponential_histogram_*"},
719 metric.Stream{Aggregation: metric.AggregationBase2ExponentialHistogram{
720 MaxSize: 10,
721 }},
722 ),
723 ),
724 )
725 meter := provider.Meter(
726 "testmeter",
727 otelmetric.WithInstrumentationVersion("v0.1.0"),
728 otelmetric.WithInstrumentationAttributes(attribute.String("fizz", "buzz")),
729 )
730
731 tc.recordMetrics(ctx, meter)
732
733 file, err := os.Open(tc.expectedFile)
734 require.NoError(t, err)
735 t.Cleanup(func() { require.NoError(t, file.Close()) })
736
737 err = testutil.GatherAndCompare(registry, file)
738 require.NoError(t, err)
739
740 if tc.checkMetricFamilies == nil {
741 return
742 }
743
744 mfs, err := registry.Gather()
745 require.NoError(t, err)
746
747 tc.checkMetricFamilies(t, mfs)
748 })
749 }
750 }
751
752 func TestMultiScopes(t *testing.T) {
753 ctx := t.Context()
754 registry := prometheus.NewRegistry()
755 exporter, err := New(
756 WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes),
757 WithRegisterer(registry),
758 )
759 require.NoError(t, err)
760
761 res, err := resource.New(ctx,
762 // always specify service.name because the default depends on the running OS
763 resource.WithAttributes(semconv.ServiceName("prometheus_test")),
764 // Overwrite the semconv.TelemetrySDKVersionKey value so we don't need to update every version
765 resource.WithAttributes(semconv.TelemetrySDKVersion("latest")),
766 )
767 require.NoError(t, err)
768 res, err = resource.Merge(resource.Default(), res)
769 require.NoError(t, err)
770
771 provider := metric.NewMeterProvider(
772 metric.WithReader(exporter),
773 metric.WithResource(res),
774 )
775
776 fooCounter, err := provider.Meter("meterfoo", otelmetric.WithInstrumentationVersion("v0.1.0")).
777 Int64Counter(
778 "foo",
779 otelmetric.WithUnit("s"),
780 otelmetric.WithDescription("meter foo counter"))
781 assert.NoError(t, err)
782 fooCounter.Add(ctx, 100, otelmetric.WithAttributes(attribute.String("type", "foo")))
783
784 barCounter, err := provider.Meter("meterbar", otelmetric.WithInstrumentationVersion("v0.1.0")).
785 Int64Counter(
786 "bar",
787 otelmetric.WithUnit("s"),
788 otelmetric.WithDescription("meter bar counter"))
789 assert.NoError(t, err)
790 barCounter.Add(ctx, 200, otelmetric.WithAttributes(attribute.String("type", "bar")))
791
792 file, err := os.Open("testdata/multi_scopes.txt")
793 require.NoError(t, err)
794 t.Cleanup(func() { require.NoError(t, file.Close()) })
795
796 err = testutil.GatherAndCompare(registry, file)
797 require.NoError(t, err)
798 }
799
800 func TestBridgeScopeIgnored(t *testing.T) {
801 var handledError error
802 eh := otel.ErrorHandlerFunc(func(e error) { handledError = errors.Join(handledError, e) })
803 otel.SetErrorHandler(eh)
804 ctx := t.Context()
805 registry := prometheus.NewRegistry()
806 exporter, err := New(
807 WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes),
808 WithRegisterer(registry),
809 )
810 require.NoError(t, err)
811
812 res, err := resource.New(ctx,
813 // always specify service.name because the default depends on the running OS
814 resource.WithAttributes(semconv.ServiceName("prometheus_test")),
815 // Overwrite the semconv.TelemetrySDKVersionKey value so we don't need to update every version
816 resource.WithAttributes(semconv.TelemetrySDKVersion("latest")),
817 )
818 require.NoError(t, err)
819 res, err = resource.Merge(resource.Default(), res)
820 require.NoError(t, err)
821
822 provider := metric.NewMeterProvider(
823 metric.WithReader(exporter),
824 metric.WithResource(res),
825 )
826
827 fooCounter, err := provider.Meter(bridgeScopeName, otelmetric.WithInstrumentationVersion("v0.1.0")).
828 Int64Counter(
829 "foo",
830 otelmetric.WithUnit("s"),
831 otelmetric.WithDescription("meter foo counter"))
832 assert.NoError(t, err)
833 fooCounter.Add(ctx, 100, otelmetric.WithAttributes(attribute.String("type", "foo")))
834
835 file, err := os.Open("testdata/just_target_info.txt")
836 require.NoError(t, err)
837 t.Cleanup(func() { require.NoError(t, file.Close()) })
838
839 err = testutil.GatherAndCompare(registry, file)
840 require.NoError(t, err)
841
842 require.ErrorIs(t, handledError, errBridgeNotSupported)
843 }
844
845 func TestDuplicateMetrics(t *testing.T) {
846 ab := attribute.NewSet(attribute.String("A", "B"))
847 withAB := otelmetric.WithAttributeSet(ab)
848 typeBar := attribute.NewSet(attribute.String("type", "bar"))
849 withTypeBar := otelmetric.WithAttributeSet(typeBar)
850 typeFoo := attribute.NewSet(attribute.String("type", "foo"))
851 withTypeFoo := otelmetric.WithAttributeSet(typeFoo)
852 testCases := []struct {
853 name string
854 customResourceAttrs []attribute.KeyValue
855 recordMetrics func(ctx context.Context, meterA, meterB otelmetric.Meter)
856 options []Option
857 possibleExpectedFiles []string
858 expectGatherError bool
859 }{
860 {
861 name: "no_conflict_two_counters",
862 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
863 fooA, err := meterA.Int64Counter("foo",
864 otelmetric.WithUnit("By"),
865 otelmetric.WithDescription("meter counter foo"))
866 assert.NoError(t, err)
867 fooA.Add(ctx, 100, withAB)
868
869 fooB, err := meterB.Int64Counter("foo",
870 otelmetric.WithUnit("By"),
871 otelmetric.WithDescription("meter counter foo"))
872 assert.NoError(t, err)
873 fooB.Add(ctx, 100, withAB)
874 },
875 possibleExpectedFiles: []string{"testdata/no_conflict_two_counters.txt"},
876 },
877 {
878 name: "no_conflict_two_updowncounters",
879 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
880 fooA, err := meterA.Int64UpDownCounter("foo",
881 otelmetric.WithUnit("By"),
882 otelmetric.WithDescription("meter gauge foo"))
883 assert.NoError(t, err)
884 fooA.Add(ctx, 100, withAB)
885
886 fooB, err := meterB.Int64UpDownCounter("foo",
887 otelmetric.WithUnit("By"),
888 otelmetric.WithDescription("meter gauge foo"))
889 assert.NoError(t, err)
890 fooB.Add(ctx, 100, withAB)
891 },
892 possibleExpectedFiles: []string{"testdata/no_conflict_two_updowncounters.txt"},
893 },
894 {
895 name: "no_conflict_two_histograms",
896 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
897 fooA, err := meterA.Int64Histogram("foo",
898 otelmetric.WithUnit("By"),
899 otelmetric.WithDescription("meter histogram foo"))
900 assert.NoError(t, err)
901 fooA.Record(ctx, 100, withAB)
902
903 fooB, err := meterB.Int64Histogram("foo",
904 otelmetric.WithUnit("By"),
905 otelmetric.WithDescription("meter histogram foo"))
906 assert.NoError(t, err)
907 fooB.Record(ctx, 100, withAB)
908 },
909 possibleExpectedFiles: []string{"testdata/no_conflict_two_histograms.txt"},
910 },
911 {
912 name: "conflict_help_two_counters",
913 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
914 barA, err := meterA.Int64Counter("bar",
915 otelmetric.WithUnit("By"),
916 otelmetric.WithDescription("meter a bar"))
917 assert.NoError(t, err)
918 barA.Add(ctx, 100, withTypeBar)
919
920 barB, err := meterB.Int64Counter("bar",
921 otelmetric.WithUnit("By"),
922 otelmetric.WithDescription("meter b bar"))
923 assert.NoError(t, err)
924 barB.Add(ctx, 100, withTypeBar)
925 },
926 possibleExpectedFiles: []string{
927 "testdata/conflict_help_two_counters_1.txt",
928 "testdata/conflict_help_two_counters_2.txt",
929 },
930 },
931 {
932 name: "conflict_help_two_updowncounters",
933 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
934 barA, err := meterA.Int64UpDownCounter("bar",
935 otelmetric.WithUnit("By"),
936 otelmetric.WithDescription("meter a bar"))
937 assert.NoError(t, err)
938 barA.Add(ctx, 100, withTypeBar)
939
940 barB, err := meterB.Int64UpDownCounter("bar",
941 otelmetric.WithUnit("By"),
942 otelmetric.WithDescription("meter b bar"))
943 assert.NoError(t, err)
944 barB.Add(ctx, 100, withTypeBar)
945 },
946 possibleExpectedFiles: []string{
947 "testdata/conflict_help_two_updowncounters_1.txt",
948 "testdata/conflict_help_two_updowncounters_2.txt",
949 },
950 },
951 {
952 name: "conflict_help_two_histograms",
953 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
954 barA, err := meterA.Int64Histogram("bar",
955 otelmetric.WithUnit("By"),
956 otelmetric.WithDescription("meter a bar"))
957 assert.NoError(t, err)
958 barA.Record(ctx, 100, withAB)
959
960 barB, err := meterB.Int64Histogram("bar",
961 otelmetric.WithUnit("By"),
962 otelmetric.WithDescription("meter b bar"))
963 assert.NoError(t, err)
964 barB.Record(ctx, 100, withAB)
965 },
966 possibleExpectedFiles: []string{
967 "testdata/conflict_help_two_histograms_1.txt",
968 "testdata/conflict_help_two_histograms_2.txt",
969 },
970 },
971 {
972 name: "conflict_unit_two_counters",
973 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
974 bazA, err := meterA.Int64Counter("bar",
975 otelmetric.WithUnit("By"),
976 otelmetric.WithDescription("meter bar"))
977 assert.NoError(t, err)
978 bazA.Add(ctx, 100, withTypeBar)
979
980 bazB, err := meterB.Int64Counter("bar",
981 otelmetric.WithUnit("s"),
982 otelmetric.WithDescription("meter bar"))
983 assert.NoError(t, err)
984 bazB.Add(ctx, 100, withTypeBar)
985 },
986 options: []Option{WithoutUnits()},
987 possibleExpectedFiles: []string{"testdata/conflict_unit_two_counters.txt"},
988 },
989 {
990 name: "conflict_unit_two_updowncounters",
991 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
992 barA, err := meterA.Int64UpDownCounter("bar",
993 otelmetric.WithUnit("By"),
994 otelmetric.WithDescription("meter gauge bar"))
995 assert.NoError(t, err)
996 barA.Add(ctx, 100, withTypeBar)
997
998 barB, err := meterB.Int64UpDownCounter("bar",
999 otelmetric.WithUnit("s"),
1000 otelmetric.WithDescription("meter gauge bar"))
1001 assert.NoError(t, err)
1002 barB.Add(ctx, 100, withTypeBar)
1003 },
1004 options: []Option{WithoutUnits()},
1005 possibleExpectedFiles: []string{"testdata/conflict_unit_two_updowncounters.txt"},
1006 },
1007 {
1008 name: "conflict_unit_two_histograms",
1009 recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
1010 barA, err := meterA.Int64Histogram("bar",
1011 otelmetric.WithUnit("By"),
1012 otelmetric.WithDescription("meter histogram bar"))
1013 assert.NoError(t, err)
1014 barA.Record(ctx, 100, withAB)
1015
1016 barB, err := meterB.Int64Histogram("bar",
1017 otelmetric.WithUnit("s"),
1018 otelmetric.WithDescription("meter histogram bar"))
1019 assert.NoError(t, err)
1020 barB.Record(ctx, 100, withAB)
1021 },
1022 options: []Option{WithoutUnits()},
1023 possibleExpectedFiles: []string{"testdata/conflict_unit_two_histograms.txt"},
1024 },
1025 {
1026 name: "conflict_type_counter_and_updowncounter",
1027 recordMetrics: func(ctx context.Context, meterA, _ otelmetric.Meter) {
1028 counter, err := meterA.Int64Counter("foo",
1029 otelmetric.WithUnit("By"),
1030 otelmetric.WithDescription("meter foo"))
1031 assert.NoError(t, err)
1032 counter.Add(ctx, 100, withTypeFoo)
1033
1034 gauge, err := meterA.Int64UpDownCounter("foo_total",
1035 otelmetric.WithUnit("By"),
1036 otelmetric.WithDescription("meter foo"))
1037 assert.NoError(t, err)
1038 gauge.Add(ctx, 200, withTypeFoo)
1039 },
1040 options: []Option{WithoutUnits()},
1041 possibleExpectedFiles: []string{
1042 "testdata/conflict_type_counter_and_updowncounter_1.txt",
1043 "testdata/conflict_type_counter_and_updowncounter_2.txt",
1044 },
1045 expectGatherError: true,
1046 },
1047 {
1048 name: "conflict_type_histogram_and_updowncounter",
1049 recordMetrics: func(ctx context.Context, meterA, _ otelmetric.Meter) {
1050 fooA, err := meterA.Int64UpDownCounter("foo",
1051 otelmetric.WithUnit("By"),
1052 otelmetric.WithDescription("meter gauge foo"))
1053 assert.NoError(t, err)
1054 fooA.Add(ctx, 100, withAB)
1055
1056 fooHistogramA, err := meterA.Int64Histogram("foo",
1057 otelmetric.WithUnit("By"),
1058 otelmetric.WithDescription("meter histogram foo"))
1059 assert.NoError(t, err)
1060 fooHistogramA.Record(ctx, 100, withAB)
1061 },
1062 possibleExpectedFiles: []string{
1063 "testdata/conflict_type_histogram_and_updowncounter_1.txt",
1064 "testdata/conflict_type_histogram_and_updowncounter_2.txt",
1065 },
1066 expectGatherError: true,
1067 },
1068 }
1069
1070 for _, tc := range testCases {
1071 t.Run(tc.name, func(t *testing.T) {
1072 // initialize registry exporter
1073 ctx := t.Context()
1074 registry := prometheus.NewRegistry()
1075 // This test does not set the Translation Strategy, so it defaults to
1076 // UnderscoreEscapingWithSuffixes.
1077 opts := append(
1078 []Option{
1079 WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes),
1080 },
1081 tc.options...,
1082 )
1083 exporter, err := New(append(opts, WithRegisterer(registry))...)
1084 require.NoError(t, err)
1085
1086 // initialize resource
1087 res, err := resource.New(ctx,
1088 resource.WithAttributes(semconv.ServiceName("prometheus_test")),
1089 resource.WithAttributes(semconv.TelemetrySDKVersion("latest")),
1090 )
1091 require.NoError(t, err)
1092 res, err = resource.Merge(resource.Default(), res)
1093 require.NoError(t, err)
1094
1095 // initialize provider
1096 provider := metric.NewMeterProvider(
1097 metric.WithReader(exporter),
1098 metric.WithResource(res),
1099 )
1100
1101 // initialize two meter a, b
1102 meterA := provider.Meter("ma", otelmetric.WithInstrumentationVersion("v0.1.0"))
1103 meterB := provider.Meter("mb", otelmetric.WithInstrumentationVersion("v0.1.0"))
1104
1105 tc.recordMetrics(ctx, meterA, meterB)
1106
1107 if tc.expectGatherError {
1108 // With improved error handling, conflicting instrument types emit an invalid metric.
1109 // Gathering should surface an error instead of silently dropping.
1110 _, err := registry.Gather()
1111 require.Error(t, err)
1112
1113 // 2) Also assert what users will see if they opt into ContinueOnError.
1114 // Compare the HTTP body to an expected file that contains only the valid series
1115 // (e.g., "target_info" and any non-conflicting families).
1116 status, body := scrapeWithContinueOnError(t.Context(), registry)
1117 require.Equal(t, http.StatusOK, status)
1118
1119 matched := false
1120 for _, filename := range tc.possibleExpectedFiles {
1121 want, ferr := os.ReadFile(filename)
1122 require.NoError(t, ferr)
1123 if body == string(want) {
1124 matched = true
1125 break
1126 }
1127 }
1128 require.Truef(t, matched, "expected export not produced under ContinueOnError; got:\n%s", body)
1129 } else {
1130 match := false
1131 for _, filename := range tc.possibleExpectedFiles {
1132 file, ferr := os.Open(filename)
1133 require.NoError(t, ferr)
1134 t.Cleanup(func() { require.NoError(t, file.Close()) })
1135
1136 err = testutil.GatherAndCompare(registry, file)
1137 if err == nil {
1138 match = true
1139 break
1140 }
1141 }
1142 require.Truef(t, match, "expected export not produced: %v", err)
1143 }
1144 })
1145 }
1146 }
1147
1148 func TestCollectorConcurrentSafe(t *testing.T) {
1149 // This tests makes sure that the implemented
1150 // https://pkg.go.dev/github.com/prometheus/client_golang/prometheus#Collector
1151 // is concurrent safe.
1152 ctx := t.Context()
1153 registry := prometheus.NewRegistry()
1154 exporter, err := New(WithRegisterer(registry))
1155 require.NoError(t, err)
1156 provider := metric.NewMeterProvider(metric.WithReader(exporter))
1157 meter := provider.Meter("testmeter")
1158 cnt, err := meter.Int64Counter("foo")
1159 require.NoError(t, err)
1160 cnt.Add(ctx, 100)
1161
1162 var wg sync.WaitGroup
1163 concurrencyLevel := 10
1164 for range concurrencyLevel {
1165 wg.Go(func() {
1166 _, err := registry.Gather() // this calls collector.Collect
1167 assert.NoError(t, err)
1168 })
1169 }
1170
1171 wg.Wait()
1172 }
1173
1174 func TestShutdownExporter(t *testing.T) {
1175 var handledError error
1176 eh := otel.ErrorHandlerFunc(func(e error) { handledError = errors.Join(handledError, e) })
1177 otel.SetErrorHandler(eh)
1178
1179 ctx := t.Context()
1180 registry := prometheus.NewRegistry()
1181
1182 for range 3 {
1183 exporter, err := New(WithRegisterer(registry))
1184 require.NoError(t, err)
1185 provider := metric.NewMeterProvider(
1186 metric.WithResource(resource.Default()),
1187 metric.WithReader(exporter))
1188 meter := provider.Meter("testmeter")
1189 cnt, err := meter.Int64Counter("foo")
1190 require.NoError(t, err)
1191 cnt.Add(ctx, 100)
1192
1193 // verify that metrics added to a previously shutdown MeterProvider
1194 // do not conflict with metrics added in this loop.
1195 _, err = registry.Gather()
1196 require.NoError(t, err)
1197
1198 // Shutdown should cause future prometheus Gather() calls to no longer
1199 // include metrics from this loop's MeterProvider.
1200 err = provider.Shutdown(ctx)
1201 require.NoError(t, err)
1202 }
1203 // ensure we aren't unnecessarily logging errors from the shutdown MeterProvider
1204 require.NoError(t, handledError)
1205 }
1206
1207 func TestExemplars(t *testing.T) {
1208 attrsOpt := otelmetric.WithAttributes(
1209 attribute.Key("A.1").String("B"),
1210 attribute.Key("C.2").String("D"),
1211 attribute.Key("E.3").Bool(true),
1212 attribute.Key("F.4").Int(42),
1213 )
1214 expectedNonEscapedLabels := map[string]string{
1215 otlptranslator.ExemplarTraceIDKey: "01000000000000000000000000000000",
1216 otlptranslator.ExemplarSpanIDKey: "0100000000000000",
1217 "A.1": "B",
1218 "C.2": "D",
1219 "E.3": "true",
1220 "F.4": "42",
1221 }
1222 expectedEscapedLabels := map[string]string{
1223 otlptranslator.ExemplarTraceIDKey: "01000000000000000000000000000000",
1224 otlptranslator.ExemplarSpanIDKey: "0100000000000000",
1225 "A_1": "B",
1226 "C_2": "D",
1227 "E_3": "true",
1228 "F_4": "42",
1229 }
1230 for _, tc := range []struct {
1231 name string
1232 recordMetrics func(ctx context.Context, meter otelmetric.Meter)
1233 expectedExemplarValue float64
1234 expectedLabels map[string]string
1235 strategy otlptranslator.TranslationStrategyOption
1236 }{
1237 {
1238 name: "escaped counter",
1239 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
1240 counter, err := meter.Float64Counter("foo")
1241 require.NoError(t, err)
1242 counter.Add(ctx, 9, attrsOpt)
1243 },
1244 expectedExemplarValue: 9,
1245 expectedLabels: expectedEscapedLabels,
1246 strategy: otlptranslator.UnderscoreEscapingWithSuffixes,
1247 },
1248 {
1249 name: "escaped histogram",
1250 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
1251 hist, err := meter.Int64Histogram("foo")
1252 require.NoError(t, err)
1253 hist.Record(ctx, 9, attrsOpt)
1254 },
1255 expectedExemplarValue: 9,
1256 expectedLabels: expectedEscapedLabels,
1257 strategy: otlptranslator.UnderscoreEscapingWithSuffixes,
1258 },
1259 {
1260 name: "non-escaped counter",
1261 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
1262 counter, err := meter.Float64Counter("foo")
1263 require.NoError(t, err)
1264 counter.Add(ctx, 9, attrsOpt)
1265 },
1266 expectedExemplarValue: 9,
1267 expectedLabels: expectedNonEscapedLabels,
1268 strategy: otlptranslator.NoTranslation,
1269 },
1270 {
1271 name: "non-escaped histogram",
1272 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
1273 hist, err := meter.Int64Histogram("foo")
1274 require.NoError(t, err)
1275 hist.Record(ctx, 9, attrsOpt)
1276 },
1277 expectedExemplarValue: 9,
1278 expectedLabels: expectedNonEscapedLabels,
1279 strategy: otlptranslator.NoTranslation,
1280 },
1281 {
1282 name: "exponential histogram",
1283 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
1284 hist, err := meter.Int64Histogram("exponential_histogram")
1285 require.NoError(t, err)
1286 hist.Record(ctx, 9, attrsOpt)
1287 },
1288 expectedExemplarValue: 9,
1289 expectedLabels: expectedNonEscapedLabels,
1290 strategy: otlptranslator.NoTranslation,
1291 },
1292 } {
1293 t.Run(tc.name, func(t *testing.T) {
1294 // initialize registry exporter
1295 ctx := t.Context()
1296 registry := prometheus.NewRegistry()
1297 exporter, err := New(
1298 WithRegisterer(registry),
1299 WithoutTargetInfo(),
1300 WithoutScopeInfo(),
1301 WithTranslationStrategy(tc.strategy),
1302 )
1303 require.NoError(t, err)
1304
1305 // initialize resource
1306 res, err := resource.New(ctx,
1307 resource.WithAttributes(semconv.ServiceName("prometheus_test")),
1308 resource.WithAttributes(semconv.TelemetrySDKVersion("latest")),
1309 )
1310 require.NoError(t, err)
1311 res, err = resource.Merge(resource.Default(), res)
1312 require.NoError(t, err)
1313
1314 // initialize provider and meter
1315 provider := metric.NewMeterProvider(
1316 metric.WithReader(exporter),
1317 metric.WithResource(res),
1318 metric.WithView(metric.NewView(
1319 metric.Instrument{Name: "foo"},
1320 metric.Stream{
1321 // filter out all attributes so they are added as filtered
1322 // attributes to the exemplar
1323 AttributeFilter: attribute.NewAllowKeysFilter(),
1324 },
1325 ),
1326 ),
1327 metric.WithView(metric.NewView(
1328 metric.Instrument{Name: "exponential_histogram"},
1329 metric.Stream{
1330 Aggregation: metric.AggregationBase2ExponentialHistogram{
1331 MaxSize: 20,
1332 },
1333 AttributeFilter: attribute.NewAllowKeysFilter(),
1334 },
1335 ),
1336 ),
1337 )
1338 meter := provider.Meter("meter", otelmetric.WithInstrumentationVersion("v0.1.0"))
1339
1340 // Add a sampled span context so that measurements get exemplars added
1341 sc := trace.NewSpanContext(trace.SpanContextConfig{
1342 SpanID: trace.SpanID{0o1},
1343 TraceID: trace.TraceID{0o1},
1344 TraceFlags: trace.FlagsSampled,
1345 })
1346 ctx = trace.ContextWithSpanContext(ctx, sc)
1347 // Record a single observation with the exemplar
1348 tc.recordMetrics(ctx, meter)
1349
1350 // Verify that the exemplar is present in the proto version of the
1351 // prometheus metrics.
1352 got, done, err := prometheus.ToTransactionalGatherer(registry).Gather()
1353 defer done()
1354 require.NoError(t, err)
1355
1356 require.Len(t, got, 1)
1357 family := got[0]
1358 require.Len(t, family.GetMetric(), 1)
1359 metric := family.GetMetric()[0]
1360 var exemplar *dto.Exemplar
1361 switch family.GetType() {
1362 case dto.MetricType_COUNTER:
1363 exemplar = metric.GetCounter().GetExemplar()
1364 case dto.MetricType_HISTOGRAM:
1365 h := metric.GetHistogram()
1366 for _, b := range h.GetBucket() {
1367 if b.GetExemplar() != nil {
1368 exemplar = b.GetExemplar()
1369 continue
1370 }
1371 }
1372 if h.GetZeroThreshold() != 0 || h.GetZeroCount() != 0 ||
1373 len(h.PositiveSpan) != 0 || len(h.NegativeSpan) != 0 {
1374 require.NotNil(t, h.Exemplars)
1375 exemplar = h.Exemplars[0]
1376 }
1377 }
1378 require.NotNil(t, exemplar)
1379 require.Equal(t, tc.expectedExemplarValue, exemplar.GetValue())
1380 require.Len(t, exemplar.GetLabel(), len(tc.expectedLabels))
1381
1382 for _, label := range exemplar.GetLabel() {
1383 val, ok := tc.expectedLabels[label.GetName()]
1384 require.True(t, ok)
1385 require.Equal(t, label.GetValue(), val)
1386 }
1387 })
1388 }
1389 }
1390
1391 func TestExponentialHistogramScaleValidation(t *testing.T) {
1392 ctx := t.Context()
1393
1394 t.Run("normal_exponential_histogram_works", func(t *testing.T) {
1395 registry := prometheus.NewRegistry()
1396 exporter, err := New(WithRegisterer(registry), WithoutTargetInfo(), WithoutScopeInfo())
1397 require.NoError(t, err)
1398
1399 provider := metric.NewMeterProvider(
1400 metric.WithReader(exporter),
1401 metric.WithResource(resource.Default()),
1402 )
1403 defer func() {
1404 err := provider.Shutdown(ctx)
1405 require.NoError(t, err)
1406 }()
1407
1408 // Create a histogram with a valid scale
1409 meter := provider.Meter("test")
1410 hist, err := meter.Float64Histogram(
1411 "test_exponential_histogram",
1412 otelmetric.WithDescription("test histogram"),
1413 )
1414 require.NoError(t, err)
1415 hist.Record(ctx, 1.0)
1416 hist.Record(ctx, 10.0)
1417 hist.Record(ctx, 100.0)
1418
1419 metricFamilies, err := registry.Gather()
1420 require.NoError(t, err)
1421 assert.NotEmpty(t, metricFamilies)
1422 })
1423
1424 t.Run("error_handling_for_invalid_scales", func(t *testing.T) {
1425 var capturedError error
1426 originalHandler := otel.GetErrorHandler()
1427 otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
1428 capturedError = err
1429 }))
1430 defer otel.SetErrorHandler(originalHandler)
1431
1432 now := time.Now()
1433 invalidScaleData := metricdata.ExponentialHistogramDataPoint[float64]{
1434 Attributes: attribute.NewSet(),
1435 StartTime: now,
1436 Time: now,
1437 Count: 1,
1438 Sum: 10.0,
1439 Scale: -5, // Invalid scale below -4
1440 ZeroCount: 0,
1441 ZeroThreshold: 0.0,
1442 PositiveBucket: metricdata.ExponentialBucket{
1443 Offset: 1,
1444 Counts: []uint64{1},
1445 },
1446 NegativeBucket: metricdata.ExponentialBucket{
1447 Offset: 1,
1448 Counts: []uint64{},
1449 },
1450 }
1451
1452 ch := make(chan prometheus.Metric, 10)
1453 defer close(ch)
1454
1455 histogram := metricdata.ExponentialHistogram[float64]{
1456 Temporality: metricdata.CumulativeTemporality,
1457 DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{invalidScaleData},
1458 }
1459
1460 m := metricdata.Metrics{
1461 Name: "test_histogram",
1462 Description: "test",
1463 }
1464
1465 addExponentialHistogramMetric(
1466 ch,
1467 histogram,
1468 m,
1469 "test_histogram",
1470 keyVals{},
1471 otlptranslator.LabelNamer{},
1472 nil,
1473 t.Context(),
1474 )
1475 // Expect an invalid metric to be sent that carries the scale error.
1476 var pm prometheus.Metric
1477 select {
1478 case pm = <-ch:
1479 default:
1480 t.Fatalf("expected an invalid metric to be emitted for invalid scale, but channel was empty")
1481 }
1482 var dtoMetric dto.Metric
1483 werr := pm.Write(&dtoMetric)
1484 require.ErrorIs(t, werr, errEHScaleBelowMin)
1485 // The exporter reports via invalid metric, not the global otel error handler.
1486 assert.NoError(t, capturedError)
1487 })
1488 }
1489
1490 func TestDownscaleExponentialBucket(t *testing.T) {
1491 tests := []struct {
1492 name string
1493 bucket metricdata.ExponentialBucket
1494 scaleDelta int32
1495 want metricdata.ExponentialBucket
1496 }{
1497 {
1498 name: "Empty bucket",
1499 bucket: metricdata.ExponentialBucket{},
1500 scaleDelta: 3,
1501 want: metricdata.ExponentialBucket{},
1502 },
1503 {
1504 name: "1 size bucket",
1505 bucket: metricdata.ExponentialBucket{
1506 Offset: 50,
1507 Counts: []uint64{7},
1508 },
1509 scaleDelta: 4,
1510 want: metricdata.ExponentialBucket{
1511 Offset: 3,
1512 Counts: []uint64{7},
1513 },
1514 },
1515 {
1516 name: "zero scale delta",
1517 bucket: metricdata.ExponentialBucket{
1518 Offset: 50,
1519 Counts: []uint64{7, 5},
1520 },
1521 scaleDelta: 0,
1522 want: metricdata.ExponentialBucket{
1523 Offset: 50,
1524 Counts: []uint64{7, 5},
1525 },
1526 },
1527 {
1528 name: "aligned bucket scale 1",
1529 bucket: metricdata.ExponentialBucket{
1530 Offset: 0,
1531 Counts: []uint64{1, 2, 3, 4, 5, 6},
1532 },
1533 scaleDelta: 1,
1534 want: metricdata.ExponentialBucket{
1535 Offset: 0,
1536 Counts: []uint64{3, 7, 11},
1537 },
1538 },
1539 {
1540 name: "aligned bucket scale 2",
1541 bucket: metricdata.ExponentialBucket{
1542 Offset: 0,
1543 Counts: []uint64{1, 2, 3, 4, 5, 6},
1544 },
1545 scaleDelta: 2,
1546 want: metricdata.ExponentialBucket{
1547 Offset: 0,
1548 Counts: []uint64{10, 11},
1549 },
1550 },
1551 {
1552 name: "unaligned bucket scale 1",
1553 bucket: metricdata.ExponentialBucket{
1554 Offset: 5,
1555 Counts: []uint64{1, 2, 3, 4, 5, 6},
1556 }, // This is equivalent to [0,0,0,0,0,1,2,3,4,5,6]
1557 scaleDelta: 1,
1558 want: metricdata.ExponentialBucket{
1559 Offset: 2,
1560 Counts: []uint64{1, 5, 9, 6},
1561 }, // This is equivalent to [0,0,1,5,9,6]
1562 },
1563 {
1564 name: "negative startBin",
1565 bucket: metricdata.ExponentialBucket{
1566 Offset: -1,
1567 Counts: []uint64{1, 0, 3},
1568 },
1569 scaleDelta: 1,
1570 want: metricdata.ExponentialBucket{
1571 Offset: -1,
1572 Counts: []uint64{1, 3},
1573 },
1574 },
1575 {
1576 name: "negative startBin 2",
1577 bucket: metricdata.ExponentialBucket{
1578 Offset: -4,
1579 Counts: []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
1580 },
1581 scaleDelta: 1,
1582 want: metricdata.ExponentialBucket{
1583 Offset: -2,
1584 Counts: []uint64{3, 7, 11, 15, 19},
1585 },
1586 },
1587 }
1588 for _, tt := range tests {
1589 t.Run(tt.name, func(t *testing.T) {
1590 got := downscaleExponentialBucket(tt.bucket, tt.scaleDelta)
1591 assert.Equal(t, tt.want, got)
1592 })
1593 }
1594 }
1595
1596 func TestExponentialHistogramHighScaleDownscaling(t *testing.T) {
1597 t.Run("scale_10_downscales_to_8", func(t *testing.T) {
1598 // Test that scale 10 gets properly downscaled to 8 with correct bucket re-aggregation
1599 ch := make(chan prometheus.Metric, 10)
1600 defer close(ch)
1601
1602 now := time.Now()
1603
1604 // Create an exponential histogram data point with scale 10
1605 dataPoint := metricdata.ExponentialHistogramDataPoint[float64]{
1606 Attributes: attribute.NewSet(),
1607 StartTime: now,
1608 Time: now,
1609 Count: 8,
1610 Sum: 55.0,
1611 Scale: 10, // This should be downscaled to 8
1612 ZeroCount: 0,
1613 ZeroThreshold: 0.0,
1614 PositiveBucket: metricdata.ExponentialBucket{
1615 Offset: 0,
1616 Counts: []uint64{1, 1, 1, 1, 1, 1, 1, 1}, // 8 buckets with 1 count each
1617 },
1618 NegativeBucket: metricdata.ExponentialBucket{
1619 Offset: 0,
1620 Counts: []uint64{},
1621 },
1622 }
1623
1624 histogram := metricdata.ExponentialHistogram[float64]{
1625 Temporality: metricdata.CumulativeTemporality,
1626 DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{dataPoint},
1627 }
1628
1629 m := metricdata.Metrics{
1630 Name: "test_high_scale_histogram",
1631 Description: "test histogram with high scale",
1632 }
1633
1634 // This should not produce any errors and should properly downscale buckets
1635 addExponentialHistogramMetric(
1636 ch,
1637 histogram,
1638 m,
1639 "test_high_scale_histogram",
1640 keyVals{},
1641 otlptranslator.LabelNamer{},
1642 nil,
1643 t.Context(),
1644 )
1645
1646 // Verify a metric was produced
1647 select {
1648 case metric := <-ch:
1649 // Check that the metric was created successfully
1650 require.NotNil(t, metric)
1651
1652 // The scale should have been clamped to 8, and buckets should be re-aggregated
1653 // With scale 10 -> 8, we have a scaleDelta of 2, meaning 2^2 = 4 buckets merge into 1
1654 // Original: 8 buckets with 1 count each at scale 10
1655 // After downscaling: 2 buckets with 4 counts each at scale 8
1656 default:
1657 t.Error("Expected a metric to be produced")
1658 }
1659 })
1660
1661 t.Run("scale_12_downscales_to_8", func(t *testing.T) {
1662 // Test that scale 12 gets properly downscaled to 8 with correct bucket re-aggregation
1663 ch := make(chan prometheus.Metric, 10)
1664 defer close(ch)
1665
1666 now := time.Now()
1667
1668 // Create an exponential histogram data point with scale 12
1669 dataPoint := metricdata.ExponentialHistogramDataPoint[float64]{
1670 Attributes: attribute.NewSet(),
1671 StartTime: now,
1672 Time: now,
1673 Count: 16,
1674 Sum: 120.0,
1675 Scale: 12, // This should be downscaled to 8
1676 ZeroCount: 0,
1677 ZeroThreshold: 0.0,
1678 PositiveBucket: metricdata.ExponentialBucket{
1679 Offset: 0,
1680 Counts: []uint64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 16 buckets with 1 count each
1681 },
1682 NegativeBucket: metricdata.ExponentialBucket{
1683 Offset: 0,
1684 Counts: []uint64{},
1685 },
1686 }
1687
1688 histogram := metricdata.ExponentialHistogram[float64]{
1689 Temporality: metricdata.CumulativeTemporality,
1690 DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{dataPoint},
1691 }
1692
1693 m := metricdata.Metrics{
1694 Name: "test_very_high_scale_histogram",
1695 Description: "test histogram with very high scale",
1696 }
1697
1698 // This should not produce any errors and should properly downscale buckets
1699 addExponentialHistogramMetric(
1700 ch,
1701 histogram,
1702 m,
1703 "test_very_high_scale_histogram",
1704 keyVals{},
1705 otlptranslator.LabelNamer{},
1706 nil,
1707 t.Context(),
1708 )
1709
1710 // Verify a metric was produced
1711 select {
1712 case metric := <-ch:
1713 // Check that the metric was created successfully
1714 require.NotNil(t, metric)
1715
1716 // The scale should have been clamped to 8, and buckets should be re-aggregated
1717 // With scale 12 -> 8, we have a scaleDelta of 4, meaning 2^4 = 16 buckets merge into 1
1718 // Original: 16 buckets with 1 count each at scale 12
1719 // After downscaling: 1 bucket with 16 counts at scale 8
1720 default:
1721 t.Error("Expected a metric to be produced")
1722 }
1723 })
1724
1725 t.Run("exponential_histogram_with_negative_buckets", func(t *testing.T) {
1726 // Test that exponential histograms with negative buckets are handled correctly
1727 ch := make(chan prometheus.Metric, 10)
1728 defer close(ch)
1729
1730 now := time.Now()
1731
1732 // Create an exponential histogram data point with both positive and negative buckets
1733 dataPoint := metricdata.ExponentialHistogramDataPoint[float64]{
1734 Attributes: attribute.NewSet(),
1735 StartTime: now,
1736 Time: now,
1737 Count: 6,
1738 Sum: 25.0,
1739 Scale: 2,
1740 ZeroCount: 0,
1741 ZeroThreshold: 0.0,
1742 PositiveBucket: metricdata.ExponentialBucket{
1743 Offset: 1,
1744 Counts: []uint64{1, 2}, // 2 positive buckets
1745 },
1746 NegativeBucket: metricdata.ExponentialBucket{
1747 Offset: 1,
1748 Counts: []uint64{2, 1}, // 2 negative buckets
1749 },
1750 }
1751
1752 histogram := metricdata.ExponentialHistogram[float64]{
1753 Temporality: metricdata.CumulativeTemporality,
1754 DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{dataPoint},
1755 }
1756
1757 m := metricdata.Metrics{
1758 Name: "test_histogram_with_negative_buckets",
1759 Description: "test histogram with negative buckets",
1760 }
1761
1762 // This should handle negative buckets correctly
1763 addExponentialHistogramMetric(
1764 ch,
1765 histogram,
1766 m,
1767 "test_histogram_with_negative_buckets",
1768 keyVals{},
1769 otlptranslator.LabelNamer{},
1770 nil,
1771 t.Context(),
1772 )
1773
1774 // Verify a metric was produced
1775 select {
1776 case metric := <-ch:
1777 require.NotNil(t, metric)
1778 default:
1779 t.Error("Expected a metric to be produced")
1780 }
1781 })
1782
1783 t.Run("exponential_histogram_int64_type", func(t *testing.T) {
1784 // Test that int64 exponential histograms are handled correctly
1785 ch := make(chan prometheus.Metric, 10)
1786 defer close(ch)
1787
1788 now := time.Now()
1789
1790 // Create an exponential histogram data point with int64 type
1791 dataPoint := metricdata.ExponentialHistogramDataPoint[int64]{
1792 Attributes: attribute.NewSet(),
1793 StartTime: now,
1794 Time: now,
1795 Count: 4,
1796 Sum: 20,
1797 Scale: 3,
1798 ZeroCount: 0,
1799 ZeroThreshold: 0.0,
1800 PositiveBucket: metricdata.ExponentialBucket{
1801 Offset: 0,
1802 Counts: []uint64{1, 1, 1, 1}, // 4 buckets with 1 count each
1803 },
1804 NegativeBucket: metricdata.ExponentialBucket{
1805 Offset: 0,
1806 Counts: []uint64{},
1807 },
1808 }
1809
1810 histogram := metricdata.ExponentialHistogram[int64]{
1811 Temporality: metricdata.CumulativeTemporality,
1812 DataPoints: []metricdata.ExponentialHistogramDataPoint[int64]{dataPoint},
1813 }
1814
1815 m := metricdata.Metrics{
1816 Name: "test_int64_exponential_histogram",
1817 Description: "test int64 exponential histogram",
1818 }
1819
1820 // This should handle int64 exponential histograms correctly
1821 addExponentialHistogramMetric(
1822 ch,
1823 histogram,
1824 m,
1825 "test_int64_exponential_histogram",
1826 keyVals{},
1827 otlptranslator.LabelNamer{},
1828 nil,
1829 t.Context(),
1830 )
1831
1832 // Verify a metric was produced
1833 select {
1834 case metric := <-ch:
1835 require.NotNil(t, metric)
1836 default:
1837 t.Error("Expected a metric to be produced")
1838 }
1839 })
1840 }
1841
1842 func TestDownscaleExponentialBucketEdgeCases(t *testing.T) {
1843 t.Run("min_idx_larger_than_current", func(t *testing.T) {
1844 // Test case where we find a minIdx that's smaller than the current
1845 bucket := metricdata.ExponentialBucket{
1846 Offset: 10, // Start at offset 10
1847 Counts: []uint64{1, 0, 0, 0, 1},
1848 }
1849
1850 // Scale delta of 3 will cause downscaling: original indices 10->1, 14->1
1851 result := downscaleExponentialBucket(bucket, 3)
1852
1853 // Both original buckets 10 and 14 should map to the same downscaled bucket at index 1
1854 expected := metricdata.ExponentialBucket{
1855 Offset: 1,
1856 Counts: []uint64{2}, // Both counts combined
1857 }
1858
1859 assert.Equal(t, expected, result)
1860 })
1861
1862 t.Run("empty_downscaled_counts", func(t *testing.T) {
1863 // Create a scenario that results in empty downscaled counts
1864 bucket := metricdata.ExponentialBucket{
1865 Offset: math.MaxInt32 - 5, // Very large offset that won't cause overflow in this case
1866 Counts: []uint64{1, 1, 1, 1, 1},
1867 }
1868
1869 // This should work normally and downscale the buckets
1870 result := downscaleExponentialBucket(bucket, 1)
1871
1872 // Should return bucket with downscaled values
1873 expected := metricdata.ExponentialBucket{
1874 Offset: 1073741821, // ((MaxInt32-5) + 0) >> 1 = 1073741821
1875 Counts: []uint64{2, 2, 1}, // Buckets get combined during downscaling
1876 }
1877
1878 assert.Equal(t, expected, result)
1879 })
1880 }
1881
1882 // TestEscapingErrorHandling increases test coverage by exercising some error
1883 // conditions.
1884 func TestEscapingErrorHandling(t *testing.T) {
1885 // Helper to create a producer that emits a Summary (unsupported) metric.
1886 makeSummaryProducer := func() metric.Producer {
1887 return producerFunc(func(_ context.Context) ([]metricdata.ScopeMetrics, error) {
1888 return []metricdata.ScopeMetrics{
1889 {
1890 Metrics: []metricdata.Metrics{
1891 {
1892 Name: "summary_metric",
1893 Description: "unsupported summary",
1894 Data: metricdata.Summary{},
1895 },
1896 },
1897 },
1898 }, nil
1899 })
1900 }
1901 // Helper to create a producer that emits a metric with an invalid name, to
1902 // force getName() to fail and exercise reportError at that branch.
1903 makeBadNameProducer := func() metric.Producer {
1904 return producerFunc(func(_ context.Context) ([]metricdata.ScopeMetrics, error) {
1905 return []metricdata.ScopeMetrics{
1906 {
1907 Metrics: []metricdata.Metrics{
1908 {
1909 Name: "$%^&", // intentionally invalid; translation should fail normalization
1910 Description: "bad name for translation",
1911 // Any supported type is fine; getName runs before add* functions.
1912 Data: metricdata.Gauge[float64]{
1913 DataPoints: []metricdata.DataPoint[float64]{
1914 {Value: 1},
1915 },
1916 },
1917 },
1918 },
1919 },
1920 }, nil
1921 })
1922 }
1923 // Helper to create a producer that emits an ExponentialHistogram with a bad
1924 // label, to exercise addExponentialHistogramMetric getAttrs error path.
1925 makeBadEHProducer := func() metric.Producer {
1926 return producerFunc(func(_ context.Context) ([]metricdata.ScopeMetrics, error) {
1927 return []metricdata.ScopeMetrics{
1928 {
1929 Metrics: []metricdata.Metrics{
1930 {
1931 Name: "exp_hist_metric",
1932 Description: "bad label",
1933 Data: metricdata.ExponentialHistogram[float64]{
1934 DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{
1935 {
1936 Attributes: attribute.NewSet(attribute.Key("$%^&").String("B")),
1937 Scale: 0,
1938 Count: 1,
1939 ZeroThreshold: 0,
1940 },
1941 },
1942 },
1943 },
1944 },
1945 },
1946 }, nil
1947 })
1948 }
1949 // Helper to create a producer that emits an ExponentialHistogram with
1950 // inconsistent bucket counts vs total Count to trigger constructor error in addExponentialHistogramMetric.
1951 makeBadEHCountProducer := func() metric.Producer {
1952 return producerFunc(func(_ context.Context) ([]metricdata.ScopeMetrics, error) {
1953 return []metricdata.ScopeMetrics{
1954 {
1955 Metrics: []metricdata.Metrics{
1956 {
1957 Name: "exp_hist_metric_bad",
1958 Data: metricdata.ExponentialHistogram[float64]{
1959 DataPoints: []metricdata.ExponentialHistogramDataPoint[float64]{
1960 {
1961 Scale: 0,
1962 Count: 0,
1963 ZeroThreshold: 0,
1964 PositiveBucket: metricdata.ExponentialBucket{
1965 Offset: 0,
1966 Counts: []uint64{1},
1967 },
1968 },
1969 },
1970 },
1971 },
1972 },
1973 },
1974 }, nil
1975 })
1976 }
1977
1978 testCases := []struct {
1979 name string
1980 namespace string
1981 counterName string
1982 customScopeAttrs []attribute.KeyValue
1983 customResourceAttrs []attribute.KeyValue
1984 labelName string
1985 producer metric.Producer
1986 skipInstrument bool
1987 record func(ctx context.Context, meter otelmetric.Meter) error
1988 expectNewErr string
1989 expectMetricErr string
1990 expectGatherErrContains string
1991 expectGatherErrIs error
1992 checkMetricFamilies func(t testing.TB, dtos []*dto.MetricFamily)
1993 }{
1994 {
1995 name: "simple happy path",
1996 counterName: "foo",
1997 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
1998 require.Len(t, mfs, 2)
1999 for _, mf := range mfs {
2000 if mf.GetName() == "target_info" {
2001 continue
2002 }
2003 require.Equal(t, "foo_seconds_total", mf.GetName())
2004 }
2005 },
2006 },
2007 {
2008 name: "bad namespace",
2009 namespace: "$%^&",
2010 counterName: "foo",
2011 expectNewErr: `normalization for label name "$%^&" resulted in invalid name "_"`,
2012 },
2013 {
2014 name: "bad translated metric name via producer",
2015 // Use a producer to emit a metric with an invalid name to trigger getName error.
2016 producer: makeBadNameProducer(),
2017 skipInstrument: true,
2018 // Error message comes from normalization in the translator; match on a stable substring.
2019 expectGatherErrContains: "normalization",
2020 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2021 // target_info should still be exported.
2022 require.NotEmpty(t, mfs)
2023 other := 0
2024 seenTarget := false
2025 for _, mf := range mfs {
2026 if mf.GetName() == "target_info" {
2027 seenTarget = true
2028 continue
2029 }
2030 other++
2031 }
2032 require.True(t, seenTarget)
2033 require.Equal(t, 0, other)
2034 },
2035 },
2036 {
2037 name: "good namespace, names should be escaped",
2038 namespace: "my-strange-namespace",
2039 counterName: "foo",
2040 labelName: "bar",
2041 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2042 for _, mf := range mfs {
2043 if mf.GetName() == "target_info" {
2044 continue
2045 }
2046 require.Contains(t, mf.GetName(), "my_strange_namespace")
2047 require.NotContains(t, mf.GetName(), "my-strange-namespace")
2048 }
2049 },
2050 },
2051 {
2052 name: "bad resource attribute",
2053 counterName: "foo",
2054 customResourceAttrs: []attribute.KeyValue{
2055 attribute.Key("$%^&").String("B"),
2056 },
2057 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2058 require.Empty(t, mfs)
2059 },
2060 },
2061 {
2062 name: "bad scope metric attribute",
2063 counterName: "foo",
2064 customScopeAttrs: []attribute.KeyValue{
2065 attribute.Key("$%^&").String("B"),
2066 },
2067 // With improved error handling, invalid scope label names result in an invalid metric
2068 // and Gather returns an error containing the normalization failure.
2069 expectGatherErrContains: `normalization for label name "$%^&" resulted in invalid name "_"`,
2070 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2071 // target_info should still be exported; metric with bad scope label dropped.
2072 require.NotEmpty(t, mfs)
2073 other := 0
2074 seenTarget := false
2075 for _, mf := range mfs {
2076 if mf.GetName() == "target_info" {
2077 seenTarget = true
2078 continue
2079 }
2080 other++
2081 }
2082 require.True(t, seenTarget)
2083 require.Equal(t, 0, other)
2084 },
2085 },
2086 {
2087 name: "bad translated metric name",
2088 counterName: "$%^&",
2089 expectMetricErr: `invalid instrument name: $%^&: must start with a letter`,
2090 },
2091 {
2092 // label names are not translated and therefore not checked until
2093 // collection time; with improved error handling, we emit an invalid metric and
2094 // surface the error during Gather.
2095 name: "bad translated label name",
2096 counterName: "foo",
2097 labelName: "$%^&",
2098 expectGatherErrContains: `normalization for label name "$%^&" resulted in invalid name "_"`,
2099 },
2100 {
2101 name: "unsupported data type via producer",
2102 // Use a producer to emit a Summary data point; no SDK instruments.
2103 producer: makeSummaryProducer(),
2104 skipInstrument: true,
2105 expectGatherErrIs: errInvalidMetricType,
2106 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2107 require.NotEmpty(t, mfs)
2108 other := 0
2109 seenTarget := false
2110 for _, mf := range mfs {
2111 if mf.GetName() == "target_info" {
2112 seenTarget = true
2113 continue
2114 }
2115 other++
2116 }
2117 require.True(t, seenTarget)
2118 require.Equal(t, 0, other)
2119 },
2120 },
2121 {
2122 name: "bad exponential histogram label name via producer",
2123 producer: makeBadEHProducer(),
2124 skipInstrument: true,
2125 expectGatherErrContains: `normalization for label name "$%^&" resulted in invalid name "_"`,
2126 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2127 require.NotEmpty(t, mfs)
2128 other := 0
2129 seenTarget := false
2130 for _, mf := range mfs {
2131 if mf.GetName() == "target_info" {
2132 seenTarget = true
2133 continue
2134 }
2135 other++
2136 }
2137 require.True(t, seenTarget)
2138 require.Equal(t, 0, other)
2139 },
2140 },
2141 {
2142 name: "exponential histogram constructor error via producer (count mismatch)",
2143 producer: makeBadEHCountProducer(),
2144 skipInstrument: true,
2145 expectGatherErrContains: "count",
2146 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2147 require.NotEmpty(t, mfs)
2148 other := 0
2149 seenTarget := false
2150 for _, mf := range mfs {
2151 if mf.GetName() == "target_info" {
2152 seenTarget = true
2153 continue
2154 }
2155 other++
2156 }
2157 require.True(t, seenTarget)
2158 require.Equal(t, 0, other)
2159 },
2160 },
2161 {
2162 name: "sum constructor error via duplicate label name",
2163 record: func(ctx context.Context, meter otelmetric.Meter) error {
2164 c, err := meter.Int64Counter("sum_metric_dup")
2165 if err != nil {
2166 return err
2167 }
2168 // Duplicate variable label name with scope label to make Desc invalid.
2169 c.Add(ctx, 1, otelmetric.WithAttributes(attribute.String(scopeNameLabel, "x")))
2170 return nil
2171 },
2172 expectGatherErrContains: "duplicate label",
2173 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2174 require.NotEmpty(t, mfs)
2175 other := 0
2176 seenTarget := false
2177 for _, mf := range mfs {
2178 if mf.GetName() == "target_info" {
2179 seenTarget = true
2180 continue
2181 }
2182 other++
2183 }
2184 require.True(t, seenTarget)
2185 require.Equal(t, 0, other)
2186 },
2187 },
2188 {
2189 name: "gauge constructor error via duplicate label name",
2190 record: func(ctx context.Context, meter otelmetric.Meter) error {
2191 g, err := meter.Float64Gauge("gauge_metric_dup")
2192 if err != nil {
2193 return err
2194 }
2195 g.Record(ctx, 1.0, otelmetric.WithAttributes(attribute.String(scopeNameLabel, "x")))
2196 return nil
2197 },
2198 expectGatherErrContains: "duplicate label",
2199 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2200 require.NotEmpty(t, mfs)
2201 other := 0
2202 seenTarget := false
2203 for _, mf := range mfs {
2204 if mf.GetName() == "target_info" {
2205 seenTarget = true
2206 continue
2207 }
2208 other++
2209 }
2210 require.True(t, seenTarget)
2211 require.Equal(t, 0, other)
2212 },
2213 },
2214 {
2215 name: "histogram constructor error via duplicate label name",
2216 record: func(ctx context.Context, meter otelmetric.Meter) error {
2217 h, err := meter.Float64Histogram("hist_metric_dup")
2218 if err != nil {
2219 return err
2220 }
2221 h.Record(ctx, 1.23, otelmetric.WithAttributes(attribute.String(scopeNameLabel, "x")))
2222 return nil
2223 },
2224 expectGatherErrContains: "duplicate label",
2225 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2226 require.NotEmpty(t, mfs)
2227 other := 0
2228 seenTarget := false
2229 for _, mf := range mfs {
2230 if mf.GetName() == "target_info" {
2231 seenTarget = true
2232 continue
2233 }
2234 other++
2235 }
2236 require.True(t, seenTarget)
2237 require.Equal(t, 0, other)
2238 },
2239 },
2240 {
2241 name: "bad gauge label name",
2242 record: func(ctx context.Context, meter otelmetric.Meter) error {
2243 g, err := meter.Float64Gauge("gauge_metric")
2244 if err != nil {
2245 return err
2246 }
2247 g.Record(ctx, 1, otelmetric.WithAttributes(attribute.Key("$%^&").String("B")))
2248 return nil
2249 },
2250 expectGatherErrContains: `normalization for label name "$%^&" resulted in invalid name "_"`,
2251 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2252 require.NotEmpty(t, mfs)
2253 other := 0
2254 seenTarget := false
2255 for _, mf := range mfs {
2256 if mf.GetName() == "target_info" {
2257 seenTarget = true
2258 continue
2259 }
2260 other++
2261 }
2262 require.True(t, seenTarget)
2263 require.Equal(t, 0, other)
2264 },
2265 },
2266 {
2267 name: "bad histogram label name",
2268 record: func(ctx context.Context, meter otelmetric.Meter) error {
2269 h, err := meter.Float64Histogram("hist_metric")
2270 if err != nil {
2271 return err
2272 }
2273 h.Record(ctx, 1.23, otelmetric.WithAttributes(attribute.Key("$%^&").String("B")))
2274 return nil
2275 },
2276 expectGatherErrContains: `normalization for label name "$%^&" resulted in invalid name "_"`,
2277 checkMetricFamilies: func(t testing.TB, mfs []*dto.MetricFamily) {
2278 require.NotEmpty(t, mfs)
2279 other := 0
2280 seenTarget := false
2281 for _, mf := range mfs {
2282 if mf.GetName() == "target_info" {
2283 seenTarget = true
2284 continue
2285 }
2286 other++
2287 }
2288 require.True(t, seenTarget)
2289 require.Equal(t, 0, other)
2290 },
2291 },
2292 }
2293
2294 for _, tc := range testCases {
2295 t.Run(tc.name, func(t *testing.T) {
2296 ctx := t.Context()
2297 registry := prometheus.NewRegistry()
2298
2299 sc := trace.NewSpanContext(trace.SpanContextConfig{
2300 SpanID: trace.SpanID{0o1},
2301 TraceID: trace.TraceID{0o1},
2302 TraceFlags: trace.FlagsSampled,
2303 })
2304 ctx = trace.ContextWithSpanContext(ctx, sc)
2305
2306 opts := []Option{
2307 WithRegisterer(registry),
2308 WithTranslationStrategy(otlptranslator.UnderscoreEscapingWithSuffixes),
2309 WithNamespace(tc.namespace),
2310 WithResourceAsConstantLabels(attribute.NewDenyKeysFilter()),
2311 }
2312 if tc.producer != nil {
2313 opts = append(opts, WithProducer(tc.producer))
2314 }
2315 exporter, err := New(opts...)
2316 if tc.expectNewErr != "" {
2317 require.ErrorContains(t, err, tc.expectNewErr)
2318 return
2319 }
2320 require.NoError(t, err)
2321 if !tc.skipInstrument {
2322 res, err := resource.New(ctx,
2323 resource.WithAttributes(semconv.ServiceName("prometheus_test")),
2324 resource.WithAttributes(semconv.TelemetrySDKVersion("latest")),
2325 resource.WithAttributes(tc.customResourceAttrs...),
2326 )
2327 require.NoError(t, err)
2328 provider := metric.NewMeterProvider(
2329 metric.WithReader(exporter),
2330 metric.WithResource(res),
2331 )
2332 meter := provider.Meter(
2333 "meterfoo",
2334 otelmetric.WithInstrumentationVersion("v0.1.0"),
2335 otelmetric.WithInstrumentationAttributes(tc.customScopeAttrs...),
2336 )
2337 if tc.record != nil {
2338 err := tc.record(ctx, meter)
2339 require.NoError(t, err)
2340 } else {
2341 fooCounter, err := meter.Int64Counter(
2342 tc.counterName,
2343 otelmetric.WithUnit("s"),
2344 otelmetric.WithDescription(fmt.Sprintf(`meter %q counter`, tc.counterName)))
2345 if tc.expectMetricErr != "" {
2346 require.ErrorContains(t, err, tc.expectMetricErr)
2347 return
2348 }
2349 require.NoError(t, err)
2350 var addOpts []otelmetric.AddOption
2351 if tc.labelName != "" {
2352 addOpts = append(addOpts, otelmetric.WithAttributes(attribute.String(tc.labelName, "foo")))
2353 }
2354 fooCounter.Add(ctx, 100, addOpts...)
2355 }
2356 } else {
2357 // When skipping instruments, still register the reader so Collect will run.
2358 _ = metric.NewMeterProvider(metric.WithReader(exporter))
2359 }
2360 got, err := registry.Gather()
2361 if tc.expectGatherErrContains != "" {
2362 require.Error(t, err)
2363 require.Contains(t, err.Error(), tc.expectGatherErrContains)
2364 return
2365 }
2366 if tc.expectGatherErrIs != nil {
2367 require.ErrorIs(t, err, tc.expectGatherErrIs)
2368 return
2369 }
2370 require.NoError(t, err)
2371 if tc.checkMetricFamilies != nil {
2372 tc.checkMetricFamilies(t, got)
2373 }
2374 })
2375 }
2376 }
2377
2378 func TestExporterSelfInstrumentation(t *testing.T) {
2379 testCases := []struct {
2380 name string
2381 enableObservability bool
2382 recordMetrics func(ctx context.Context, meter otelmetric.Meter)
2383 expectedObservMetrics []string
2384 expectedMainMetrics int
2385 checkMetrics func(t *testing.T, mainMetrics []*dto.MetricFamily, observMetrics metricdata.ScopeMetrics)
2386 }{
2387 {
2388 name: "self instrumentation disabled",
2389 enableObservability: false,
2390 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
2391 counter, err := meter.Int64Counter("test_counter")
2392 require.NoError(t, err)
2393 counter.Add(ctx, 1)
2394 },
2395 expectedObservMetrics: []string{}, // No observability metrics expected
2396 expectedMainMetrics: 2, // test counter + target_info
2397 },
2398 {
2399 name: "self instrumentation enabled with counter",
2400 enableObservability: true,
2401 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
2402 counter, err := meter.Int64Counter("test_counter", otelmetric.WithDescription("test counter"))
2403 require.NoError(t, err)
2404 counter.Add(ctx, 1, otelmetric.WithAttributes(attribute.String("key", "value")))
2405 },
2406 expectedObservMetrics: []string{
2407 "otel.sdk.exporter.metric_data_point.inflight",
2408 "otel.sdk.exporter.metric_data_point.exported",
2409 "otel.sdk.exporter.operation.duration",
2410 "otel.sdk.metric_reader.collection.duration",
2411 },
2412 expectedMainMetrics: 2, // test counter + target_info
2413 checkMetrics: func(t *testing.T, _ []*dto.MetricFamily, observMetrics metricdata.ScopeMetrics) {
2414 // Check that exported metrics include success count
2415 for _, m := range observMetrics.Metrics {
2416 if m.Name == "otel.sdk.exporter.metric_data_point.exported" {
2417 sum, ok := m.Data.(metricdata.Sum[int64])
2418 require.True(t, ok)
2419 require.Len(t, sum.DataPoints, 1)
2420 require.Equal(t, int64(1), sum.DataPoints[0].Value)
2421 }
2422 }
2423 },
2424 },
2425 {
2426 name: "self instrumentation enabled with gauge",
2427 enableObservability: true,
2428 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
2429 gauge, err := meter.Float64Gauge("test_gauge", otelmetric.WithDescription("test gauge"))
2430 require.NoError(t, err)
2431 gauge.Record(ctx, 42.5, otelmetric.WithAttributes(attribute.String("key", "value")))
2432 },
2433 expectedObservMetrics: []string{
2434 "otel.sdk.exporter.metric_data_point.inflight",
2435 "otel.sdk.exporter.metric_data_point.exported",
2436 "otel.sdk.exporter.operation.duration",
2437 "otel.sdk.metric_reader.collection.duration",
2438 },
2439 expectedMainMetrics: 2,
2440 },
2441 {
2442 name: "self instrumentation enabled with histogram",
2443 enableObservability: true,
2444 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
2445 histogram, err := meter.Float64Histogram("test_histogram", otelmetric.WithDescription("test histogram"))
2446 require.NoError(t, err)
2447 histogram.Record(ctx, 1.5, otelmetric.WithAttributes(attribute.String("key", "value")))
2448 histogram.Record(ctx, 2.5, otelmetric.WithAttributes(attribute.String("key", "value")))
2449 },
2450 expectedObservMetrics: []string{
2451 "otel.sdk.exporter.metric_data_point.inflight",
2452 "otel.sdk.exporter.metric_data_point.exported",
2453 "otel.sdk.exporter.operation.duration",
2454 "otel.sdk.metric_reader.collection.duration",
2455 },
2456 expectedMainMetrics: 2,
2457 },
2458 {
2459 name: "self instrumentation with multiple metrics",
2460 enableObservability: true,
2461 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
2462 counter, err := meter.Int64Counter("test_counter")
2463 require.NoError(t, err)
2464 counter.Add(ctx, 5, otelmetric.WithAttributes(attribute.String("type", "requests")))
2465 counter.Add(ctx, 3, otelmetric.WithAttributes(attribute.String("type", "errors")))
2466
2467 gauge, err := meter.Float64Gauge("test_gauge")
2468 require.NoError(t, err)
2469 gauge.Record(ctx, 100.0, otelmetric.WithAttributes(attribute.String("status", "active")))
2470
2471 histogram, err := meter.Float64Histogram("test_histogram")
2472 require.NoError(t, err)
2473 histogram.Record(ctx, 0.1)
2474 histogram.Record(ctx, 0.2)
2475 histogram.Record(ctx, 0.3)
2476 },
2477 expectedObservMetrics: []string{
2478 "otel.sdk.exporter.metric_data_point.inflight",
2479 "otel.sdk.exporter.metric_data_point.exported",
2480 "otel.sdk.exporter.operation.duration",
2481 "otel.sdk.metric_reader.collection.duration",
2482 },
2483 expectedMainMetrics: 4, // 3 test metrics + target_info
2484 checkMetrics: func(t *testing.T, _ []*dto.MetricFamily, observMetrics metricdata.ScopeMetrics) {
2485 // Check that exported metrics track multiple data points
2486 for _, m := range observMetrics.Metrics {
2487 if m.Name == "otel.sdk.exporter.metric_data_point.exported" {
2488 sum, ok := m.Data.(metricdata.Sum[int64])
2489 require.True(t, ok)
2490 require.Len(t, sum.DataPoints, 1)
2491 // Counter: 2 data points, Gauge: 1 data point, Histogram: 1 data point = 4 total
2492 require.Equal(t, int64(4), sum.DataPoints[0].Value)
2493 }
2494 }
2495 },
2496 },
2497 {
2498 name: "self instrumentation enabled with up-down counter",
2499 enableObservability: true,
2500 recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
2501 upDownCounter, err := meter.Int64UpDownCounter(
2502 "test_updown_counter",
2503 otelmetric.WithDescription("test up-down counter"),
2504 )
2505 require.NoError(t, err)
2506 upDownCounter.Add(ctx, 10, otelmetric.WithAttributes(attribute.String("direction", "up")))
2507 upDownCounter.Add(ctx, -5, otelmetric.WithAttributes(attribute.String("direction", "down")))
2508 },
2509 expectedObservMetrics: []string{
2510 "otel.sdk.exporter.metric_data_point.inflight",
2511 "otel.sdk.exporter.metric_data_point.exported",
2512 "otel.sdk.exporter.operation.duration",
2513 "otel.sdk.metric_reader.collection.duration",
2514 },
2515 expectedMainMetrics: 2,
2516 },
2517 }
2518
2519 for _, tc := range testCases {
2520 t.Run(tc.name, func(t *testing.T) {
2521 if tc.enableObservability {
2522 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
2523 } else {
2524 t.Setenv("OTEL_GO_X_OBSERVABILITY", "")
2525 }
2526
2527 // Setup observability metric collection
2528 var observReader *metric.ManualReader
2529 var observMetricsFunc func() metricdata.ScopeMetrics
2530
2531 if tc.enableObservability {
2532 originalMP := otel.GetMeterProvider()
2533 defer otel.SetMeterProvider(originalMP)
2534
2535 observReader = metric.NewManualReader()
2536 observMP := metric.NewMeterProvider(metric.WithReader(observReader))
2537 otel.SetMeterProvider(observMP)
2538
2539 observMetricsFunc = func() metricdata.ScopeMetrics {
2540 var rm metricdata.ResourceMetrics
2541 err := observReader.Collect(t.Context(), &rm)
2542 require.NoError(t, err)
2543
2544 // Find the Prometheus exporter observability scope specifically.
2545 for _, sm := range rm.ScopeMetrics {
2546 if sm.Scope.Name == observ.ScopeName {
2547 return sm
2548 }
2549 }
2550 // Exporter scope not found (e.g., if disabled or no scrape yet).
2551 return metricdata.ScopeMetrics{}
2552 }
2553 }
2554
2555 ctx := t.Context()
2556 registry := prometheus.NewRegistry()
2557
2558 exporter, err := New(WithRegisterer(registry))
2559 require.NoError(t, err)
2560
2561 provider := metric.NewMeterProvider(metric.WithReader(exporter))
2562 meter := provider.Meter("test", otelmetric.WithInstrumentationVersion("v1.0.0"))
2563
2564 // Record the test metrics
2565 tc.recordMetrics(ctx, meter)
2566
2567 // Collect main metrics
2568 mainMetrics, err := registry.Gather()
2569 require.NoError(t, err)
2570
2571 // Verify the number of main metric families
2572 assert.Len(t, mainMetrics, tc.expectedMainMetrics)
2573
2574 // Collect and check observability metrics if enabled
2575 if tc.enableObservability {
2576 observMetrics := observMetricsFunc()
2577
2578 // Check that expected observability metrics are present
2579 observedMetrics := make(map[string]bool)
2580 for _, m := range observMetrics.Metrics {
2581 observedMetrics[m.Name] = true
2582 }
2583
2584 for _, expectedMetric := range tc.expectedObservMetrics {
2585 assert.True(
2586 t,
2587 observedMetrics[expectedMetric],
2588 "Expected observability metric %s not found",
2589 expectedMetric,
2590 )
2591 }
2592
2593 // Verify observability metrics have expected structure
2594 expectedScope := instrumentation.Scope{
2595 Name: observ.ScopeName,
2596 Version: observ.Version,
2597 SchemaURL: observ.SchemaURL,
2598 }
2599 assert.Equal(t, expectedScope, observMetrics.Scope, "Expected observability scope")
2600 assert.Len(
2601 t,
2602 observMetrics.Metrics,
2603 len(tc.expectedObservMetrics),
2604 "Expected number of observability metrics",
2605 )
2606
2607 // Run custom metric checks if provided
2608 if tc.checkMetrics != nil {
2609 tc.checkMetrics(t, mainMetrics, observMetrics)
2610 }
2611 }
2612 })
2613 }
2614 }
2615
2616 func TestExporterSelfInstrumentationErrors(t *testing.T) {
2617 testCases := []struct {
2618 name string
2619 setupError func() (metric.Reader, func())
2620 expectedMinMetrics int // Minimum expected metrics in error scenarios
2621 checkErrorAttributes func(t *testing.T, observMetrics metricdata.ScopeMetrics)
2622 }{
2623 {
2624 name: "reader shutdown error",
2625 setupError: func() (metric.Reader, func()) {
2626 reader := metric.NewManualReader()
2627 //nolint:usetesting // required to avoid getting a canceled context at cleanup.
2628 return reader, func() { _ = reader.Shutdown(context.Background()) }
2629 },
2630 expectedMinMetrics: 1, // At least some metrics should be present
2631 },
2632 {
2633 name: "reader not registered error",
2634 setupError: func() (metric.Reader, func()) {
2635 reader := metric.NewManualReader()
2636 // Don't register the reader with a provider
2637 return reader, func() {}
2638 },
2639 expectedMinMetrics: 1, // At least some metrics should be present
2640 },
2641 }
2642
2643 for _, tc := range testCases {
2644 t.Run(tc.name, func(t *testing.T) {
2645 // Enable observability
2646 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
2647
2648 // Setup observability metric collection
2649 originalMP := otel.GetMeterProvider()
2650 defer otel.SetMeterProvider(originalMP)
2651
2652 observReader := metric.NewManualReader()
2653 observMP := metric.NewMeterProvider(metric.WithReader(observReader))
2654 otel.SetMeterProvider(observMP)
2655
2656 registry := prometheus.NewRegistry()
2657
2658 reader, cleanup := tc.setupError()
2659 defer cleanup()
2660
2661 // Create exporter with the error-prone reader
2662 cfg := newConfig()
2663 cfg.registerer = registry
2664
2665 collector := &collector{
2666 reader: reader,
2667 disableTargetInfo: cfg.disableTargetInfo,
2668 withoutUnits: cfg.withoutUnits,
2669 withoutCounterSuffixes: cfg.withoutCounterSuffixes,
2670 disableScopeInfo: cfg.disableScopeInfo,
2671 metricFamilies: make(map[string]*dto.MetricFamily),
2672 namespace: cfg.namespace,
2673 resourceAttributesFilter: cfg.resourceAttributesFilter,
2674 metricNamer: otlptranslator.NewMetricNamer(cfg.namespace, cfg.translationStrategy),
2675 }
2676
2677 var err error
2678 collector.inst, err = observ.NewInstrumentation(0)
2679 require.NoError(t, err)
2680
2681 err = registry.Register(collector)
2682 require.NoError(t, err)
2683
2684 // Trigger collection which should encounter the error
2685 _, err = registry.Gather()
2686 require.NoError(t, err)
2687
2688 // Collect observability metrics
2689 var observMetrics metricdata.ResourceMetrics
2690 err = observReader.Collect(t.Context(), &observMetrics)
2691 require.NoError(t, err)
2692
2693 if len(observMetrics.ScopeMetrics) > 0 {
2694 // Verify observability metrics are still present (at least some)
2695 scopeMetrics := observMetrics.ScopeMetrics[0]
2696 foundObservMetrics := 0
2697 for _, m := range scopeMetrics.Metrics {
2698 switch m.Name {
2699 case "otel.sdk.exporter.metric_data_point.inflight",
2700 "otel.sdk.exporter.metric_data_point.exported",
2701 "otel.sdk.exporter.operation.duration",
2702 "otel.sdk.metric_reader.collection.duration":
2703 foundObservMetrics++
2704 }
2705 }
2706 assert.GreaterOrEqual(
2707 t,
2708 foundObservMetrics,
2709 tc.expectedMinMetrics,
2710 "Should have at least some observability metrics even with errors",
2711 )
2712
2713 if tc.checkErrorAttributes != nil {
2714 tc.checkErrorAttributes(t, scopeMetrics)
2715 }
2716 }
2717 })
2718 }
2719 }
2720
2721 func TestExporterSelfInstrumentationConcurrency(t *testing.T) {
2722 // Enable observability
2723 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
2724
2725 // Setup observability metric collection
2726 originalMP := otel.GetMeterProvider()
2727 defer otel.SetMeterProvider(originalMP)
2728
2729 observReader := metric.NewManualReader()
2730 observMP := metric.NewMeterProvider(metric.WithReader(observReader))
2731 otel.SetMeterProvider(observMP)
2732
2733 ctx := t.Context()
2734 registry := prometheus.NewRegistry()
2735
2736 exporter, err := New(WithRegisterer(registry))
2737 require.NoError(t, err)
2738
2739 provider := metric.NewMeterProvider(metric.WithReader(exporter))
2740 meter := provider.Meter("concurrent_test", otelmetric.WithInstrumentationVersion("v1.0.0"))
2741
2742 counter, err := meter.Int64Counter("concurrent_counter")
2743 require.NoError(t, err)
2744
2745 // Run concurrent operations
2746 const numGoroutines = 10
2747 const numOperations = 100
2748 var wg sync.WaitGroup
2749
2750 for i := range numGoroutines {
2751 wg.Add(1)
2752 go func(id int) {
2753 defer wg.Done()
2754 for j := range numOperations {
2755 counter.Add(ctx, 1, otelmetric.WithAttributes(attribute.Int("goroutine", id)))
2756
2757 // Occasionally trigger collection
2758 if j%10 == 0 {
2759 _, _ = registry.Gather()
2760 }
2761 }
2762 }(i)
2763 }
2764
2765 wg.Wait()
2766
2767 // Final collection
2768 _, err = registry.Gather()
2769 require.NoError(t, err)
2770
2771 // Collect observability metrics
2772 var observMetrics metricdata.ResourceMetrics
2773 err = observReader.Collect(t.Context(), &observMetrics)
2774 require.NoError(t, err)
2775
2776 if len(observMetrics.ScopeMetrics) > 0 {
2777 scopeMetrics := observMetrics.ScopeMetrics[0]
2778 // Verify observability metrics are present and have reasonable values
2779 for _, m := range scopeMetrics.Metrics {
2780 switch m.Name {
2781 case "otel.sdk.exporter.metric_data_point.exported":
2782 sum, ok := m.Data.(metricdata.Sum[int64])
2783 require.True(t, ok)
2784 require.NotEmpty(t, sum.DataPoints)
2785 // Should have exported many data points
2786 assert.Positive(t, sum.DataPoints[0].Value)
2787 case "otel.sdk.exporter.operation.duration":
2788 hist, ok := m.Data.(metricdata.Histogram[float64])
2789 require.True(t, ok)
2790 require.NotEmpty(t, hist.DataPoints)
2791 // Should have recorded operation durations
2792 assert.Positive(t, hist.DataPoints[0].Count)
2793 case "otel.sdk.metric_reader.collection.duration":
2794 hist, ok := m.Data.(metricdata.Histogram[float64])
2795 require.True(t, ok)
2796 require.NotEmpty(t, hist.DataPoints)
2797 // Should have recorded collection durations
2798 assert.Positive(t, hist.DataPoints[0].Count)
2799 }
2800 }
2801 }
2802 }
2803
2804 func TestExporterSelfInstrumentationExemplarHandling(t *testing.T) {
2805 // Enable observability
2806 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
2807
2808 // Setup observability metric collection
2809 originalMP := otel.GetMeterProvider()
2810 defer otel.SetMeterProvider(originalMP)
2811
2812 observReader := metric.NewManualReader()
2813 observMP := metric.NewMeterProvider(metric.WithReader(observReader))
2814 otel.SetMeterProvider(observMP)
2815
2816 ctx := t.Context()
2817 registry := prometheus.NewRegistry()
2818
2819 exporter, err := New(WithRegisterer(registry))
2820 require.NoError(t, err)
2821
2822 provider := metric.NewMeterProvider(metric.WithReader(exporter))
2823 meter := provider.Meter("exemplar_test", otelmetric.WithInstrumentationVersion("v1.0.0"))
2824
2825 // Create trace context for exemplars
2826 sc := trace.NewSpanContext(trace.SpanContextConfig{
2827 TraceID: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
2828 SpanID: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
2829 TraceFlags: trace.FlagsSampled,
2830 })
2831 ctx = trace.ContextWithSpanContext(ctx, sc)
2832
2833 histogram, err := meter.Float64Histogram("test_histogram_with_exemplars")
2834 require.NoError(t, err)
2835
2836 // Record values that should generate exemplars
2837 histogram.Record(ctx, 1.0, otelmetric.WithAttributes(attribute.String("key", "value1")))
2838 histogram.Record(ctx, 2.0, otelmetric.WithAttributes(attribute.String("key", "value2")))
2839
2840 // Collect metrics
2841 got, err := registry.Gather()
2842 require.NoError(t, err)
2843
2844 // Verify that metrics are collected without errors even when exemplars are present
2845 foundTestHistogram := false
2846
2847 for _, mf := range got {
2848 if *mf.Name == "test_histogram_with_exemplars" {
2849 foundTestHistogram = true
2850 assert.Equal(t, dto.MetricType_HISTOGRAM, *mf.Type)
2851 }
2852 }
2853
2854 assert.True(t, foundTestHistogram, "Test histogram should be present")
2855
2856 // Collect observability metrics
2857 var observMetrics metricdata.ResourceMetrics
2858 err = observReader.Collect(t.Context(), &observMetrics)
2859 require.NoError(t, err)
2860
2861 if len(observMetrics.ScopeMetrics) > 0 {
2862 expectedMetrics := map[string]bool{
2863 "otel.sdk.exporter.metric_data_point.inflight": false,
2864 "otel.sdk.exporter.metric_data_point.exported": false,
2865 "otel.sdk.exporter.operation.duration": false,
2866 "otel.sdk.metric_reader.collection.duration": false,
2867 }
2868
2869 // Check all scope metrics, not just the first one
2870 for _, scopeMetrics := range observMetrics.ScopeMetrics {
2871 for _, m := range scopeMetrics.Metrics {
2872 if _, exists := expectedMetrics[m.Name]; exists {
2873 expectedMetrics[m.Name] = true
2874 }
2875 }
2876 }
2877
2878 foundObservabilityMetrics := 0
2879 for _, found := range expectedMetrics {
2880 if found {
2881 foundObservabilityMetrics++
2882 }
2883 }
2884 assert.Equal(t, 4, foundObservabilityMetrics, "All observability metrics should be present")
2885 }
2886 }
2887
2888 func TestExporterSelfInstrumentationInitErrors(t *testing.T) {
2889 // Test when NewInstrumentation returns an error
2890 t.Setenv("OTEL_GO_X_OBSERVABILITY", "true")
2891
2892 // Set up a meter provider that will cause NewInstrumentation to fail
2893 original := otel.GetMeterProvider()
2894 defer otel.SetMeterProvider(original)
2895
2896 errMP := &errMeterProvider{err: assert.AnError}
2897 otel.SetMeterProvider(errMP)
2898
2899 registry := prometheus.NewRegistry()
2900
2901 // This should fail during exporter creation due to instrumentation init error
2902 _, err := New(WithRegisterer(registry))
2903 require.ErrorIs(t, err, assert.AnError, "Expected NewInstrumentation error to be propagated")
2904 }
2905
2906 // Helper types for testing NewInstrumentation errors.
2907 type errMeterProvider struct {
2908 otelmetric.MeterProvider
2909 err error
2910 }
2911
2912 func (m *errMeterProvider) Meter(string, ...otelmetric.MeterOption) otelmetric.Meter {
2913 return &errMeter{err: m.err}
2914 }
2915
2916 type errMeter struct {
2917 otelmetric.Meter
2918 err error
2919 }
2920
2921 func (m *errMeter) Int64UpDownCounter(
2922 string,
2923 ...otelmetric.Int64UpDownCounterOption,
2924 ) (otelmetric.Int64UpDownCounter, error) {
2925 return nil, m.err
2926 }
2927
2928 func (m *errMeter) Int64Counter(string, ...otelmetric.Int64CounterOption) (otelmetric.Int64Counter, error) {
2929 return nil, m.err
2930 }
2931
2932 func (m *errMeter) Float64Histogram(string, ...otelmetric.Float64HistogramOption) (otelmetric.Float64Histogram, error) {
2933 return nil, m.err
2934 }