Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make InMemoryMetricReader not a bean #642

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading