From 4b06f0950408922e53762c137dd335a9bd96790e Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Wed, 23 Aug 2023 18:25:35 +0200 Subject: [PATCH] Implement attributes advice for the rest of the instruments (#5722) --- extensions/build.gradle.kts | 2 + ...ava => DoubleCounterAdviceConfigurer.java} | 4 +- .../metrics/DoubleGaugeAdviceConfigurer.java | 16 + .../DoubleHistogramAdviceConfigurer.java | 4 + .../DoubleUpDownCounterAdviceConfigurer.java | 17 + .../metrics/ExtendedDoubleCounterBuilder.java | 2 +- .../metrics/ExtendedDoubleGaugeBuilder.java | 18 ++ .../ExtendedDoubleUpDownCounterBuilder.java | 19 ++ .../metrics/ExtendedLongCounterBuilder.java | 18 ++ .../metrics/ExtendedLongGaugeBuilder.java | 18 ++ .../ExtendedLongUpDownCounterBuilder.java | 19 ++ .../metrics/LongCounterAdviceConfigurer.java | 17 + .../metrics/LongGaugeAdviceConfigurer.java | 16 + .../LongHistogramAdviceConfigurer.java | 4 + .../LongUpDownCounterAdviceConfigurer.java | 17 + sdk-extensions/build.gradle.kts | 2 + .../sdk/metrics/SdkDoubleCounter.java | 8 +- .../sdk/metrics/SdkDoubleGaugeBuilder.java | 18 +- .../sdk/metrics/SdkDoubleHistogram.java | 7 + .../sdk/metrics/SdkDoubleUpDownCounter.java | 19 +- .../sdk/metrics/SdkLongCounter.java | 18 +- .../sdk/metrics/SdkLongGaugeBuilder.java | 18 +- .../sdk/metrics/SdkLongHistogram.java | 7 + .../sdk/metrics/SdkLongUpDownCounter.java | 19 +- .../sdk/metrics/AttributesAdviceTest.java | 294 +++++++++++++++--- 25 files changed, 547 insertions(+), 54 deletions(-) rename extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/{CounterAdviceConfigurer.java => DoubleCounterAdviceConfigurer.java} (76%) create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleGaugeAdviceConfigurer.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleUpDownCounterAdviceConfigurer.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleGaugeBuilder.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleUpDownCounterBuilder.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongCounterBuilder.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongGaugeBuilder.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongUpDownCounterBuilder.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongCounterAdviceConfigurer.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongGaugeAdviceConfigurer.java create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongUpDownCounterAdviceConfigurer.java diff --git a/extensions/build.gradle.kts b/extensions/build.gradle.kts index 82930834d2e..f0744dfe9ef 100644 --- a/extensions/build.gradle.kts +++ b/extensions/build.gradle.kts @@ -1,4 +1,6 @@ subprojects { + // Workaround https://github.com/gradle/gradle/issues/847 + group = "io.opentelemetry.extensions" val proj = this plugins.withId("java") { configure { diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/CounterAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleCounterAdviceConfigurer.java similarity index 76% rename from extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/CounterAdviceConfigurer.java rename to extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleCounterAdviceConfigurer.java index 0d183d35654..51c3a6c3447 100644 --- a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/CounterAdviceConfigurer.java +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleCounterAdviceConfigurer.java @@ -10,8 +10,8 @@ import java.util.List; /** Configure advice for implementation of {@link DoubleCounter}. */ -public interface CounterAdviceConfigurer { +public interface DoubleCounterAdviceConfigurer { /** Specify the recommended set of attribute keys to be used for this counter. */ - CounterAdviceConfigurer setAttributes(List> attributes); + DoubleCounterAdviceConfigurer setAttributes(List> attributes); } diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleGaugeAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleGaugeAdviceConfigurer.java new file mode 100644 index 00000000000..3c99c1d6df5 --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleGaugeAdviceConfigurer.java @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import java.util.List; + +/** Configure advice for implementation of {@code DoubleGauge}. */ +public interface DoubleGaugeAdviceConfigurer { + + /** Specify the recommended set of attribute keys to be used for this gauge. */ + DoubleGaugeAdviceConfigurer setAttributes(List> attributes); +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleHistogramAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleHistogramAdviceConfigurer.java index 3b78d5849fe..404a3db9d16 100644 --- a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleHistogramAdviceConfigurer.java +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleHistogramAdviceConfigurer.java @@ -5,6 +5,7 @@ package io.opentelemetry.extension.incubator.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.metrics.DoubleHistogram; import java.util.List; @@ -13,4 +14,7 @@ public interface DoubleHistogramAdviceConfigurer { /** Specify recommended set of explicit bucket boundaries for this histogram. */ DoubleHistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries); + + /** Specify the recommended set of attribute keys to be used for this histogram. */ + DoubleHistogramAdviceConfigurer setAttributes(List> attributes); } diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleUpDownCounterAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleUpDownCounterAdviceConfigurer.java new file mode 100644 index 00000000000..06acffb4e3a --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleUpDownCounterAdviceConfigurer.java @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.metrics.DoubleUpDownCounter; +import java.util.List; + +/** Configure advice for implementation of {@link DoubleUpDownCounter}. */ +public interface DoubleUpDownCounterAdviceConfigurer { + + /** Specify the recommended set of attribute keys to be used for this up down counter. */ + DoubleUpDownCounterAdviceConfigurer setAttributes(List> attributes); +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleCounterBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleCounterBuilder.java index a5d0873eb01..a6bcc97a8c2 100644 --- a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleCounterBuilder.java +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleCounterBuilder.java @@ -12,7 +12,7 @@ public interface ExtendedDoubleCounterBuilder extends DoubleCounterBuilder { /** Specify advice for counter implementations. */ - default DoubleCounterBuilder setAdvice(Consumer adviceConsumer) { + default DoubleCounterBuilder setAdvice(Consumer adviceConsumer) { return this; } } diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleGaugeBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleGaugeBuilder.java new file mode 100644 index 00000000000..c79aa9d7d48 --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleGaugeBuilder.java @@ -0,0 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.metrics.DoubleGaugeBuilder; +import java.util.function.Consumer; + +/** Extended {@link DoubleGaugeBuilder} with experimental APIs. */ +public interface ExtendedDoubleGaugeBuilder extends DoubleGaugeBuilder { + + /** Specify advice for gauge implementations. */ + default DoubleGaugeBuilder setAdvice(Consumer adviceConsumer) { + return this; + } +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleUpDownCounterBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleUpDownCounterBuilder.java new file mode 100644 index 00000000000..b50e0e155db --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleUpDownCounterBuilder.java @@ -0,0 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; +import java.util.function.Consumer; + +/** Extended {@link DoubleUpDownCounterBuilder} with experimental APIs. */ +public interface ExtendedDoubleUpDownCounterBuilder extends DoubleUpDownCounterBuilder { + + /** Specify advice for up down counter implementations. */ + default DoubleUpDownCounterBuilder setAdvice( + Consumer adviceConsumer) { + return this; + } +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongCounterBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongCounterBuilder.java new file mode 100644 index 00000000000..9171156d8ae --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongCounterBuilder.java @@ -0,0 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.metrics.LongCounterBuilder; +import java.util.function.Consumer; + +/** Extended {@link LongCounterBuilder} with experimental APIs. */ +public interface ExtendedLongCounterBuilder extends LongCounterBuilder { + + /** Specify advice for counter implementations. */ + default LongCounterBuilder setAdvice(Consumer adviceConsumer) { + return this; + } +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongGaugeBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongGaugeBuilder.java new file mode 100644 index 00000000000..5861fb5e315 --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongGaugeBuilder.java @@ -0,0 +1,18 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.metrics.LongGaugeBuilder; +import java.util.function.Consumer; + +/** Extended {@link LongGaugeBuilder} with experimental APIs. */ +public interface ExtendedLongGaugeBuilder extends LongGaugeBuilder { + + /** Specify advice for gauge implementations. */ + default LongGaugeBuilder setAdvice(Consumer adviceConsumer) { + return this; + } +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongUpDownCounterBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongUpDownCounterBuilder.java new file mode 100644 index 00000000000..75cb286376e --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongUpDownCounterBuilder.java @@ -0,0 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; +import java.util.function.Consumer; + +/** Extended {@link LongUpDownCounterBuilder} with experimental APIs. */ +public interface ExtendedLongUpDownCounterBuilder extends LongUpDownCounterBuilder { + + /** Specify advice for up down counter implementations. */ + default LongUpDownCounterBuilder setAdvice( + Consumer adviceConsumer) { + return this; + } +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongCounterAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongCounterAdviceConfigurer.java new file mode 100644 index 00000000000..3a0e9bbe70d --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongCounterAdviceConfigurer.java @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.metrics.LongCounter; +import java.util.List; + +/** Configure advice for implementation of {@link LongCounter}. */ +public interface LongCounterAdviceConfigurer { + + /** Specify the recommended set of attribute keys to be used for this counter. */ + LongCounterAdviceConfigurer setAttributes(List> attributes); +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongGaugeAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongGaugeAdviceConfigurer.java new file mode 100644 index 00000000000..c424a878971 --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongGaugeAdviceConfigurer.java @@ -0,0 +1,16 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import java.util.List; + +/** Configure advice for implementation of {@code LongGauge}. */ +public interface LongGaugeAdviceConfigurer { + + /** Specify the recommended set of attribute keys to be used for this gauge. */ + LongGaugeAdviceConfigurer setAttributes(List> attributes); +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongHistogramAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongHistogramAdviceConfigurer.java index 208ceebc5a7..0c5fc91ad27 100644 --- a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongHistogramAdviceConfigurer.java +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongHistogramAdviceConfigurer.java @@ -5,6 +5,7 @@ package io.opentelemetry.extension.incubator.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.metrics.LongHistogram; import java.util.List; @@ -13,4 +14,7 @@ public interface LongHistogramAdviceConfigurer { /** Specify recommended set of explicit bucket boundaries for this histogram. */ LongHistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries); + + /** Specify the recommended set of attribute keys to be used for this histogram. */ + LongHistogramAdviceConfigurer setAttributes(List> attributes); } diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongUpDownCounterAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongUpDownCounterAdviceConfigurer.java new file mode 100644 index 00000000000..5090a824997 --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongUpDownCounterAdviceConfigurer.java @@ -0,0 +1,17 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.extension.incubator.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.metrics.LongUpDownCounter; +import java.util.List; + +/** Configure advice for implementation of {@link LongUpDownCounter}. */ +public interface LongUpDownCounterAdviceConfigurer { + + /** Specify the recommended set of attribute keys to be used for this up down counter. */ + LongUpDownCounterAdviceConfigurer setAttributes(List> attributes); +} diff --git a/sdk-extensions/build.gradle.kts b/sdk-extensions/build.gradle.kts index 0f94cb7c259..bcb76a95ae0 100644 --- a/sdk-extensions/build.gradle.kts +++ b/sdk-extensions/build.gradle.kts @@ -1,4 +1,6 @@ subprojects { + // Workaround https://github.com/gradle/gradle/issues/847 + group = "io.opentelemetry.sdk.extensions" val proj = this plugins.withId("java") { configure { diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java index 0aa48dae4d5..04372eeb886 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java @@ -12,7 +12,7 @@ import io.opentelemetry.api.metrics.ObservableDoubleCounter; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; import io.opentelemetry.context.Context; -import io.opentelemetry.extension.incubator.metrics.CounterAdviceConfigurer; +import io.opentelemetry.extension.incubator.metrics.DoubleCounterAdviceConfigurer; import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleCounterBuilder; import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; @@ -61,7 +61,7 @@ public void add(double increment) { static final class SdkDoubleCounterBuilder extends AbstractInstrumentBuilder - implements ExtendedDoubleCounterBuilder, CounterAdviceConfigurer { + implements ExtendedDoubleCounterBuilder, DoubleCounterAdviceConfigurer { SdkDoubleCounterBuilder( MeterProviderSharedState meterProviderSharedState, @@ -87,7 +87,7 @@ protected SdkDoubleCounterBuilder getThis() { } @Override - public DoubleCounterBuilder setAdvice(Consumer adviceConsumer) { + public DoubleCounterBuilder setAdvice(Consumer adviceConsumer) { adviceConsumer.accept(this); return this; } @@ -109,7 +109,7 @@ public ObservableDoubleMeasurement buildObserver() { } @Override - public CounterAdviceConfigurer setAttributes(List> attributes) { + public DoubleCounterAdviceConfigurer setAttributes(List> attributes) { adviceBuilder.setAttributes(attributes); return this; } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGaugeBuilder.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGaugeBuilder.java index fda925d4c32..38c4a237404 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGaugeBuilder.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGaugeBuilder.java @@ -5,16 +5,20 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.metrics.DoubleGaugeBuilder; import io.opentelemetry.api.metrics.LongGaugeBuilder; import io.opentelemetry.api.metrics.ObservableDoubleGauge; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; +import io.opentelemetry.extension.incubator.metrics.DoubleGaugeAdviceConfigurer; +import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleGaugeBuilder; import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; +import java.util.List; import java.util.function.Consumer; final class SdkDoubleGaugeBuilder extends AbstractInstrumentBuilder - implements DoubleGaugeBuilder { + implements ExtendedDoubleGaugeBuilder, DoubleGaugeAdviceConfigurer { SdkDoubleGaugeBuilder( MeterProviderSharedState meterProviderSharedState, @@ -35,6 +39,12 @@ protected SdkDoubleGaugeBuilder getThis() { return this; } + @Override + public DoubleGaugeBuilder setAdvice(Consumer adviceConsumer) { + adviceConsumer.accept(this); + return this; + } + @Override public LongGaugeBuilder ofLongs() { return swapBuilder(SdkLongGaugeBuilder::new); @@ -49,4 +59,10 @@ public ObservableDoubleGauge buildWithCallback(Consumer> attributes) { + adviceBuilder.setAttributes(attributes); + return this; + } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java index 4e6b11ab0d5..e9b671a3d6e 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java @@ -5,6 +5,7 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleHistogram; import io.opentelemetry.api.metrics.LongHistogramBuilder; @@ -101,5 +102,11 @@ public DoubleHistogramAdviceConfigurer setExplicitBucketBoundaries( adviceBuilder.setExplicitBucketBoundaries(bucketBoundaries); return this; } + + @Override + public DoubleHistogramAdviceConfigurer setAttributes(List> attributes) { + adviceBuilder.setAttributes(attributes); + return this; + } } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java index 5471f964905..22a6b68b6e3 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java @@ -5,17 +5,21 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleUpDownCounter; import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; import io.opentelemetry.api.metrics.ObservableDoubleUpDownCounter; import io.opentelemetry.context.Context; +import io.opentelemetry.extension.incubator.metrics.DoubleUpDownCounterAdviceConfigurer; +import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleUpDownCounterBuilder; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; import java.util.function.Consumer; final class SdkDoubleUpDownCounter extends AbstractInstrument implements DoubleUpDownCounter { @@ -44,7 +48,7 @@ public void add(double increment) { static final class SdkDoubleUpDownCounterBuilder extends AbstractInstrumentBuilder - implements DoubleUpDownCounterBuilder { + implements ExtendedDoubleUpDownCounterBuilder, DoubleUpDownCounterAdviceConfigurer { SdkDoubleUpDownCounterBuilder( MeterProviderSharedState meterProviderSharedState, @@ -69,6 +73,13 @@ protected SdkDoubleUpDownCounterBuilder getThis() { return this; } + @Override + public DoubleUpDownCounterBuilder setAdvice( + Consumer adviceConsumer) { + adviceConsumer.accept(this); + return this; + } + @Override public DoubleUpDownCounter build() { return buildSynchronousInstrument(SdkDoubleUpDownCounter::new); @@ -85,5 +96,11 @@ public ObservableDoubleUpDownCounter buildWithCallback( public ObservableDoubleMeasurement buildObserver() { return buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); } + + @Override + public DoubleUpDownCounterAdviceConfigurer setAttributes(List> attributes) { + adviceBuilder.setAttributes(attributes); + return this; + } } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java index b84858b6fef..dcdca53858a 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java @@ -5,6 +5,7 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleCounterBuilder; import io.opentelemetry.api.metrics.LongCounter; @@ -12,11 +13,14 @@ import io.opentelemetry.api.metrics.ObservableLongCounter; import io.opentelemetry.api.metrics.ObservableLongMeasurement; import io.opentelemetry.context.Context; +import io.opentelemetry.extension.incubator.metrics.ExtendedLongCounterBuilder; +import io.opentelemetry.extension.incubator.metrics.LongCounterAdviceConfigurer; import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; @@ -57,7 +61,7 @@ public void add(long increment) { } static final class SdkLongCounterBuilder extends AbstractInstrumentBuilder - implements LongCounterBuilder { + implements ExtendedLongCounterBuilder, LongCounterAdviceConfigurer { SdkLongCounterBuilder( MeterProviderSharedState meterProviderSharedState, @@ -78,6 +82,12 @@ protected SdkLongCounterBuilder getThis() { return this; } + @Override + public LongCounterBuilder setAdvice(Consumer adviceConsumer) { + adviceConsumer.accept(this); + return this; + } + @Override public SdkLongCounter build() { return buildSynchronousInstrument(SdkLongCounter::new); @@ -97,5 +107,11 @@ public ObservableLongCounter buildWithCallback(Consumer> attributes) { + adviceBuilder.setAttributes(attributes); + return this; + } } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGaugeBuilder.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGaugeBuilder.java index 8ed3a40a7f9..30a9d4385f6 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGaugeBuilder.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGaugeBuilder.java @@ -5,16 +5,20 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.metrics.LongGaugeBuilder; import io.opentelemetry.api.metrics.ObservableLongGauge; import io.opentelemetry.api.metrics.ObservableLongMeasurement; +import io.opentelemetry.extension.incubator.metrics.ExtendedLongGaugeBuilder; +import io.opentelemetry.extension.incubator.metrics.LongGaugeAdviceConfigurer; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; +import java.util.List; import java.util.function.Consumer; final class SdkLongGaugeBuilder extends AbstractInstrumentBuilder - implements LongGaugeBuilder { + implements ExtendedLongGaugeBuilder, LongGaugeAdviceConfigurer { SdkLongGaugeBuilder( MeterProviderSharedState meterProviderSharedState, @@ -39,6 +43,12 @@ protected SdkLongGaugeBuilder getThis() { return this; } + @Override + public LongGaugeBuilder setAdvice(Consumer adviceConsumer) { + adviceConsumer.accept(this); + return this; + } + @Override public ObservableLongGauge buildWithCallback(Consumer callback) { return registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); @@ -48,4 +58,10 @@ public ObservableLongGauge buildWithCallback(Consumer public ObservableLongMeasurement buildObserver() { return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE); } + + @Override + public LongGaugeAdviceConfigurer setAttributes(List> attributes) { + adviceBuilder.setAttributes(attributes); + return this; + } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java index e30aec268c6..5e0d33b6578 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java @@ -5,6 +5,7 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.LongHistogram; import io.opentelemetry.context.Context; @@ -102,5 +103,11 @@ public LongHistogramAdviceConfigurer setExplicitBucketBoundaries(List buck adviceBuilder.setExplicitBucketBoundaries(doubleBoundaries); return this; } + + @Override + public LongHistogramAdviceConfigurer setAttributes(List> attributes) { + adviceBuilder.setAttributes(attributes); + return this; + } } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java index ea132c3b692..7c167acc28f 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java @@ -5,6 +5,7 @@ package io.opentelemetry.sdk.metrics; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; import io.opentelemetry.api.metrics.LongUpDownCounter; @@ -12,10 +13,13 @@ import io.opentelemetry.api.metrics.ObservableLongMeasurement; import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; import io.opentelemetry.context.Context; +import io.opentelemetry.extension.incubator.metrics.ExtendedLongUpDownCounterBuilder; +import io.opentelemetry.extension.incubator.metrics.LongUpDownCounterAdviceConfigurer; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState; import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; import java.util.function.Consumer; final class SdkLongUpDownCounter extends AbstractInstrument implements LongUpDownCounter { @@ -44,7 +48,7 @@ public void add(long increment) { static final class SdkLongUpDownCounterBuilder extends AbstractInstrumentBuilder - implements LongUpDownCounterBuilder { + implements ExtendedLongUpDownCounterBuilder, LongUpDownCounterAdviceConfigurer { SdkLongUpDownCounterBuilder( MeterProviderSharedState meterProviderSharedState, @@ -65,6 +69,13 @@ protected SdkLongUpDownCounterBuilder getThis() { return this; } + @Override + public LongUpDownCounterBuilder setAdvice( + Consumer adviceConsumer) { + adviceConsumer.accept(this); + return this; + } + @Override public LongUpDownCounter build() { return buildSynchronousInstrument(SdkLongUpDownCounter::new); @@ -86,5 +97,11 @@ public ObservableLongUpDownCounter buildWithCallback( public ObservableLongMeasurement buildObserver() { return buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); } + + @Override + public LongUpDownCounterAdviceConfigurer setAttributes(List> attributes) { + adviceBuilder.setAttributes(attributes); + return this; + } } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java index f6b5f67f7f9..efc1aff53f5 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java @@ -9,14 +9,51 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; import static java.util.Arrays.asList; +import static org.junit.jupiter.params.provider.Arguments.arguments; +import com.google.common.util.concurrent.AtomicDouble; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.DoubleCounter; import io.opentelemetry.api.metrics.DoubleCounterBuilder; +import io.opentelemetry.api.metrics.DoubleGaugeBuilder; +import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.DoubleUpDownCounter; +import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.LongCounterBuilder; +import io.opentelemetry.api.metrics.LongGaugeBuilder; +import io.opentelemetry.api.metrics.LongHistogram; +import io.opentelemetry.api.metrics.LongHistogramBuilder; +import io.opentelemetry.api.metrics.LongUpDownCounter; +import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleCounterBuilder; +import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleGaugeBuilder; +import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder; +import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleUpDownCounterBuilder; +import io.opentelemetry.extension.incubator.metrics.ExtendedLongCounterBuilder; +import io.opentelemetry.extension.incubator.metrics.ExtendedLongGaugeBuilder; +import io.opentelemetry.extension.incubator.metrics.ExtendedLongHistogramBuilder; +import io.opentelemetry.extension.incubator.metrics.ExtendedLongUpDownCounterBuilder; +import io.opentelemetry.sdk.testing.assertj.AbstractPointAssert; +import io.opentelemetry.sdk.testing.assertj.DoublePointAssert; +import io.opentelemetry.sdk.testing.assertj.HistogramPointAssert; +import io.opentelemetry.sdk.testing.assertj.LongPointAssert; +import io.opentelemetry.sdk.testing.assertj.MetricAssert; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.stream.Stream; +import javax.annotation.Nullable; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; class AttributesAdviceTest { @@ -34,78 +71,251 @@ void cleanup() { meterProvider.close(); } - @Test - void counterWithoutAdvice() { + @ParameterizedTest + @ArgumentsSource(InstrumentsProvider.class) + void instrumentWithoutAdvice( + InstrumentFactory instrumentFactory, PointsAssert> pointsAssert) { InMemoryMetricReader reader = InMemoryMetricReader.create(); meterProvider = SdkMeterProvider.builder().registerMetricReader(reader).build(); - DoubleCounter counter = - meterProvider.get("meter").counterBuilder("counter").ofDoubles().build(); - counter.add(1, ATTRIBUTES); + Instrument instrument = instrumentFactory.create(meterProvider, "test", null); + instrument.record(1, ATTRIBUTES); assertThat(reader.collectAllMetrics()) .satisfiesExactly( metric -> - assertThat(metric) - .hasDoubleSumSatisfying( - sum -> sum.hasPointsSatisfying(point -> point.hasAttributes(ATTRIBUTES)))); + pointsAssert.hasPointSatisfying( + assertThat(metric), point -> point.hasAttributes(ATTRIBUTES))); } - @Test - void counterWithAdvice() { + @ParameterizedTest + @ArgumentsSource(InstrumentsProvider.class) + void instrumentWithAdvice( + InstrumentFactory instrumentFactory, PointsAssert> pointsAssert) { InMemoryMetricReader reader = InMemoryMetricReader.create(); meterProvider = SdkMeterProvider.builder().registerMetricReader(reader).build(); - DoubleCounterBuilder doubleCounterBuilder = - meterProvider.get("meter").counterBuilder("counter").ofDoubles(); - ((ExtendedDoubleCounterBuilder) doubleCounterBuilder) - .setAdvice(advice -> advice.setAttributes(asList(stringKey("key1"), stringKey("key2")))); - DoubleCounter counter = doubleCounterBuilder.build(); - counter.add(1, ATTRIBUTES); + Instrument instrument = + instrumentFactory.create( + meterProvider, "test", asList(stringKey("key1"), stringKey("key2"))); + instrument.record(1, ATTRIBUTES); assertThat(reader.collectAllMetrics()) .satisfiesExactly( metric -> - assertThat(metric) - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point.hasAttributesSatisfyingExactly( - equalTo(stringKey("key1"), "1"), - equalTo(stringKey("key2"), "2"))))); + pointsAssert.hasPointSatisfying( + assertThat(metric), + point -> + point.hasAttributesSatisfyingExactly( + equalTo(stringKey("key1"), "1"), equalTo(stringKey("key2"), "2")))); } - @Test - void counterWithAdviceAndViews() { + @ParameterizedTest + @ArgumentsSource(InstrumentsProvider.class) + void instrumentWithAdviceAndViews( + InstrumentFactory instrumentFactory, PointsAssert> pointsAssert) { InMemoryMetricReader reader = InMemoryMetricReader.create(); meterProvider = SdkMeterProvider.builder() .registerMetricReader(reader) .registerView( - InstrumentSelector.builder().setType(InstrumentType.COUNTER).build(), + InstrumentSelector.builder().setName("test").build(), View.builder() .setAttributeFilter(key -> "key2".equals(key) || "key3".equals(key)) .build()) .build(); - DoubleCounterBuilder doubleCounterBuilder = - meterProvider.get("meter").counterBuilder("counter").ofDoubles(); - ((ExtendedDoubleCounterBuilder) doubleCounterBuilder) - .setAdvice(advice -> advice.setAttributes(asList(stringKey("key1"), stringKey("key2")))); - DoubleCounter counter = doubleCounterBuilder.build(); - counter.add(1, ATTRIBUTES); + Instrument instrument = + instrumentFactory.create( + meterProvider, "test", asList(stringKey("key1"), stringKey("key2"))); + instrument.record(1, ATTRIBUTES); assertThat(reader.collectAllMetrics()) .satisfiesExactly( metric -> - assertThat(metric) - .hasDoubleSumSatisfying( - sum -> - sum.hasPointsSatisfying( - point -> - point.hasAttributesSatisfyingExactly( - equalTo(stringKey("key2"), "2"), - equalTo(stringKey("key3"), "3"))))); + pointsAssert.hasPointSatisfying( + assertThat(metric), + point -> + point.hasAttributesSatisfyingExactly( + equalTo(stringKey("key2"), "2"), equalTo(stringKey("key3"), "3")))); + } + + static final class InstrumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext context) { + return Stream.of( + // double counter + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + DoubleCounterBuilder doubleCounterBuilder = + meterProvider.get("meter").counterBuilder(name).ofDoubles(); + if (attributesAdvice != null) { + ((ExtendedDoubleCounterBuilder) doubleCounterBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + DoubleCounter counter = doubleCounterBuilder.build(); + return counter::add; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasDoubleSumSatisfying( + sum -> sum.hasPointsSatisfying(assertions))), + // long counter + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + LongCounterBuilder doubleCounterBuilder = + meterProvider.get("meter").counterBuilder(name); + if (attributesAdvice != null) { + ((ExtendedLongCounterBuilder) doubleCounterBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + LongCounter counter = doubleCounterBuilder.build(); + return counter::add; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasLongSumSatisfying( + sum -> sum.hasPointsSatisfying(assertions))), + // double gauge + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + DoubleGaugeBuilder doubleGaugeBuilder = + meterProvider.get("meter").gaugeBuilder(name); + if (attributesAdvice != null) { + ((ExtendedDoubleGaugeBuilder) doubleGaugeBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + AtomicDouble valueRef = new AtomicDouble(); + AtomicReference attributesRef = new AtomicReference<>(); + doubleGaugeBuilder.buildWithCallback( + measurement -> + measurement.record(valueRef.doubleValue(), attributesRef.get())); + return (value, attributes) -> { + valueRef.set((double) value); + attributesRef.set(attributes); + }; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasDoubleGaugeSatisfying( + sum -> sum.hasPointsSatisfying(assertions))), + // long gauge + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + LongGaugeBuilder doubleGaugeBuilder = + meterProvider.get("meter").gaugeBuilder(name).ofLongs(); + if (attributesAdvice != null) { + ((ExtendedLongGaugeBuilder) doubleGaugeBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + AtomicLong valueRef = new AtomicLong(); + AtomicReference attributesRef = new AtomicReference<>(); + doubleGaugeBuilder.buildWithCallback( + measurement -> + measurement.record(valueRef.longValue(), attributesRef.get())); + return (value, attributes) -> { + valueRef.set(value); + attributesRef.set(attributes); + }; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasLongGaugeSatisfying( + sum -> sum.hasPointsSatisfying(assertions))), + // double histogram + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + DoubleHistogramBuilder doubleHistogramBuilder = + meterProvider.get("meter").histogramBuilder(name); + if (attributesAdvice != null) { + ((ExtendedDoubleHistogramBuilder) doubleHistogramBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + DoubleHistogram histogram = doubleHistogramBuilder.build(); + return histogram::record; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasHistogramSatisfying( + sum -> sum.hasPointsSatisfying(assertions))), + // long histogram + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + LongHistogramBuilder doubleHistogramBuilder = + meterProvider.get("meter").histogramBuilder(name).ofLongs(); + if (attributesAdvice != null) { + ((ExtendedLongHistogramBuilder) doubleHistogramBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + LongHistogram histogram = doubleHistogramBuilder.build(); + return histogram::record; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasHistogramSatisfying( + sum -> sum.hasPointsSatisfying(assertions))), + // double up down counter + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + DoubleUpDownCounterBuilder doubleUpDownCounterBuilder = + meterProvider.get("meter").upDownCounterBuilder(name).ofDoubles(); + if (attributesAdvice != null) { + ((ExtendedDoubleUpDownCounterBuilder) doubleUpDownCounterBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + DoubleUpDownCounter upDownCounter = doubleUpDownCounterBuilder.build(); + return upDownCounter::add; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasDoubleSumSatisfying( + sum -> sum.hasPointsSatisfying(assertions))), + // long up down counter + arguments( + (InstrumentFactory) + (meterProvider, name, attributesAdvice) -> { + LongUpDownCounterBuilder doubleUpDownCounterBuilder = + meterProvider.get("meter").upDownCounterBuilder(name); + if (attributesAdvice != null) { + ((ExtendedLongUpDownCounterBuilder) doubleUpDownCounterBuilder) + .setAdvice(advice -> advice.setAttributes(attributesAdvice)); + } + LongUpDownCounter upDownCounter = doubleUpDownCounterBuilder.build(); + return upDownCounter::add; + }, + (PointsAssert) + (metricAssert, assertions) -> + metricAssert.hasLongSumSatisfying( + sum -> sum.hasPointsSatisfying(assertions)))); + } + } + + @FunctionalInterface + interface InstrumentFactory { + + Instrument create( + SdkMeterProvider meterProvider, + String name, + @Nullable List> attributesAdvice); + } + + @FunctionalInterface + interface Instrument { + + void record(long value, Attributes attributes); + } + + @FunctionalInterface + interface PointsAssert

> { + + void hasPointSatisfying(MetricAssert metricAssert, Consumer

assertion); } }