Skip to content

Commit a055aef

Browse files
authored
Give Prometheus exemplar trace_id and span_id precedence over filtered attributes (#8959)
1 parent acf8d60 commit a055aef

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverter.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,9 @@ private Labels convertAttributes(
540540

541541
Map<String, String> labelNameToValue = buildAttributeLabels(attributes);
542542

543+
// Additional attributes (exemplar trace_id and span_id) take precedence over attributes.
543544
for (int i = 0; i < additionalAttributes.length; i += 2) {
544-
labelNameToValue.putIfAbsent(
545-
requireNonNull(additionalAttributes[i]), additionalAttributes[i + 1]);
545+
labelNameToValue.put(requireNonNull(additionalAttributes[i]), additionalAttributes[i + 1]);
546546
}
547547

548548
if (scope != null && otelScopeLabelsEnabled) {

‎exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverterTest.java‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,48 @@ private static Stream<Arguments> exemplarLabelLimitArgs() {
14081408
new String[] {}));
14091409
}
14101410

1411+
@ParameterizedTest
1412+
@MethodSource("exemplarSpanContextLabelsTakePrecedenceArgs")
1413+
void exemplarSpanContextLabelsTakePrecedence(Attributes filteredAttributes) {
1414+
SpanContext spanContext =
1415+
SpanContext.create(
1416+
"00000000000000000000000000000001",
1417+
"0000000000000001",
1418+
TraceFlags.getSampled(),
1419+
TraceState.getDefault());
1420+
ImmutableDoubleExemplarData exemplar =
1421+
(ImmutableDoubleExemplarData)
1422+
ImmutableDoubleExemplarData.create(filteredAttributes, 1000L, spanContext, 1.0);
1423+
1424+
MetricData metricData =
1425+
ImmutableMetricData.createDoubleGauge(
1426+
Resource.getDefault(),
1427+
InstrumentationScopeInfo.create("test"),
1428+
"my.gauge",
1429+
"desc",
1430+
"unit",
1431+
ImmutableGaugeData.create(
1432+
Collections.singletonList(
1433+
ImmutableDoublePointData.create(
1434+
0, 1000, Attributes.empty(), 1.0, Collections.singletonList(exemplar)))));
1435+
1436+
MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData));
1437+
GaugeDataPointSnapshot point = (GaugeDataPointSnapshot) snapshots.get(0).getDataPoints().get(0);
1438+
Labels exemplarLabels = point.getExemplar().getLabels();
1439+
assertThat(exemplarLabels.get("trace_id")).isEqualTo("00000000000000000000000000000001");
1440+
assertThat(exemplarLabels.get("span_id")).isEqualTo("0000000000000001");
1441+
}
1442+
1443+
private static Stream<Arguments> exemplarSpanContextLabelsTakePrecedenceArgs() {
1444+
return Stream.of(
1445+
Arguments.argumentSet(
1446+
"sameLabelName",
1447+
Attributes.of(stringKey("trace_id"), "bogus", stringKey("span_id"), "bogus")),
1448+
Arguments.argumentSet(
1449+
"sameLabelNameAfterEscaping",
1450+
Attributes.of(stringKey("trace.id"), "bogus", stringKey("span.id"), "bogus")));
1451+
}
1452+
14111453
private static InfoSnapshot makeInfoSnapshot(String id) {
14121454
return new InfoSnapshot(
14131455
MetricMetadata.builder().name("target").build(),

0 commit comments

Comments
 (0)