diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractCounter.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractCounter.java index ddf2d0f09b4..64dcf299fa1 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractCounter.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractCounter.java @@ -19,8 +19,6 @@ import io.opentelemetry.metrics.Counter; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; abstract class AbstractCounter extends AbstractInstrumentWithBinding { @@ -28,17 +26,16 @@ abstract class AbstractCounter private final InstrumentValueType instrumentValueType; AbstractCounter( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, InstrumentValueType instrumentValueType, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean monotonic) { super( - name, description, unit, constantLabels, labelKeys, new ActiveBatcher(Batchers.getNoop())); + descriptor, + meterProviderSharedState, + instrumentationLibraryInfo, + new ActiveBatcher(Batchers.getNoop())); this.monotonic = monotonic; this.instrumentValueType = instrumentValueType; } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrument.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrument.java index 9fa1bdff002..5f43b20a9bd 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrument.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrument.java @@ -29,48 +29,33 @@ abstract class AbstractInstrument implements Instrument { - private final String name; - private final String description; - private final String unit; - private final Map constantLabels; - private final List labelKeys; + private final InstrumentDescriptor descriptor; + private final MeterProviderSharedState meterProviderSharedState; + private final InstrumentationLibraryInfo instrumentationLibraryInfo; private final ActiveBatcher activeBatcher; // All arguments cannot be null because they are checked in the abstract builder classes. AbstractInstrument( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, + MeterProviderSharedState meterProviderSharedState, + InstrumentationLibraryInfo instrumentationLibraryInfo, ActiveBatcher activeBatcher) { - this.name = name; - this.description = description; - this.unit = unit; - this.constantLabels = constantLabels; - this.labelKeys = labelKeys; - // TODO: Allow to install views from config instead of always installing the default View. + this.descriptor = descriptor; + this.meterProviderSharedState = meterProviderSharedState; + this.instrumentationLibraryInfo = instrumentationLibraryInfo; this.activeBatcher = activeBatcher; } - final String getName() { - return name; + final InstrumentDescriptor getDescriptor() { + return descriptor; } - final String getDescription() { - return description; + final MeterProviderSharedState getMeterProviderSharedState() { + return meterProviderSharedState; } - final String getUnit() { - return unit; - } - - final Map getConstantLabels() { - return constantLabels; - } - - final List getLabelKeys() { - return labelKeys; + final InstrumentationLibraryInfo getInstrumentationLibraryInfo() { + return instrumentationLibraryInfo; } final ActiveBatcher getActiveBatcher() { @@ -90,21 +75,12 @@ public boolean equals(Object o) { AbstractInstrument that = (AbstractInstrument) o; - return name.equals(that.name) - && description.equals(that.description) - && unit.equals(that.unit) - && constantLabels.equals(that.constantLabels) - && labelKeys.equals(that.labelKeys); + return descriptor.equals(that.descriptor); } @Override public int hashCode() { - int result = name.hashCode(); - result = 31 * result + description.hashCode(); - result = 31 * result + unit.hashCode(); - result = 31 * result + constantLabels.hashCode(); - result = 31 * result + labelKeys.hashCode(); - return result; + return descriptor.hashCode(); } abstract static class Builder, V> @@ -163,10 +139,6 @@ public final B setConstantLabels(Map constantLabels) { return getThis(); } - final String getName() { - return name; - } - final MeterProviderSharedState getMeterProviderSharedState() { return meterProviderSharedState; } @@ -175,20 +147,8 @@ final InstrumentationLibraryInfo getInstrumentationLibraryInfo() { return instrumentationLibraryInfo; } - final String getDescription() { - return description; - } - - final String getUnit() { - return unit; - } - - final List getLabelKeys() { - return labelKeys; - } - - final Map getConstantLabels() { - return constantLabels; + final InstrumentDescriptor getInstrumentDescriptor() { + return InstrumentDescriptor.create(name, description, unit, constantLabels, labelKeys); } abstract B getThis(); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrumentWithBinding.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrumentWithBinding.java index 3f1579a8e3a..b1dce983df7 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrumentWithBinding.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractInstrumentWithBinding.java @@ -17,6 +17,7 @@ package io.opentelemetry.sdk.metrics; import io.opentelemetry.metrics.LabelSet; +import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.data.MetricData; import java.util.List; import java.util.Map; @@ -29,13 +30,11 @@ abstract class AbstractInstrumentWithBinding private final ReentrantLock collectLock; AbstractInstrumentWithBinding( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, + MeterProviderSharedState meterProviderSharedState, + InstrumentationLibraryInfo instrumentationLibraryInfo, ActiveBatcher activeBatcher) { - super(name, description, unit, constantLabels, labelKeys, activeBatcher); + super(descriptor, meterProviderSharedState, instrumentationLibraryInfo, activeBatcher); boundLabels = new ConcurrentHashMap<>(); collectLock = new ReentrantLock(); } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractMeasure.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractMeasure.java index 7ba468b6ce7..255068e68f7 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractMeasure.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractMeasure.java @@ -19,8 +19,6 @@ import io.opentelemetry.metrics.Measure; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; abstract class AbstractMeasure extends AbstractInstrumentWithBinding { @@ -28,17 +26,16 @@ abstract class AbstractMeasure private final InstrumentValueType instrumentValueType; AbstractMeasure( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, InstrumentValueType instrumentValueType, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean absolute) { super( - name, description, unit, constantLabels, labelKeys, new ActiveBatcher(Batchers.getNoop())); + descriptor, + meterProviderSharedState, + instrumentationLibraryInfo, + new ActiveBatcher(Batchers.getNoop())); this.absolute = absolute; this.instrumentValueType = instrumentValueType; } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractObserver.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractObserver.java index f60731a0bda..7a5e45e02b9 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractObserver.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractObserver.java @@ -22,23 +22,22 @@ import io.opentelemetry.sdk.metrics.data.MetricData; import java.util.Collections; import java.util.List; -import java.util.Map; class AbstractObserver extends AbstractInstrument { private final boolean monotonic; private final InstrumentValueType instrumentValueType; AbstractObserver( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, InstrumentValueType instrumentValueType, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean monotonic) { - super(name, description, unit, constantLabels, labelKeys, null); + super( + descriptor, + meterProviderSharedState, + instrumentationLibraryInfo, + new ActiveBatcher(Batchers.getNoop())); this.monotonic = monotonic; this.instrumentValueType = instrumentValueType; } diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleCounterSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleCounterSdk.java index 34e8b2d3ba9..1148de2e401 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleCounterSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleCounterSdk.java @@ -21,26 +21,16 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.DoubleCounterSdk.BoundInstrument; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; final class DoubleCounterSdk extends AbstractCounter implements DoubleCounter { private DoubleCounterSdk( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, boolean monotonic, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo) { super( - name, - description, - unit, - constantLabels, - labelKeys, + descriptor, InstrumentValueType.DOUBLE, meterProviderSharedState, instrumentationLibraryInfo, @@ -108,11 +98,7 @@ Builder getThis() { @Override public DoubleCounter build() { return new DoubleCounterSdk( - getName(), - getDescription(), - getUnit(), - getConstantLabels(), - getLabelKeys(), + getInstrumentDescriptor(), isMonotonic(), getMeterProviderSharedState(), getInstrumentationLibraryInfo()); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdk.java index 377780b6c4d..7cdb7493938 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdk.java @@ -21,26 +21,16 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.DoubleMeasureSdk.BoundInstrument; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; final class DoubleMeasureSdk extends AbstractMeasure implements DoubleMeasure { private DoubleMeasureSdk( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean absolute) { super( - name, - description, - unit, - constantLabels, - labelKeys, + descriptor, InstrumentValueType.DOUBLE, meterProviderSharedState, instrumentationLibraryInfo, @@ -108,11 +98,7 @@ Builder getThis() { @Override public DoubleMeasure build() { return new DoubleMeasureSdk( - getName(), - getDescription(), - getUnit(), - getConstantLabels(), - getLabelKeys(), + getInstrumentDescriptor(), getMeterProviderSharedState(), getInstrumentationLibraryInfo(), isAbsolute()); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleObserverSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleObserverSdk.java index b0b66d4623e..aac9b63cc27 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleObserverSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleObserverSdk.java @@ -19,26 +19,16 @@ import io.opentelemetry.metrics.DoubleObserver; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; final class DoubleObserverSdk extends AbstractObserver implements DoubleObserver { DoubleObserverSdk( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean monotonic) { super( - name, - description, - unit, - constantLabels, - labelKeys, - InstrumentValueType.DOUBLE, + descriptor, + InstrumentValueType.LONG, meterProviderSharedState, instrumentationLibraryInfo, monotonic); @@ -75,11 +65,7 @@ Builder getThis() { @Override public DoubleObserver build() { return new DoubleObserverSdk( - getName(), - getDescription(), - getUnit(), - getConstantLabels(), - getLabelKeys(), + getInstrumentDescriptor(), getMeterProviderSharedState(), getInstrumentationLibraryInfo(), isMonotonic()); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/InstrumentDescriptor.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/InstrumentDescriptor.java new file mode 100644 index 00000000000..e7b819847bf --- /dev/null +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/InstrumentDescriptor.java @@ -0,0 +1,45 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.sdk.metrics; + +import com.google.auto.value.AutoValue; +import java.util.List; +import java.util.Map; +import javax.annotation.concurrent.Immutable; + +@AutoValue +@Immutable +abstract class InstrumentDescriptor { + static InstrumentDescriptor create( + String name, + String description, + String unit, + Map constantLabels, + List labelKeys) { + return new AutoValue_InstrumentDescriptor(name, description, unit, constantLabels, labelKeys); + } + + abstract String getName(); + + abstract String getDescription(); + + abstract String getUnit(); + + abstract Map getConstantLabels(); + + abstract List getLabelKeys(); +} diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongCounterSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongCounterSdk.java index 11924bd96c7..52d5edd5fc4 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongCounterSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongCounterSdk.java @@ -21,26 +21,16 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.LongCounterSdk.BoundInstrument; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; final class LongCounterSdk extends AbstractCounter implements LongCounter { private LongCounterSdk( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean monotonic) { super( - name, - description, - unit, - constantLabels, - labelKeys, + descriptor, InstrumentValueType.LONG, meterProviderSharedState, instrumentationLibraryInfo, @@ -108,11 +98,7 @@ Builder getThis() { @Override public LongCounter build() { return new LongCounterSdk( - getName(), - getDescription(), - getUnit(), - getConstantLabels(), - getLabelKeys(), + getInstrumentDescriptor(), getMeterProviderSharedState(), getInstrumentationLibraryInfo(), isMonotonic()); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongMeasureSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongMeasureSdk.java index d5bc5b89ccd..36fbfa55a02 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongMeasureSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongMeasureSdk.java @@ -21,26 +21,16 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.LongMeasureSdk.BoundInstrument; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; final class LongMeasureSdk extends AbstractMeasure implements LongMeasure { private LongMeasureSdk( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean absolute) { super( - name, - description, - unit, - constantLabels, - labelKeys, + descriptor, InstrumentValueType.LONG, meterProviderSharedState, instrumentationLibraryInfo, @@ -109,11 +99,7 @@ Builder getThis() { @Override public LongMeasure build() { return new LongMeasureSdk( - getName(), - getDescription(), - getUnit(), - getConstantLabels(), - getLabelKeys(), + getInstrumentDescriptor(), getMeterProviderSharedState(), getInstrumentationLibraryInfo(), isAbsolute()); diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongObserverSdk.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongObserverSdk.java index 2de14c5eee6..671433f1a9c 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongObserverSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongObserverSdk.java @@ -19,25 +19,15 @@ import io.opentelemetry.metrics.LongObserver; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; -import java.util.List; -import java.util.Map; final class LongObserverSdk extends AbstractObserver implements LongObserver { LongObserverSdk( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys, + InstrumentDescriptor descriptor, MeterProviderSharedState meterProviderSharedState, InstrumentationLibraryInfo instrumentationLibraryInfo, boolean monotonic) { super( - name, - description, - unit, - constantLabels, - labelKeys, + descriptor, InstrumentValueType.LONG, meterProviderSharedState, instrumentationLibraryInfo, @@ -75,11 +65,7 @@ Builder getThis() { @Override public LongObserver build() { return new LongObserverSdk( - getName(), - getDescription(), - getUnit(), - getConstantLabels(), - getLabelKeys(), + getInstrumentDescriptor(), getMeterProviderSharedState(), getInstrumentationLibraryInfo(), isMonotonic()); diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterBuilderTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterBuilderTest.java index e2ef0a755dd..654789e988f 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterBuilderTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterBuilderTest.java @@ -19,10 +19,12 @@ import static com.google.common.truth.Truth.assertThat; import io.opentelemetry.metrics.Counter; -import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument; import io.opentelemetry.metrics.LabelSet; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.internal.TestClock; +import io.opentelemetry.sdk.metrics.aggregator.Aggregator; +import io.opentelemetry.sdk.metrics.aggregator.NoopAggregator; +import io.opentelemetry.sdk.metrics.common.InstrumentValueType; import io.opentelemetry.sdk.resources.Resource; import org.junit.Rule; import org.junit.Test; @@ -43,28 +45,29 @@ public class AbstractCounterBuilderTest { @Test public void defaultValue() { - TestInstrumentBuilder testMetricBuilder = + TestInstrumentBuilder testInstrumentBuilder = new TestInstrumentBuilder(NAME, METER_SHARED_STATE, INSTRUMENTATION_LIBRARY_INFO); - assertThat(testMetricBuilder.getName()).isEqualTo(NAME); - assertThat(testMetricBuilder.getDescription()).isEmpty(); - assertThat(testMetricBuilder.getUnit()).isEqualTo("1"); - assertThat(testMetricBuilder.getLabelKeys()).isEmpty(); - assertThat(testMetricBuilder.getConstantLabels()).isEmpty(); - assertThat(testMetricBuilder.isMonotonic()).isTrue(); - assertThat(testMetricBuilder.getMeterProviderSharedState()).isEqualTo(METER_SHARED_STATE); - assertThat(testMetricBuilder.getInstrumentationLibraryInfo()) + assertThat(testInstrumentBuilder.isMonotonic()).isTrue(); + assertThat(testInstrumentBuilder.getMeterProviderSharedState()).isEqualTo(METER_SHARED_STATE); + assertThat(testInstrumentBuilder.getInstrumentationLibraryInfo()) .isEqualTo(INSTRUMENTATION_LIBRARY_INFO); - assertThat(testMetricBuilder.build()).isInstanceOf(TestInstrument.class); + + TestInstrument testInstrument = testInstrumentBuilder.build(); + assertThat(testInstrument).isInstanceOf(TestInstrument.class); + assertThat(testInstrument.getDescriptor().getName()).isEqualTo(NAME); + assertThat(testInstrument.getDescriptor().getDescription()).isEmpty(); + assertThat(testInstrument.getDescriptor().getUnit()).isEqualTo("1"); + assertThat(testInstrument.getDescriptor().getLabelKeys()).isEmpty(); + assertThat(testInstrument.getDescriptor().getConstantLabels()).isEmpty(); } @Test public void setAndGetValues() { - TestInstrumentBuilder testMetricBuilder = + TestInstrumentBuilder testInstrumentBuilder = new TestInstrumentBuilder(NAME, METER_SHARED_STATE, INSTRUMENTATION_LIBRARY_INFO) .setMonotonic(false); - assertThat(testMetricBuilder.getName()).isEqualTo(NAME); - assertThat(testMetricBuilder.isMonotonic()).isFalse(); - assertThat(testMetricBuilder.build()).isInstanceOf(TestInstrument.class); + assertThat(testInstrumentBuilder.isMonotonic()).isFalse(); + assertThat(testInstrumentBuilder.build()).isInstanceOf(TestInstrument.class); } private static final class TestInstrumentBuilder @@ -83,21 +86,43 @@ TestInstrumentBuilder getThis() { @Override public TestInstrument build() { - return new TestInstrument(); + return new TestInstrument( + getInstrumentDescriptor(), + getMeterProviderSharedState(), + getInstrumentationLibraryInfo(), + isMonotonic()); } } - private static final class TestInstrument implements Counter { - private static final TestBoundInstrument HANDLE = new TestBoundInstrument(); + private static final class TestInstrument extends AbstractCounter + implements Counter { + TestInstrument( + InstrumentDescriptor descriptor, + MeterProviderSharedState meterProviderSharedState, + InstrumentationLibraryInfo instrumentationLibraryInfo, + boolean monotonic) { + super( + descriptor, + InstrumentValueType.LONG, + meterProviderSharedState, + instrumentationLibraryInfo, + monotonic); + } @Override - public TestBoundInstrument bind(LabelSet labelSet) { - return HANDLE; + TestBoundCounter newBinding(Batcher batcher) { + return new TestBoundCounter(NoopAggregator.getFactory().getAggregator()); } - } - private static final class TestBoundInstrument implements BoundInstrument { @Override - public void unbind() {} + public TestBoundCounter bind(LabelSet labelSet) { + return bindInternal(labelSet); + } + } + + private static final class TestBoundCounter extends AbstractBoundInstrument { + private TestBoundCounter(Aggregator aggregator) { + super(aggregator); + } } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterTest.java index 884c2aff6da..eb16d73d3ca 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterTest.java @@ -25,8 +25,6 @@ import io.opentelemetry.sdk.metrics.common.InstrumentValueType; import io.opentelemetry.sdk.resources.Resource; import java.util.Collections; -import java.util.List; -import java.util.Map; import org.junit.Test; public class AbstractCounterTest { @@ -60,12 +58,13 @@ public void attributeValue_EqualsAndHashCode() { } private static final class TestCounterInstrument extends AbstractCounter { - private static final String NAME = "name"; - private static final String DESCRIPTION = "description"; - private static final String UNIT = "1"; - private static final Map CONSTANT_LABELS = - Collections.singletonMap("key_2", "value_2"); - private static final List LABEL_KEY = Collections.singletonList("key"); + private static final InstrumentDescriptor INSTRUMENT_DESCRIPTOR = + InstrumentDescriptor.create( + "name", + "description", + "1", + Collections.singletonMap("key_2", "value_2"), + Collections.singletonList("key")); private static final MeterProviderSharedState METER_SHARED_STATE = MeterProviderSharedState.create(TestClock.create(), Resource.getEmpty()); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = @@ -73,11 +72,7 @@ private static final class TestCounterInstrument extends AbstractCountersingletonList(null)); + .setLabelKeys(Collections.singletonList(null)) + .build(); } @Test @@ -126,19 +131,21 @@ public void preventNull_ConstantLabels() { thrown.expect(NullPointerException.class); thrown.expectMessage("constantLabels"); new TestInstrumentBuilder(NAME, METER_SHARED_STATE, INSTRUMENTATION_LIBRARY_INFO) - .setConstantLabels(null); + .setConstantLabels(null) + .build(); } @Test public void defaultValue() { TestInstrumentBuilder testInstrumentBuilder = new TestInstrumentBuilder(NAME, METER_SHARED_STATE, INSTRUMENTATION_LIBRARY_INFO); - assertThat(testInstrumentBuilder.getName()).isEqualTo(NAME); - assertThat(testInstrumentBuilder.getDescription()).isEmpty(); - assertThat(testInstrumentBuilder.getUnit()).isEqualTo("1"); - assertThat(testInstrumentBuilder.getLabelKeys()).isEmpty(); - assertThat(testInstrumentBuilder.getConstantLabels()).isEmpty(); - assertThat(testInstrumentBuilder.build()).isInstanceOf(TestInstrument.class); + TestInstrument testInstrument = testInstrumentBuilder.build(); + assertThat(testInstrument).isInstanceOf(TestInstrument.class); + assertThat(testInstrument.getDescriptor().getName()).isEqualTo(NAME); + assertThat(testInstrument.getDescriptor().getDescription()).isEmpty(); + assertThat(testInstrument.getDescriptor().getUnit()).isEqualTo("1"); + assertThat(testInstrument.getDescriptor().getLabelKeys()).isEmpty(); + assertThat(testInstrument.getDescriptor().getConstantLabels()).isEmpty(); } @Test @@ -149,15 +156,17 @@ public void setAndGetValues() { .setUnit(UNIT) .setLabelKeys(LABEL_KEY) .setConstantLabels(CONSTANT_LABELS); - assertThat(testInstrumentBuilder.getName()).isEqualTo(NAME); - assertThat(testInstrumentBuilder.getDescription()).isEqualTo(DESCRIPTION); - assertThat(testInstrumentBuilder.getUnit()).isEqualTo(UNIT); - assertThat(testInstrumentBuilder.getLabelKeys()).isEqualTo(LABEL_KEY); - assertThat(testInstrumentBuilder.getConstantLabels()).isEqualTo(CONSTANT_LABELS); assertThat(testInstrumentBuilder.getMeterProviderSharedState()).isEqualTo(METER_SHARED_STATE); assertThat(testInstrumentBuilder.getInstrumentationLibraryInfo()) .isEqualTo(INSTRUMENTATION_LIBRARY_INFO); - assertThat(testInstrumentBuilder.build()).isInstanceOf(TestInstrument.class); + + TestInstrument testInstrument = testInstrumentBuilder.build(); + assertThat(testInstrument).isInstanceOf(TestInstrument.class); + assertThat(testInstrument.getDescriptor().getName()).isEqualTo(NAME); + assertThat(testInstrument.getDescriptor().getDescription()).isEqualTo(DESCRIPTION); + assertThat(testInstrument.getDescriptor().getUnit()).isEqualTo(UNIT); + assertThat(testInstrument.getDescriptor().getLabelKeys()).isEqualTo(LABEL_KEY); + assertThat(testInstrument.getDescriptor().getConstantLabels()).isEqualTo(CONSTANT_LABELS); } private static final class TestInstrumentBuilder @@ -176,9 +185,24 @@ TestInstrumentBuilder getThis() { @Override public TestInstrument build() { - return new TestInstrument(); + return new TestInstrument( + getInstrumentDescriptor(), + getMeterProviderSharedState(), + getInstrumentationLibraryInfo()); } } - private static final class TestInstrument implements Instrument {} + private static final class TestInstrument extends AbstractInstrument { + TestInstrument( + InstrumentDescriptor descriptor, + MeterProviderSharedState meterProviderSharedState, + InstrumentationLibraryInfo instrumentationLibraryInfo) { + super(descriptor, meterProviderSharedState, instrumentationLibraryInfo, null); + } + + @Override + List collect() { + return Collections.emptyList(); + } + } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentTest.java index 4eeb0b9d09f..29fb56debba 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractInstrumentTest.java @@ -18,10 +18,12 @@ import static com.google.common.truth.Truth.assertThat; +import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; +import io.opentelemetry.sdk.internal.TestClock; import io.opentelemetry.sdk.metrics.data.MetricData; +import io.opentelemetry.sdk.resources.Resource; import java.util.Collections; import java.util.List; -import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -29,32 +31,41 @@ /** Unit tests for {@link AbstractInstrument}. */ @RunWith(JUnit4.class) public class AbstractInstrumentTest { - private static final String NAME = "name"; - private static final String DESCRIPTION = "description"; - private static final String UNIT = "1"; - private static final Map CONSTANT_LABELS = - Collections.singletonMap("key_2", "value_2"); - private static final List LABEL_KEY = Collections.singletonList("key"); + private static final InstrumentDescriptor INSTRUMENT_DESCRIPTOR = + InstrumentDescriptor.create( + "name", + "description", + "1", + Collections.singletonMap("key_2", "value_2"), + Collections.singletonList("key")); + private static final MeterProviderSharedState METER_SHARED_STATE = + MeterProviderSharedState.create(TestClock.create(), Resource.getEmpty()); + private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = + InstrumentationLibraryInfo.create("test_abstract_instrument", ""); + private static final ActiveBatcher ACTIVE_BATCHER = new ActiveBatcher(Batchers.getNoop()); @Test public void getValues() { TestInstrument testInstrument = - new TestInstrument(NAME, DESCRIPTION, UNIT, CONSTANT_LABELS, LABEL_KEY); - assertThat(testInstrument.getName()).isEqualTo(NAME); - assertThat(testInstrument.getDescription()).isEqualTo(DESCRIPTION); - assertThat(testInstrument.getUnit()).isEqualTo(UNIT); - assertThat(testInstrument.getConstantLabels()).isEqualTo(CONSTANT_LABELS); - assertThat(testInstrument.getLabelKeys()).isEqualTo(LABEL_KEY); + new TestInstrument( + INSTRUMENT_DESCRIPTOR, + METER_SHARED_STATE, + INSTRUMENTATION_LIBRARY_INFO, + ACTIVE_BATCHER); + assertThat(testInstrument.getDescriptor()).isSameInstanceAs(INSTRUMENT_DESCRIPTOR); + assertThat(testInstrument.getMeterProviderSharedState()).isSameInstanceAs(METER_SHARED_STATE); + assertThat(testInstrument.getInstrumentationLibraryInfo()) + .isSameInstanceAs(INSTRUMENTATION_LIBRARY_INFO); + assertThat(testInstrument.getActiveBatcher()).isSameInstanceAs(ACTIVE_BATCHER); } private static final class TestInstrument extends AbstractInstrument { TestInstrument( - String name, - String description, - String unit, - Map constantLabels, - List labelKeys) { - super(name, description, unit, constantLabels, labelKeys, null); + InstrumentDescriptor descriptor, + MeterProviderSharedState meterSharedState, + InstrumentationLibraryInfo instrumentationLibraryInfo, + ActiveBatcher activeBatcher) { + super(descriptor, meterSharedState, instrumentationLibraryInfo, activeBatcher); } @Override diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureBuilderTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureBuilderTest.java index 4f4807f2895..3b89b311fe6 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureBuilderTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureBuilderTest.java @@ -18,11 +18,13 @@ import static com.google.common.truth.Truth.assertThat; -import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument; import io.opentelemetry.metrics.LabelSet; import io.opentelemetry.metrics.Measure; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.internal.TestClock; +import io.opentelemetry.sdk.metrics.aggregator.Aggregator; +import io.opentelemetry.sdk.metrics.aggregator.NoopAggregator; +import io.opentelemetry.sdk.metrics.common.InstrumentValueType; import io.opentelemetry.sdk.resources.Resource; import org.junit.Rule; import org.junit.Test; @@ -43,28 +45,29 @@ public class AbstractMeasureBuilderTest { @Test public void defaultValue() { - TestInstrumentBuilder testMetricBuilder = + TestInstrumentBuilder testInstrumentBuilder = new TestInstrumentBuilder(NAME, METER_SHARED_STATE, INSTRUMENTATION_LIBRARY_INFO); - assertThat(testMetricBuilder.getName()).isEqualTo(NAME); - assertThat(testMetricBuilder.getDescription()).isEmpty(); - assertThat(testMetricBuilder.getUnit()).isEqualTo("1"); - assertThat(testMetricBuilder.getLabelKeys()).isEmpty(); - assertThat(testMetricBuilder.getConstantLabels()).isEmpty(); - assertThat(testMetricBuilder.isAbsolute()).isTrue(); - assertThat(testMetricBuilder.getMeterProviderSharedState()).isEqualTo(METER_SHARED_STATE); - assertThat(testMetricBuilder.getInstrumentationLibraryInfo()) + assertThat(testInstrumentBuilder.isAbsolute()).isTrue(); + assertThat(testInstrumentBuilder.getMeterProviderSharedState()).isEqualTo(METER_SHARED_STATE); + assertThat(testInstrumentBuilder.getInstrumentationLibraryInfo()) .isEqualTo(INSTRUMENTATION_LIBRARY_INFO); - assertThat(testMetricBuilder.build()).isInstanceOf(TestInstrument.class); + + TestInstrument testInstrument = testInstrumentBuilder.build(); + assertThat(testInstrument).isInstanceOf(TestInstrument.class); + assertThat(testInstrument.getDescriptor().getName()).isEqualTo(NAME); + assertThat(testInstrument.getDescriptor().getDescription()).isEmpty(); + assertThat(testInstrument.getDescriptor().getUnit()).isEqualTo("1"); + assertThat(testInstrument.getDescriptor().getLabelKeys()).isEmpty(); + assertThat(testInstrument.getDescriptor().getConstantLabels()).isEmpty(); } @Test public void setAndGetValues() { - TestInstrumentBuilder testMetricBuilder = + TestInstrumentBuilder testInstrumentBuilder = new TestInstrumentBuilder(NAME, METER_SHARED_STATE, INSTRUMENTATION_LIBRARY_INFO) .setAbsolute(false); - assertThat(testMetricBuilder.getName()).isEqualTo(NAME); - assertThat(testMetricBuilder.isAbsolute()).isFalse(); - assertThat(testMetricBuilder.build()).isInstanceOf(TestInstrument.class); + assertThat(testInstrumentBuilder.isAbsolute()).isFalse(); + assertThat(testInstrumentBuilder.build()).isInstanceOf(TestInstrument.class); } private static final class TestInstrumentBuilder @@ -83,21 +86,44 @@ TestInstrumentBuilder getThis() { @Override public TestInstrument build() { - return new TestInstrument(); + return new TestInstrument( + getInstrumentDescriptor(), + getMeterProviderSharedState(), + getInstrumentationLibraryInfo(), + isAbsolute()); } } - private static final class TestInstrument implements Measure { - private static final TestBoundMeasure HANDLE = new TestBoundMeasure(); + private static final class TestInstrument extends AbstractMeasure + implements Measure { + + TestInstrument( + InstrumentDescriptor descriptor, + MeterProviderSharedState meterSharedState, + InstrumentationLibraryInfo instrumentationLibraryInfo, + boolean absolute) { + super( + descriptor, + InstrumentValueType.LONG, + meterSharedState, + instrumentationLibraryInfo, + absolute); + } + + @Override + TestBoundMeasure newBinding(Batcher batcher) { + return new TestBoundMeasure(NoopAggregator.getFactory().getAggregator()); + } @Override public TestBoundMeasure bind(LabelSet labelSet) { - return HANDLE; + return bindInternal(labelSet); } } - private static final class TestBoundMeasure implements BoundInstrument { - @Override - public void unbind() {} + private static final class TestBoundMeasure extends AbstractBoundInstrument { + private TestBoundMeasure(Aggregator aggregator) { + super(aggregator); + } } } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureTest.java index 97c6b3715b9..9be37b8ae36 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureTest.java @@ -25,8 +25,6 @@ import io.opentelemetry.sdk.metrics.common.InstrumentValueType; import io.opentelemetry.sdk.resources.Resource; import java.util.Collections; -import java.util.List; -import java.util.Map; import org.junit.Test; public class AbstractMeasureTest { @@ -59,12 +57,13 @@ public void attributeValue_EqualsAndHashCode() { } private static final class TestMeasureInstrument extends AbstractMeasure { - private static final String NAME = "name"; - private static final String DESCRIPTION = "description"; - private static final String UNIT = "1"; - private static final Map CONSTANT_LABELS = - Collections.singletonMap("key_2", "value_2"); - private static final List LABEL_KEY = Collections.singletonList("key"); + private static final InstrumentDescriptor INSTRUMENT_DESCRIPTOR = + InstrumentDescriptor.create( + "name", + "description", + "1", + Collections.singletonMap("key_2", "value_2"), + Collections.singletonList("key")); private static final MeterProviderSharedState METER_SHARED_STATE = MeterProviderSharedState.create(TestClock.create(), Resource.getEmpty()); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = @@ -72,11 +71,7 @@ private static final class TestMeasureInstrument extends AbstractMeasure { + private static final class TestInstrument extends AbstractObserver + implements Observer { + + TestInstrument( + InstrumentDescriptor descriptor, + MeterProviderSharedState meterSharedState, + InstrumentationLibraryInfo instrumentationLibraryInfo, + boolean monotonic) { + super( + descriptor, + InstrumentValueType.LONG, + meterSharedState, + instrumentationLibraryInfo, + monotonic); + } + @Override public void setCallback(Callback metricUpdater) {} } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractObserverTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractObserverTest.java index 2598ab25f04..d5c6d661066 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractObserverTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractObserverTest.java @@ -24,8 +24,6 @@ import io.opentelemetry.sdk.metrics.common.InstrumentValueType; import io.opentelemetry.sdk.resources.Resource; import java.util.Collections; -import java.util.List; -import java.util.Map; import org.junit.Test; public class AbstractObserverTest { @@ -59,12 +57,13 @@ public void attributeValue_EqualsAndHashCode() { } private static final class TestObserverInstrument extends AbstractObserver { - private static final String NAME = "name"; - private static final String DESCRIPTION = "description"; - private static final String UNIT = "1"; - private static final Map CONSTANT_LABELS = - Collections.singletonMap("key_2", "value_2"); - private static final List LABEL_KEY = Collections.singletonList("key"); + private static final InstrumentDescriptor INSTRUMENT_DESCRIPTOR = + InstrumentDescriptor.create( + "name", + "description", + "1", + Collections.singletonMap("key_2", "value_2"), + Collections.singletonList("key")); private static final MeterProviderSharedState METER_SHARED_STATE = MeterProviderSharedState.create(TestClock.create(), Resource.getEmpty()); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = @@ -72,11 +71,7 @@ private static final class TestObserverInstrument extends AbstractObserver { TestObserverInstrument(InstrumentValueType instrumentValueType, boolean monotonic) { super( - NAME, - DESCRIPTION, - UNIT, - CONSTANT_LABELS, - LABEL_KEY, + INSTRUMENT_DESCRIPTOR, instrumentValueType, METER_SHARED_STATE, INSTRUMENTATION_LIBRARY_INFO,