Skip to content

Commit

Permalink
Move unbind to the Bound to simplify usage (#834)
Browse files Browse the repository at this point in the history
* Move unbind to the Bound to simplify usage

Signed-off-by: Bogdan Cristian Drutu <[email protected]>

* Change names to be English friendly

Signed-off-by: Bogdan Cristian Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Feb 7, 2020
1 parent c779885 commit e7be014
Show file tree
Hide file tree
Showing 21 changed files with 85 additions and 122 deletions.
4 changes: 3 additions & 1 deletion api/src/main/java/io/opentelemetry/metrics/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package io.opentelemetry.metrics;

import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument;

/**
* Base interface for all the Counter metrics.
*
* @param <H> the Bound Counter type.
* @since 0.1.0
*/
public interface Counter<H> extends InstrumentWithBinding<H> {
public interface Counter<H extends BoundInstrument> extends InstrumentWithBinding<H> {

/** Builder class for {@link Counter}. */
interface Builder<B extends Counter.Builder<B, V>, V> extends Instrument.Builder<B, V> {
Expand Down
32 changes: 12 additions & 20 deletions api/src/main/java/io/opentelemetry/metrics/DefaultMeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,16 @@ 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 {
INSTANCE;

@Override
public void add(double delta) {}

@Override
public void unbind() {}
}

private static final class NoopBuilder
Expand Down Expand Up @@ -186,18 +184,16 @@ 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 {
INSTANCE;

@Override
public void add(long delta) {}

@Override
public void unbind() {}
}

private static final class NoopBuilder extends NoopAbstractCounterBuilder<Builder, LongCounter>
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/java/io/opentelemetry/metrics/DoubleCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,13 @@ public interface DoubleCounter extends Counter<BoundDoubleCounter> {
@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}.
Expand All @@ -87,6 +84,9 @@ interface BoundDoubleCounter {
* @since 0.1.0
*/
void add(double delta);

@Override
void unbind();
}

/** Builder class for {@link DoubleCounter}. */
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/java/io/opentelemetry/metrics/DoubleMeasure.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ public interface DoubleMeasure extends Measure<BoundDoubleMeasure> {
@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}.
*
Expand All @@ -81,6 +78,9 @@ interface BoundDoubleMeasure {
* @since 0.1.0
*/
void record(double value);

@Override
void unbind();
}

/** Builder class for {@link DoubleMeasure}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <B> the specific type of Bound Instrument this instrument can provide.
* @since 0.1.0
* @since 0.3.0
*/
public interface InstrumentWithBinding<B> extends Instrument {
public interface InstrumentWithBinding<B extends BoundInstrument> 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.
Expand All @@ -37,12 +39,15 @@ public interface InstrumentWithBinding<B> 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.
*
* <p>After this method returns the current instance {@code Bound} is considered invalid (not
* being managed by the instrument).
*
* @since 0.3.0
*/
void unbind();
}
}
8 changes: 4 additions & 4 deletions api/src/main/java/io/opentelemetry/metrics/LongCounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,13 @@ public interface LongCounter extends Counter<BoundLongCounter> {
@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
Expand All @@ -88,6 +85,9 @@ interface BoundLongCounter {
* @since 0.1.0
*/
void add(long delta);

@Override
void unbind();
}

/** Builder class for {@link LongCounter}. */
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/java/io/opentelemetry/metrics/LongMeasure.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,13 @@ public interface LongMeasure extends Measure<BoundLongMeasure> {
@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}.
*
Expand All @@ -81,6 +78,9 @@ interface BoundLongMeasure {
* @since 0.1.0
*/
void record(long value);

@Override
void unbind();
}

/** Builder class for {@link LongMeasure}. */
Expand Down
4 changes: 3 additions & 1 deletion api/src/main/java/io/opentelemetry/metrics/Measure.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

package io.opentelemetry.metrics;

import io.opentelemetry.metrics.InstrumentWithBinding.BoundInstrument;

/**
* Base interface for all the Measure instruments.
*
* @param <H> the Bound Instrument type.
* @since 0.1.0
*/
public interface Measure<H> extends InstrumentWithBinding<H> {
public interface Measure<H extends BoundInstrument> extends InstrumentWithBinding<H> {

/** Builder class for {@link Measure}. */
interface Builder<B extends Measure.Builder<B, V>, V> extends Instrument.Builder<B, V> {
Expand Down
14 changes: 0 additions & 14 deletions api/src/test/java/io/opentelemetry/metrics/DoubleCounterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
14 changes: 0 additions & 14 deletions api/src/test/java/io/opentelemetry/metrics/LongCounterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@

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) {
this.labels = labels;
// todo: associate with an aggregator/accumulator
}

@Override
public void unbind() {}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit e7be014

Please sign in to comment.