diff --git a/api/metrics/README.md b/api/metrics/README.md
index 9452f1b19c..bc5c618277 100644
--- a/api/metrics/README.md
+++ b/api/metrics/README.md
@@ -2,8 +2,11 @@
[![Javadocs][javadoc-image]][javadoc-url]
+* The code in this module is the implementation of the [experimental OpenTelemetry metrics signal][metrics-spec].
+* The default implementation of the interfaces in this module is in the OpenTelemetry metrics SDK module.
* The interfaces in this directory can be implemented to create alternative
implementations of the OpenTelemetry library.
[javadoc-image]: https://www.javadoc.io/badge/io.opentelemetry/opentelemetry-api-metrics.svg
-[javadoc-url]: https://www.javadoc.io/doc/io.opentelemetry/opentelemetry-api-metrics
\ No newline at end of file
+[javadoc-url]: https://www.javadoc.io/doc/io.opentelemetry/opentelemetry-api-metrics
+[metrics-spec]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md
\ No newline at end of file
diff --git a/api/metrics/build.gradle.kts b/api/metrics/build.gradle.kts
index 6965cd451a..9acaf0dfec 100644
--- a/api/metrics/build.gradle.kts
+++ b/api/metrics/build.gradle.kts
@@ -11,6 +11,7 @@ otelJava.moduleName.set("io.opentelemetry.api.metrics")
dependencies {
api(project(":api:all"))
+ api(project(":context"))
annotationProcessor("com.google.auto.value:auto-value")
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/AsynchronousInstrument.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/AsynchronousInstrument.java
deleted file mode 100644
index 4d68b81b68..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/AsynchronousInstrument.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import io.opentelemetry.api.metrics.common.Labels;
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * {@code AsynchronousInstrument} is an interface that defines a type of instruments that are used
- * to report measurements asynchronously.
- *
- *
They are reported by a callback, once per collection interval, and lack Context. They are
- * permitted to report only one value per distinct label set per period. If the application observes
- * multiple values for the same label set, in a single callback, the last value is the only value
- * kept.
- */
-@ThreadSafe
-public interface AsynchronousInstrument extends Instrument {
-
- /** The result pass to the updater. */
- interface LongResult {
- void observe(long value, Labels labels);
- }
-
- /** The result pass to the updater. */
- interface DoubleResult {
- void observe(double value, Labels labels);
- }
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/AsynchronousInstrumentBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/AsynchronousInstrumentBuilder.java
deleted file mode 100644
index 046f87ed59..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/AsynchronousInstrumentBuilder.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import java.util.function.Consumer;
-
-/** Builder class for {@link AsynchronousInstrument}. */
-public interface AsynchronousInstrumentBuilder extends InstrumentBuilder {
- /**
- * Sets a consumer that gets executed every collection interval.
- *
- *
Evaluation is deferred until needed, if this {@code AsynchronousInstrument} metric is not
- * exported then it will never be called.
- *
- * @param updater the consumer to be executed before export.
- */
- AsynchronousInstrumentBuilder setUpdater(Consumer updater);
-
- @Override
- AsynchronousInstrument build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BatchRecorder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BatchRecorder.java
deleted file mode 100644
index f7075839c6..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BatchRecorder.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * Util class that can be use to atomically record measurements associated with a set of Metrics.
- *
- *
This class is equivalent with individually calling record on every Measure, but has the
- * advantage that all these operations are recorded atomically and it is more efficient.
- */
-@ThreadSafe
-public interface BatchRecorder {
- /**
- * Associates the {@link LongValueRecorder} with the given value. Subsequent updates to the same
- * {@link LongValueRecorder} will overwrite the previous value.
- *
- * @param valueRecorder the {@link LongValueRecorder}.
- * @param value the value to be associated with {@code valueRecorder}.
- * @return this.
- */
- BatchRecorder put(LongValueRecorder valueRecorder, long value);
-
- /**
- * Associates the {@link DoubleValueRecorder} with the given value. Subsequent updates to the same
- * {@link DoubleValueRecorder} will overwrite the previous value.
- *
- * @param valueRecorder the {@link DoubleValueRecorder}.
- * @param value the value to be associated with {@code valueRecorder}.
- * @return this.
- */
- BatchRecorder put(DoubleValueRecorder valueRecorder, double value);
-
- /**
- * Associates the {@link LongCounter} with the given value. Subsequent updates to the same {@link
- * LongCounter} will overwrite the previous value.
- *
- * @param counter the {@link LongCounter}.
- * @param value the value to be associated with {@code counter}.
- * @return this.
- */
- BatchRecorder put(LongCounter counter, long value);
-
- /**
- * Associates the {@link DoubleCounter} with the given value. Subsequent updates to the same
- * {@link DoubleCounter} will overwrite the previous value.
- *
- * @param counter the {@link DoubleCounter}.
- * @param value the value to be associated with {@code counter}.
- * @return this.
- */
- BatchRecorder put(DoubleCounter counter, double value);
-
- /**
- * Associates the {@link LongUpDownCounter} with the given value. Subsequent updates to the same
- * {@link LongCounter} will overwrite the previous value.
- *
- * @param upDownCounter the {@link LongCounter}.
- * @param value the value to be associated with {@code counter}.
- * @return this.
- */
- BatchRecorder put(LongUpDownCounter upDownCounter, long value);
-
- /**
- * Associates the {@link DoubleUpDownCounter} with the given value. Subsequent updates to the same
- * {@link DoubleCounter} will overwrite the previous value.
- *
- * @param upDownCounter the {@link DoubleCounter}.
- * @param value the value to be associated with {@code counter}.
- * @return this.
- */
- BatchRecorder put(DoubleUpDownCounter upDownCounter, double value);
-
- /**
- * Records all of measurements at the same time.
- *
- *
This method records all measurements every time it is called, so make sure it is not called
- * twice if not needed.
- */
- void record();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleCounter.java
index 6a3c450c81..d6233d317d 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleCounter.java
@@ -5,20 +5,35 @@
package io.opentelemetry.api.metrics;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/** A {@code Bound Instrument} for a {@link DoubleCounter}. */
+/** A counter instrument that records {@code double} values with pre-associated attributes. */
@ThreadSafe
-public interface BoundDoubleCounter extends BoundSynchronousInstrument {
+public interface BoundDoubleCounter {
/**
- * Adds the given {@code increment} to the current value. The values cannot be negative.
+ * Records a value with pre-bound attributes.
*
- *
The value added is associated with the current {@code Context}.
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
*
- * @param increment the value to add.
+ * @param value The increment amount. MUST be non-negative.
*/
- void add(double increment);
+ void add(double value);
- @Override
+ /**
+ * Records a value with pre-bound attributes.
+ *
+ * @param value The increment amount. MUST be non-negative.
+ * @param context The explicit context to associate with this measurement.
+ */
+ void add(double value, Context context);
+
+ /**
+ * Unbinds the current bound instance from the {@link DoubleCounter}.
+ *
+ *
After this method returns the current instance is considered invalid (not being managed by
+ * the instrument).
+ */
void unbind();
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleHistogram.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleHistogram.java
new file mode 100644
index 0000000000..45e2527e46
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleHistogram.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import io.opentelemetry.context.Context;
+import javax.annotation.concurrent.ThreadSafe;
+
+/** A histogram instrument that records {@code long} values with pre-associated attributes. */
+@ThreadSafe
+public interface BoundDoubleHistogram {
+ /**
+ * Records a value with a pre-bound set of attributes.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The amount of the measurement.
+ */
+ void record(double value);
+
+ /**
+ * Records a value with a pre-bound set of attributes.
+ *
+ * @param value The amount of the measurement.
+ * @param context The explicit context to associate with this measurement.
+ */
+ void record(double value, Context context);
+
+ /**
+ * Unbinds the current bound instance from the {@link DoubleHistogram}.
+ *
+ *
After this method returns the current instance is considered invalid (not being managed by
+ * the instrument).
+ */
+ void unbind();
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleUpDownCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleUpDownCounter.java
index 24223cb177..6393b9a018 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleUpDownCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleUpDownCounter.java
@@ -5,20 +5,35 @@
package io.opentelemetry.api.metrics;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/** A {@code Bound Instrument} for a {@link DoubleUpDownCounter}. */
+/** An up-down-counter instrument with pre-bound attributes. */
@ThreadSafe
-public interface BoundDoubleUpDownCounter extends BoundSynchronousInstrument {
+public interface BoundDoubleUpDownCounter {
/**
- * Adds the given {@code increment} to the current value.
+ * Records a value with pre-bound attributes.
*
- *
The value added is associated with the current {@code Context}.
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
*
- * @param increment the value to add.
+ * @param value The increment amount. May be positive, negative or zero.
*/
- void add(double increment);
+ void add(double value);
- @Override
+ /**
+ * Records a value with a pre-bound attributes.
+ *
+ * @param value The increment amount. May be positive, negative or zero.
+ * @param context The explicit context to associate with this measurement.
+ */
+ void add(double value, Context context);
+
+ /**
+ * Unbinds the current bound instance from the {@link DoubleUpDownCounter}.
+ *
+ *
After this method returns the current instance is considered invalid (not being managed by
+ * the instrument).
+ */
void unbind();
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleValueRecorder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleValueRecorder.java
deleted file mode 100644
index 6fc3866d14..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundDoubleValueRecorder.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/** A {@code Bound Instrument} for a {@link DoubleValueRecorder}. */
-@ThreadSafe
-public interface BoundDoubleValueRecorder extends BoundSynchronousInstrument {
- /**
- * Records the given measurement, associated with the current {@code Context}.
- *
- * @param value the measurement to record.
- * @throws IllegalArgumentException if value is negative.
- */
- void record(double value);
-
- @Override
- void unbind();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongCounter.java
index c7fa15e053..81fb520af1 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongCounter.java
@@ -5,21 +5,35 @@
package io.opentelemetry.api.metrics;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/** A {@code Bound Instrument} for a {@link LongCounter}. */
+/** A counter instrument that records {@code long} values with pre-associated attributes. */
@ThreadSafe
-public interface BoundLongCounter extends BoundSynchronousInstrument {
+public interface BoundLongCounter {
+ /**
+ * Records a value with pre-bound attributes.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The increment amount. MUST be non-negative.
+ */
+ void add(long value);
/**
- * Adds the given {@code increment} to the current value. The values cannot be negative.
+ * Records a value with pre-bound attributes.
*
- *
The value added is associated with the current {@code Context}.
- *
- * @param increment the value to add.
+ * @param value The increment amount. MUST be non-negative.
+ * @param context The explicit context to associate with this measurement.
*/
- void add(long increment);
+ void add(long value, Context context);
- @Override
+ /**
+ * Unbinds the current bound instance from the {@link LongCounter}.
+ *
+ *
After this method returns the current instance is considered invalid (not being managed by
+ * the instrument).
+ */
void unbind();
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongHistogram.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongHistogram.java
new file mode 100644
index 0000000000..24997f6e04
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongHistogram.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import io.opentelemetry.context.Context;
+import javax.annotation.concurrent.ThreadSafe;
+
+/** A histogram instrument that records {@code long} values with pre-associated attributes. */
+@ThreadSafe
+public interface BoundLongHistogram {
+ /**
+ * Records a value with a pre-bound set of attributes.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The amount of the measurement.
+ */
+ void record(long value);
+
+ /**
+ * Records a value with a pre-bound set of attributes.
+ *
+ * @param value The amount of the measurement.
+ * @param context The explicit context to associate with this measurement.
+ */
+ void record(long value, Context context);
+
+ /**
+ * Unbinds the current bound instance from the {@link LongHistogram}.
+ *
+ *
After this method returns the current instance is considered invalid (not being managed by
+ * the instrument).
+ */
+ void unbind();
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongUpDownCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongUpDownCounter.java
index b0c253f1ae..7ca78a37b8 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongUpDownCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongUpDownCounter.java
@@ -5,21 +5,35 @@
package io.opentelemetry.api.metrics;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/** A {@code Bound Instrument} for a {@link LongUpDownCounter}. */
+/** An up-down-counter instrument with pre-bound attributes. */
@ThreadSafe
-public interface BoundLongUpDownCounter extends BoundSynchronousInstrument {
+public interface BoundLongUpDownCounter {
+ /**
+ * Records a value with pre-bound attributes.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The increment amount. May be positive, negative or zero.
+ */
+ void add(long value);
/**
- * Adds the given {@code increment} to the current value.
+ * Records a value with a pre-bound attributes.
*
- *
The value added is associated with the current {@code Context}.
- *
- * @param increment the value to add.
+ * @param value The increment amount. May be positive, negative or zero.
+ * @param context The explicit context to associate with this measurement.
*/
- void add(long increment);
+ void add(long value, Context context);
- @Override
+ /**
+ * Unbinds the current bound instance from the {@link LongUpDownCounter}.
+ *
+ *
After this method returns the current instance is considered invalid (not being managed by
+ * the instrument).
+ */
void unbind();
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongValueRecorder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongValueRecorder.java
deleted file mode 100644
index f7c716c767..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundLongValueRecorder.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/** A {@code Bound Instrument} for a {@link LongValueRecorder}. */
-@ThreadSafe
-public interface BoundLongValueRecorder extends BoundSynchronousInstrument {
- /**
- * Records the given measurement, associated with the current {@code Context}.
- *
- * @param value the measurement to record.
- * @throws IllegalArgumentException if value is negative.
- */
- void record(long value);
-
- @Override
- void unbind();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundSynchronousInstrument.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundSynchronousInstrument.java
deleted file mode 100644
index 17fdc0da48..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/BoundSynchronousInstrument.java
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-interface BoundSynchronousInstrument {
- /**
- * 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).
- */
- void unbind();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeter.java
deleted file mode 100644
index 0a61f93374..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeter.java
+++ /dev/null
@@ -1,661 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import io.opentelemetry.api.internal.Utils;
-import io.opentelemetry.api.metrics.common.Labels;
-import io.opentelemetry.api.metrics.internal.MetricsStringUtils;
-import java.util.Objects;
-import java.util.function.Consumer;
-import javax.annotation.concurrent.Immutable;
-import javax.annotation.concurrent.ThreadSafe;
-
-/** No-op implementations of {@link Meter}. */
-@ThreadSafe
-final class DefaultMeter implements Meter {
-
- private static final DefaultMeter INSTANCE = new DefaultMeter();
- private static final String COUNTERS_CAN_ONLY_INCREASE = "Counters can only increase";
-
- /* VisibleForTesting */ static final String ERROR_MESSAGE_INVALID_NAME =
- "Name should be a ASCII string with a length no greater than "
- + MetricsStringUtils.METRIC_NAME_MAX_LENGTH
- + " characters.";
-
- static Meter getInstance() {
- return INSTANCE;
- }
-
- @Override
- public DoubleCounterBuilder doubleCounterBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopDoubleCounter.NoopBuilder();
- }
-
- @Override
- public LongCounterBuilder longCounterBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopLongCounter.NoopBuilder();
- }
-
- @Override
- public DoubleUpDownCounterBuilder doubleUpDownCounterBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopDoubleUpDownCounter.NoopBuilder();
- }
-
- @Override
- public LongUpDownCounterBuilder longUpDownCounterBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopLongUpDownCounter.NoopBuilder();
- }
-
- @Override
- public DoubleValueRecorderBuilder doubleValueRecorderBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopDoubleValueRecorder.NoopBuilder();
- }
-
- @Override
- public LongValueRecorderBuilder longValueRecorderBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopLongValueRecorder.NoopBuilder();
- }
-
- @Override
- public DoubleSumObserverBuilder doubleSumObserverBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopDoubleSumObserver.NoopBuilder();
- }
-
- @Override
- public LongSumObserverBuilder longSumObserverBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopLongSumObserver.NoopBuilder();
- }
-
- @Override
- public DoubleUpDownSumObserverBuilder doubleUpDownSumObserverBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopDoubleUpDownSumObserver.NoopBuilder();
- }
-
- @Override
- public LongUpDownSumObserverBuilder longUpDownSumObserverBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopLongUpDownSumObserver.NoopBuilder();
- }
-
- @Override
- public DoubleValueObserverBuilder doubleValueObserverBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopDoubleValueObserver.NoopBuilder();
- }
-
- @Override
- public LongValueObserverBuilder longValueObserverBuilder(String name) {
- Objects.requireNonNull(name, "name");
- Utils.checkArgument(MetricsStringUtils.isValidMetricName(name), ERROR_MESSAGE_INVALID_NAME);
- return new NoopLongValueObserver.NoopBuilder();
- }
-
- @Override
- public BatchRecorder newBatchRecorder(String... keyValuePairs) {
- validateLabelPairs(keyValuePairs);
- return NoopBatchRecorder.INSTANCE;
- }
-
- private DefaultMeter() {}
-
- /** No-op implementation of {@link DoubleCounter} interface. */
- @Immutable
- private static final class NoopDoubleCounter implements DoubleCounter {
-
- private NoopDoubleCounter() {}
-
- @Override
- public void add(double increment, Labels labels) {
- Objects.requireNonNull(labels, "labels");
- Utils.checkArgument(increment >= 0.0, COUNTERS_CAN_ONLY_INCREASE);
- }
-
- @Override
- public void add(double increment) {
- add(increment, Labels.empty());
- }
-
- @Override
- public NoopBoundDoubleCounter bind(Labels labels) {
- Objects.requireNonNull(labels, "labels");
- return NoopBoundDoubleCounter.INSTANCE;
- }
-
- @Immutable
- private enum NoopBoundDoubleCounter implements BoundDoubleCounter {
- INSTANCE;
-
- @Override
- public void add(double increment) {
- Utils.checkArgument(increment >= 0.0, COUNTERS_CAN_ONLY_INCREASE);
- }
-
- @Override
- public void unbind() {}
- }
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements DoubleCounterBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public DoubleCounter build() {
- return new NoopDoubleCounter();
- }
- }
- }
-
- /** No-op implementation of {@link LongCounter} interface. */
- @Immutable
- private static final class NoopLongCounter implements LongCounter {
-
- private NoopLongCounter() {}
-
- @Override
- public void add(long increment, Labels labels) {
- Objects.requireNonNull(labels, "labels");
- Utils.checkArgument(increment >= 0, COUNTERS_CAN_ONLY_INCREASE);
- }
-
- @Override
- public void add(long increment) {
- add(increment, Labels.empty());
- }
-
- @Override
- public NoopBoundLongCounter bind(Labels labels) {
- Objects.requireNonNull(labels, "labels");
- return NoopBoundLongCounter.INSTANCE;
- }
-
- @Immutable
- private enum NoopBoundLongCounter implements BoundLongCounter {
- INSTANCE;
-
- @Override
- public void add(long increment) {
- Utils.checkArgument(increment >= 0, COUNTERS_CAN_ONLY_INCREASE);
- }
-
- @Override
- public void unbind() {}
- }
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements LongCounterBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public LongCounter build() {
- return new NoopLongCounter();
- }
- }
- }
-
- /** No-op implementation of {@link DoubleUpDownCounter} interface. */
- @Immutable
- private static final class NoopDoubleUpDownCounter implements DoubleUpDownCounter {
-
- private NoopDoubleUpDownCounter() {}
-
- @Override
- public void add(double increment, Labels labels) {
- Objects.requireNonNull(labels, "labels");
- }
-
- @Override
- public void add(double increment) {
- add(increment, Labels.empty());
- }
-
- @Override
- public NoopBoundDoubleUpDownCounter bind(Labels labels) {
- Objects.requireNonNull(labels, "labels");
- return NoopBoundDoubleUpDownCounter.INSTANCE;
- }
-
- @Immutable
- private enum NoopBoundDoubleUpDownCounter implements BoundDoubleUpDownCounter {
- INSTANCE;
-
- @Override
- public void add(double increment) {}
-
- @Override
- public void unbind() {}
- }
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements DoubleUpDownCounterBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public DoubleUpDownCounter build() {
- return new NoopDoubleUpDownCounter();
- }
- }
- }
-
- /** No-op implementation of {@link LongUpDownCounter} interface. */
- @Immutable
- private static final class NoopLongUpDownCounter implements LongUpDownCounter {
-
- private NoopLongUpDownCounter() {}
-
- @Override
- public void add(long increment, Labels labels) {
- Objects.requireNonNull(labels, "labels");
- }
-
- @Override
- public void add(long increment) {
- add(increment, Labels.empty());
- }
-
- @Override
- public NoopBoundLongUpDownCounter bind(Labels labels) {
- Objects.requireNonNull(labels, "labels");
- return NoopBoundLongUpDownCounter.INSTANCE;
- }
-
- @Immutable
- private enum NoopBoundLongUpDownCounter implements BoundLongUpDownCounter {
- INSTANCE;
-
- @Override
- public void add(long increment) {}
-
- @Override
- public void unbind() {}
- }
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements LongUpDownCounterBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public LongUpDownCounter build() {
- return new NoopLongUpDownCounter();
- }
- }
- }
-
- /** No-op implementation of {@link DoubleValueRecorder} interface. */
- @Immutable
- private static final class NoopDoubleValueRecorder implements DoubleValueRecorder {
-
- private NoopDoubleValueRecorder() {}
-
- @Override
- public void record(double value, Labels labels) {
- Objects.requireNonNull(labels, "labels");
- }
-
- @Override
- public void record(double value) {
- record(value, Labels.empty());
- }
-
- @Override
- public NoopBoundDoubleValueRecorder bind(Labels labels) {
- Objects.requireNonNull(labels, "labels");
- return NoopBoundDoubleValueRecorder.INSTANCE;
- }
-
- @Immutable
- private enum NoopBoundDoubleValueRecorder implements BoundDoubleValueRecorder {
- INSTANCE;
-
- @Override
- public void record(double value) {}
-
- @Override
- public void unbind() {}
- }
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements DoubleValueRecorderBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public DoubleValueRecorder build() {
- return new NoopDoubleValueRecorder();
- }
- }
- }
-
- /** No-op implementation of {@link LongValueRecorder} interface. */
- @Immutable
- private static final class NoopLongValueRecorder implements LongValueRecorder {
-
- private NoopLongValueRecorder() {}
-
- @Override
- public void record(long value, Labels labels) {
- Objects.requireNonNull(labels, "labels");
- }
-
- @Override
- public void record(long value) {
- record(value, Labels.empty());
- }
-
- @Override
- public NoopBoundLongValueRecorder bind(Labels labels) {
- Objects.requireNonNull(labels, "labels");
- return NoopBoundLongValueRecorder.INSTANCE;
- }
-
- @Immutable
- private enum NoopBoundLongValueRecorder implements BoundLongValueRecorder {
- INSTANCE;
-
- @Override
- public void record(long value) {}
-
- @Override
- public void unbind() {}
- }
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements LongValueRecorderBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public LongValueRecorder build() {
- return new NoopLongValueRecorder();
- }
- }
- }
-
- /** No-op implementation of {@link DoubleSumObserver} interface. */
- @Immutable
- private static final class NoopDoubleSumObserver implements DoubleSumObserver {
-
- private NoopDoubleSumObserver() {}
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements DoubleSumObserverBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public DoubleSumObserverBuilder setUpdater(Consumer updater) {
- Objects.requireNonNull(updater, "callback");
- return this;
- }
-
- @Override
- public DoubleSumObserver build() {
- return new NoopDoubleSumObserver();
- }
- }
- }
-
- /** No-op implementation of {@link LongSumObserver} interface. */
- @Immutable
- private static final class NoopLongSumObserver implements LongSumObserver {
-
- private NoopLongSumObserver() {}
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements LongSumObserverBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public NoopBuilder setUpdater(Consumer updater) {
- Objects.requireNonNull(updater, "callback");
- return this;
- }
-
- @Override
- public LongSumObserver build() {
- return new NoopLongSumObserver();
- }
- }
- }
-
- /** No-op implementation of {@link DoubleUpDownSumObserver} interface. */
- @Immutable
- private static final class NoopDoubleUpDownSumObserver implements DoubleUpDownSumObserver {
-
- private NoopDoubleUpDownSumObserver() {}
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements DoubleUpDownSumObserverBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public DoubleUpDownSumObserverBuilder setUpdater(Consumer updater) {
- Objects.requireNonNull(updater, "callback");
- return this;
- }
-
- @Override
- public DoubleUpDownSumObserver build() {
- return new NoopDoubleUpDownSumObserver();
- }
- }
- }
-
- /** No-op implementation of {@link LongUpDownSumObserver} interface. */
- @Immutable
- private static final class NoopLongUpDownSumObserver implements LongUpDownSumObserver {
-
- private NoopLongUpDownSumObserver() {}
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements LongUpDownSumObserverBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public LongUpDownSumObserverBuilder setUpdater(Consumer updater) {
- Objects.requireNonNull(updater, "callback");
- return this;
- }
-
- @Override
- public LongUpDownSumObserver build() {
- return new NoopLongUpDownSumObserver();
- }
- }
- }
-
- /** No-op implementation of {@link DoubleValueObserver} interface. */
- @Immutable
- private static final class NoopDoubleValueObserver implements DoubleValueObserver {
-
- private NoopDoubleValueObserver() {}
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements DoubleValueObserverBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public DoubleValueObserverBuilder setUpdater(Consumer updater) {
- Objects.requireNonNull(updater, "callback");
- return this;
- }
-
- @Override
- public DoubleValueObserver build() {
- return new NoopDoubleValueObserver();
- }
- }
- }
-
- /** No-op implementation of {@link LongValueObserver} interface. */
- @Immutable
- private static final class NoopLongValueObserver implements LongValueObserver {
-
- private NoopLongValueObserver() {}
-
- private static final class NoopBuilder extends NoopAbstractInstrumentBuilder
- implements LongValueObserverBuilder {
-
- @Override
- protected NoopBuilder getThis() {
- return this;
- }
-
- @Override
- public LongValueObserverBuilder setUpdater(Consumer updater) {
- Objects.requireNonNull(updater, "callback");
- return this;
- }
-
- @Override
- public LongValueObserver build() {
- return new NoopLongValueObserver();
- }
- }
- }
-
- /** No-op implementation of {@link BatchRecorder} interface. */
- private enum NoopBatchRecorder implements BatchRecorder {
- INSTANCE;
-
- @Override
- public BatchRecorder put(LongValueRecorder valueRecorder, long value) {
- Objects.requireNonNull(valueRecorder, "valueRecorder");
- return this;
- }
-
- @Override
- public BatchRecorder put(DoubleValueRecorder valueRecorder, double value) {
- Objects.requireNonNull(valueRecorder, "valueRecorder");
- return this;
- }
-
- @Override
- public BatchRecorder put(LongCounter counter, long value) {
- Objects.requireNonNull(counter, "counter");
- Utils.checkArgument(value >= 0, COUNTERS_CAN_ONLY_INCREASE);
- return this;
- }
-
- @Override
- public BatchRecorder put(DoubleCounter counter, double value) {
- Objects.requireNonNull(counter, "counter");
- Utils.checkArgument(value >= 0.0, COUNTERS_CAN_ONLY_INCREASE);
- return this;
- }
-
- @Override
- public BatchRecorder put(LongUpDownCounter upDownCounter, long value) {
- Objects.requireNonNull(upDownCounter, "upDownCounter");
- return this;
- }
-
- @Override
- public BatchRecorder put(DoubleUpDownCounter upDownCounter, double value) {
- Objects.requireNonNull(upDownCounter, "upDownCounter");
- return this;
- }
-
- @Override
- public void record() {}
- }
-
- private abstract static class NoopAbstractInstrumentBuilder<
- B extends NoopAbstractInstrumentBuilder>
- implements InstrumentBuilder {
-
- @Override
- public B setDescription(String description) {
- Objects.requireNonNull(description, "description");
- return getThis();
- }
-
- @Override
- public B setUnit(String unit) {
- Objects.requireNonNull(unit, "unit");
- return getThis();
- }
-
- protected abstract B getThis();
- }
-
- /**
- * Validates that the array of Strings is 1) even in length, and 2) they can be formed into valid
- * pairs where the first item in the pair is not null.
- *
- * @param keyValuePairs The String[] to validate for correctness.
- * @throws IllegalArgumentException if any of the preconditions are violated.
- */
- private static void validateLabelPairs(String[] keyValuePairs) {
- Utils.checkArgument(
- keyValuePairs.length % 2 == 0,
- "You must provide an even number of key/value pair arguments.");
- for (int i = 0; i < keyValuePairs.length; i += 2) {
- String key = keyValuePairs[i];
- Objects.requireNonNull(key, "You cannot provide null keys for label creation.");
- }
- }
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeterBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeterBuilder.java
deleted file mode 100644
index 5ea9decafa..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeterBuilder.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-class DefaultMeterBuilder implements MeterBuilder {
- private static final MeterBuilder INSTANCE = new DefaultMeterBuilder();
-
- static MeterBuilder getInstance() {
- return INSTANCE;
- }
-
- @Override
- public MeterBuilder setSchemaUrl(String schemaUrl) {
- return this;
- }
-
- @Override
- public MeterBuilder setInstrumentationVersion(String instrumentationVersion) {
- return this;
- }
-
- @Override
- public Meter build() {
- return DefaultMeter.getInstance();
- }
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeterProvider.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeterProvider.java
deleted file mode 100644
index 91db009775..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DefaultMeterProvider.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-@ThreadSafe
-final class DefaultMeterProvider implements MeterProvider {
-
- private static final MeterProvider INSTANCE = new DefaultMeterProvider();
-
- static MeterProvider getInstance() {
- return INSTANCE;
- }
-
- @Override
- public Meter get(String instrumentationName) {
- return get(instrumentationName, null);
- }
-
- @Override
- public Meter get(String instrumentationName, String instrumentationVersion) {
- return DefaultMeter.getInstance();
- }
-
- private DefaultMeterProvider() {}
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounter.java
index 48134d0077..33d11e25ca 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounter.java
@@ -5,59 +5,46 @@
package io.opentelemetry.api.metrics;
-import io.opentelemetry.api.metrics.common.Labels;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/**
- * Counter is the most common synchronous instrument. This instrument supports an {@link
- * #add(double, Labels)}` function for reporting an increment, and is restricted to non-negative
- * increments. The default aggregation is `Sum`.
- *
- *
Example:
- *
- *
{@code
- * class YourClass {
- * private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * private static final DoubleCounter counter =
- * meter.
- * .doubleCounterBuilder("allocated_resources")
- * .setDescription("Total allocated resources")
- * .setUnit("1")
- * .build();
- *
- * // It is recommended that the API user keep references to a Bound Counters.
- * private static final BoundDoubleCounter someWorkBound =
- * counter.bind("work_name", "some_work");
- *
- * void doSomeWork() {
- * someWorkBound.add(10.2); // Resources needed for this task.
- * // Your code here.
- * }
- * }
- * }
- */
+/** A counter instrument that records {@code double} values. */
@ThreadSafe
-public interface DoubleCounter extends SynchronousInstrument {
+public interface DoubleCounter {
+ /**
+ * Records a value.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The increment amount. MUST be non-negative.
+ */
+ void add(double value);
/**
- * Adds the given {@code increment} to the current value. The values cannot be negative.
+ * Records a value with a set of attributes.
*
- *
The value added is associated with the current {@code Context} and provided set of labels.
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
*
- * @param increment the value to add.
- * @param labels the labels to be associated to this recording.
+ * @param value The increment amount. MUST be non-negative.
+ * @param attributes A set of attributes to associate with the count.
*/
- void add(double increment, Labels labels);
+ void add(double value, Attributes attributes);
/**
- * Adds the given {@code increment} to the current value. The values cannot be negative.
+ * Records a value with a set of attributes.
*
- *
The value added is associated with the current {@code Context} and with empty labels.
- *
- * @param increment the value to add.
+ * @param value The increment amount. MUST be non-negative.
+ * @param attributes A set of attributes to associate with the count.
+ * @param context The explicit context to associate with this measurement.
*/
- void add(double increment);
+ void add(double value, Attributes attributes, Context context);
- @Override
- BoundDoubleCounter bind(Labels labels);
+ /**
+ * Constructs a bound version of this instrument where all recorded values use the given
+ * attributes.
+ */
+ BoundDoubleCounter bind(Attributes attributes);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounterBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounterBuilder.java
index d7bf2dd367..bcbb2387ff 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounterBuilder.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleCounterBuilder.java
@@ -5,14 +5,42 @@
package io.opentelemetry.api.metrics;
+import java.util.function.Consumer;
+
/** Builder class for {@link DoubleCounter}. */
-public interface DoubleCounterBuilder extends SynchronousInstrumentBuilder {
- @Override
+public interface DoubleCounterBuilder {
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description stirngs should follw the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
DoubleCounterBuilder setDescription(String description);
- @Override
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
DoubleCounterBuilder setUnit(String unit);
- @Override
+ /** Sets the counter for recording {@code long} values. */
+ LongCounterBuilder ofLongs();
+
+ /**
+ * Builds and returns a {@code DoubleCounter} with the desired options.
+ *
+ * @return a {@code DoubleCounter} with the desired options.
+ */
DoubleCounter build();
+
+ /**
+ * Builds this asynchronous insturment with the given callback.
+ *
+ *
The callback will only be called when the {@link Meter} is being observed.
+ *
+ * @param callback A state-capturing callback used to observe values on-demand.
+ */
+ void buildWithCallback(Consumer callback);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleGaugeBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleGaugeBuilder.java
new file mode 100644
index 0000000000..a1a5d4c7ef
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleGaugeBuilder.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import java.util.function.Consumer;
+
+/** A builder for Gauge metric types. These can only be asynchronously collected. */
+public interface DoubleGaugeBuilder {
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description strings should follow the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
+ DoubleGaugeBuilder setDescription(String description);
+
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
+ DoubleGaugeBuilder setUnit(String unit);
+
+ /** Sets the gauge for recording {@code long} values. */
+ LongGaugeBuilder ofLongs();
+
+ /**
+ * Builds this asynchronous instrument with the given callback.
+ *
+ *
The callback will only be called when the {@link Meter} is being observed.
+ *
+ * @param callback A state-capturing callback used to observe values on-demand.
+ */
+ void buildWithCallback(Consumer callback);
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleHistogram.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleHistogram.java
new file mode 100644
index 0000000000..1205acf9aa
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleHistogram.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.context.Context;
+import javax.annotation.concurrent.ThreadSafe;
+
+/** A histogram instrument that records {@code long} values. */
+@ThreadSafe
+public interface DoubleHistogram {
+
+ /**
+ * Records a value.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The amount of the measurement.
+ */
+ void record(double value);
+
+ /**
+ * Records a value with a set of attributes.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The amount of the measurement.
+ * @param attributes A set of attributes to associate with the count.
+ */
+ void record(double value, Attributes attributes);
+
+ /**
+ * Records a value with a set of attributes.
+ *
+ * @param value The amount of the measurement.
+ * @param attributes A set of attributes to associate with the count.
+ * @param context The explicit context to associate with this measurement.
+ */
+ void record(double value, Attributes attributes, Context context);
+
+ /**
+ * Constructs a bound version of this instrument where all recorded values use the given
+ * attributes.
+ */
+ BoundDoubleHistogram bind(Attributes attributes);
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleHistogramBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleHistogramBuilder.java
new file mode 100644
index 0000000000..f31b7cbaa5
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleHistogramBuilder.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+/** Builder class for {@link DoubleHistogram}. */
+public interface DoubleHistogramBuilder {
+
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description strings should follow the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
+ DoubleHistogramBuilder setDescription(String description);
+
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
+ DoubleHistogramBuilder setUnit(String unit);
+
+ /** Sets the counter for recording {@code long} values. */
+ LongHistogramBuilder ofLongs();
+
+ /**
+ * Builds and returns a {@code DoubleHistogram} with the desired options.
+ *
+ * @return a {@code DoubleHistogram} with the desired options.
+ */
+ DoubleHistogram build();
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleSumObserver.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleSumObserver.java
deleted file mode 100644
index 2b2dd27d52..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleSumObserver.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * {@code SumObserver} is the asynchronous instrument corresponding to Counter, used to capture a
- * monotonic sum with Observe(sum).
- *
- *
"Sum" appears in the name to remind that it is used to capture sums directly. Use a
- * SumObserver to capture any value that starts at zero and rises throughout the process lifetime
- * and never falls.
- *
- *
A {@code SumObserver} is a good choice in situations where a measurement is expensive to
- * compute, such that it would be wasteful to compute on every request.
- *
- *
- */
-@ThreadSafe
-public interface DoubleSumObserver extends AsynchronousInstrument {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleSumObserverBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleSumObserverBuilder.java
deleted file mode 100644
index c0e2054177..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleSumObserverBuilder.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import java.util.function.Consumer;
-
-/** Builder class for {@link DoubleSumObserver}. */
-public interface DoubleSumObserverBuilder
- extends AsynchronousInstrumentBuilder {
- @Override
- DoubleSumObserverBuilder setDescription(String description);
-
- @Override
- DoubleSumObserverBuilder setUnit(String unit);
-
- @Override
- DoubleSumObserverBuilder setUpdater(Consumer updater);
-
- @Override
- DoubleSumObserver build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounter.java
index cc521224d4..d8af95372e 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounter.java
@@ -5,62 +5,46 @@
package io.opentelemetry.api.metrics;
-import io.opentelemetry.api.metrics.common.Labels;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/**
- * UpDownCounter is a synchronous instrument and very similar to Counter except that Add(increment)
- * supports negative increments. This makes UpDownCounter not useful for computing a rate
- * aggregation. The default aggregation is `Sum`, only the sum is non-monotonic. It is generally
- * useful for capturing changes in an amount of resources used, or any quantity that rises and falls
- * during a request.
- *
- *
Example:
- *
- *
{@code
- * class YourClass {
- * private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * private static final DoubleUpDownCounter upDownCounter =
- * meter.
- * .doubleUpDownCounterBuilder("resource_usage")
- * .setDescription("Current resource usage")
- * .setUnit("1")
- * .build();
- *
- * // It is recommended that the API user keep references to a Bound Counters.
- * private static final BoundDoubleUpDownCounter someWorkBound =
- * upDownCounter.bind("work_name", "some_work");
- *
- * void doSomeWork() {
- * someWorkBound.add(10.2); // Resources needed for this task.
- * // Your code here.
- * someWorkBound.add(-10.0);
- * }
- * }
- * }
- */
+/** An up-down-counter instrument that records {@code double} values. */
@ThreadSafe
-public interface DoubleUpDownCounter extends SynchronousInstrument {
+public interface DoubleUpDownCounter {
+ /**
+ * Records a value.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The increment amount. May be positive, negative or zero.
+ */
+ void add(double value);
/**
- * Adds the given {@code increment} to the current value.
+ * Records a value with a set of attributes.
*
- *
The value added is associated with the current {@code Context} and provided set of labels.
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
*
- * @param increment the value to add.
- * @param labels the labels to be associated to this recording.
+ * @param value The increment amount. May be positive, negative or zero.
+ * @param attributes A set of attributes to associate with the count.
*/
- void add(double increment, Labels labels);
+ void add(double value, Attributes attributes);
/**
- * Adds the given {@code increment} to the current value.
+ * Records a value with a set of attributes.
*
- *
The value added is associated with the current {@code Context} and empty labels.
- *
- * @param increment the value to add.
+ * @param value The increment amount. May be positive, negative or zero.
+ * @param attributes A set of attributes to associate with the count.
+ * @param context The explicit context to associate with this measurement.
*/
- void add(double increment);
+ void add(double value, Attributes attributes, Context context);
- @Override
- BoundDoubleUpDownCounter bind(Labels labels);
+ /**
+ * Constructs a bound version of this instrument where all recorded values use the given
+ * attributes.
+ */
+ BoundDoubleUpDownCounter bind(Attributes attributes);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounterBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounterBuilder.java
index 953da7a86f..1600648fe8 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounterBuilder.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounterBuilder.java
@@ -5,14 +5,42 @@
package io.opentelemetry.api.metrics;
+import java.util.function.Consumer;
+
/** Builder class for {@link DoubleUpDownCounter}. */
-public interface DoubleUpDownCounterBuilder extends SynchronousInstrumentBuilder {
- @Override
+public interface DoubleUpDownCounterBuilder {
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description strings should follow the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
DoubleUpDownCounterBuilder setDescription(String description);
- @Override
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
DoubleUpDownCounterBuilder setUnit(String unit);
- @Override
+ /** Sets the counter for recording {@code long} values. */
+ LongUpDownCounterBuilder ofLongs();
+
+ /**
+ * Builds and returns a {@code DoubleUpDownCounter} with the desired options.
+ *
+ * @return a {@code DoubleUpDownCounter} with the desired options.
+ */
DoubleUpDownCounter build();
+
+ /**
+ * Builds this asynchronous instrument with the given callback.
+ *
+ *
The callback will only be called when the {@link Meter} is being observed.
+ *
+ * @param callback A state-capturing callback used to observe values on-demand.
+ */
+ void buildWithCallback(Consumer callback);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownSumObserver.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownSumObserver.java
deleted file mode 100644
index 05f1157567..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownSumObserver.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * UpDownSumObserver is the asynchronous instrument corresponding to UpDownCounter, used to capture
- * a non-monotonic count with Observe(sum).
- *
- *
"Sum" appears in the name to remind that it is used to capture sums directly. Use a
- * UpDownSumObserver to capture any value that starts at zero and rises or falls throughout the
- * process lifetime.
- *
- *
A {@code UpDownSumObserver} is a good choice in situations where a measurement is expensive to
- * compute, such that it would be wasteful to compute on every request.
- *
- *
- */
-@ThreadSafe
-public interface DoubleUpDownSumObserver extends AsynchronousInstrument {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownSumObserverBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownSumObserverBuilder.java
deleted file mode 100644
index e824091e1f..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownSumObserverBuilder.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import java.util.function.Consumer;
-
-/** Builder class for {@link DoubleUpDownSumObserver}. */
-public interface DoubleUpDownSumObserverBuilder
- extends AsynchronousInstrumentBuilder {
- @Override
- DoubleUpDownSumObserverBuilder setDescription(String description);
-
- @Override
- DoubleUpDownSumObserverBuilder setUnit(String unit);
-
- @Override
- DoubleUpDownSumObserverBuilder setUpdater(Consumer updater);
-
- @Override
- DoubleUpDownSumObserver build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueObserver.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueObserver.java
deleted file mode 100644
index 018806beaa..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueObserver.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * {@code ValueObserver} is the asynchronous instrument corresponding to ValueRecorder, used to
- * capture values that are treated as individual observations, recorded with the observe(value)
- * method.
- *
- *
A {@code ValueObserver} is a good choice in situations where a measurement is expensive to
- * compute, such that it would be wasteful to compute on every request.
- *
- *
Example:
- *
- *
{@code
- * // class YourClass {
- * //
- * // private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * // private static final DoubleValueObserver cpuObserver =
- * // meter.
- * // .doubleValueObserverBuilder("cpu_temperature")
- * // .setDescription("System CPU temperature")
- * // .setUnit("ms")
- * // .build();
- * //
- * // void init() {
- * // cpuObserver.setUpdater(
- * // new DoubleValueObserver.Callback() {
- * // @Override
- * // public void update(DoubleResult result) {
- * // // Get system cpu temperature
- * // result.observe(cpuTemperature);
- * // }
- * // });
- * // }
- * // }
- * }
- */
-@ThreadSafe
-public interface DoubleValueObserver extends AsynchronousInstrument {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueObserverBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueObserverBuilder.java
deleted file mode 100644
index eafa8f0dcd..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueObserverBuilder.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import java.util.function.Consumer;
-
-/** Builder class for {@link DoubleValueObserver}. */
-public interface DoubleValueObserverBuilder
- extends AsynchronousInstrumentBuilder {
- @Override
- DoubleValueObserverBuilder setDescription(String description);
-
- @Override
- DoubleValueObserverBuilder setUnit(String unit);
-
- @Override
- DoubleValueObserverBuilder setUpdater(Consumer updater);
-
- @Override
- DoubleValueObserver build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueRecorder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueRecorder.java
deleted file mode 100644
index 80abefe089..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueRecorder.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import io.opentelemetry.api.metrics.common.Labels;
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * ValueRecorder is a synchronous instrument useful for recording any number, positive or negative.
- * Values captured by a Record(value) are treated as individual events belonging to a distribution
- * that is being summarized.
- *
- *
ValueRecorder should be chosen either when capturing measurements that do not contribute
- * meaningfully to a sum, or when capturing numbers that are additive in nature, but where the
- * distribution of individual increments is considered interesting.
- *
- *
One of the most common uses for ValueRecorder is to capture latency measurements. Latency
- * measurements are not additive in the sense that there is little need to know the latency-sum of
- * all processed requests. We use a ValueRecorder instrument to capture latency measurements
- * typically because we are interested in knowing mean, median, and other summary statistics about
- * individual events.
- *
- *
Example:
- *
- *
{@code
- * class YourClass {
- *
- * private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * private static final DoubleValueRecorder valueRecorder =
- * meter.
- * .doubleValueRecorderBuilder("doWork_latency")
- * .setDescription("gRPC Latency")
- * .setUnit("ms")
- * .build();
- *
- * // It is recommended that the API user keep references to a Bound Counters.
- * private static final BoundDoubleValueRecorder someWorkBound =
- * valueRecorder.bind("work_name", "some_work");
- *
- * void doWork() {
- * long startTime = System.nanoTime();
- * // Your code here.
- * someWorkBound.record((System.nanoTime() - startTime) / 1e6);
- * }
- * }
- * }
- */
-@ThreadSafe
-public interface DoubleValueRecorder extends SynchronousInstrument {
-
- /**
- * Records the given measurement, associated with the current {@code Context} and provided set of
- * labels.
- *
- * @param value the measurement to record.
- * @param labels the set of labels to be associated to this recording
- * @throws IllegalArgumentException if value is negative.
- */
- void record(double value, Labels labels);
-
- /**
- * Records the given measurement, associated with the current {@code Context} and empty labels.
- *
- * @param value the measurement to record.
- * @throws IllegalArgumentException if value is negative.
- */
- void record(double value);
-
- @Override
- BoundDoubleValueRecorder bind(Labels labels);
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueRecorderBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueRecorderBuilder.java
deleted file mode 100644
index 17b787ea79..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/DoubleValueRecorderBuilder.java
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-/** Builder class for {@link DoubleValueRecorder}. */
-public interface DoubleValueRecorderBuilder extends SynchronousInstrumentBuilder {
- @Override
- DoubleValueRecorderBuilder setDescription(String description);
-
- @Override
- DoubleValueRecorderBuilder setUnit(String unit);
-
- @Override
- DoubleValueRecorder build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/GlobalMeterProvider.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/GlobalMeterProvider.java
index 9d1b0c9715..833ddc561b 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/GlobalMeterProvider.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/GlobalMeterProvider.java
@@ -5,29 +5,17 @@
package io.opentelemetry.api.metrics;
-import java.util.concurrent.atomic.AtomicReference;
+import io.opentelemetry.api.metrics.internal.NoopMeterProvider;
-/**
- * IMPORTANT: This is a temporary class, and solution for the metrics package until it will be
- * marked as stable.
- */
-public final class GlobalMeterProvider {
- private static final Object mutex = new Object();
- private static final AtomicReference globalMeterProvider = new AtomicReference<>();
+/** This class is a temporary solution until metrics SDK is marked stable. */
+public class GlobalMeterProvider {
+ private static volatile MeterProvider globalMeterProvider = NoopMeterProvider.getInstance();
private GlobalMeterProvider() {}
/** Returns the globally registered {@link MeterProvider}. */
public static MeterProvider get() {
- MeterProvider meterProvider = globalMeterProvider.get();
- if (meterProvider == null) {
- synchronized (mutex) {
- if (globalMeterProvider.get() == null) {
- return MeterProvider.noop();
- }
- }
- }
- return meterProvider;
+ return globalMeterProvider;
}
/**
@@ -36,50 +24,7 @@ public final class GlobalMeterProvider {
* early as possible in your application initialization logic, often in a {@code static} block in
* your main class.
*/
- public static void set(MeterProvider meterProvider) {
- globalMeterProvider.set(meterProvider);
- }
-
- /**
- * Gets or creates a named meter instance from the globally registered {@link MeterProvider}.
- *
- *
This is a shortcut method for {@code getGlobalMeterProvider().get(instrumentationName)}
- *
- * @param instrumentationName The name of the instrumentation library, not the name of the
- * instrument*ed* library.
- * @return a tracer instance.
- */
- public static Meter getMeter(String instrumentationName) {
- return get().get(instrumentationName);
- }
-
- /**
- * Gets or creates a named and versioned meter instance from the globally registered {@link
- * MeterProvider}.
- *
- *
This is a shortcut method for {@code getGlobalMeterProvider().get(instrumentationName,
- * instrumentationVersion)}
- *
- * @param instrumentationName The name of the instrumentation library, not the name of the
- * instrument*ed* library.
- * @param instrumentationVersion The version of the instrumentation library.
- * @return a tracer instance.
- */
- public static Meter getMeter(String instrumentationName, String instrumentationVersion) {
- return get().get(instrumentationName, instrumentationVersion);
- }
-
- /**
- * Creates a {@link MeterBuilder} for a named meter instance.
- *
- *
This is a shortcut method for {@code get().meterBuilder(instrumentationName)}
- *
- * @param instrumentationName The name of the instrumentation library, not the name of the
- * instrument*ed* library.
- * @return a MeterBuilder instance.
- * @since 1.4.0
- */
- public static MeterBuilder meterBuilder(String instrumentationName) {
- return get().meterBuilder(instrumentationName);
+ public static void set(MeterProvider provider) {
+ globalMeterProvider = (provider == null) ? NoopMeterProvider.getInstance() : provider;
}
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/Instrument.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/Instrument.java
deleted file mode 100644
index d9e6228fe8..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/Instrument.java
+++ /dev/null
@@ -1,13 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/** Base interface for all metrics defined in this package. */
-@ThreadSafe
-@SuppressWarnings("InterfaceWithOnlyStatics")
-public interface Instrument {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/InstrumentBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/InstrumentBuilder.java
deleted file mode 100644
index ef146ed4df..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/InstrumentBuilder.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-/** The {@code Builder} class for the {@code Instrument}. */
-public interface InstrumentBuilder {
- /**
- * Sets the description of the {@code Instrument}.
- *
- *
Default value is {@code ""}.
- *
- * @param description the description of the Instrument.
- * @return this.
- */
- InstrumentBuilder setDescription(String description);
-
- /**
- * Sets the unit of the {@code Instrument}.
- *
- *
Default value is {@code "1"}.
- *
- * @param unit the unit of the Instrument.
- * @return this.
- */
- InstrumentBuilder setUnit(String unit);
-
- /**
- * Builds and returns a {@code Instrument} with the desired options.
- *
- * @return a {@code Instrument} with the desired options.
- */
- Instrument build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounter.java
index 3cf662c387..6fdbd2c479 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounter.java
@@ -5,59 +5,47 @@
package io.opentelemetry.api.metrics;
-import io.opentelemetry.api.metrics.common.Labels;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/**
- * Counter is the most common synchronous instrument. This instrument supports an {@link #add(long,
- * Labels)}` function for reporting an increment, and is restricted to non-negative increments. The
- * default aggregation is `Sum`.
- *
- *
Example:
- *
- *
{@code
- * class YourClass {
- * private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * private static final LongCounter counter =
- * meter.
- * .longCounterBuilder("processed_jobs")
- * .setDescription("Processed jobs")
- * .setUnit("1")
- * .build();
- *
- * // It is recommended that the API user keep a reference to a Bound Counter.
- * private static final BoundLongCounter someWorkBound =
- * counter.bind("work_name", "some_work");
- *
- * void doSomeWork() {
- * // Your code here.
- * someWorkBound.add(10);
- * }
- * }
- * }
- */
+/** A counter instrument that records {@code long} values. */
@ThreadSafe
-public interface LongCounter extends SynchronousInstrument {
+public interface LongCounter {
/**
- * Adds the given {@code increment} to the current value. The values cannot be negative.
+ * Records a value.
*
- *
The value added is associated with the current {@code Context} and provided set of labels.
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
*
- * @param increment the value to add.
- * @param labels the set of labels to be associated to this recording.
+ * @param value The increment amount. MUST be non-negative.
*/
- void add(long increment, Labels labels);
+ void add(long value);
/**
- * Adds the given {@code increment} to the current value. The values cannot be negative.
+ * Records a value with a set of attributes.
*
- *
The value added is associated with the current {@code Context} and empty labels.
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
*
- * @param increment the value to add.
+ * @param value The increment amount. MUST be non-negative.
+ * @param attributes A set of attributes to associate with the count.
*/
- void add(long increment);
+ void add(long value, Attributes attributes);
- @Override
- BoundLongCounter bind(Labels labels);
+ /**
+ * Records a value with a set of attributes.
+ *
+ * @param value The increment amount. MUST be non-negative.
+ * @param attributes A set of attributes to associate with the count.
+ * @param context The explicit context to associate with this measurement.
+ */
+ void add(long value, Attributes attributes, Context context);
+
+ /**
+ * Constructs a bound version of this instrument where all recorded values use the given
+ * attributes.
+ */
+ BoundLongCounter bind(Attributes attributes);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounterBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounterBuilder.java
index fd0811b43c..0361517dbf 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounterBuilder.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongCounterBuilder.java
@@ -5,14 +5,43 @@
package io.opentelemetry.api.metrics;
+import java.util.function.Consumer;
+
/** Builder class for {@link LongCounter}. */
-public interface LongCounterBuilder extends SynchronousInstrumentBuilder {
- @Override
+public interface LongCounterBuilder {
+
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description strings should follow the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
LongCounterBuilder setDescription(String description);
- @Override
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
LongCounterBuilder setUnit(String unit);
- @Override
+ /** Sets the counter for recording {@code double} values. */
+ DoubleCounterBuilder ofDoubles();
+
+ /**
+ * Builds and returns a {@code LongCounter} with the desired options.
+ *
+ * @return a {@code LongCounter} with the desired options.
+ */
LongCounter build();
+
+ /**
+ * Builds this asynchronous instrument with the given callback.
+ *
+ *
The callback will only be called when the {@link Meter} is being observed.
+ *
+ * @param callback A state-capturing callback used to observe values on-demand.
+ */
+ void buildWithCallback(Consumer callback);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongGaugeBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongGaugeBuilder.java
new file mode 100644
index 0000000000..bbac5c5da3
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongGaugeBuilder.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import java.util.function.Consumer;
+
+/** A builder for Gauge metric types. These can only be asynchronously collected. */
+public interface LongGaugeBuilder {
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description strings should follow the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
+ public LongGaugeBuilder setDescription(String description);
+
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
+ LongGaugeBuilder setUnit(String unit);
+
+ /** Sets the gauge for recording {@code double} values. */
+ DoubleGaugeBuilder ofDoubles();
+
+ /**
+ * Builds this asynchronous insturment with the given callback.
+ *
+ *
The callback will only be called when the {@link Meter} is being observed.
+ *
+ * @param callback A state-capturing callback used to observe values on-demand.
+ */
+ void buildWithCallback(Consumer callback);
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongHistogram.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongHistogram.java
new file mode 100644
index 0000000000..f074b1bf7b
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongHistogram.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.context.Context;
+import javax.annotation.concurrent.ThreadSafe;
+
+/** A histogram instrument that records {@code long} values. */
+@ThreadSafe
+public interface LongHistogram {
+
+ /**
+ * Records a value.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The amount of the measurement.
+ */
+ void record(long value);
+
+ /**
+ * Records a value with a set of attributes.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The amount of the measurement.
+ * @param attributes A set of attributes to associate with the count.
+ */
+ void record(long value, Attributes attributes);
+
+ /**
+ * Records a value with a set of attributes.
+ *
+ * @param value The amount of the measurement.
+ * @param attributes A set of attributes to associate with the count.
+ * @param context The explicit context to associate with this measurement.
+ */
+ void record(long value, Attributes attributes, Context context);
+
+ /**
+ * Construct a bound version of this instrument where all recorded values use the given
+ * attributes.
+ */
+ BoundLongHistogram bind(Attributes attributes);
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongHistogramBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongHistogramBuilder.java
new file mode 100644
index 0000000000..669f9a7cf8
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongHistogramBuilder.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+/** Builder class for {@link LongHistogram}. */
+public interface LongHistogramBuilder {
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description strings should follow the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
+ LongHistogramBuilder setDescription(String description);
+
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
+ LongHistogramBuilder setUnit(String unit);
+
+ /** Sets the histogram for recording {@code double} values. */
+ DoubleHistogramBuilder ofDoubles();
+
+ /**
+ * Builds and returns a {@code LongHistogram} with the desired options.
+ *
+ * @return a {@code LongHistogram} with the desired options.
+ */
+ LongHistogram build();
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongSumObserver.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongSumObserver.java
deleted file mode 100644
index 11af6f5ed2..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongSumObserver.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * {@code SumObserver} is the asynchronous instrument corresponding to Counter, used to capture a
- * monotonic sum with Observe(sum).
- *
- *
"Sum" appears in the name to remind that it is used to capture sums directly. Use a
- * SumObserver to capture any value that starts at zero and rises throughout the process lifetime
- * and never falls.
- *
- *
A {@code SumObserver} is a good choice in situations where a measurement is expensive to
- * compute, such that it would be wasteful to compute on every request.
- *
- *
- */
-@ThreadSafe
-public interface LongSumObserver extends AsynchronousInstrument {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongSumObserverBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongSumObserverBuilder.java
deleted file mode 100644
index 0d80e50cf6..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongSumObserverBuilder.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import java.util.function.Consumer;
-
-/** Builder class for {@link LongSumObserver}. */
-public interface LongSumObserverBuilder
- extends AsynchronousInstrumentBuilder {
- @Override
- LongSumObserverBuilder setDescription(String description);
-
- @Override
- LongSumObserverBuilder setUnit(String unit);
-
- @Override
- LongSumObserverBuilder setUpdater(Consumer updater);
-
- @Override
- LongSumObserver build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounter.java
index 2789e26bc9..2867963598 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounter.java
@@ -5,62 +5,46 @@
package io.opentelemetry.api.metrics;
-import io.opentelemetry.api.metrics.common.Labels;
+import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.context.Context;
import javax.annotation.concurrent.ThreadSafe;
-/**
- * UpDownCounter is a synchronous instrument and very similar to Counter except that Add(increment)
- * supports negative increments. This makes UpDownCounter not useful for computing a rate
- * aggregation. The default aggregation is `Sum`, only the sum is non-monotonic. It is generally
- * useful for capturing changes in an amount of resources used, or any quantity that rises and falls
- * during a request.
- *
- *
Example:
- *
- *
{@code
- * class YourClass {
- * private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * private static final LongUpDownCounter upDownCounter =
- * meter.
- * .longUpDownCounterBuilder("active_tasks")
- * .setDescription("Number of active tasks")
- * .setUnit("1")
- * .build();
- *
- * // It is recommended that the API user keep a reference to a Bound Counter.
- * private static final BoundLongUpDownCounter someWorkBound =
- * upDownCounter.bind("work_name", "some_work");
- *
- * void doSomeWork() {
- * someWorkBound.add(1);
- * // Your code here.
- * someWorkBound.add(-1);
- * }
- * }
- * }
- */
+/** An up-down-counter instrument that records {@code long} values. */
@ThreadSafe
-public interface LongUpDownCounter extends SynchronousInstrument {
+public interface LongUpDownCounter {
+ /**
+ * Records a value.
+ *
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
+ *
+ * @param value The increment amount. May be positive, negative or zero.
+ */
+ void add(long value);
/**
- * Adds the given {@code increment} to the current value.
+ * Record a value with a set of attributes.
*
- *
The value added is associated with the current {@code Context} and provided set of labels.
+ *
Note: This may use {@code Context.current()} to pull the context associated with this
+ * measurement.
*
- * @param increment the value to add.
- * @param labels the set of labels to be associated to this recording.
+ * @param value The increment amount. May be positive, negative or zero.
+ * @param attributes A set of attributes to associate with the count.
*/
- void add(long increment, Labels labels);
+ void add(long value, Attributes attributes);
/**
- * Adds the given {@code increment} to the current value.
+ * Records a value with a set of attributes.
*
- *
The value added is associated with the current {@code Context} and empty labels.
- *
- * @param increment the value to add.
+ * @param value The increment amount. May be positive, negative or zero.
+ * @param attributes A set of attributes to associate with the count.
+ * @param context The explicit context to associate with this measurement.
*/
- void add(long increment);
+ void add(long value, Attributes attributes, Context context);
- @Override
- BoundLongUpDownCounter bind(Labels labels);
+ /**
+ * Construct a bound version of this instrument where all recorded values use the given
+ * attributes.
+ */
+ BoundLongUpDownCounter bind(Attributes attributes);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounterBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounterBuilder.java
index 94c203099a..ba17ea2686 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounterBuilder.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownCounterBuilder.java
@@ -5,14 +5,42 @@
package io.opentelemetry.api.metrics;
+import java.util.function.Consumer;
+
/** Builder class for {@link LongUpDownCounter}. */
-public interface LongUpDownCounterBuilder extends SynchronousInstrumentBuilder {
- @Override
+public interface LongUpDownCounterBuilder {
+ /**
+ * Sets the description for this instrument.
+ *
+ *
Description strings should follow the instrument description rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-description
+ */
LongUpDownCounterBuilder setDescription(String description);
- @Override
+ /**
+ * Sets the unit of measure for this instrument.
+ *
+ *
Unit strings should follow the instrument unit rules:
+ * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument-unit
+ */
LongUpDownCounterBuilder setUnit(String unit);
- @Override
+ /** Sets the counter for recording {@code double} values. */
+ DoubleUpDownCounterBuilder ofDoubles();
+
+ /**
+ * Builds and returns a {@code LongUpDownCounter} with the desired options.
+ *
+ * @return a {@code LongUpDownCounter} with the desired options.
+ */
LongUpDownCounter build();
+
+ /**
+ * Builds this asynchronous instrument with the given callback.
+ *
+ *
The callback will only be called when the {@link Meter} is being observed.
+ *
+ * @param callback A state-capturing callback used to observe values on-demand.
+ */
+ void buildWithCallback(Consumer callback);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownSumObserver.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownSumObserver.java
deleted file mode 100644
index 57828ce687..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownSumObserver.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * UpDownSumObserver is the asynchronous instrument corresponding to UpDownCounter, used to capture
- * a non-monotonic count with Observe(sum).
- *
- *
"Sum" appears in the name to remind that it is used to capture sums directly. Use a
- * UpDownSumObserver to capture any value that starts at zero and rises or falls throughout the
- * process lifetime.
- *
- *
A {@code UpDownSumObserver} is a good choice in situations where a measurement is expensive to
- * compute, such that it would be wasteful to compute on every request.
- *
- *
- */
-@ThreadSafe
-public interface LongUpDownSumObserver extends AsynchronousInstrument {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownSumObserverBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownSumObserverBuilder.java
deleted file mode 100644
index 547f07e159..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongUpDownSumObserverBuilder.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import java.util.function.Consumer;
-
-/** Builder class for {@link LongUpDownSumObserver}. */
-public interface LongUpDownSumObserverBuilder
- extends AsynchronousInstrumentBuilder {
- @Override
- LongUpDownSumObserverBuilder setDescription(String description);
-
- @Override
- LongUpDownSumObserverBuilder setUnit(String unit);
-
- @Override
- LongUpDownSumObserverBuilder setUpdater(Consumer updater);
-
- @Override
- LongUpDownSumObserver build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueObserver.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueObserver.java
deleted file mode 100644
index 035dac16db..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueObserver.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * {@code ValueObserver} is the asynchronous instrument corresponding to ValueRecorder, used to
- * capture values that are treated as individual observations, recorded with the observe(value)
- * method.
- *
- *
A {@code ValueObserver} is a good choice in situations where a measurement is expensive to
- * compute, such that it would be wasteful to compute on every request.
- *
- *
Example:
- *
- *
{@code
- * // class YourClass {
- * //
- * // private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * // private static final LongValueObserver cpuObserver =
- * // meter.
- * // .longValueObserverBuilder("cpu_fan_speed")
- * // .setDescription("System CPU fan speed")
- * // .setUnit("ms")
- * // .build();
- * //
- * // void init() {
- * // cpuObserver.setUpdater(
- * // new LongValueObserver.Callback() {
- * // @Override
- * // public void update(LongResult result) {
- * // // Get system cpu fan speed
- * // result.observe(cpuFanSpeed);
- * // }
- * // });
- * // }
- * // }
- * }
- */
-@ThreadSafe
-public interface LongValueObserver extends AsynchronousInstrument {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueObserverBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueObserverBuilder.java
deleted file mode 100644
index 2757bea6c9..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueObserverBuilder.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import java.util.function.Consumer;
-
-/** Builder class for {@link LongValueObserver}. */
-public interface LongValueObserverBuilder
- extends AsynchronousInstrumentBuilder {
- @Override
- LongValueObserverBuilder setDescription(String description);
-
- @Override
- LongValueObserverBuilder setUnit(String unit);
-
- @Override
- LongValueObserverBuilder setUpdater(Consumer updater);
-
- @Override
- LongValueObserver build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueRecorder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueRecorder.java
deleted file mode 100644
index 9ddf39c300..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueRecorder.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import io.opentelemetry.api.metrics.common.Labels;
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * ValueRecorder is a synchronous instrument useful for recording any number, positive or negative.
- * Values captured by a Record(value) are treated as individual events belonging to a distribution
- * that is being summarized.
- *
- *
ValueRecorder should be chosen either when capturing measurements that do not contribute
- * meaningfully to a sum, or when capturing numbers that are additive in nature, but where the
- * distribution of individual increments is considered interesting.
- *
- *
One of the most common uses for ValueRecorder is to capture latency measurements. Latency
- * measurements are not additive in the sense that there is little need to know the latency-sum of
- * all processed requests. We use a ValueRecorder instrument to capture latency measurements
- * typically because we are interested in knowing mean, median, and other summary statistics about
- * individual events.
- *
- *
Example:
- *
- *
{@code
- * class YourClass {
- *
- * private static final Meter meter = OpenTelemetry.getMeterProvider().get("my_library_name");
- * private static final LongValueRecorder valueRecorder =
- * meter.
- * .longValueRecorderBuilder("doWork_latency")
- * .setDescription("gRPC Latency")
- * .setUnit("ns")
- * .build();
- *
- * // It is recommended that the API user keep a reference to a Bound Counter.
- * private static final BoundLongValueRecorder someWorkBound =
- * valueRecorder.bind("work_name", "some_work");
- *
- * void doWork() {
- * long startTime = System.nanoTime();
- * // Your code here.
- * someWorkBound.record(System.nanoTime() - startTime);
- * }
- * }
- * }
- */
-@ThreadSafe
-public interface LongValueRecorder extends SynchronousInstrument {
-
- /**
- * Records the given measurement, associated with the current {@code Context} and provided set of
- * labels.
- *
- * @param value the measurement to record.
- * @param labels the set of labels to be associated to this recording
- * @throws IllegalArgumentException if value is negative.
- */
- void record(long value, Labels labels);
-
- /**
- * Records the given measurement, associated with the current {@code Context} and empty labels.
- *
- * @param value the measurement to record.
- * @throws IllegalArgumentException if value is negative.
- */
- void record(long value);
-
- @Override
- BoundLongValueRecorder bind(Labels labels);
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueRecorderBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueRecorderBuilder.java
deleted file mode 100644
index 05bbc94af2..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/LongValueRecorderBuilder.java
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-/** Builder class for {@link LongValueRecorder}. */
-public interface LongValueRecorderBuilder extends SynchronousInstrumentBuilder {
- @Override
- LongValueRecorderBuilder setDescription(String description);
-
- @Override
- LongValueRecorderBuilder setUnit(String unit);
-
- @Override
- LongValueRecorder build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/Meter.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/Meter.java
index 96a81e2e05..681a82ed76 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/Meter.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/Meter.java
@@ -8,174 +8,53 @@ package io.opentelemetry.api.metrics;
import javax.annotation.concurrent.ThreadSafe;
/**
- * Meter is a simple, interface that allows users to record measurements (metrics).
+ * Provides instruments used to produce metrics.
*
- *
There are two ways to record measurements:
+ *
Instruments are obtained through builders provided by this interface. Each builder has a
+ * default "type" associated with recordings that may be changed.
*
- *
- *
Record raw measurements, and defer defining the aggregation and the labels for the exported
- * Instrument. This should be used in libraries like gRPC to record measurements like
- * "server_latency" or "received_bytes".
- *
Record pre-defined aggregation data (or already aggregated data). This should be used to
- * report cpu/memory usage, or simple metrics like "queue_length".
- *
- *
- *
TODO: Update comment.
+ *
A Meter is generally associated with an instrumentation library, e.g. "I monitor apache
+ * httpclient".
*/
@ThreadSafe
public interface Meter {
+ /**
+ * Constructs a counter instrument.
+ *
+ *
This is used to build both synchronous (in-context) instruments and asynchronous (callback)
+ * instruments.
+ *
+ * @param name the name used for the counter.
+ * @return a builder for configuring a new Counter instrument. Defaults to recording long values,
+ * but may be changed.
+ */
+ LongCounterBuilder counterBuilder(String name);
/**
- * Returns a builder for a {@link DoubleCounter}.
+ * Constructs an up-down-counter instrument.
*
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a {@code DoubleCounter.Builder}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
+ *
This is used to build both synchronous (in-context) instruments and asynchronous (callback)
+ * instruments.
+ *
+ * @param name the name used for the counter.
+ * @return a builder for configuring a new Counter synchronous instrument. Defaults to recording
+ * long values, but may be changed.
*/
- DoubleCounterBuilder doubleCounterBuilder(String name);
+ LongUpDownCounterBuilder upDownCounterBuilder(String name);
/**
- * Returns a builder for a {@link LongCounter}.
+ * Constructs a Histogram instrument.
*
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a {@code LongCounter.Builder}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
+ * @param name the name used for the counter.
+ * @return a builder for configuring a new Histogram synchronous instrument. Defaults to recording
+ * double values, but may be changed.
*/
- LongCounterBuilder longCounterBuilder(String name);
+ DoubleHistogramBuilder histogramBuilder(String name);
/**
- * Returns a builder for a {@link DoubleUpDownCounter}.
+ * Constructs an asynchronous gauge.
*
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a {@code DoubleCounter.Builder}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
+ * @return a builder used for configuring how to report gauge measurements on demand.
*/
- DoubleUpDownCounterBuilder doubleUpDownCounterBuilder(String name);
-
- /**
- * Returns a builder for a {@link LongUpDownCounter}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a {@code LongCounter.Builder}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- LongUpDownCounterBuilder longUpDownCounterBuilder(String name);
-
- /**
- * Returns a new builder for a {@link DoubleValueRecorder}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code DoubleValueRecorder}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- DoubleValueRecorderBuilder doubleValueRecorderBuilder(String name);
-
- /**
- * Returns a new builder for a {@link LongValueRecorder}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code LongValueRecorder}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- LongValueRecorderBuilder longValueRecorderBuilder(String name);
-
- /**
- * Returns a new builder for a {@link DoubleSumObserver}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code DoubleSumObserver}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- DoubleSumObserverBuilder doubleSumObserverBuilder(String name);
-
- /**
- * Returns a new builder for a {@link LongSumObserver}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code LongSumObserver}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- LongSumObserverBuilder longSumObserverBuilder(String name);
-
- /**
- * Returns a new builder for a {@link DoubleUpDownSumObserver}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code DoubleUpDownObserver}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- DoubleUpDownSumObserverBuilder doubleUpDownSumObserverBuilder(String name);
-
- /**
- * Returns a new builder for a {@link LongUpDownSumObserver}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code LongUpDownSumObserver}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- LongUpDownSumObserverBuilder longUpDownSumObserverBuilder(String name);
-
- /**
- * Returns a new builder for a {@link DoubleValueObserver}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code DoubleValueObserver}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- DoubleValueObserverBuilder doubleValueObserverBuilder(String name);
-
- /**
- * Returns a new builder for a {@link LongValueObserver}.
- *
- * @param name the name of the instrument. Should be a ASCII string with a length no greater than
- * 255 characters.
- * @return a new builder for a {@code LongValueObserver}.
- * @throws NullPointerException if {@code name} is null.
- * @throws IllegalArgumentException if different metric with the same name already registered.
- * @throws IllegalArgumentException if the {@code name} does not match the requirements.
- */
- LongValueObserverBuilder longValueObserverBuilder(String name);
-
- /**
- * Utility method that allows users to atomically record measurements to a set of Instruments with
- * a common set of labels.
- *
- * @param keyValuePairs The set of labels to associate with this recorder and all it's recordings.
- * @return a {@code MeasureBatchRecorder} that can be use to atomically record a set of
- * measurements associated with different Measures.
- */
- BatchRecorder newBatchRecorder(String... keyValuePairs);
+ DoubleGaugeBuilder gaugeBuilder(String name);
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterBuilder.java
index c84f36ce9d..680ddf2fc9 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterBuilder.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterBuilder.java
@@ -13,7 +13,7 @@ package io.opentelemetry.api.metrics;
public interface MeterBuilder {
/**
- * Assign an OpenTelemetry schema URL to the resulting Meter.
+ * Assigns an OpenTelemetry schema URL to the resulting Meter.
*
* @param schemaUrl The URL of the OpenTelemetry schema being used by this instrumentation
* library.
@@ -22,7 +22,7 @@ public interface MeterBuilder {
MeterBuilder setSchemaUrl(String schemaUrl);
/**
- * Assign a version to the instrumentation library that is using the resulting Meter.
+ * Assigns a version to the instrumentation library that is using the resulting Meter.
*
* @param instrumentationVersion The version of the instrumentation library.
* @return this
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterProvider.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterProvider.java
index d57d5a2225..4a708d76c6 100644
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterProvider.java
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/MeterProvider.java
@@ -5,33 +5,32 @@
package io.opentelemetry.api.metrics;
+import io.opentelemetry.api.metrics.internal.NoopMeterProvider;
import javax.annotation.concurrent.ThreadSafe;
/**
- * A registry for creating named {@link Meter}s. The name Provider is for consistency with
- * other languages and it is NOT loaded using reflection.
+ * A registry for creating named {@link Meter}s.
+ *
+ *
A MeterProvider represents a configured (or noop) Metric collection system that can be used to
+ * instrument code.
+ *
+ *
The name Provider is for consistency with other languages and it is NOT loaded
+ * using reflection.
*
* @see io.opentelemetry.api.metrics.Meter
*/
@ThreadSafe
public interface MeterProvider {
-
/**
- * Returns a {@link MeterProvider} that only creates no-op {@link Instrument}s that neither record
- * nor are emitted.
- */
- static MeterProvider noop() {
- return DefaultMeterProvider.getInstance();
- }
-
- /**
- * Gets or creates a named meter instance.
+ * Gets or creates a named and versioned meter instance.
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
- * @return a tracer instance.
+ * @return a meter instance.
*/
- Meter get(String instrumentationName);
+ default Meter get(String instrumentationName) {
+ return meterBuilder(instrumentationName).build();
+ }
/**
* Gets or creates a named and versioned meter instance.
@@ -39,9 +38,15 @@ public interface MeterProvider {
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @param instrumentationVersion The version of the instrumentation library.
- * @return a tracer instance.
+ * @param schemaUrl Specifies the Schema URL that should be recorded in the emitted metrics.
+ * @return a meter instance.
*/
- Meter get(String instrumentationName, String instrumentationVersion);
+ default Meter get(String instrumentationName, String instrumentationVersion, String schemaUrl) {
+ return meterBuilder(instrumentationName)
+ .setInstrumentationVersion(instrumentationVersion)
+ .setSchemaUrl(schemaUrl)
+ .build();
+ }
/**
* Creates a MeterBuilder for a named meter instance.
@@ -51,7 +56,10 @@ public interface MeterProvider {
* @return a MeterBuilder instance.
* @since 1.4.0
*/
- default MeterBuilder meterBuilder(String instrumentationName) {
- return DefaultMeterBuilder.getInstance();
+ MeterBuilder meterBuilder(String instrumentationName);
+
+ /** Returns a MeterProvider that does nothing. */
+ public static MeterProvider noop() {
+ return NoopMeterProvider.getInstance();
}
}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableDoubleMeasurement.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableDoubleMeasurement.java
new file mode 100644
index 0000000000..9b821c0567
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableDoubleMeasurement.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import io.opentelemetry.api.common.Attributes;
+
+/** An interface for observing measurements with {@code double} values. */
+public interface ObservableDoubleMeasurement extends ObservableMeasurement {
+ /**
+ * Records a measurement.
+ *
+ * @param value The measurement amount. MUST be non-negative.
+ */
+ void observe(double value);
+
+ /**
+ * Records a measurement with a set of attributes.
+ *
+ * @param value The measurement amount. MUST be non-negative.
+ * @param attributes A set of attributes to associate with the count.
+ */
+ void observe(double value, Attributes attributes);
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableLongMeasurement.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableLongMeasurement.java
new file mode 100644
index 0000000000..87a5a57b9b
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableLongMeasurement.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+import io.opentelemetry.api.common.Attributes;
+
+/** An interface for observing measurements with {@code long} values. */
+public interface ObservableLongMeasurement extends ObservableMeasurement {
+ /**
+ * Records a measurement.
+ *
+ * @param value The measurement amount. MUST be non-negative.
+ */
+ void observe(long value);
+
+ /**
+ * Records a measurement with a set of attributes.
+ *
+ * @param value The measurement amount. MUST be non-negative.
+ * @param attributes A set of attributes to associate with the count.
+ */
+ void observe(long value, Attributes attributes);
+}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableMeasurement.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableMeasurement.java
new file mode 100644
index 0000000000..1a20132362
--- /dev/null
+++ b/api/metrics/src/main/java/io/opentelemetry/api/metrics/ObservableMeasurement.java
@@ -0,0 +1,13 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.api.metrics;
+
+/**
+ * A mechanism for observing measurments.
+ *
+ *
see {@link ObservableDoubleMeasurement} or {@link ObservableLongMeasurement}.
+ */
+public interface ObservableMeasurement {}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/SynchronousInstrument.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/SynchronousInstrument.java
deleted file mode 100644
index d68de6785d..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/SynchronousInstrument.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-import io.opentelemetry.api.metrics.common.Labels;
-import javax.annotation.concurrent.ThreadSafe;
-
-/**
- * SynchronousInstrument is an interface that defines a type of instruments that are used to report
- * measurements synchronously. That is, when the user reports individual measurements as they occur.
- *
- *
Synchronous instrument events additionally have a Context associated with them, describing
- * properties of the associated trace and distributed correlation values.
- *
- * @param the specific type of Bound Instrument this instrument can provide.
- */
-@ThreadSafe
-public interface SynchronousInstrument extends Instrument {
- /**
- * Returns a {@code Bound Instrument} associated with the specified labels. Multiples requests
- * with the same set of labels may return the same {@code Bound Instrument} instance.
- *
- *
It is recommended that callers keep a reference to the Bound Instrument instead of always
- * calling this method for every operation.
- *
- * @param labels the set of labels, as key-value pairs.
- * @return a {@code Bound Instrument}
- * @throws NullPointerException if {@code labelValues} is null.
- */
- B bind(Labels labels);
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/SynchronousInstrumentBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/SynchronousInstrumentBuilder.java
deleted file mode 100644
index 40961b7a66..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/SynchronousInstrumentBuilder.java
+++ /dev/null
@@ -1,12 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics;
-
-/** Builder class for {@link SynchronousInstrument}. */
-public interface SynchronousInstrumentBuilder extends InstrumentBuilder {
- @Override
- SynchronousInstrument> build();
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/common/ArrayBackedLabels.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/common/ArrayBackedLabels.java
deleted file mode 100644
index 99ea6bba24..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/common/ArrayBackedLabels.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics.common;
-
-import io.opentelemetry.api.internal.ImmutableKeyValuePairs;
-import javax.annotation.concurrent.Immutable;
-
-@Immutable
-final class ArrayBackedLabels extends ImmutableKeyValuePairs implements Labels {
- private static final Labels EMPTY = Labels.builder().build();
-
- static Labels empty() {
- return EMPTY;
- }
-
- private ArrayBackedLabels(Object[] data) {
- super(data);
- }
-
- static Labels sortAndFilterToLabels(Object... data) {
- return new ArrayBackedLabels(data);
- }
-
- @Override
- public LabelsBuilder toBuilder() {
- return new ArrayBackedLabelsBuilder(data());
- }
-}
diff --git a/api/metrics/src/main/java/io/opentelemetry/api/metrics/common/ArrayBackedLabelsBuilder.java b/api/metrics/src/main/java/io/opentelemetry/api/metrics/common/ArrayBackedLabelsBuilder.java
deleted file mode 100644
index 44ca9aaf99..0000000000
--- a/api/metrics/src/main/java/io/opentelemetry/api/metrics/common/ArrayBackedLabelsBuilder.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.api.metrics.common;
-
-import java.util.ArrayList;
-import java.util.List;
-
-class ArrayBackedLabelsBuilder implements LabelsBuilder {
- private final List