Skip to content

Commit

Permalink
Merge pull request #642 from Azquelt/non-bean-metric-reader
Browse files Browse the repository at this point in the history
Make InMemoryMetricReader not a bean
  • Loading branch information
Azquelt authored Aug 1, 2024
2 parents 1aaae4a + 064276e commit 33812a9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -85,6 +89,9 @@ public CompletableResultCode forceFlush() {
public CompletableResultCode shutdown() {
collectionRegistration = null;
isShutdown = true;
synchronized (InMemoryMetricReader.class) {
instance = null;
}
return CompletableResultCode.ofSuccess();
}

Expand All @@ -106,6 +113,10 @@ public Optional<MetricData> getMetric(TelemetryMetricID id) {
}

private Optional<MetricData> getMetric(String name) {
if (collectionRegistration == null) {
throw new IllegalStateException("InMemoryMetricReader has not been registered");
}

Collection<MetricData> allMetrics = collectionRegistration.collectAllMetrics();
List<MetricData> matchingMetrics = allMetrics.stream()
.filter(md -> md.getName().equals(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<LongPointData> latest = exporter.getMetric(metricId)
.flatMap(md -> InMemoryMetricReader.getLongPointData(md, metricId));
return latest.isPresent();
Expand Down

0 comments on commit 33812a9

Please sign in to comment.