From 57d83344178f578eb5d2f043b0ffc5c42b6719a5 Mon Sep 17 00:00:00 2001 From: jason plumb <75337021+breedx-splk@users.noreply.github.com> Date: Fri, 13 Oct 2023 12:24:05 -0700 Subject: [PATCH] Dismantle AbstractInstrumentBuilder inheritance hierarchy (#5820) --- ...entBuilder.java => InstrumentBuilder.java} | 116 ++++++++---------- .../sdk/metrics/SdkDoubleCounter.java | 50 +++++--- .../sdk/metrics/SdkDoubleGauge.java | 47 ++++--- .../sdk/metrics/SdkDoubleHistogram.java | 44 ++++--- .../sdk/metrics/SdkDoubleUpDownCounter.java | 48 +++++--- .../sdk/metrics/SdkLongCounter.java | 45 ++++--- .../sdk/metrics/SdkLongGauge.java | 50 +++++--- .../sdk/metrics/SdkLongHistogram.java | 46 ++++--- .../sdk/metrics/SdkLongUpDownCounter.java | 46 ++++--- .../AbstractInstrumentBuilderTest.java | 67 ---------- .../sdk/metrics/InstrumentBuilderTest.java | 81 ++++++++++++ 11 files changed, 372 insertions(+), 268 deletions(-) rename sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/{AbstractInstrumentBuilder.java => InstrumentBuilder.java} (59%) delete mode 100644 sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentBuilderTest.java create mode 100644 sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/InstrumentBuilderTest.java diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrumentBuilder.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/InstrumentBuilder.java similarity index 59% rename from sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrumentBuilder.java rename to sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/InstrumentBuilder.java index db6a552bad2..f1da522e15b 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrumentBuilder.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/InstrumentBuilder.java @@ -5,6 +5,7 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; import io.opentelemetry.api.metrics.ObservableLongMeasurement; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; @@ -15,95 +16,64 @@ import io.opentelemetry.sdk.metrics.internal.state.SdkObservableMeasurement; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; import java.util.Collections; +import java.util.List; import java.util.function.BiFunction; import java.util.function.Consumer; /** Helper to make implementing builders easier. */ -abstract class AbstractInstrumentBuilder> { - - static final String DEFAULT_UNIT = ""; +final class InstrumentBuilder { + private final String name; private final MeterProviderSharedState meterProviderSharedState; - private final InstrumentType type; + private final MeterSharedState meterSharedState; private final InstrumentValueType valueType; - private String description; - private String unit; - - protected final MeterSharedState meterSharedState; - protected final String instrumentName; - protected final Advice.AdviceBuilder adviceBuilder; + private InstrumentType type; + private Advice.AdviceBuilder adviceBuilder = Advice.builder(); + private String description = ""; + private String unit = ""; - AbstractInstrumentBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, - InstrumentType type, - InstrumentValueType valueType, + InstrumentBuilder( String name, - String description, - String unit) { - this( - meterProviderSharedState, - meterSharedState, - type, - valueType, - name, - description, - unit, - Advice.builder()); - } - - AbstractInstrumentBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, InstrumentType type, InstrumentValueType valueType, - String name, - String description, - String unit, - Advice.AdviceBuilder adviceBuilder) { + MeterProviderSharedState meterProviderSharedState, + MeterSharedState meterSharedState) { + this.name = name; this.type = type; this.valueType = valueType; - this.instrumentName = name; - this.description = description; - this.unit = unit; this.meterProviderSharedState = meterProviderSharedState; this.meterSharedState = meterSharedState; - this.adviceBuilder = adviceBuilder; } - protected abstract BuilderT getThis(); - - public BuilderT setUnit(String unit) { + InstrumentBuilder setUnit(String unit) { this.unit = unit; - return getThis(); + return this; + } + + InstrumentBuilder setAdviceBuilder(Advice.AdviceBuilder adviceBuilder) { + this.adviceBuilder = adviceBuilder; + return this; } - public BuilderT setDescription(String description) { + InstrumentBuilder setDescription(String description) { this.description = description; - return getThis(); + return this; } - protected T swapBuilder(SwapBuilder swapper) { + T swapBuilder(SwapBuilder swapper) { return swapper.newBuilder( - meterProviderSharedState, - meterSharedState, - instrumentName, - description, - unit, - adviceBuilder); + meterProviderSharedState, meterSharedState, name, description, unit, adviceBuilder); } - final I buildSynchronousInstrument( + I buildSynchronousInstrument( BiFunction instrumentFactory) { - InstrumentDescriptor descriptor = - InstrumentDescriptor.create( - instrumentName, description, unit, type, valueType, adviceBuilder.build()); + InstrumentDescriptor descriptor = newDescriptor(); WriteableMetricStorage storage = meterSharedState.registerSynchronousMetricStorage(descriptor, meterProviderSharedState); return instrumentFactory.apply(descriptor, storage); } - final SdkObservableInstrument registerDoubleAsynchronousInstrument( + SdkObservableInstrument buildDoubleAsynchronousInstrument( InstrumentType type, Consumer updater) { SdkObservableMeasurement sdkObservableMeasurement = buildObservableMeasurement(type); Runnable runnable = () -> updater.accept(sdkObservableMeasurement); @@ -113,7 +83,7 @@ final SdkObservableInstrument registerDoubleAsynchronousInstrument( return new SdkObservableInstrument(meterSharedState, callbackRegistration); } - final SdkObservableInstrument registerLongAsynchronousInstrument( + SdkObservableInstrument buildLongAsynchronousInstrument( InstrumentType type, Consumer updater) { SdkObservableMeasurement sdkObservableMeasurement = buildObservableMeasurement(type); Runnable runnable = () -> updater.accept(sdkObservableMeasurement); @@ -123,20 +93,24 @@ final SdkObservableInstrument registerLongAsynchronousInstrument( return new SdkObservableInstrument(meterSharedState, callbackRegistration); } - final SdkObservableMeasurement buildObservableMeasurement(InstrumentType type) { - InstrumentDescriptor descriptor = - InstrumentDescriptor.create( - instrumentName, description, unit, type, valueType, adviceBuilder.build()); + SdkObservableMeasurement buildObservableMeasurement(InstrumentType type) { + this.type = type; + InstrumentDescriptor descriptor = newDescriptor(); return meterSharedState.registerObservableMeasurement(descriptor); } + private InstrumentDescriptor newDescriptor() { + return InstrumentDescriptor.create( + name, description, unit, type, valueType, adviceBuilder.build()); + } + @Override public String toString() { - return this.getClass().getSimpleName() - + "{descriptor=" - + InstrumentDescriptor.create( - instrumentName, description, unit, type, valueType, adviceBuilder.build()) - + "}"; + return toStringHelper(getClass().getSimpleName()); + } + + String toStringHelper(String className) { + return className + "{descriptor=" + newDescriptor() + "}"; } @FunctionalInterface @@ -149,4 +123,12 @@ T newBuilder( String unit, Advice.AdviceBuilder adviceBuilder); } + + void setAdviceAttributes(List> attributes) { + adviceBuilder.setAttributes(attributes); + } + + void setExplicitBucketBoundaries(List bucketBoundaries) { + adviceBuilder.setExplicitBucketBoundaries(bucketBoundaries); + } } 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 1b54f8a623d..d92cd29076c 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 @@ -8,6 +8,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleCounter; +import io.opentelemetry.api.metrics.DoubleCounterBuilder; import io.opentelemetry.api.metrics.ObservableDoubleCounter; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; import io.opentelemetry.context.Context; @@ -57,9 +58,9 @@ public void add(double increment) { add(increment, Attributes.empty()); } - static final class SdkDoubleCounterBuilder - extends AbstractInstrumentBuilder - implements ExtendedDoubleCounterBuilder { + static final class SdkDoubleCounterBuilder implements ExtendedDoubleCounterBuilder { + + private final InstrumentBuilder builder; SdkDoubleCounterBuilder( MeterProviderSharedState meterProviderSharedState, @@ -68,42 +69,55 @@ static final class SdkDoubleCounterBuilder String description, String unit, Advice.AdviceBuilder adviceBuilder) { - super( - meterProviderSharedState, - sharedState, - InstrumentType.COUNTER, - InstrumentValueType.DOUBLE, - name, - description, - unit, - adviceBuilder); + this.builder = + new InstrumentBuilder( + name, + InstrumentType.COUNTER, + InstrumentValueType.DOUBLE, + meterProviderSharedState, + sharedState) + .setUnit(unit) + .setDescription(description) + .setAdviceBuilder(adviceBuilder); + } + + @Override + public SdkDoubleCounter build() { + return builder.buildSynchronousInstrument(SdkDoubleCounter::new); } @Override - protected SdkDoubleCounterBuilder getThis() { + public DoubleCounterBuilder setDescription(String description) { + builder.setDescription(description); return this; } @Override - public SdkDoubleCounter build() { - return buildSynchronousInstrument(SdkDoubleCounter::new); + public DoubleCounterBuilder setUnit(String unit) { + builder.setUnit(unit); + return this; } @Override public ObservableDoubleCounter buildWithCallback( Consumer callback) { - return registerDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback); + return builder.buildDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback); } @Override public ObservableDoubleMeasurement buildObserver() { - return buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER); + return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER); } @Override public ExtendedDoubleCounterBuilder setAttributesAdvice(List> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); + } } } 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 008745b2652..e9f852510b5 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 @@ -7,6 +7,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.DoubleGaugeBuilder; import io.opentelemetry.api.metrics.LongGaugeBuilder; import io.opentelemetry.api.metrics.ObservableDoubleGauge; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; @@ -39,55 +40,67 @@ public void set(double increment) { set(increment, Attributes.empty()); } - static final class SdkDoubleGaugeBuilder extends AbstractInstrumentBuilder - implements ExtendedDoubleGaugeBuilder { + static final class SdkDoubleGaugeBuilder implements ExtendedDoubleGaugeBuilder { + private final InstrumentBuilder builder; SdkDoubleGaugeBuilder( MeterProviderSharedState meterProviderSharedState, MeterSharedState meterSharedState, String name) { - super( - meterProviderSharedState, - meterSharedState, - // TODO: use InstrumentType.GAUGE when available - InstrumentType.OBSERVABLE_GAUGE, - InstrumentValueType.DOUBLE, - name, - "", - DEFAULT_UNIT); + + // TODO: use InstrumentType.GAUGE when available + builder = + new InstrumentBuilder( + name, + InstrumentType.OBSERVABLE_GAUGE, + InstrumentValueType.DOUBLE, + meterProviderSharedState, + meterSharedState); } @Override - protected SdkDoubleGaugeBuilder getThis() { + public DoubleGaugeBuilder setDescription(String description) { + builder.setDescription(description); + return this; + } + + @Override + public DoubleGaugeBuilder setUnit(String unit) { + builder.setUnit(unit); return this; } @Override public SdkDoubleGauge build() { - return buildSynchronousInstrument(SdkDoubleGauge::new); + return builder.buildSynchronousInstrument(SdkDoubleGauge::new); } @Override public ExtendedDoubleGaugeBuilder setAttributesAdvice(List> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } @Override public LongGaugeBuilder ofLongs() { - return swapBuilder(SdkLongGauge.SdkLongGaugeBuilder::new); + return builder.swapBuilder(SdkLongGauge.SdkLongGaugeBuilder::new); } @Override public ObservableDoubleGauge buildWithCallback(Consumer callback) { // TODO: use InstrumentType.GAUGE when available - return registerDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); + return builder.buildDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); } @Override public ObservableDoubleMeasurement buildObserver() { // TODO: use InstrumentType.GAUGE when available - return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE); + return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE); + } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); } } } 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 c2e8d9b4126..64a0daac68e 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 @@ -8,6 +8,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; import io.opentelemetry.api.metrics.LongHistogramBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder; @@ -54,50 +55,61 @@ public void record(double value) { record(value, Attributes.empty()); } - static final class SdkDoubleHistogramBuilder - extends AbstractInstrumentBuilder - implements ExtendedDoubleHistogramBuilder { + static final class SdkDoubleHistogramBuilder implements ExtendedDoubleHistogramBuilder { + + private final InstrumentBuilder builder; SdkDoubleHistogramBuilder( MeterProviderSharedState meterProviderSharedState, MeterSharedState meterSharedState, String name) { - super( - meterProviderSharedState, - meterSharedState, - InstrumentType.HISTOGRAM, - InstrumentValueType.DOUBLE, - name, - "", - DEFAULT_UNIT); + builder = + new InstrumentBuilder( + name, + InstrumentType.HISTOGRAM, + InstrumentValueType.DOUBLE, + meterProviderSharedState, + meterSharedState); + } + + @Override + public DoubleHistogramBuilder setDescription(String description) { + builder.setDescription(description); + return this; } @Override - protected SdkDoubleHistogramBuilder getThis() { + public DoubleHistogramBuilder setUnit(String unit) { + builder.setUnit(unit); return this; } @Override public SdkDoubleHistogram build() { - return buildSynchronousInstrument(SdkDoubleHistogram::new); + return builder.buildSynchronousInstrument(SdkDoubleHistogram::new); } @Override public LongHistogramBuilder ofLongs() { - return swapBuilder(SdkLongHistogram.SdkLongHistogramBuilder::new); + return builder.swapBuilder(SdkLongHistogram.SdkLongHistogramBuilder::new); } @Override public ExtendedDoubleHistogramBuilder setExplicitBucketBoundariesAdvice( List bucketBoundaries) { - adviceBuilder.setExplicitBucketBoundaries(bucketBoundaries); + builder.setExplicitBucketBoundaries(bucketBoundaries); return this; } @Override public ExtendedDoubleHistogramBuilder setAttributesAdvice(List> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); + } } } 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 a11fd3f2f61..6521aeef871 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 @@ -8,6 +8,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleUpDownCounter; +import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; import io.opentelemetry.api.metrics.ObservableDoubleUpDownCounter; import io.opentelemetry.context.Context; @@ -44,9 +45,9 @@ public void add(double increment) { add(increment, Attributes.empty()); } - static final class SdkDoubleUpDownCounterBuilder - extends AbstractInstrumentBuilder - implements ExtendedDoubleUpDownCounterBuilder { + static final class SdkDoubleUpDownCounterBuilder implements ExtendedDoubleUpDownCounterBuilder { + + private final InstrumentBuilder builder; SdkDoubleUpDownCounterBuilder( MeterProviderSharedState meterProviderSharedState, @@ -55,44 +56,57 @@ static final class SdkDoubleUpDownCounterBuilder String description, String unit, Advice.AdviceBuilder adviceBuilder) { - super( - meterProviderSharedState, - sharedState, - InstrumentType.UP_DOWN_COUNTER, - InstrumentValueType.DOUBLE, - name, - description, - unit, - adviceBuilder); + this.builder = + new InstrumentBuilder( + name, + InstrumentType.UP_DOWN_COUNTER, + InstrumentValueType.DOUBLE, + meterProviderSharedState, + sharedState) + .setDescription(description) + .setUnit(unit) + .setAdviceBuilder(adviceBuilder); + } + + @Override + public DoubleUpDownCounterBuilder setDescription(String description) { + builder.setDescription(description); + return this; } @Override - protected SdkDoubleUpDownCounterBuilder getThis() { + public DoubleUpDownCounterBuilder setUnit(String unit) { + builder.setUnit(unit); return this; } @Override public DoubleUpDownCounter build() { - return buildSynchronousInstrument(SdkDoubleUpDownCounter::new); + return builder.buildSynchronousInstrument(SdkDoubleUpDownCounter::new); } @Override public ObservableDoubleUpDownCounter buildWithCallback( Consumer callback) { - return registerDoubleAsynchronousInstrument( + return builder.buildDoubleAsynchronousInstrument( InstrumentType.OBSERVABLE_UP_DOWN_COUNTER, callback); } @Override public ObservableDoubleMeasurement buildObserver() { - return buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); + return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); } @Override public ExtendedDoubleUpDownCounterBuilder setAttributesAdvice( List> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); + } } } 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 58383d9e783..98f806165af 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 @@ -9,6 +9,7 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleCounterBuilder; import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.LongCounterBuilder; import io.opentelemetry.api.metrics.ObservableLongCounter; import io.opentelemetry.api.metrics.ObservableLongMeasurement; import io.opentelemetry.context.Context; @@ -58,52 +59,64 @@ public void add(long increment) { add(increment, Attributes.empty()); } - static final class SdkLongCounterBuilder extends AbstractInstrumentBuilder - implements ExtendedLongCounterBuilder { + static final class SdkLongCounterBuilder implements ExtendedLongCounterBuilder { + + private final InstrumentBuilder builder; SdkLongCounterBuilder( MeterProviderSharedState meterProviderSharedState, MeterSharedState meterSharedState, String name) { - super( - meterProviderSharedState, - meterSharedState, - InstrumentType.COUNTER, - InstrumentValueType.LONG, - name, - "", - DEFAULT_UNIT); + this.builder = + new InstrumentBuilder( + name, + InstrumentType.COUNTER, + InstrumentValueType.LONG, + meterProviderSharedState, + meterSharedState); + } + + @Override + public LongCounterBuilder setDescription(String description) { + builder.setDescription(description); + return this; } @Override - protected SdkLongCounterBuilder getThis() { + public LongCounterBuilder setUnit(String unit) { + builder.setUnit(unit); return this; } @Override public SdkLongCounter build() { - return buildSynchronousInstrument(SdkLongCounter::new); + return builder.buildSynchronousInstrument(SdkLongCounter::new); } @Override public DoubleCounterBuilder ofDoubles() { - return swapBuilder(SdkDoubleCounter.SdkDoubleCounterBuilder::new); + return builder.swapBuilder(SdkDoubleCounter.SdkDoubleCounterBuilder::new); } @Override public ObservableLongCounter buildWithCallback(Consumer callback) { - return registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback); + return builder.buildLongAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback); } @Override public ObservableLongMeasurement buildObserver() { - return buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER); + return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER); } @Override public ExtendedLongCounterBuilder setAttributesAdvice(List> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); + } } } 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 24e6be8e6a5..73492eb9849 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 @@ -7,6 +7,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.LongGaugeBuilder; import io.opentelemetry.api.metrics.ObservableLongGauge; import io.opentelemetry.api.metrics.ObservableLongMeasurement; import io.opentelemetry.context.Context; @@ -39,8 +40,9 @@ public void set(long increment) { set(increment, Attributes.empty()); } - static final class SdkLongGaugeBuilder extends AbstractInstrumentBuilder - implements ExtendedLongGaugeBuilder { + static final class SdkLongGaugeBuilder implements ExtendedLongGaugeBuilder { + + private final InstrumentBuilder builder; SdkLongGaugeBuilder( MeterProviderSharedState meterProviderSharedState, @@ -49,44 +51,58 @@ static final class SdkLongGaugeBuilder extends AbstractInstrumentBuilder> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } @Override public ObservableLongGauge buildWithCallback(Consumer callback) { // TODO: use InstrumentType.GAUGE when available - return registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); + return builder.buildLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); } @Override public ObservableLongMeasurement buildObserver() { // TODO: use InstrumentType.GAUGE when available - return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE); + return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE); + } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); } } } 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 c087a0c282e..4e2e223fa90 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 @@ -8,6 +8,7 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.LongHistogram; +import io.opentelemetry.api.metrics.LongHistogramBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.extension.incubator.metrics.ExtendedLongHistogramBuilder; import io.opentelemetry.sdk.internal.ThrottlingLogger; @@ -55,9 +56,9 @@ public void record(long value) { record(value, Attributes.empty()); } - static final class SdkLongHistogramBuilder - extends AbstractInstrumentBuilder - implements ExtendedLongHistogramBuilder { + static final class SdkLongHistogramBuilder implements ExtendedLongHistogramBuilder { + + private final InstrumentBuilder builder; SdkLongHistogramBuilder( MeterProviderSharedState meterProviderSharedState, @@ -66,25 +67,33 @@ static final class SdkLongHistogramBuilder String description, String unit, Advice.AdviceBuilder adviceBuilder) { - super( - meterProviderSharedState, - sharedState, - InstrumentType.HISTOGRAM, - InstrumentValueType.LONG, - name, - description, - unit, - adviceBuilder); + builder = + new InstrumentBuilder( + name, + InstrumentType.HISTOGRAM, + InstrumentValueType.LONG, + meterProviderSharedState, + sharedState) + .setDescription(description) + .setUnit(unit) + .setAdviceBuilder(adviceBuilder); + } + + @Override + public LongHistogramBuilder setDescription(String description) { + builder.setDescription(description); + return this; } @Override - protected SdkLongHistogramBuilder getThis() { + public LongHistogramBuilder setUnit(String unit) { + builder.setUnit(unit); return this; } @Override public SdkLongHistogram build() { - return buildSynchronousInstrument(SdkLongHistogram::new); + return builder.buildSynchronousInstrument(SdkLongHistogram::new); } @Override @@ -92,14 +101,19 @@ public ExtendedLongHistogramBuilder setExplicitBucketBoundariesAdvice( List bucketBoundaries) { List doubleBoundaries = bucketBoundaries.stream().map(Long::doubleValue).collect(Collectors.toList()); - adviceBuilder.setExplicitBucketBoundaries(doubleBoundaries); + builder.setExplicitBucketBoundaries(doubleBoundaries); return this; } @Override public ExtendedLongHistogramBuilder setAttributesAdvice(List> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); + } } } 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 4351b28e5ce..9c0aff76c43 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 @@ -9,6 +9,7 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; import io.opentelemetry.api.metrics.LongUpDownCounter; +import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; import io.opentelemetry.api.metrics.ObservableLongMeasurement; import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; import io.opentelemetry.context.Context; @@ -44,55 +45,66 @@ public void add(long increment) { add(increment, Attributes.empty()); } - static final class SdkLongUpDownCounterBuilder - extends AbstractInstrumentBuilder - implements ExtendedLongUpDownCounterBuilder { + static final class SdkLongUpDownCounterBuilder implements ExtendedLongUpDownCounterBuilder { + + private final InstrumentBuilder builder; SdkLongUpDownCounterBuilder( MeterProviderSharedState meterProviderSharedState, MeterSharedState meterSharedState, String name) { - super( - meterProviderSharedState, - meterSharedState, - InstrumentType.UP_DOWN_COUNTER, - InstrumentValueType.LONG, - name, - "", - DEFAULT_UNIT); + this.builder = + new InstrumentBuilder( + name, + InstrumentType.UP_DOWN_COUNTER, + InstrumentValueType.LONG, + meterProviderSharedState, + meterSharedState); + } + + @Override + public LongUpDownCounterBuilder setDescription(String description) { + builder.setDescription(description); + return this; } @Override - protected SdkLongUpDownCounterBuilder getThis() { + public LongUpDownCounterBuilder setUnit(String unit) { + builder.setUnit(unit); return this; } @Override public LongUpDownCounter build() { - return buildSynchronousInstrument(SdkLongUpDownCounter::new); + return builder.buildSynchronousInstrument(SdkLongUpDownCounter::new); } @Override public DoubleUpDownCounterBuilder ofDoubles() { - return swapBuilder(SdkDoubleUpDownCounter.SdkDoubleUpDownCounterBuilder::new); + return builder.swapBuilder(SdkDoubleUpDownCounter.SdkDoubleUpDownCounterBuilder::new); } @Override public ObservableLongUpDownCounter buildWithCallback( Consumer callback) { - return registerLongAsynchronousInstrument( + return builder.buildLongAsynchronousInstrument( InstrumentType.OBSERVABLE_UP_DOWN_COUNTER, callback); } @Override public ObservableLongMeasurement buildObserver() { - return buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); + return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); } @Override public ExtendedLongUpDownCounterBuilder setAttributesAdvice(List> attributes) { - adviceBuilder.setAttributes(attributes); + builder.setAdviceAttributes(attributes); return this; } + + @Override + public String toString() { + return builder.toStringHelper(getClass().getSimpleName()); + } } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentBuilderTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentBuilderTest.java deleted file mode 100644 index 15f6ba3030f..00000000000 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentBuilderTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.metrics; - -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.sdk.common.InstrumentationScopeInfo; -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; -import org.junit.jupiter.api.Test; - -class AbstractInstrumentBuilderTest { - - @Test - void stringRepresentation() { - InstrumentationScopeInfo scope = InstrumentationScopeInfo.create("scope-name"); - TestInstrumentBuilder builder = - new TestInstrumentBuilder( - MeterProviderSharedState.create( - TestClock.create(), Resource.getDefault(), ExemplarFilter.alwaysOff(), 0), - MeterSharedState.create(scope, Collections.emptyList()), - InstrumentType.COUNTER, - InstrumentValueType.LONG, - "instrument-name", - "instrument-description", - "instrument-unit"); - assertThat(builder.toString()) - .isEqualTo( - "TestInstrumentBuilder{" - + "descriptor=" - + "InstrumentDescriptor{" - + "name=instrument-name, " - + "description=instrument-description, " - + "unit=instrument-unit, " - + "type=COUNTER, " - + "valueType=LONG, " - + "advice=Advice{explicitBucketBoundaries=null, attributes=null}" - + "}}"); - } - - private static class TestInstrumentBuilder - extends AbstractInstrumentBuilder { - - TestInstrumentBuilder( - MeterProviderSharedState meterProviderSharedState, - MeterSharedState meterSharedState, - InstrumentType type, - InstrumentValueType valueType, - String name, - String description, - String unit) { - super(meterProviderSharedState, meterSharedState, type, valueType, name, description, unit); - } - - @Override - protected TestInstrumentBuilder getThis() { - return this; - } - } -} 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 new file mode 100644 index 00000000000..a351c95d0e6 --- /dev/null +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/InstrumentBuilderTest.java @@ -0,0 +1,81 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +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; +import org.junit.jupiter.api.Test; + +class InstrumentBuilderTest { + + public static final MeterProviderSharedState PROVIDER_SHARED_STATE = + 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()); + + @Test + void stringRepresentation() { + InstrumentBuilder builder = + new InstrumentBuilder( + "instrument-name", + InstrumentType.COUNTER, + InstrumentValueType.LONG, + PROVIDER_SHARED_STATE, + METER_SHARED_STATE) + .setDescription("instrument-description") + .setUnit("instrument-unit") + .setAdviceBuilder(Advice.builder()); + assertThat(builder.toString()) + .isEqualTo( + "InstrumentBuilder{" + + "descriptor=" + + "InstrumentDescriptor{" + + "name=instrument-name, " + + "description=instrument-description, " + + "unit=instrument-unit, " + + "type=COUNTER, " + + "valueType=LONG, " + + "advice=Advice{explicitBucketBoundaries=null, attributes=null}" + + "}}"); + } + + @Test + void toStringHelper() { + InstrumentBuilder builder = + new InstrumentBuilder( + "instrument-name", + InstrumentType.HISTOGRAM, + InstrumentValueType.DOUBLE, + PROVIDER_SHARED_STATE, + METER_SHARED_STATE) + .setDescription("instrument-description") + .setUnit("instrument-unit") + .setAdviceBuilder(Advice.builder()); + + assertThat(builder.toStringHelper("FooBuilder")) + .isEqualTo( + "FooBuilder{" + + "descriptor=" + + "InstrumentDescriptor{" + + "name=instrument-name, " + + "description=instrument-description, " + + "unit=instrument-unit, " + + "type=HISTOGRAM, " + + "valueType=DOUBLE, " + + "advice=Advice{explicitBucketBoundaries=null, attributes=null}" + + "}}"); + } +}