From 5a341ab4400c5b0e48f32b9348d7e60e076cd525 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Fri, 1 Nov 2024 16:12:31 -0500 Subject: [PATCH] Delete MeterSharedState --- .../sdk/metrics/InstrumentBuilder.java | 39 ++-- .../sdk/metrics/SdkDoubleCounter.java | 22 +- .../sdk/metrics/SdkDoubleGauge.java | 24 +- .../sdk/metrics/SdkDoubleHistogram.java | 23 +- .../sdk/metrics/SdkDoubleUpDownCounter.java | 21 +- .../sdk/metrics/SdkLongCounter.java | 24 +- .../sdk/metrics/SdkLongGauge.java | 22 +- .../sdk/metrics/SdkLongHistogram.java | 22 +- .../sdk/metrics/SdkLongUpDownCounter.java | 23 +- .../opentelemetry/sdk/metrics/SdkMeter.java | 207 ++++++++++++++++-- .../sdk/metrics/SdkObservableInstrument.java | 10 +- .../state/AsynchronousMetricStorage.java | 11 +- .../internal/state/CallbackRegistration.java | 2 +- .../state/MeterProviderSharedState.java | 2 +- .../internal/state/MeterSharedState.java | 199 ----------------- .../internal/state/MetricStorageRegistry.java | 2 +- .../state/MultiWritableMetricStorage.java | 42 ---- .../sdk/metrics/InstrumentBuilderTest.java | 18 +- .../metrics/SdkObservableInstrumentTest.java | 21 +- 19 files changed, 275 insertions(+), 459 deletions(-) delete mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterSharedState.java delete mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MultiWritableMetricStorage.java diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/InstrumentBuilder.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/InstrumentBuilder.java index afed0778796..14fd54d7bd1 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/InstrumentBuilder.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/InstrumentBuilder.java @@ -11,8 +11,6 @@ import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.CallbackRegistration; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.SdkObservableMeasurement; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.Collections; @@ -23,8 +21,7 @@ final class InstrumentBuilder { private final String name; - private final MeterProviderSharedState meterProviderSharedState; - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final InstrumentValueType valueType; private InstrumentType type; private Advice.AdviceBuilder adviceBuilder = Advice.builder(); @@ -32,16 +29,11 @@ final class InstrumentBuilder { private String unit = ""; InstrumentBuilder( - String name, - InstrumentType type, - InstrumentValueType valueType, - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState) { + String name, InstrumentType type, InstrumentValueType valueType, SdkMeter sdkMeter) { this.name = name; this.type = type; this.valueType = valueType; - this.meterProviderSharedState = meterProviderSharedState; - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; } InstrumentBuilder setUnit(String unit) { @@ -60,8 +52,7 @@ InstrumentBuilder setDescription(String description) { } T swapBuilder(SwapBuilder swapper) { - return swapper.newBuilder( - meterProviderSharedState, meterSharedState, name, description, unit, adviceBuilder); + return swapper.newBuilder(sdkMeter, name, description, unit, adviceBuilder); } @FunctionalInterface @@ -69,16 +60,15 @@ interface SynchronousInstrumentConstructor { I createInstrument( InstrumentDescriptor instrumentDescriptor, - MeterSharedState meterSharedState, + SdkMeter sdkMeter, WriteableMetricStorage storage); } I buildSynchronousInstrument( SynchronousInstrumentConstructor instrumentFactory) { InstrumentDescriptor descriptor = newDescriptor(); - WriteableMetricStorage storage = - meterSharedState.registerSynchronousMetricStorage(descriptor, meterProviderSharedState); - return instrumentFactory.createInstrument(descriptor, meterSharedState, storage); + WriteableMetricStorage storage = sdkMeter.registerSynchronousMetricStorage(descriptor); + return instrumentFactory.createInstrument(descriptor, sdkMeter, storage); } SdkObservableInstrument buildDoubleAsynchronousInstrument( @@ -87,8 +77,8 @@ SdkObservableInstrument buildDoubleAsynchronousInstrument( Runnable runnable = () -> updater.accept(sdkObservableMeasurement); CallbackRegistration callbackRegistration = CallbackRegistration.create(Collections.singletonList(sdkObservableMeasurement), runnable); - meterSharedState.registerCallback(callbackRegistration); - return new SdkObservableInstrument(meterSharedState, callbackRegistration); + sdkMeter.registerCallback(callbackRegistration); + return new SdkObservableInstrument(sdkMeter, callbackRegistration); } SdkObservableInstrument buildLongAsynchronousInstrument( @@ -97,14 +87,14 @@ SdkObservableInstrument buildLongAsynchronousInstrument( Runnable runnable = () -> updater.accept(sdkObservableMeasurement); CallbackRegistration callbackRegistration = CallbackRegistration.create(Collections.singletonList(sdkObservableMeasurement), runnable); - meterSharedState.registerCallback(callbackRegistration); - return new SdkObservableInstrument(meterSharedState, callbackRegistration); + sdkMeter.registerCallback(callbackRegistration); + return new SdkObservableInstrument(sdkMeter, callbackRegistration); } SdkObservableMeasurement buildObservableMeasurement(InstrumentType type) { this.type = type; InstrumentDescriptor descriptor = newDescriptor(); - return meterSharedState.registerObservableMeasurement(descriptor); + return sdkMeter.registerObservableMeasurement(descriptor); } private InstrumentDescriptor newDescriptor() { @@ -122,10 +112,9 @@ String toStringHelper(String className) { } @FunctionalInterface - protected interface SwapBuilder { + interface SwapBuilder { T newBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, + SdkMeter sdkMeter, String name, String description, String unit, diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java index 5e4eb4a7ef5..de645016076 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java @@ -16,8 +16,6 @@ import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.function.Consumer; @@ -28,15 +26,13 @@ final class SdkDoubleCounter extends AbstractInstrument implements ExtendedDoubl private static final Logger logger = Logger.getLogger(SdkDoubleCounter.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkDoubleCounter( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -65,7 +61,7 @@ public void add(double increment) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkDoubleCounterBuilder implements ExtendedDoubleCounterBuilder { @@ -73,19 +69,13 @@ static final class SdkDoubleCounterBuilder implements ExtendedDoubleCounterBuild private final InstrumentBuilder builder; SdkDoubleCounterBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState sharedState, + SdkMeter sdkMeter, String name, String description, String unit, Advice.AdviceBuilder adviceBuilder) { this.builder = - new InstrumentBuilder( - name, - InstrumentType.COUNTER, - InstrumentValueType.DOUBLE, - meterProviderSharedState, - sharedState) + new InstrumentBuilder(name, InstrumentType.COUNTER, InstrumentValueType.DOUBLE, sdkMeter) .setUnit(unit) .setDescription(description) .setAdviceBuilder(adviceBuilder); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java index 8cc9d468257..03b3f43838d 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java @@ -15,23 +15,19 @@ import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.function.Consumer; final class SdkDoubleGauge extends AbstractInstrument implements ExtendedDoubleGauge { - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkDoubleGauge( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -52,24 +48,16 @@ public void set(double increment) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkDoubleGaugeBuilder implements ExtendedDoubleGaugeBuilder { private final InstrumentBuilder builder; - SdkDoubleGaugeBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, - String name) { + SdkDoubleGaugeBuilder(SdkMeter sdkMeter, String name) { builder = - new InstrumentBuilder( - name, - InstrumentType.GAUGE, - InstrumentValueType.DOUBLE, - meterProviderSharedState, - meterSharedState); + new InstrumentBuilder(name, InstrumentType.GAUGE, InstrumentValueType.DOUBLE, sdkMeter); } @Override diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java index 4861a631ab7..5aa8f49de31 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java @@ -15,8 +15,6 @@ import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.aggregator.ExplicitBucketHistogramUtils; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.Objects; @@ -27,15 +25,13 @@ final class SdkDoubleHistogram extends AbstractInstrument implements ExtendedDou private static final Logger logger = Logger.getLogger(SdkDoubleHistogram.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkDoubleHistogram( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -64,24 +60,17 @@ public void record(double value) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkDoubleHistogramBuilder implements ExtendedDoubleHistogramBuilder { private final InstrumentBuilder builder; - SdkDoubleHistogramBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, - String name) { + SdkDoubleHistogramBuilder(SdkMeter sdkMeter, String name) { builder = new InstrumentBuilder( - name, - InstrumentType.HISTOGRAM, - InstrumentValueType.DOUBLE, - meterProviderSharedState, - meterSharedState); + name, InstrumentType.HISTOGRAM, InstrumentValueType.DOUBLE, sdkMeter); } @Override diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java index da6316d6b77..fe21e295c98 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java @@ -16,8 +16,6 @@ import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.function.Consumer; @@ -25,15 +23,13 @@ final class SdkDoubleUpDownCounter extends AbstractInstrument implements ExtendedDoubleUpDownCounter { - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkDoubleUpDownCounter( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -54,7 +50,7 @@ public void add(double increment) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkDoubleUpDownCounterBuilder implements ExtendedDoubleUpDownCounterBuilder { @@ -62,19 +58,14 @@ static final class SdkDoubleUpDownCounterBuilder implements ExtendedDoubleUpDown private final InstrumentBuilder builder; SdkDoubleUpDownCounterBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState sharedState, + SdkMeter sdkMeter, String name, String description, String unit, Advice.AdviceBuilder adviceBuilder) { this.builder = new InstrumentBuilder( - name, - InstrumentType.UP_DOWN_COUNTER, - InstrumentValueType.DOUBLE, - meterProviderSharedState, - sharedState) + name, InstrumentType.UP_DOWN_COUNTER, InstrumentValueType.DOUBLE, sdkMeter) .setDescription(description) .setUnit(unit) .setAdviceBuilder(adviceBuilder); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java index 91b52fee7fd..df2f9b4e660 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java @@ -16,8 +16,6 @@ import io.opentelemetry.context.Context; import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.function.Consumer; @@ -29,15 +27,13 @@ final class SdkLongCounter extends AbstractInstrument implements ExtendedLongCou private static final Logger logger = Logger.getLogger(SdkLongCounter.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkLongCounter( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -66,24 +62,16 @@ public void add(long increment) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkLongCounterBuilder implements ExtendedLongCounterBuilder { private final InstrumentBuilder builder; - SdkLongCounterBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, - String name) { + SdkLongCounterBuilder(SdkMeter sdkMeter, String name) { this.builder = - new InstrumentBuilder( - name, - InstrumentType.COUNTER, - InstrumentValueType.LONG, - meterProviderSharedState, - meterSharedState); + new InstrumentBuilder(name, InstrumentType.COUNTER, InstrumentValueType.LONG, sdkMeter); } @Override diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java index 62ab826df87..499c4a4443a 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java @@ -15,23 +15,19 @@ import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.function.Consumer; final class SdkLongGauge extends AbstractInstrument implements ExtendedLongGauge { - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkLongGauge( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -52,7 +48,7 @@ public void set(long increment) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkLongGaugeBuilder implements ExtendedLongGaugeBuilder { @@ -60,20 +56,14 @@ static final class SdkLongGaugeBuilder implements ExtendedLongGaugeBuilder { private final InstrumentBuilder builder; SdkLongGaugeBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState sharedState, + SdkMeter sdkMeter, String name, String description, String unit, Advice.AdviceBuilder adviceBuilder) { builder = - new InstrumentBuilder( - name, - InstrumentType.GAUGE, - InstrumentValueType.LONG, - meterProviderSharedState, - sharedState) + new InstrumentBuilder(name, InstrumentType.GAUGE, InstrumentValueType.LONG, sdkMeter) .setDescription(description) .setUnit(unit) .setAdviceBuilder(adviceBuilder); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java index ef8ee31e707..0822b1eff57 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java @@ -15,8 +15,6 @@ import io.opentelemetry.sdk.metrics.internal.aggregator.ExplicitBucketHistogramUtils; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.Objects; @@ -28,15 +26,13 @@ final class SdkLongHistogram extends AbstractInstrument implements ExtendedLongH private static final Logger logger = Logger.getLogger(SdkLongHistogram.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkLongHistogram( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -65,7 +61,7 @@ public void record(long value) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkLongHistogramBuilder implements ExtendedLongHistogramBuilder { @@ -73,19 +69,13 @@ static final class SdkLongHistogramBuilder implements ExtendedLongHistogramBuild private final InstrumentBuilder builder; SdkLongHistogramBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState sharedState, + SdkMeter sdkMeter, String name, String description, String unit, Advice.AdviceBuilder adviceBuilder) { builder = - new InstrumentBuilder( - name, - InstrumentType.HISTOGRAM, - InstrumentValueType.LONG, - meterProviderSharedState, - sharedState) + new InstrumentBuilder(name, InstrumentType.HISTOGRAM, InstrumentValueType.LONG, sdkMeter) .setDescription(description) .setUnit(unit) .setAdviceBuilder(adviceBuilder); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java index a74ac786d1a..daf1e2f97a8 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java @@ -16,23 +16,19 @@ import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.List; import java.util.function.Consumer; final class SdkLongUpDownCounter extends AbstractInstrument implements ExtendedLongUpDownCounter { - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final WriteableMetricStorage storage; private SdkLongUpDownCounter( - InstrumentDescriptor descriptor, - MeterSharedState meterSharedState, - WriteableMetricStorage storage) { + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); - this.meterSharedState = meterSharedState; + this.sdkMeter = sdkMeter; this.storage = storage; } @@ -53,24 +49,17 @@ public void add(long increment) { @Override public boolean isEnabled() { - return meterSharedState.isMeterEnabled() && storage.isEnabled(); + return sdkMeter.isMeterEnabled() && storage.isEnabled(); } static final class SdkLongUpDownCounterBuilder implements ExtendedLongUpDownCounterBuilder { private final InstrumentBuilder builder; - SdkLongUpDownCounterBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, - String name) { + SdkLongUpDownCounterBuilder(SdkMeter sdkMeter, String name) { this.builder = new InstrumentBuilder( - name, - InstrumentType.UP_DOWN_COUNTER, - InstrumentValueType.LONG, - meterProviderSharedState, - meterSharedState); + name, InstrumentType.UP_DOWN_COUNTER, InstrumentValueType.LONG, sdkMeter); } @Override diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java index da57ef3506c..fe278cba4d6 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java @@ -5,6 +5,10 @@ package io.opentelemetry.sdk.metrics; +import static java.util.stream.Collectors.toMap; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.internal.GuardedBy; import io.opentelemetry.api.metrics.BatchCallback; import io.opentelemetry.api.metrics.DoubleGaugeBuilder; import io.opentelemetry.api.metrics.DoubleHistogramBuilder; @@ -13,20 +17,30 @@ import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.api.metrics.ObservableMeasurement; +import io.opentelemetry.context.Context; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.metrics.internal.MeterConfig; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.export.RegisteredReader; +import io.opentelemetry.sdk.metrics.internal.state.AsynchronousMetricStorage; import io.opentelemetry.sdk.metrics.internal.state.CallbackRegistration; import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; +import io.opentelemetry.sdk.metrics.internal.state.MetricStorage; +import io.opentelemetry.sdk.metrics.internal.state.MetricStorageRegistry; import io.opentelemetry.sdk.metrics.internal.state.SdkObservableMeasurement; +import io.opentelemetry.sdk.metrics.internal.state.SynchronousMetricStorage; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import io.opentelemetry.sdk.metrics.internal.view.RegisteredView; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.function.Function; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; @@ -53,9 +67,16 @@ final class SdkMeter implements Meter { private static final Meter NOOP_METER = MeterProvider.noop().get("noop"); private static final String NOOP_INSTRUMENT_NAME = "noop"; - private final InstrumentationScopeInfo instrumentationScopeInfo; + private final Object collectLock = new Object(); + private final Object callbackLock = new Object(); + + @GuardedBy("callbackLock") + private final List callbackRegistrations = new ArrayList<>(); + private final MeterProviderSharedState meterProviderSharedState; - private final MeterSharedState meterSharedState; + private final InstrumentationScopeInfo instrumentationScopeInfo; + private final Map readerStorageRegistries; + private final boolean meterEnabled; SdkMeter( MeterProviderSharedState meterProviderSharedState, @@ -64,8 +85,10 @@ final class SdkMeter implements Meter { MeterConfig meterConfig) { this.instrumentationScopeInfo = instrumentationScopeInfo; this.meterProviderSharedState = meterProviderSharedState; - this.meterSharedState = - MeterSharedState.create(instrumentationScopeInfo, registeredReaders, meterConfig); + this.readerStorageRegistries = + registeredReaders.stream() + .collect(toMap(Function.identity(), unused -> new MetricStorageRegistry())); + this.meterEnabled = meterConfig.isEnabled(); } // Visible for testing @@ -75,41 +98,76 @@ InstrumentationScopeInfo getInstrumentationScopeInfo() { /** Collect all metrics for the meter. */ Collection collectAll(RegisteredReader registeredReader, long epochNanos) { - return meterSharedState.collectAll(registeredReader, meterProviderSharedState, epochNanos); + // Short circuit collection process if meter is disabled + if (!meterEnabled) { + return Collections.emptyList(); + } + List currentRegisteredCallbacks; + synchronized (callbackLock) { + currentRegisteredCallbacks = new ArrayList<>(callbackRegistrations); + } + // Collections across all readers are sequential + synchronized (collectLock) { + for (CallbackRegistration callbackRegistration : currentRegisteredCallbacks) { + callbackRegistration.invokeCallback( + registeredReader, meterProviderSharedState.getStartEpochNanos(), epochNanos); + } + + Collection storages = + Objects.requireNonNull(readerStorageRegistries.get(registeredReader)).getStorages(); + List result = new ArrayList<>(storages.size()); + for (MetricStorage storage : storages) { + MetricData current = + storage.collect( + meterProviderSharedState.getResource(), + getInstrumentationScopeInfo(), + meterProviderSharedState.getStartEpochNanos(), + epochNanos); + // Ignore if the metric data doesn't have any data points, for example when aggregation is + // Aggregation#drop() + if (!current.isEmpty()) { + result.add(current); + } + } + return Collections.unmodifiableList(result); + } } - /** Reset the meter, clearing all registered instruments. */ + /** Reset the meter, clearing all registered callbacks and storages. */ void resetForTest() { - this.meterSharedState.resetForTest(); + synchronized (collectLock) { + synchronized (callbackLock) { + callbackRegistrations.clear(); + } + this.readerStorageRegistries.values().forEach(MetricStorageRegistry::resetForTest); + } } @Override public LongCounterBuilder counterBuilder(String name) { return checkValidInstrumentName(name) - ? new SdkLongCounter.SdkLongCounterBuilder(meterProviderSharedState, meterSharedState, name) + ? new SdkLongCounter.SdkLongCounterBuilder(this, name) : NOOP_METER.counterBuilder(NOOP_INSTRUMENT_NAME); } @Override public LongUpDownCounterBuilder upDownCounterBuilder(String name) { return checkValidInstrumentName(name) - ? new SdkLongUpDownCounter.SdkLongUpDownCounterBuilder( - meterProviderSharedState, meterSharedState, name) + ? new SdkLongUpDownCounter.SdkLongUpDownCounterBuilder(this, name) : NOOP_METER.upDownCounterBuilder(NOOP_INSTRUMENT_NAME); } @Override public DoubleHistogramBuilder histogramBuilder(String name) { return checkValidInstrumentName(name) - ? new SdkDoubleHistogram.SdkDoubleHistogramBuilder( - meterProviderSharedState, meterSharedState, name) + ? new SdkDoubleHistogram.SdkDoubleHistogramBuilder(this, name) : NOOP_METER.histogramBuilder(NOOP_INSTRUMENT_NAME); } @Override public DoubleGaugeBuilder gaugeBuilder(String name) { return checkValidInstrumentName(name) - ? new SdkDoubleGauge.SdkDoubleGaugeBuilder(meterProviderSharedState, meterSharedState, name) + ? new SdkDoubleGauge.SdkDoubleGaugeBuilder(this, name) : NOOP_METER.gaugeBuilder(NOOP_INSTRUMENT_NAME); } @@ -131,9 +189,7 @@ public BatchCallback batchCallback( continue; } SdkObservableMeasurement sdkMeasurement = (SdkObservableMeasurement) measurement; - if (!meterSharedState - .getInstrumentationScopeInfo() - .equals(sdkMeasurement.getInstrumentationScopeInfo())) { + if (!instrumentationScopeInfo.equals(sdkMeasurement.getInstrumentationScopeInfo())) { logger.log( Level.WARNING, "batchCallback called with instruments that belong to a different Meter."); @@ -144,8 +200,89 @@ public BatchCallback batchCallback( CallbackRegistration callbackRegistration = CallbackRegistration.create(sdkMeasurements, callback); - meterSharedState.registerCallback(callbackRegistration); - return new SdkObservableInstrument(meterSharedState, callbackRegistration); + registerCallback(callbackRegistration); + return new SdkObservableInstrument(this, callbackRegistration); + } + + /** + * Unregister the callback. + * + *

Callbacks are originally registered via {@link #registerCallback(CallbackRegistration)}. + */ + void removeCallback(CallbackRegistration callbackRegistration) { + synchronized (callbackLock) { + this.callbackRegistrations.remove(callbackRegistration); + } + } + + /** + * Register the callback. + * + *

The callback will be invoked once per collection until unregistered via {@link + * #removeCallback(CallbackRegistration)}. + */ + void registerCallback(CallbackRegistration callbackRegistration) { + synchronized (callbackLock) { + callbackRegistrations.add(callbackRegistration); + } + } + + /** Returns {@code true} if the {@link MeterConfig#enabled()} of the meter is {@code true}. */ + boolean isMeterEnabled() { + return meterEnabled; + } + + /** Registers new synchronous storage associated with a given instrument. */ + WriteableMetricStorage registerSynchronousMetricStorage(InstrumentDescriptor instrument) { + + List registeredStorages = new ArrayList<>(); + for (Map.Entry entry : + readerStorageRegistries.entrySet()) { + RegisteredReader reader = entry.getKey(); + MetricStorageRegistry registry = entry.getValue(); + for (RegisteredView registeredView : + reader.getViewRegistry().findViews(instrument, getInstrumentationScopeInfo())) { + if (Aggregation.drop() == registeredView.getView().getAggregation()) { + continue; + } + registeredStorages.add( + registry.register( + SynchronousMetricStorage.create( + reader, + registeredView, + instrument, + meterProviderSharedState.getExemplarFilter()))); + } + } + + if (registeredStorages.size() == 1) { + return registeredStorages.get(0); + } + + return new MultiWritableMetricStorage(registeredStorages); + } + + /** Register new asynchronous storage associated with a given instrument. */ + SdkObservableMeasurement registerObservableMeasurement( + InstrumentDescriptor instrumentDescriptor) { + List> registeredStorages = new ArrayList<>(); + for (Map.Entry entry : + readerStorageRegistries.entrySet()) { + RegisteredReader reader = entry.getKey(); + MetricStorageRegistry registry = entry.getValue(); + for (RegisteredView registeredView : + reader.getViewRegistry().findViews(instrumentDescriptor, getInstrumentationScopeInfo())) { + if (Aggregation.drop() == registeredView.getView().getAggregation()) { + continue; + } + registeredStorages.add( + registry.register( + AsynchronousMetricStorage.create(reader, registeredView, instrumentDescriptor))); + } + } + + return SdkObservableMeasurement.create( + instrumentationScopeInfo, instrumentDescriptor, registeredStorages); } @Override @@ -170,4 +307,36 @@ static boolean checkValidInstrumentName(String name) { return false; } + + private static class MultiWritableMetricStorage implements WriteableMetricStorage { + private final List storages; + + private MultiWritableMetricStorage(List storages) { + this.storages = storages; + } + + @Override + public void recordLong(long value, Attributes attributes, Context context) { + for (WriteableMetricStorage storage : storages) { + storage.recordLong(value, attributes, context); + } + } + + @Override + public void recordDouble(double value, Attributes attributes, Context context) { + for (WriteableMetricStorage storage : storages) { + storage.recordDouble(value, attributes, context); + } + } + + @Override + public boolean isEnabled() { + for (WriteableMetricStorage storage : storages) { + if (storage.isEnabled()) { + return true; + } + } + return false; + } + } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkObservableInstrument.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkObservableInstrument.java index 33c1adbc1e7..5b7731e9d53 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkObservableInstrument.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkObservableInstrument.java @@ -14,7 +14,6 @@ import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.state.CallbackRegistration; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; @@ -31,13 +30,12 @@ class SdkObservableInstrument private static final Logger logger = Logger.getLogger(SdkObservableInstrument.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final MeterSharedState meterSharedState; + private final SdkMeter sdkMeter; private final CallbackRegistration callbackRegistration; private final AtomicBoolean removed = new AtomicBoolean(false); - SdkObservableInstrument( - MeterSharedState meterSharedState, CallbackRegistration callbackRegistration) { - this.meterSharedState = meterSharedState; + SdkObservableInstrument(SdkMeter sdkMeter, CallbackRegistration callbackRegistration) { + this.sdkMeter = sdkMeter; this.callbackRegistration = callbackRegistration; } @@ -48,7 +46,7 @@ public void close() { Level.WARNING, callbackRegistration + " has called close() multiple times."); return; } - meterSharedState.removeCallback(callbackRegistration); + sdkMeter.removeCallback(callbackRegistration); } @Override diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/AsynchronousMetricStorage.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/AsynchronousMetricStorage.java index 328710144da..42110e2cb1e 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/AsynchronousMetricStorage.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/AsynchronousMetricStorage.java @@ -41,7 +41,7 @@ *

This class is internal and is hence not for public use. Its APIs are unstable and can change * at any time. */ -final class AsynchronousMetricStorage +public final class AsynchronousMetricStorage implements MetricStorage { private static final Logger logger = Logger.getLogger(AsynchronousMetricStorage.class.getName()); @@ -101,10 +101,11 @@ private AsynchronousMetricStorage( * Create an asynchronous storage instance for the {@link View} and {@link InstrumentDescriptor}. */ // TODO(anuraaga): The cast to generic type here looks suspicious. - static AsynchronousMetricStorage create( - RegisteredReader registeredReader, - RegisteredView registeredView, - InstrumentDescriptor instrumentDescriptor) { + public static + AsynchronousMetricStorage create( + RegisteredReader registeredReader, + RegisteredView registeredView, + InstrumentDescriptor instrumentDescriptor) { View view = registeredView.getView(); MetricDescriptor metricDescriptor = MetricDescriptor.create(view, registeredView.getViewSourceInfo(), instrumentDescriptor); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/CallbackRegistration.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/CallbackRegistration.java index 6634dd728ac..fec792096a2 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/CallbackRegistration.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/CallbackRegistration.java @@ -70,7 +70,7 @@ public String toString() { return "CallbackRegistration{instrumentDescriptors=" + instrumentDescriptors + "}"; } - void invokeCallback(RegisteredReader reader, long startEpochNanos, long epochNanos) { + public void invokeCallback(RegisteredReader reader, long startEpochNanos, long epochNanos) { // Return early if no storages are registered if (!hasStorages) { return; diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterProviderSharedState.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterProviderSharedState.java index 8f3647c6d46..aaf932d9202 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterProviderSharedState.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterProviderSharedState.java @@ -41,5 +41,5 @@ public static MeterProviderSharedState create( public abstract long getStartEpochNanos(); /** Returns the {@link ExemplarFilter} for remembering synchronous measurements. */ - abstract ExemplarFilter getExemplarFilter(); + public abstract ExemplarFilter getExemplarFilter(); } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterSharedState.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterSharedState.java deleted file mode 100644 index 30a3a838c45..00000000000 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MeterSharedState.java +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.metrics.internal.state; - -import static java.util.stream.Collectors.toMap; - -import io.opentelemetry.api.internal.GuardedBy; -import io.opentelemetry.sdk.common.InstrumentationScopeInfo; -import io.opentelemetry.sdk.metrics.Aggregation; -import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.metrics.internal.MeterConfig; -import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; -import io.opentelemetry.sdk.metrics.internal.export.RegisteredReader; -import io.opentelemetry.sdk.metrics.internal.view.RegisteredView; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.Function; - -/** - * State for a {@code Meter}. - * - *

This class is internal and is hence not for public use. Its APIs are unstable and can change - * at any time. - */ -public class MeterSharedState { - - private final Object collectLock = new Object(); - private final Object callbackLock = new Object(); - - @GuardedBy("callbackLock") - private final List callbackRegistrations = new ArrayList<>(); - - private final Map readerStorageRegistries; - private final InstrumentationScopeInfo instrumentationScopeInfo; - private final boolean meterEnabled; - - private MeterSharedState( - InstrumentationScopeInfo instrumentationScopeInfo, - List registeredReaders, - MeterConfig meterConfig) { - this.instrumentationScopeInfo = instrumentationScopeInfo; - this.readerStorageRegistries = - registeredReaders.stream() - .collect(toMap(Function.identity(), unused -> new MetricStorageRegistry())); - this.meterEnabled = meterConfig.isEnabled(); - } - - public static MeterSharedState create( - InstrumentationScopeInfo instrumentationScopeInfo, - List registeredReaders, - MeterConfig meterConfig) { - return new MeterSharedState(instrumentationScopeInfo, registeredReaders, meterConfig); - } - - /** - * Unregister the callback. - * - *

Callbacks are originally registered via {@link #registerCallback(CallbackRegistration)}. - */ - public void removeCallback(CallbackRegistration callbackRegistration) { - synchronized (callbackLock) { - this.callbackRegistrations.remove(callbackRegistration); - } - } - - /** - * Register the callback. - * - *

The callback will be invoked once per collection until unregistered via {@link - * #removeCallback(CallbackRegistration)}. - */ - public final void registerCallback(CallbackRegistration callbackRegistration) { - synchronized (callbackLock) { - callbackRegistrations.add(callbackRegistration); - } - } - - // only visible for testing. - /** Returns the {@link InstrumentationScopeInfo} for this {@code Meter}. */ - public InstrumentationScopeInfo getInstrumentationScopeInfo() { - return instrumentationScopeInfo; - } - - /** Returns {@code true} if the {@link MeterConfig#enabled()} of the meter is {@code true}. */ - public boolean isMeterEnabled() { - return meterEnabled; - } - - /** Collects all metrics. */ - public List collectAll( - RegisteredReader registeredReader, - MeterProviderSharedState meterProviderSharedState, - long epochNanos) { - // Short circuit collection process if meter is disabled - if (!meterEnabled) { - return Collections.emptyList(); - } - List currentRegisteredCallbacks; - synchronized (callbackLock) { - currentRegisteredCallbacks = new ArrayList<>(callbackRegistrations); - } - // Collections across all readers are sequential - synchronized (collectLock) { - for (CallbackRegistration callbackRegistration : currentRegisteredCallbacks) { - callbackRegistration.invokeCallback( - registeredReader, meterProviderSharedState.getStartEpochNanos(), epochNanos); - } - - Collection storages = - Objects.requireNonNull(readerStorageRegistries.get(registeredReader)).getStorages(); - List result = new ArrayList<>(storages.size()); - for (MetricStorage storage : storages) { - MetricData current = - storage.collect( - meterProviderSharedState.getResource(), - getInstrumentationScopeInfo(), - meterProviderSharedState.getStartEpochNanos(), - epochNanos); - // Ignore if the metric data doesn't have any data points, for example when aggregation is - // Aggregation#drop() - if (!current.isEmpty()) { - result.add(current); - } - } - return Collections.unmodifiableList(result); - } - } - - /** Reset the meter state, clearing all registered callbacks and storages. */ - public void resetForTest() { - synchronized (collectLock) { - synchronized (callbackLock) { - callbackRegistrations.clear(); - } - this.readerStorageRegistries.values().forEach(MetricStorageRegistry::resetForTest); - } - } - - /** Registers new synchronous storage associated with a given instrument. */ - public final WriteableMetricStorage registerSynchronousMetricStorage( - InstrumentDescriptor instrument, MeterProviderSharedState meterProviderSharedState) { - - List registeredStorages = new ArrayList<>(); - for (Map.Entry entry : - readerStorageRegistries.entrySet()) { - RegisteredReader reader = entry.getKey(); - MetricStorageRegistry registry = entry.getValue(); - for (RegisteredView registeredView : - reader.getViewRegistry().findViews(instrument, getInstrumentationScopeInfo())) { - if (Aggregation.drop() == registeredView.getView().getAggregation()) { - continue; - } - registeredStorages.add( - registry.register( - SynchronousMetricStorage.create( - reader, - registeredView, - instrument, - meterProviderSharedState.getExemplarFilter()))); - } - } - - if (registeredStorages.size() == 1) { - return registeredStorages.get(0); - } - - return new MultiWritableMetricStorage(registeredStorages); - } - - /** Register new asynchronous storage associated with a given instrument. */ - public final SdkObservableMeasurement registerObservableMeasurement( - InstrumentDescriptor instrumentDescriptor) { - List> registeredStorages = new ArrayList<>(); - for (Map.Entry entry : - readerStorageRegistries.entrySet()) { - RegisteredReader reader = entry.getKey(); - MetricStorageRegistry registry = entry.getValue(); - for (RegisteredView registeredView : - reader.getViewRegistry().findViews(instrumentDescriptor, getInstrumentationScopeInfo())) { - if (Aggregation.drop() == registeredView.getView().getAggregation()) { - continue; - } - registeredStorages.add( - registry.register( - AsynchronousMetricStorage.create(reader, registeredView, instrumentDescriptor))); - } - } - - return SdkObservableMeasurement.create( - instrumentationScopeInfo, instrumentDescriptor, registeredStorages); - } -} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MetricStorageRegistry.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MetricStorageRegistry.java index a070728f06a..34bfd577a86 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MetricStorageRegistry.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MetricStorageRegistry.java @@ -88,7 +88,7 @@ public I register(I newStorage) { } /** Reset the storage registry, clearing all storages. */ - void resetForTest() { + public void resetForTest() { synchronized (lock) { registry.clear(); } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MultiWritableMetricStorage.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MultiWritableMetricStorage.java deleted file mode 100644 index 700fee4c3fb..00000000000 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/MultiWritableMetricStorage.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.metrics.internal.state; - -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.context.Context; -import java.util.List; - -class MultiWritableMetricStorage implements WriteableMetricStorage { - private final List storages; - - MultiWritableMetricStorage(List storages) { - this.storages = storages; - } - - @Override - public void recordLong(long value, Attributes attributes, Context context) { - for (WriteableMetricStorage storage : storages) { - storage.recordLong(value, attributes, context); - } - } - - @Override - public void recordDouble(double value, Attributes attributes, Context context) { - for (WriteableMetricStorage storage : storages) { - storage.recordDouble(value, attributes, context); - } - } - - @Override - public boolean isEnabled() { - for (WriteableMetricStorage storage : storages) { - if (storage.isEnabled()) { - return true; - } - } - return false; - } -} diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/InstrumentBuilderTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/InstrumentBuilderTest.java index c6ef1957428..de1a351e619 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/InstrumentBuilderTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/InstrumentBuilderTest.java @@ -12,7 +12,6 @@ import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarFilter; import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.time.TestClock; import java.util.Collections; @@ -24,18 +23,15 @@ class InstrumentBuilderTest { MeterProviderSharedState.create( TestClock.create(), Resource.getDefault(), ExemplarFilter.alwaysOff(), 0); static final InstrumentationScopeInfo SCOPE = InstrumentationScopeInfo.create("scope-name"); - public static final MeterSharedState METER_SHARED_STATE = - MeterSharedState.create(SCOPE, Collections.emptyList(), MeterConfig.defaultConfig()); + public static final SdkMeter SDK_METER = + new SdkMeter( + PROVIDER_SHARED_STATE, SCOPE, Collections.emptyList(), MeterConfig.defaultConfig()); @Test void stringRepresentation() { InstrumentBuilder builder = new InstrumentBuilder( - "instrument-name", - InstrumentType.COUNTER, - InstrumentValueType.LONG, - PROVIDER_SHARED_STATE, - METER_SHARED_STATE) + "instrument-name", InstrumentType.COUNTER, InstrumentValueType.LONG, SDK_METER) .setDescription("instrument-description") .setUnit("instrument-unit") .setAdviceBuilder(Advice.builder()); @@ -57,11 +53,7 @@ void stringRepresentation() { void toStringHelper() { InstrumentBuilder builder = new InstrumentBuilder( - "instrument-name", - InstrumentType.HISTOGRAM, - InstrumentValueType.DOUBLE, - PROVIDER_SHARED_STATE, - METER_SHARED_STATE) + "instrument-name", InstrumentType.HISTOGRAM, InstrumentValueType.DOUBLE, SDK_METER) .setDescription("instrument-description") .setUnit("instrument-unit") .setAdviceBuilder(Advice.builder()); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkObservableInstrumentTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkObservableInstrumentTest.java index 03e52331dc7..385e6f65a8c 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkObservableInstrumentTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkObservableInstrumentTest.java @@ -6,18 +6,16 @@ package io.opentelemetry.sdk.metrics; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import io.github.netmikey.logunit.api.LogCapturer; import io.opentelemetry.internal.testing.slf4j.SuppressLogger; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; -import io.opentelemetry.sdk.metrics.internal.MeterConfig; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.CallbackRegistration; -import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.SdkObservableMeasurement; import java.util.Collections; import org.junit.jupiter.api.BeforeEach; @@ -30,18 +28,13 @@ class SdkObservableInstrumentTest { @RegisterExtension LogCapturer logs = LogCapturer.create().captureForType(SdkObservableInstrument.class); - private MeterSharedState meterSharedState; private CallbackRegistration callbackRegistration; + private SdkMeter sdkMeter; private SdkObservableInstrument observableInstrument; @BeforeEach void setup() { - meterSharedState = - spy( - MeterSharedState.create( - InstrumentationScopeInfo.empty(), - Collections.emptyList(), - MeterConfig.defaultConfig())); + sdkMeter = mock(SdkMeter.class); callbackRegistration = CallbackRegistration.create( Collections.singletonList( @@ -57,7 +50,7 @@ void setup() { Collections.emptyList())), () -> {}); - observableInstrument = new SdkObservableInstrument(meterSharedState, callbackRegistration); + observableInstrument = new SdkObservableInstrument(sdkMeter, callbackRegistration); } @Test @@ -65,13 +58,13 @@ void setup() { void close() { // First call to close should trigger remove from meter shared state observableInstrument.close(); - verify(meterSharedState).removeCallback(callbackRegistration); + verify(sdkMeter).removeCallback(callbackRegistration); logs.assertDoesNotContain("has called close() multiple times."); // Close a second time should not trigger remove from meter shared state - Mockito.reset(meterSharedState); + Mockito.reset(sdkMeter); observableInstrument.close(); - verify(meterSharedState, never()).removeCallback(callbackRegistration); + verify(sdkMeter, never()).removeCallback(callbackRegistration); logs.assertContains("has called close() multiple times."); }