Skip to content

Commit b5396a5

Browse files
committed
add otel sdk compatibility test
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
1 parent cbcd1ec commit b5396a5

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package io.prometheus.metrics.model.registry;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatCode;
5+
6+
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
7+
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
8+
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
9+
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
10+
import java.util.Collections;
11+
import org.junit.jupiter.api.Test;
12+
13+
/**
14+
* Tests that use the Prometheus registry in the same way as the OpenTelemetry Java SDK Prometheus
15+
* exporter ({@code io.opentelemetry.exporter.prometheus}). The SDK's {@code PrometheusMetricReader}
16+
* implements {@link MultiCollector} with default implementations for all optional methods: {@link
17+
* MultiCollector#getPrometheusNames()} returns an empty list, and {@link
18+
* MultiCollector#getMetricType(String)}, {@link MultiCollector#getLabelNames(String)}, and {@link
19+
* MultiCollector#getMetadata(String)} return null. This test suite ensures that registration,
20+
* scrape, and unregister continue to work for that usage pattern and that a shared registry with
21+
* both SDK-style and validated collectors behaves correctly.
22+
*/
23+
class OpenTelemetryExporterRegistryCompatibilityTest {
24+
25+
/**
26+
* A MultiCollector that mimics the OpenTelemetry Java SDK's PrometheusMetricReader: it does not
27+
* override getPrometheusNames() (empty list), getMetricType(String), getLabelNames(String), or
28+
* getMetadata(String) (all null). Only collect() is implemented and returns MetricSnapshots.
29+
*/
30+
private static final MultiCollector OTEL_STYLE_MULTI_COLLECTOR =
31+
new MultiCollector() {
32+
@Override
33+
public MetricSnapshots collect() {
34+
return new MetricSnapshots(
35+
CounterSnapshot.builder()
36+
.name("otel_metric")
37+
.help("A metric produced by an OTel-style converter")
38+
.dataPoint(
39+
CounterSnapshot.CounterDataPointSnapshot.builder().value(42.0).build())
40+
.build());
41+
}
42+
};
43+
44+
@Test
45+
void registerOtelStyleMultiCollector_succeeds() {
46+
PrometheusRegistry registry = new PrometheusRegistry();
47+
48+
assertThatCode(() -> registry.register(OTEL_STYLE_MULTI_COLLECTOR)).doesNotThrowAnyException();
49+
}
50+
51+
@Test
52+
void scrape_afterRegisteringOtelStyleMultiCollector_returnsSnapshotsFromCollector() {
53+
PrometheusRegistry registry = new PrometheusRegistry();
54+
registry.register(OTEL_STYLE_MULTI_COLLECTOR);
55+
56+
MetricSnapshots snapshots = registry.scrape();
57+
58+
assertThat(snapshots).hasSize(1);
59+
MetricSnapshot snapshot = snapshots.get(0);
60+
assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("otel_metric");
61+
}
62+
63+
@Test
64+
void unregisterOtelStyleMultiCollector_succeedsAndScrapeNoLongerIncludesIt() {
65+
PrometheusRegistry registry = new PrometheusRegistry();
66+
registry.register(OTEL_STYLE_MULTI_COLLECTOR);
67+
68+
assertThat(registry.scrape()).hasSize(1);
69+
70+
assertThatCode(() -> registry.unregister(OTEL_STYLE_MULTI_COLLECTOR))
71+
.doesNotThrowAnyException();
72+
73+
assertThat(registry.scrape()).isEmpty();
74+
}
75+
76+
@Test
77+
void sharedRegistry_otelStyleMultiCollectorAndValidatedCollector_bothParticipateInScrape() {
78+
PrometheusRegistry registry = new PrometheusRegistry();
79+
80+
Collector validatedCollector =
81+
new Collector() {
82+
@Override
83+
public MetricSnapshot collect() {
84+
return GaugeSnapshot.builder().name("app_gauge").help("App gauge").build();
85+
}
86+
87+
@Override
88+
public String getPrometheusName() {
89+
return "app_gauge";
90+
}
91+
92+
@Override
93+
public MetricType getMetricType() {
94+
return MetricType.GAUGE;
95+
}
96+
97+
@Override
98+
public java.util.Set<String> getLabelNames() {
99+
return Collections.emptySet();
100+
}
101+
};
102+
103+
registry.register(validatedCollector);
104+
registry.register(OTEL_STYLE_MULTI_COLLECTOR);
105+
106+
MetricSnapshots snapshots = registry.scrape();
107+
108+
assertThat(snapshots).hasSize(2);
109+
assertThat(snapshots)
110+
.extracting(s -> s.getMetadata().getPrometheusName())
111+
.containsExactlyInAnyOrder("app_gauge", "otel_metric");
112+
113+
registry.unregister(OTEL_STYLE_MULTI_COLLECTOR);
114+
assertThat(registry.scrape()).hasSize(1);
115+
assertThat(registry.scrape().get(0).getMetadata().getPrometheusName()).isEqualTo("app_gauge");
116+
}
117+
}

0 commit comments

Comments
 (0)