diff --git a/api/src/main/java/io/opentelemetry/metrics/Counter.java b/api/src/main/java/io/opentelemetry/metrics/Counter.java index 5fd895f2ee7..aeac50a1d03 100644 --- a/api/src/main/java/io/opentelemetry/metrics/Counter.java +++ b/api/src/main/java/io/opentelemetry/metrics/Counter.java @@ -16,13 +16,15 @@ package io.opentelemetry.metrics; +import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument; + /** * Base interface for all the Counter metrics. * * @param the Bound Counter type. * @since 0.1.0 */ -public interface Counter extends InstrumentWithBinding { +public interface Counter extends InstrumentWithBinding { /** Builder class for {@link Counter}. */ interface Builder, V> extends Instrument.Builder { diff --git a/api/src/main/java/io/opentelemetry/metrics/DefaultMeter.java b/api/src/main/java/io/opentelemetry/metrics/DefaultMeter.java index 70fc486e882..549e8955ade 100644 --- a/api/src/main/java/io/opentelemetry/metrics/DefaultMeter.java +++ b/api/src/main/java/io/opentelemetry/metrics/DefaultMeter.java @@ -141,11 +141,6 @@ public NoopBoundDoubleCounter bind(LabelSet labelSet) { return NoopBoundDoubleCounter.INSTANCE; } - @Override - public void unbind(BoundDoubleCounter boundInstrument) { - Utils.checkNotNull(boundInstrument, "boundDoubleCounter"); - } - /** No-op implementation of BoundDoubleCounter interface. */ @Immutable private enum NoopBoundDoubleCounter implements BoundDoubleCounter { @@ -153,6 +148,9 @@ private enum NoopBoundDoubleCounter implements BoundDoubleCounter { @Override public void add(double delta) {} + + @Override + public void unbind() {} } private static final class NoopBuilder @@ -186,11 +184,6 @@ public NoopBoundLongCounter bind(LabelSet labelSet) { return NoopBoundLongCounter.INSTANCE; } - @Override - public void unbind(BoundLongCounter boundInstrument) { - Utils.checkNotNull(boundInstrument, "boundLongCounter"); - } - /** No-op implementation of BoundLongCounter interface. */ @Immutable private enum NoopBoundLongCounter implements BoundLongCounter { @@ -198,6 +191,9 @@ private enum NoopBoundLongCounter implements BoundLongCounter { @Override public void add(long delta) {} + + @Override + public void unbind() {} } private static final class NoopBuilder extends NoopAbstractCounterBuilder @@ -232,11 +228,6 @@ public NoopBoundDoubleMeasure bind(LabelSet labelSet) { return NoopBoundDoubleMeasure.INSTANCE; } - @Override - public void unbind(BoundDoubleMeasure boundInstrument) { - Utils.checkNotNull(boundInstrument, "boundDoubleMeasure"); - } - /** No-op implementation of BoundDoubleMeasure interface. */ @Immutable private enum NoopBoundDoubleMeasure implements BoundDoubleMeasure { @@ -246,6 +237,9 @@ private enum NoopBoundDoubleMeasure implements BoundDoubleMeasure { public void record(double value) { Utils.checkArgument(value >= 0.0, "Unsupported negative values."); } + + @Override + public void unbind() {} } private static final class NoopBuilder @@ -284,11 +278,6 @@ public NoopBoundLongMeasure bind(LabelSet labelSet) { return NoopBoundLongMeasure.INSTANCE; } - @Override - public void unbind(BoundLongMeasure boundInstrument) { - Utils.checkNotNull(boundInstrument, "boundLongMeasure"); - } - /** No-op implementations of BoundLongMeasure interface. */ @Immutable private enum NoopBoundLongMeasure implements BoundLongMeasure { @@ -298,6 +287,9 @@ private enum NoopBoundLongMeasure implements BoundLongMeasure { public void record(long value) { Utils.checkArgument(value >= 0, "Unsupported negative values."); } + + @Override + public void unbind() {} } private static final class NoopBuilder diff --git a/api/src/main/java/io/opentelemetry/metrics/DoubleCounter.java b/api/src/main/java/io/opentelemetry/metrics/DoubleCounter.java index 5a031a82098..8bcd19d42a5 100644 --- a/api/src/main/java/io/opentelemetry/metrics/DoubleCounter.java +++ b/api/src/main/java/io/opentelemetry/metrics/DoubleCounter.java @@ -67,16 +67,13 @@ public interface DoubleCounter extends Counter { @Override BoundDoubleCounter bind(LabelSet labelSet); - @Override - void unbind(BoundDoubleCounter boundInstrument); - /** * A {@code Bound Instrument} for a {@code CounterDouble}. * * @since 0.1.0 */ @ThreadSafe - interface BoundDoubleCounter { + interface BoundDoubleCounter extends BoundInstrument { /** * Adds the given {@code delta} to the current value. The values can be negative iff monotonic * was set to {@code false}. @@ -87,6 +84,9 @@ interface BoundDoubleCounter { * @since 0.1.0 */ void add(double delta); + + @Override + void unbind(); } /** Builder class for {@link DoubleCounter}. */ diff --git a/api/src/main/java/io/opentelemetry/metrics/DoubleMeasure.java b/api/src/main/java/io/opentelemetry/metrics/DoubleMeasure.java index 6ef7f31e0a1..e3045ea6cf3 100644 --- a/api/src/main/java/io/opentelemetry/metrics/DoubleMeasure.java +++ b/api/src/main/java/io/opentelemetry/metrics/DoubleMeasure.java @@ -63,16 +63,13 @@ public interface DoubleMeasure extends Measure { @Override BoundDoubleMeasure bind(LabelSet labelSet); - @Override - void unbind(BoundDoubleMeasure boundInstrument); - /** * A {@code Bound Instrument} for a {@code LongMeasure}. * * @since 0.1.0 */ @ThreadSafe - interface BoundDoubleMeasure { + interface BoundDoubleMeasure extends BoundInstrument { /** * Records the given measurement, associated with the current {@code Context}. * @@ -81,6 +78,9 @@ interface BoundDoubleMeasure { * @since 0.1.0 */ void record(double value); + + @Override + void unbind(); } /** Builder class for {@link DoubleMeasure}. */ diff --git a/api/src/main/java/io/opentelemetry/metrics/InstrumentWithBinding.java b/api/src/main/java/io/opentelemetry/metrics/InstrumentWithBinding.java index e016da1c7d6..65a623b8f28 100644 --- a/api/src/main/java/io/opentelemetry/metrics/InstrumentWithBinding.java +++ b/api/src/main/java/io/opentelemetry/metrics/InstrumentWithBinding.java @@ -16,13 +16,15 @@ package io.opentelemetry.metrics; +import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument; + /** * Base interface for all metrics with bounds defined in this package. * * @param the specific type of Bound Instrument this instrument can provide. - * @since 0.1.0 + * @since 0.3.0 */ -public interface InstrumentWithBinding extends Instrument { +public interface InstrumentWithBinding extends Instrument { /** * Returns a {@code Bound Instrument} associated with the specified {@code labelSet}. Multiples * requests with the same {@code labelSet} may return the same {@code Bound Instrument} instance. @@ -37,12 +39,15 @@ public interface InstrumentWithBinding extends Instrument { */ B bind(LabelSet labelSet); - /** - * Removes the {@code Bound Instrument} from the Instrument. i.e. references to previous {@code - * Bound Instrument} are invalid (not being managed by the instrument). - * - * @param boundInstrument the {@code Bound Instrument} to be removed. - * @since 0.1.0 - */ - void unbind(B boundInstrument); + interface BoundInstrument { + /** + * Unbinds the current {@code Bound} from the Instrument. + * + *

After this method returns the current instance {@code Bound} is considered invalid (not + * being managed by the instrument). + * + * @since 0.3.0 + */ + void unbind(); + } } diff --git a/api/src/main/java/io/opentelemetry/metrics/LongCounter.java b/api/src/main/java/io/opentelemetry/metrics/LongCounter.java index f7a596a4a4c..49bbe82144f 100644 --- a/api/src/main/java/io/opentelemetry/metrics/LongCounter.java +++ b/api/src/main/java/io/opentelemetry/metrics/LongCounter.java @@ -67,16 +67,13 @@ public interface LongCounter extends Counter { @Override BoundLongCounter bind(LabelSet labelSet); - @Override - void unbind(BoundLongCounter boundInstrument); - /** * A {@code Bound Instrument} for a {@code LongCounter}. * * @since 0.1.0 */ @ThreadSafe - interface BoundLongCounter { + interface BoundLongCounter extends BoundInstrument { /** * Adds the given {@code delta} to the current value. The values can be negative iff monotonic @@ -88,6 +85,9 @@ interface BoundLongCounter { * @since 0.1.0 */ void add(long delta); + + @Override + void unbind(); } /** Builder class for {@link LongCounter}. */ diff --git a/api/src/main/java/io/opentelemetry/metrics/LongMeasure.java b/api/src/main/java/io/opentelemetry/metrics/LongMeasure.java index 1227ad2f739..73bf85b643f 100644 --- a/api/src/main/java/io/opentelemetry/metrics/LongMeasure.java +++ b/api/src/main/java/io/opentelemetry/metrics/LongMeasure.java @@ -63,16 +63,13 @@ public interface LongMeasure extends Measure { @Override BoundLongMeasure bind(LabelSet labelSet); - @Override - void unbind(BoundLongMeasure boundInstrument); - /** * A {@code Bound Instrument} for a {@code LongMeasure}. * * @since 0.1.0 */ @ThreadSafe - interface BoundLongMeasure { + interface BoundLongMeasure extends BoundInstrument { /** * Records the given measurement, associated with the current {@code Context}. * @@ -81,6 +78,9 @@ interface BoundLongMeasure { * @since 0.1.0 */ void record(long value); + + @Override + void unbind(); } /** Builder class for {@link LongMeasure}. */ diff --git a/api/src/main/java/io/opentelemetry/metrics/Measure.java b/api/src/main/java/io/opentelemetry/metrics/Measure.java index 936817dad63..656abbb8da9 100644 --- a/api/src/main/java/io/opentelemetry/metrics/Measure.java +++ b/api/src/main/java/io/opentelemetry/metrics/Measure.java @@ -16,13 +16,15 @@ package io.opentelemetry.metrics; +import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument; + /** * Base interface for all the Measure instruments. * * @param the Bound Instrument type. * @since 0.1.0 */ -public interface Measure extends InstrumentWithBinding { +public interface Measure extends InstrumentWithBinding { /** Builder class for {@link Measure}. */ interface Builder, V> extends Instrument.Builder { diff --git a/api/src/test/java/io/opentelemetry/metrics/DoubleCounterTest.java b/api/src/test/java/io/opentelemetry/metrics/DoubleCounterTest.java index d813f25f200..58842f2e660 100644 --- a/api/src/test/java/io/opentelemetry/metrics/DoubleCounterTest.java +++ b/api/src/test/java/io/opentelemetry/metrics/DoubleCounterTest.java @@ -107,20 +107,6 @@ public void noopBind_WithNullLabelSet() { doubleCounter.bind(null); } - @Test - public void noopUnbind_WithNullInstrument() { - DoubleCounter doubleCounter = - meter - .doubleCounterBuilder(NAME) - .setDescription(DESCRIPTION) - .setLabelKeys(LABEL_KEY) - .setUnit(UNIT) - .build(); - thrown.expect(NullPointerException.class); - thrown.expectMessage("boundDoubleCounter"); - doubleCounter.unbind(null); - } - @Test public void doesNotThrow() { DoubleCounter doubleCounter = diff --git a/api/src/test/java/io/opentelemetry/metrics/LongCounterTest.java b/api/src/test/java/io/opentelemetry/metrics/LongCounterTest.java index fc9d4f02716..dc4d9528b69 100644 --- a/api/src/test/java/io/opentelemetry/metrics/LongCounterTest.java +++ b/api/src/test/java/io/opentelemetry/metrics/LongCounterTest.java @@ -106,20 +106,6 @@ public void noopBind_WithNullLabelSet() { longCounter.bind(null); } - @Test - public void noopUnbind_WithNullInstrument() { - LongCounter longCounter = - meter - .longCounterBuilder(NAME) - .setDescription(DESCRIPTION) - .setLabelKeys(LABEL_KEY) - .setUnit(UNIT) - .build(); - thrown.expect(NullPointerException.class); - thrown.expectMessage("boundLongCounter"); - longCounter.unbind(null); - } - @Test public void doesNotThrow() { LongCounter longCounter = diff --git a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractBoundInstrument.java b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractBoundInstrument.java index d907c5e7b97..44cf11dc21b 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractBoundInstrument.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/AbstractBoundInstrument.java @@ -16,9 +16,10 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument; import io.opentelemetry.metrics.LabelSet; -class AbstractBoundInstrument { +abstract class AbstractBoundInstrument implements BoundInstrument { private final LabelSet labels; AbstractBoundInstrument(LabelSet labels) { @@ -26,6 +27,9 @@ class AbstractBoundInstrument { // todo: associate with an aggregator/accumulator } + @Override + public void unbind() {} + @Override public boolean equals(Object o) { if (this == o) { 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 984c214fd6d..2c4e5aac19a 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleCounterSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleCounterSdk.java @@ -43,12 +43,7 @@ public void add(double delta, LabelSet labelSet) { @Override public BoundDoubleCounter bind(LabelSet labelSet) { - return new Bound(labelSet, monotonic); - } - - @Override - public void unbind(BoundDoubleCounter boundInstrument) { - // TODO: Implement this. + return new BoundInstrument(labelSet, monotonic); } @Override @@ -75,11 +70,12 @@ public int hashCode() { return result; } - private static final class Bound extends AbstractBoundInstrument implements BoundDoubleCounter { + private static final class BoundInstrument extends AbstractBoundInstrument + implements BoundDoubleCounter { private final boolean monotonic; - Bound(LabelSet labels, boolean monotonic) { + BoundInstrument(LabelSet labels, boolean monotonic) { super(labels); this.monotonic = monotonic; } 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 b1693715fc1..e3241385384 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdk.java @@ -43,12 +43,7 @@ public void record(double value, LabelSet labelSet) { @Override public BoundDoubleMeasure bind(LabelSet labelSet) { - return new Bound(labelSet, this.absolute); - } - - @Override - public void unbind(BoundDoubleMeasure boundInstrument) { - // TODO: Implement this. + return new BoundInstrument(labelSet, this.absolute); } @Override @@ -75,11 +70,12 @@ public int hashCode() { return result; } - private static final class Bound extends AbstractBoundInstrument implements BoundDoubleMeasure { + private static final class BoundInstrument extends AbstractBoundInstrument + implements BoundDoubleMeasure { private final boolean absolute; - Bound(LabelSet labels, boolean absolute) { + BoundInstrument(LabelSet labels, boolean absolute) { super(labels); this.absolute = absolute; } 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 c2227f8a5f4..d9197f9e078 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongCounterSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongCounterSdk.java @@ -43,12 +43,7 @@ public void add(long delta, LabelSet labelSet) { @Override public BoundLongCounter bind(LabelSet labelSet) { - return new Bound(labelSet, monotonic); - } - - @Override - public void unbind(BoundLongCounter boundInstrument) { - // TODO: Implement this. + return new BoundInstrument(labelSet, monotonic); } @Override @@ -75,11 +70,12 @@ public int hashCode() { return result; } - private static final class Bound extends AbstractBoundInstrument implements BoundLongCounter { + private static final class BoundInstrument extends AbstractBoundInstrument + implements BoundLongCounter { private final boolean monotonic; - Bound(LabelSet labels, boolean monotonic) { + BoundInstrument(LabelSet labels, boolean monotonic) { super(labels); this.monotonic = monotonic; } 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 2f018609d3f..1f26a5bf287 100644 --- a/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongMeasureSdk.java +++ b/sdk/src/main/java/io/opentelemetry/sdk/metrics/LongMeasureSdk.java @@ -43,12 +43,7 @@ public void record(long value, LabelSet labelSet) { @Override public BoundLongMeasure bind(LabelSet labelSet) { - return new Bound(labelSet, this.absolute); - } - - @Override - public void unbind(BoundLongMeasure boundInstrument) { - // TODO: Implement this. + return new BoundInstrument(labelSet, this.absolute); } @Override @@ -75,11 +70,12 @@ public int hashCode() { return result; } - private static final class Bound extends AbstractBoundInstrument implements BoundLongMeasure { + private static final class BoundInstrument extends AbstractBoundInstrument + implements BoundLongMeasure { private final boolean absolute; - Bound(LabelSet labels, boolean absolute) { + BoundInstrument(LabelSet labels, boolean absolute) { super(labels); this.absolute = absolute; } 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 3d178f4cad3..ffa5e249424 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterBuilderTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractCounterBuilderTest.java @@ -19,6 +19,7 @@ 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 org.junit.Rule; import org.junit.Test; @@ -75,17 +76,17 @@ public TestInstrument build() { } } - private static final class TestInstrument implements Counter { - private static final TestBound HANDLE = new TestBound(); + private static final class TestInstrument implements Counter { + private static final TestBoundInstrument HANDLE = new TestBoundInstrument(); @Override - public TestBound bind(LabelSet labelSet) { + public TestBoundInstrument bind(LabelSet labelSet) { return HANDLE; } + } + private static final class TestBoundInstrument implements BoundInstrument { @Override - public void unbind(TestBound boundInstrument) {} + public void unbind() {} } - - private static final class TestBound {} } 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 09199670904..fa26877a55b 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureBuilderTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/AbstractMeasureBuilderTest.java @@ -18,6 +18,7 @@ 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 org.junit.Rule; @@ -82,10 +83,10 @@ private static final class TestInstrument implements Measure { public TestBoundMeasure bind(LabelSet labelSet) { return HANDLE; } + } + private static final class TestBoundMeasure implements BoundInstrument { @Override - public void unbind(TestBoundMeasure boundInstrument) {} + public void unbind() {} } - - private static final class TestBoundMeasure {} } diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java index 0fd537cb9d2..8b01930df3b 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java @@ -58,7 +58,7 @@ public void testDoubleCounter() { assertThat(duplicateBoundCounter).isEqualTo(boundDoubleCounter); // todo: verify that this has done something, when it has been done. - doubleCounter.unbind(boundDoubleCounter); + boundDoubleCounter.unbind(); } @Test diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdkTest.java index a49c8a100e8..1ba47cf959d 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/DoubleMeasureSdkTest.java @@ -58,7 +58,7 @@ public void testDoubleMeasure() { assertThat(duplicateBoundMeasure).isEqualTo(boundDoubleMeasure); // todo: verify that this has done something, when it has been done. - doubleMeasure.unbind(boundDoubleMeasure); + boundDoubleMeasure.unbind(); } @Test diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java index b37444f89b0..0db33f3a5ed 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java @@ -58,7 +58,7 @@ public void testLongCounter() { assertThat(duplicateBoundCounter).isEqualTo(boundLongCounter); // todo: verify that this has done something, when it has been done. - longCounter.unbind(boundLongCounter); + boundLongCounter.unbind(); } @Test diff --git a/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongMeasureSdkTest.java b/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongMeasureSdkTest.java index 2cd88723349..b8635948f99 100644 --- a/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongMeasureSdkTest.java +++ b/sdk/src/test/java/io/opentelemetry/sdk/metrics/LongMeasureSdkTest.java @@ -58,7 +58,7 @@ public void testLongMeasure() { assertThat(duplicateBoundMeasure).isEqualTo(boundLongMeasure); // todo: verify that this has done something, when it has been done. - longMeasure.unbind(boundLongMeasure); + boundLongMeasure.unbind(); } @Test