diff --git a/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/InMemoryMetricReader.java b/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/InMemoryMetricReader.java index 6beb732f..0abdc03c 100644 --- a/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/InMemoryMetricReader.java +++ b/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/InMemoryMetricReader.java @@ -49,17 +49,21 @@ import io.opentelemetry.sdk.metrics.data.SumData; import io.opentelemetry.sdk.metrics.export.CollectionRegistration; import io.opentelemetry.sdk.metrics.export.MetricReader; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.inject.spi.CDI; -@ApplicationScoped public class InMemoryMetricReader implements MetricReader { + private static InMemoryMetricReader instance; + private CollectionRegistration collectionRegistration; private boolean isShutdown = false; public static InMemoryMetricReader current() { - return CDI.current().select(InMemoryMetricReader.class).get(); + synchronized (InMemoryMetricReader.class) { + if (instance == null) { + instance = new InMemoryMetricReader(); + } + return instance; + } } @Override @@ -85,6 +89,9 @@ public CompletableResultCode forceFlush() { public CompletableResultCode shutdown() { collectionRegistration = null; isShutdown = true; + synchronized (InMemoryMetricReader.class) { + instance = null; + } return CompletableResultCode.ofSuccess(); } @@ -106,6 +113,10 @@ public Optional getMetric(TelemetryMetricID id) { } private Optional getMetric(String name) { + if (collectionRegistration == null) { + throw new IllegalStateException("InMemoryMetricReader has not been registered"); + } + Collection allMetrics = collectionRegistration.collectAllMetrics(); List matchingMetrics = allMetrics.stream() .filter(md -> md.getName().equals(name)) diff --git a/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/PullExporterAutoConfigurationCustomizerProvider.java b/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/PullExporterAutoConfigurationCustomizerProvider.java index b872db33..aa33742c 100644 --- a/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/PullExporterAutoConfigurationCustomizerProvider.java +++ b/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/PullExporterAutoConfigurationCustomizerProvider.java @@ -23,7 +23,6 @@ import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; -import jakarta.enterprise.inject.spi.CDI; public class PullExporterAutoConfigurationCustomizerProvider implements AutoConfigurationCustomizerProvider { @@ -33,7 +32,7 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) { private SdkMeterProviderBuilder registerMeterProvider(SdkMeterProviderBuilder builder, ConfigProperties properties) { - InMemoryMetricReader exporter = CDI.current().select(InMemoryMetricReader.class).get(); + InMemoryMetricReader exporter = InMemoryMetricReader.current(); builder.registerMetricReader(exporter); return builder; } diff --git a/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/TelemetryLongMetric.java b/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/TelemetryLongMetric.java index 5bbeae04..d83e1ec3 100644 --- a/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/TelemetryLongMetric.java +++ b/tck/src/main/java/org/eclipse/microprofile/fault/tolerance/tck/telemetryMetrics/util/TelemetryLongMetric.java @@ -27,7 +27,6 @@ import org.eclipse.microprofile.fault.tolerance.tck.telemetryMetrics.util.TelemetryMetricDefinition.MetricType; import io.opentelemetry.sdk.metrics.data.LongPointData; -import jakarta.enterprise.inject.spi.CDI; /** * Allows tests to get the value of a {@code Long} counter or gauge and compare it with a baseline. @@ -55,12 +54,12 @@ public TelemetryLongMetric(TelemetryMetricID metricId) { * @return the counter value, or zero if the metric doesn't exist */ public long value() { - InMemoryMetricReader reader = CDI.current().select(InMemoryMetricReader.class).get(); + InMemoryMetricReader reader = InMemoryMetricReader.current(); return reader.readLongData(metricId); } public boolean isPresent() { - InMemoryMetricReader exporter = CDI.current().select(InMemoryMetricReader.class).get(); + InMemoryMetricReader exporter = InMemoryMetricReader.current(); Optional latest = exporter.getMetric(metricId) .flatMap(md -> InMemoryMetricReader.getLongPointData(md, metricId)); return latest.isPresent();