|
| 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(CounterSnapshot.CounterDataPointSnapshot.builder().value(42.0).build()) |
| 39 | + .build()); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + @Test |
| 44 | + void registerOtelStyleMultiCollector_succeeds() { |
| 45 | + PrometheusRegistry registry = new PrometheusRegistry(); |
| 46 | + |
| 47 | + assertThatCode(() -> registry.register(OTEL_STYLE_MULTI_COLLECTOR)).doesNotThrowAnyException(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void scrape_afterRegisteringOtelStyleMultiCollector_returnsSnapshotsFromCollector() { |
| 52 | + PrometheusRegistry registry = new PrometheusRegistry(); |
| 53 | + registry.register(OTEL_STYLE_MULTI_COLLECTOR); |
| 54 | + |
| 55 | + MetricSnapshots snapshots = registry.scrape(); |
| 56 | + |
| 57 | + assertThat(snapshots).hasSize(1); |
| 58 | + MetricSnapshot snapshot = snapshots.get(0); |
| 59 | + assertThat(snapshot.getMetadata().getPrometheusName()).isEqualTo("otel_metric"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void unregisterOtelStyleMultiCollector_succeedsAndScrapeNoLongerIncludesIt() { |
| 64 | + PrometheusRegistry registry = new PrometheusRegistry(); |
| 65 | + registry.register(OTEL_STYLE_MULTI_COLLECTOR); |
| 66 | + |
| 67 | + assertThat(registry.scrape()).hasSize(1); |
| 68 | + |
| 69 | + assertThatCode(() -> registry.unregister(OTEL_STYLE_MULTI_COLLECTOR)) |
| 70 | + .doesNotThrowAnyException(); |
| 71 | + |
| 72 | + assertThat(registry.scrape()).isEmpty(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void sharedRegistry_otelStyleMultiCollectorAndValidatedCollector_bothParticipateInScrape() { |
| 77 | + PrometheusRegistry registry = new PrometheusRegistry(); |
| 78 | + |
| 79 | + Collector validatedCollector = |
| 80 | + new Collector() { |
| 81 | + @Override |
| 82 | + public MetricSnapshot collect() { |
| 83 | + return GaugeSnapshot.builder().name("app_gauge").help("App gauge").build(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String getPrometheusName() { |
| 88 | + return "app_gauge"; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public MetricType getMetricType() { |
| 93 | + return MetricType.GAUGE; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public java.util.Set<String> getLabelNames() { |
| 98 | + return Collections.emptySet(); |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + registry.register(validatedCollector); |
| 103 | + registry.register(OTEL_STYLE_MULTI_COLLECTOR); |
| 104 | + |
| 105 | + MetricSnapshots snapshots = registry.scrape(); |
| 106 | + |
| 107 | + assertThat(snapshots).hasSize(2); |
| 108 | + assertThat(snapshots) |
| 109 | + .extracting(s -> s.getMetadata().getPrometheusName()) |
| 110 | + .containsExactlyInAnyOrder("app_gauge", "otel_metric"); |
| 111 | + |
| 112 | + registry.unregister(OTEL_STYLE_MULTI_COLLECTOR); |
| 113 | + assertThat(registry.scrape()).hasSize(1); |
| 114 | + assertThat(registry.scrape().get(0).getMetadata().getPrometheusName()).isEqualTo("app_gauge"); |
| 115 | + } |
| 116 | +} |
0 commit comments