Skip to content

Commit 4291d57

Browse files
committed
Add coverage for invalid span context exemplar branches
1 parent a0627db commit 4291d57

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,61 @@ void exemplarLabelsExceedingLimitDropsFilteredAttributes() {
635635
// filtered attribute is dropped to stay within limit
636636
assertThat(exemplarLabels.get("long_attr")).isNull();
637637
}
638+
639+
@Test
640+
void exemplarWithoutSpanContextExceedingLimitDropsFilteredAttributes() {
641+
char[] chars = new char[150];
642+
Arrays.fill(chars, 'x');
643+
String longValue = new String(chars);
644+
ImmutableDoubleExemplarData exemplar =
645+
(ImmutableDoubleExemplarData)
646+
ImmutableDoubleExemplarData.create(
647+
Attributes.of(stringKey("long_attr"), longValue),
648+
1000L,
649+
SpanContext.getInvalid(),
650+
1.0);
651+
652+
MetricData metricData =
653+
ImmutableMetricData.createDoubleGauge(
654+
Resource.getDefault(),
655+
InstrumentationScopeInfo.create("test"),
656+
"my.gauge",
657+
"desc",
658+
"unit",
659+
ImmutableGaugeData.create(
660+
Collections.singletonList(
661+
ImmutableDoublePointData.create(
662+
0, 1000, Attributes.empty(), 1.0, Collections.singletonList(exemplar)))));
663+
664+
MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData));
665+
GaugeDataPointSnapshot point = (GaugeDataPointSnapshot) snapshots.get(0).getDataPoints().get(0);
666+
Labels exemplarLabels = point.getExemplar().getLabels();
667+
// No span context means no trace_id/span_id, and oversized filtered attr is dropped
668+
assertThat(exemplarLabels.size()).isZero();
669+
}
670+
671+
@Test
672+
void exemplarWithoutSpanContextWithinLimit() {
673+
ImmutableDoubleExemplarData exemplar =
674+
(ImmutableDoubleExemplarData)
675+
ImmutableDoubleExemplarData.create(
676+
Attributes.of(stringKey("short"), "val"), 1000L, SpanContext.getInvalid(), 1.0);
677+
678+
MetricData metricData =
679+
ImmutableMetricData.createDoubleGauge(
680+
Resource.getDefault(),
681+
InstrumentationScopeInfo.create("test"),
682+
"my.gauge",
683+
"desc",
684+
"unit",
685+
ImmutableGaugeData.create(
686+
Collections.singletonList(
687+
ImmutableDoublePointData.create(
688+
0, 1000, Attributes.empty(), 1.0, Collections.singletonList(exemplar)))));
689+
690+
MetricSnapshots snapshots = converter.convert(Collections.singletonList(metricData));
691+
GaugeDataPointSnapshot point = (GaugeDataPointSnapshot) snapshots.get(0).getDataPoints().get(0);
692+
Labels exemplarLabels = point.getExemplar().getLabels();
693+
assertThat(exemplarLabels.get("short")).isEqualTo("val");
694+
}
638695
}

0 commit comments

Comments
 (0)