From 877070329e2a6f535632a7ed63a7e86baebfd553 Mon Sep 17 00:00:00 2001 From: jason plumb <75337021+breedx-splk@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:49:41 -0700 Subject: [PATCH] Add LongHistogramAdviceConfigurer to improve api surface types (#5689) --- .../metrics/DoubleHistogramAdviceConfigurer.java | 16 ++++++++++++++++ .../metrics/ExtendedDoubleHistogramBuilder.java | 3 ++- .../metrics/ExtendedLongHistogramBuilder.java | 2 +- ...r.java => LongHistogramAdviceConfigurer.java} | 7 +++---- .../sdk/metrics/SdkDoubleHistogram.java | 10 ++++++---- .../sdk/metrics/SdkLongHistogram.java | 14 +++++++++----- .../io/opentelemetry/sdk/metrics/AdviceTest.java | 3 +-- 7 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleHistogramAdviceConfigurer.java rename extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/{HistogramAdviceConfigurer.java => LongHistogramAdviceConfigurer.java} (62%) 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 new file mode 100644 index 00000000000..3b78d5849fe --- /dev/null +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleHistogramAdviceConfigurer.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.metrics.DoubleHistogram; +import java.util.List; + +/** Configure advice for implementations of {@link DoubleHistogram}. */ +public interface DoubleHistogramAdviceConfigurer { + + /** Specify recommended set of explicit bucket boundaries for this histogram. */ + DoubleHistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries); +} diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleHistogramBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleHistogramBuilder.java index bc45f29227a..c48cf9420de 100644 --- a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleHistogramBuilder.java +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleHistogramBuilder.java @@ -12,7 +12,8 @@ public interface ExtendedDoubleHistogramBuilder extends DoubleHistogramBuilder { /** Specify advice for histogram implementations. */ - default DoubleHistogramBuilder setAdvice(Consumer adviceConsumer) { + default DoubleHistogramBuilder setAdvice( + Consumer adviceConsumer) { return this; } } diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongHistogramBuilder.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongHistogramBuilder.java index 2e95b798619..211c86fe7cd 100644 --- a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongHistogramBuilder.java +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongHistogramBuilder.java @@ -12,7 +12,7 @@ public interface ExtendedLongHistogramBuilder extends LongHistogramBuilder { /** Specify advice for histogram implementations. */ - default LongHistogramBuilder setAdvice(Consumer adviceConsumer) { + default LongHistogramBuilder setAdvice(Consumer adviceConsumer) { return this; } } diff --git a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/HistogramAdviceConfigurer.java b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongHistogramAdviceConfigurer.java similarity index 62% rename from extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/HistogramAdviceConfigurer.java rename to extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongHistogramAdviceConfigurer.java index 67fca913b8c..208ceebc5a7 100644 --- a/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/HistogramAdviceConfigurer.java +++ b/extensions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongHistogramAdviceConfigurer.java @@ -5,13 +5,12 @@ package io.opentelemetry.extension.incubator.metrics; -import io.opentelemetry.api.metrics.DoubleHistogram; import io.opentelemetry.api.metrics.LongHistogram; import java.util.List; -/** Configure advice for implementations of {@link LongHistogram} and {@link DoubleHistogram}. */ -public interface HistogramAdviceConfigurer { +/** Configure advice for implementations of {@link LongHistogram}. */ +public interface LongHistogramAdviceConfigurer { /** Specify recommended set of explicit bucket boundaries for this histogram. */ - HistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries); + LongHistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries); } 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 983af957273..534258153ac 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 @@ -9,8 +9,8 @@ import io.opentelemetry.api.metrics.DoubleHistogram; import io.opentelemetry.api.metrics.LongHistogramBuilder; import io.opentelemetry.context.Context; +import io.opentelemetry.extension.incubator.metrics.DoubleHistogramAdviceConfigurer; import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleHistogramBuilder; -import io.opentelemetry.extension.incubator.metrics.HistogramAdviceConfigurer; import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; @@ -58,7 +58,7 @@ public void record(double value) { static final class SdkDoubleHistogramBuilder extends AbstractInstrumentBuilder - implements ExtendedDoubleHistogramBuilder, HistogramAdviceConfigurer { + implements ExtendedDoubleHistogramBuilder, DoubleHistogramAdviceConfigurer { SdkDoubleHistogramBuilder( MeterProviderSharedState meterProviderSharedState, @@ -80,7 +80,8 @@ protected SdkDoubleHistogramBuilder getThis() { } @Override - public SdkDoubleHistogramBuilder setAdvice(Consumer adviceConsumer) { + public SdkDoubleHistogramBuilder setAdvice( + Consumer adviceConsumer) { adviceConsumer.accept(this); return this; } @@ -96,7 +97,8 @@ public LongHistogramBuilder ofLongs() { } @Override - public HistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries) { + public DoubleHistogramAdviceConfigurer setExplicitBucketBoundaries( + List bucketBoundaries) { setAdvice(Advice.create(bucketBoundaries)); 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 40469287489..dd86f3f9ab2 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 @@ -9,7 +9,7 @@ import io.opentelemetry.api.metrics.LongHistogram; import io.opentelemetry.context.Context; import io.opentelemetry.extension.incubator.metrics.ExtendedLongHistogramBuilder; -import io.opentelemetry.extension.incubator.metrics.HistogramAdviceConfigurer; +import io.opentelemetry.extension.incubator.metrics.LongHistogramAdviceConfigurer; import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; @@ -20,6 +20,7 @@ import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; final class SdkLongHistogram extends AbstractInstrument implements LongHistogram { private static final Logger logger = Logger.getLogger(SdkLongHistogram.class.getName()); @@ -57,7 +58,7 @@ public void record(long value) { static final class SdkLongHistogramBuilder extends AbstractInstrumentBuilder - implements ExtendedLongHistogramBuilder, HistogramAdviceConfigurer { + implements ExtendedLongHistogramBuilder, LongHistogramAdviceConfigurer { SdkLongHistogramBuilder( MeterProviderSharedState meterProviderSharedState, @@ -83,7 +84,8 @@ protected SdkLongHistogramBuilder getThis() { } @Override - public SdkLongHistogramBuilder setAdvice(Consumer adviceConsumer) { + public SdkLongHistogramBuilder setAdvice( + Consumer adviceConsumer) { adviceConsumer.accept(this); return this; } @@ -94,8 +96,10 @@ public SdkLongHistogram build() { } @Override - public HistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries) { - setAdvice(Advice.create(bucketBoundaries)); + public LongHistogramAdviceConfigurer setExplicitBucketBoundaries(List bucketBoundaries) { + List doubleBoundaries = + bucketBoundaries.stream().map(Long::doubleValue).collect(Collectors.toList()); + setAdvice(Advice.create(doubleBoundaries)); return this; } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AdviceTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AdviceTest.java index 8325fd7350a..27975d8dce6 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AdviceTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AdviceTest.java @@ -194,8 +194,7 @@ private static Stream histogramsWithAdvice() { meterProvider.get("meter").histogramBuilder("histogram").ofLongs()) .setAdvice( advice -> - advice.setExplicitBucketBoundaries( - Arrays.asList(10.0, 20.0, 30.0))) + advice.setExplicitBucketBoundaries(Arrays.asList(10L, 20L, 30L))) .build(); return build::record; }));