Implement attributes advice for the rest of the instruments (#5722)

This commit is contained in:
Mateusz Rzeszutek 2023-08-23 18:25:35 +02:00 committed by GitHub
parent 32d591ae53
commit 4b06f09504
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 547 additions and 54 deletions

View File

@ -1,4 +1,6 @@
subprojects {
// Workaround https://github.com/gradle/gradle/issues/847
group = "io.opentelemetry.extensions"
val proj = this
plugins.withId("java") {
configure<BasePluginExtension> {

View File

@ -10,8 +10,8 @@ import io.opentelemetry.api.metrics.DoubleCounter;
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<AttributeKey<?>> attributes);
DoubleCounterAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes);
}

View File

@ -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<AttributeKey<?>> attributes);
}

View File

@ -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<Double> bucketBoundaries);
/** Specify the recommended set of attribute keys to be used for this histogram. */
DoubleHistogramAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes);
}

View File

@ -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<AttributeKey<?>> attributes);
}

View File

@ -12,7 +12,7 @@ import java.util.function.Consumer;
public interface ExtendedDoubleCounterBuilder extends DoubleCounterBuilder {
/** Specify advice for counter implementations. */
default DoubleCounterBuilder setAdvice(Consumer<CounterAdviceConfigurer> adviceConsumer) {
default DoubleCounterBuilder setAdvice(Consumer<DoubleCounterAdviceConfigurer> adviceConsumer) {
return this;
}
}

View File

@ -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<DoubleGaugeAdviceConfigurer> adviceConsumer) {
return this;
}
}

View File

@ -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<DoubleUpDownCounterAdviceConfigurer> adviceConsumer) {
return this;
}
}

View File

@ -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<LongCounterAdviceConfigurer> adviceConsumer) {
return this;
}
}

View File

@ -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<LongGaugeAdviceConfigurer> adviceConsumer) {
return this;
}
}

View File

@ -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<LongUpDownCounterAdviceConfigurer> adviceConsumer) {
return this;
}
}

View File

@ -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<AttributeKey<?>> attributes);
}

View File

@ -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<AttributeKey<?>> attributes);
}

View File

@ -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<Long> bucketBoundaries);
/** Specify the recommended set of attribute keys to be used for this histogram. */
LongHistogramAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes);
}

View File

@ -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<AttributeKey<?>> attributes);
}

View File

@ -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<BasePluginExtension> {

View File

@ -12,7 +12,7 @@ import io.opentelemetry.api.metrics.DoubleCounterBuilder;
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 @@ final class SdkDoubleCounter extends AbstractInstrument implements DoubleCounter
static final class SdkDoubleCounterBuilder
extends AbstractInstrumentBuilder<SdkDoubleCounterBuilder>
implements ExtendedDoubleCounterBuilder, CounterAdviceConfigurer {
implements ExtendedDoubleCounterBuilder, DoubleCounterAdviceConfigurer {
SdkDoubleCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
@ -87,7 +87,7 @@ final class SdkDoubleCounter extends AbstractInstrument implements DoubleCounter
}
@Override
public DoubleCounterBuilder setAdvice(Consumer<CounterAdviceConfigurer> adviceConsumer) {
public DoubleCounterBuilder setAdvice(Consumer<DoubleCounterAdviceConfigurer> adviceConsumer) {
adviceConsumer.accept(this);
return this;
}
@ -109,7 +109,7 @@ final class SdkDoubleCounter extends AbstractInstrument implements DoubleCounter
}
@Override
public CounterAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
public DoubleCounterAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}

View File

@ -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<SdkDoubleGaugeBuilder>
implements DoubleGaugeBuilder {
implements ExtendedDoubleGaugeBuilder, DoubleGaugeAdviceConfigurer {
SdkDoubleGaugeBuilder(
MeterProviderSharedState meterProviderSharedState,
@ -35,6 +39,12 @@ final class SdkDoubleGaugeBuilder extends AbstractInstrumentBuilder<SdkDoubleGau
return this;
}
@Override
public DoubleGaugeBuilder setAdvice(Consumer<DoubleGaugeAdviceConfigurer> adviceConsumer) {
adviceConsumer.accept(this);
return this;
}
@Override
public LongGaugeBuilder ofLongs() {
return swapBuilder(SdkLongGaugeBuilder::new);
@ -49,4 +59,10 @@ final class SdkDoubleGaugeBuilder extends AbstractInstrumentBuilder<SdkDoubleGau
public ObservableDoubleMeasurement buildObserver() {
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE);
}
@Override
public DoubleGaugeAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}
}

View File

@ -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 @@ final class SdkDoubleHistogram extends AbstractInstrument implements DoubleHisto
adviceBuilder.setExplicitBucketBoundaries(bucketBoundaries);
return this;
}
@Override
public DoubleHistogramAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}
}
}

View File

@ -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 @@ final class SdkDoubleUpDownCounter extends AbstractInstrument implements DoubleU
static final class SdkDoubleUpDownCounterBuilder
extends AbstractInstrumentBuilder<SdkDoubleUpDownCounterBuilder>
implements DoubleUpDownCounterBuilder {
implements ExtendedDoubleUpDownCounterBuilder, DoubleUpDownCounterAdviceConfigurer {
SdkDoubleUpDownCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
@ -69,6 +73,13 @@ final class SdkDoubleUpDownCounter extends AbstractInstrument implements DoubleU
return this;
}
@Override
public DoubleUpDownCounterBuilder setAdvice(
Consumer<DoubleUpDownCounterAdviceConfigurer> adviceConsumer) {
adviceConsumer.accept(this);
return this;
}
@Override
public DoubleUpDownCounter build() {
return buildSynchronousInstrument(SdkDoubleUpDownCounter::new);
@ -85,5 +96,11 @@ final class SdkDoubleUpDownCounter extends AbstractInstrument implements DoubleU
public ObservableDoubleMeasurement buildObserver() {
return buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER);
}
@Override
public DoubleUpDownCounterAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}
}
}

View File

@ -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.LongCounterBuilder;
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 @@ final class SdkLongCounter extends AbstractInstrument implements LongCounter {
}
static final class SdkLongCounterBuilder extends AbstractInstrumentBuilder<SdkLongCounterBuilder>
implements LongCounterBuilder {
implements ExtendedLongCounterBuilder, LongCounterAdviceConfigurer {
SdkLongCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
@ -78,6 +82,12 @@ final class SdkLongCounter extends AbstractInstrument implements LongCounter {
return this;
}
@Override
public LongCounterBuilder setAdvice(Consumer<LongCounterAdviceConfigurer> adviceConsumer) {
adviceConsumer.accept(this);
return this;
}
@Override
public SdkLongCounter build() {
return buildSynchronousInstrument(SdkLongCounter::new);
@ -97,5 +107,11 @@ final class SdkLongCounter extends AbstractInstrument implements LongCounter {
public ObservableLongMeasurement buildObserver() {
return buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER);
}
@Override
public LongCounterAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}
}
}

View File

@ -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<SdkLongGaugeBuilder>
implements LongGaugeBuilder {
implements ExtendedLongGaugeBuilder, LongGaugeAdviceConfigurer {
SdkLongGaugeBuilder(
MeterProviderSharedState meterProviderSharedState,
@ -39,6 +43,12 @@ final class SdkLongGaugeBuilder extends AbstractInstrumentBuilder<SdkLongGaugeBu
return this;
}
@Override
public LongGaugeBuilder setAdvice(Consumer<LongGaugeAdviceConfigurer> adviceConsumer) {
adviceConsumer.accept(this);
return this;
}
@Override
public ObservableLongGauge buildWithCallback(Consumer<ObservableLongMeasurement> callback) {
return registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback);
@ -48,4 +58,10 @@ final class SdkLongGaugeBuilder extends AbstractInstrumentBuilder<SdkLongGaugeBu
public ObservableLongMeasurement buildObserver() {
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE);
}
@Override
public LongGaugeAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}
}

View File

@ -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 @@ final class SdkLongHistogram extends AbstractInstrument implements LongHistogram
adviceBuilder.setExplicitBucketBoundaries(doubleBoundaries);
return this;
}
@Override
public LongHistogramAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}
}
}

View File

@ -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.LongUpDownCounterBuilder;
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 @@ final class SdkLongUpDownCounter extends AbstractInstrument implements LongUpDow
static final class SdkLongUpDownCounterBuilder
extends AbstractInstrumentBuilder<SdkLongUpDownCounterBuilder>
implements LongUpDownCounterBuilder {
implements ExtendedLongUpDownCounterBuilder, LongUpDownCounterAdviceConfigurer {
SdkLongUpDownCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
@ -65,6 +69,13 @@ final class SdkLongUpDownCounter extends AbstractInstrument implements LongUpDow
return this;
}
@Override
public LongUpDownCounterBuilder setAdvice(
Consumer<LongUpDownCounterAdviceConfigurer> adviceConsumer) {
adviceConsumer.accept(this);
return this;
}
@Override
public LongUpDownCounter build() {
return buildSynchronousInstrument(SdkLongUpDownCounter::new);
@ -86,5 +97,11 @@ final class SdkLongUpDownCounter extends AbstractInstrument implements LongUpDow
public ObservableLongMeasurement buildObserver() {
return buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER);
}
@Override
public LongUpDownCounterAdviceConfigurer setAttributes(List<AttributeKey<?>> attributes) {
adviceBuilder.setAttributes(attributes);
return this;
}
}
}

View File

@ -9,14 +9,51 @@ import static io.opentelemetry.api.common.AttributeKey.stringKey;
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 @@ class AttributesAdviceTest {
meterProvider.close();
}
@Test
void counterWithoutAdvice() {
@ParameterizedTest
@ArgumentsSource(InstrumentsProvider.class)
void instrumentWithoutAdvice(
InstrumentFactory instrumentFactory, PointsAssert<AbstractPointAssert<?, ?>> 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<AbstractPointAssert<?, ?>> 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<AbstractPointAssert<?, ?>> 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<? extends Arguments> 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<DoublePointAssert>)
(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<LongPointAssert>)
(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<Attributes> attributesRef = new AtomicReference<>();
doubleGaugeBuilder.buildWithCallback(
measurement ->
measurement.record(valueRef.doubleValue(), attributesRef.get()));
return (value, attributes) -> {
valueRef.set((double) value);
attributesRef.set(attributes);
};
},
(PointsAssert<DoublePointAssert>)
(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<Attributes> attributesRef = new AtomicReference<>();
doubleGaugeBuilder.buildWithCallback(
measurement ->
measurement.record(valueRef.longValue(), attributesRef.get()));
return (value, attributes) -> {
valueRef.set(value);
attributesRef.set(attributes);
};
},
(PointsAssert<LongPointAssert>)
(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<HistogramPointAssert>)
(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<HistogramPointAssert>)
(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<DoublePointAssert>)
(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<LongPointAssert>)
(metricAssert, assertions) ->
metricAssert.hasLongSumSatisfying(
sum -> sum.hasPointsSatisfying(assertions))));
}
}
@FunctionalInterface
interface InstrumentFactory {
Instrument create(
SdkMeterProvider meterProvider,
String name,
@Nullable List<AttributeKey<?>> attributesAdvice);
}
@FunctionalInterface
interface Instrument {
void record(long value, Attributes attributes);
}
@FunctionalInterface
interface PointsAssert<P extends AbstractPointAssert<?, ?>> {
void hasPointSatisfying(MetricAssert metricAssert, Consumer<P> assertion);
}
}