Skip to content

Commit 664a075

Browse files
trthompsdmathieudashpole
authored
Fix exemplars being added to gauge metrics in the prometheus exporter (open-telemetry#5912)
Prometheus Gauge metrics don't support exemplars and while `addGaugeMetric()` doesn't add them, `addSumMetric()` will if the metric is monotonic. This causes the prometheus client to throw an error: ``` * error collecting metric Desc{fqName: "http_server_request_body_size_bytes", help: "Measures size of RPC request messages (uncompressed).", constLabels: {}, variableLabels: {net_protocol_name,net_protocol_version,http_method,http_route,http_scheme,net_host_name,net_host_port,otel_scope_name,otel_scope_version}}: cannot inject exemplar into Gauge, Summary or Untyped ``` --------- Co-authored-by: Damien Mathieu <[email protected]> Co-authored-by: David Ashpole <[email protected]>
1 parent 30c4a9a commit 664a075

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2121
### Fixed
2222

2323
- Global MeterProvider registration unwraps global instrument Observers, the undocumented Unwrap() methods are now private. (#5881)
24+
- Fix `go.opentelemetry.io/otel/exporters/prometheus` trying to add exemplars to Gauge metrics, which is unsupported. (#5912)
2425

2526
### Changed
2627

exporters/prometheus/exporter.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ func addSumMetric[N int64 | float64](ch chan<- prometheus.Metric, sum metricdata
277277
otel.Handle(err)
278278
continue
279279
}
280-
m = addExemplars(m, dp.Exemplars)
280+
// GaugeValues don't support Exemplars at this time
281+
// https://github.com/prometheus/client_golang/blob/aef8aedb4b6e1fb8ac1c90790645169125594096/prometheus/metric.go#L199
282+
if valueType != prometheus.GaugeValue {
283+
m = addExemplars(m, dp.Exemplars)
284+
}
281285
ch <- m
282286
}
283287
}

exporters/prometheus/exporter_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,42 @@ func TestPrometheusExporter(t *testing.T) {
431431
counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
432432
},
433433
},
434+
{
435+
name: "non-monotonic sum does not add exemplars",
436+
expectedFile: "testdata/non_monotonic_sum_does_not_add_exemplars.txt",
437+
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
438+
sc := trace.NewSpanContext(trace.SpanContextConfig{
439+
SpanID: trace.SpanID{0o1},
440+
TraceID: trace.TraceID{0o1},
441+
TraceFlags: trace.FlagsSampled,
442+
})
443+
ctx = trace.ContextWithSpanContext(ctx, sc)
444+
opt := otelmetric.WithAttributes(
445+
attribute.Key("A").String("B"),
446+
attribute.Key("C").String("D"),
447+
attribute.Key("E").Bool(true),
448+
attribute.Key("F").Int(42),
449+
)
450+
counter, err := meter.Float64UpDownCounter(
451+
"foo",
452+
otelmetric.WithDescription("a simple up down counter"),
453+
otelmetric.WithUnit("s"),
454+
)
455+
require.NoError(t, err)
456+
counter.Add(ctx, 5, opt)
457+
counter.Add(ctx, 10.3, opt)
458+
counter.Add(ctx, 9, opt)
459+
counter.Add(ctx, -1, opt)
460+
461+
attrs2 := attribute.NewSet(
462+
attribute.Key("A").String("D"),
463+
attribute.Key("C").String("B"),
464+
attribute.Key("E").Bool(true),
465+
attribute.Key("F").Int(42),
466+
)
467+
counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
468+
},
469+
},
434470
}
435471

436472
for _, tc := range testCases {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# HELP foo_seconds a simple up down counter
2+
# TYPE foo_seconds gauge
3+
foo_seconds{A="B",C="D",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 23.3
4+
foo_seconds{A="D",C="B",E="true",F="42",otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 5
5+
# HELP otel_scope_info Instrumentation Scope metadata
6+
# TYPE otel_scope_info gauge
7+
otel_scope_info{otel_scope_name="testmeter",otel_scope_version="v0.1.0"} 1
8+
# HELP target_info Target metadata
9+
# TYPE target_info gauge
10+
target_info{service_name="prometheus_test",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="latest"} 1

0 commit comments

Comments
 (0)