Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedDoubleCounterBuilder extends DoubleCounterBuilder {
default ExtendedDoubleCounterBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this counter should be disabled by default
* and require explicit opt-in via a view to produce metric data.
*/
default ExtendedDoubleCounterBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedDoubleGaugeBuilder extends DoubleGaugeBuilder {
default ExtendedDoubleGaugeBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this gauge should be disabled by default and
* require explicit opt-in via a view to produce metric data.
*/
default ExtendedDoubleGaugeBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedDoubleHistogramBuilder extends DoubleHistogramBuilder {
default ExtendedDoubleHistogramBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this histogram should be disabled by default
* and require explicit opt-in via a view to produce metric data.
*/
default ExtendedDoubleHistogramBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedDoubleUpDownCounterBuilder extends DoubleUpDownCounterB
default ExtendedDoubleUpDownCounterBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this up down counter should be disabled by
* default and require explicit opt-in via a view to produce metric data.
*/
default ExtendedDoubleUpDownCounterBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedLongCounterBuilder extends LongCounterBuilder {
default ExtendedLongCounterBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this counter should be disabled by default
* and require explicit opt-in via a view to produce metric data.
*/
default ExtendedLongCounterBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedLongGaugeBuilder extends LongGaugeBuilder {
default ExtendedLongGaugeBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this gauge should be disabled by default and
* require explicit opt-in via a view to produce metric data.
*/
default ExtendedLongGaugeBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedLongHistogramBuilder extends LongHistogramBuilder {
default ExtendedLongHistogramBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this histogram should be disabled by default
* and require explicit opt-in via a view to produce metric data.
*/
default ExtendedLongHistogramBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public interface ExtendedLongUpDownCounterBuilder extends LongUpDownCounterBuild
default ExtendedLongUpDownCounterBuilder setAttributesAdvice(List<AttributeKey<?>> attributes) {
return this;
}

/**
* Specify the opt-in advice, which indicates whether this up down counter should be disabled by
* default and require explicit opt-in via a view to produce metric data.
*/
default ExtendedLongUpDownCounterBuilder setOptInAdvice(boolean optIn) {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,43 @@ void incubatingApiIsLoaded() {
assertThat(meter.upDownCounterBuilder("test").ofDoubles().build().isEnabled()).isFalse();
assertThat(meter.upDownCounterBuilder("test").ofDoubles().build())
.isInstanceOf(ExtendedDoubleUpDownCounter.class);
assertThat(
((ExtendedLongGaugeBuilder) meter.gaugeBuilder("test").ofLongs())
.setOptInAdvice(true)
.build())
.isInstanceOf(ExtendedLongGauge.class);
assertThat(
((ExtendedDoubleGaugeBuilder) meter.gaugeBuilder("test")).setOptInAdvice(true).build())
.isInstanceOf(ExtendedDoubleGauge.class);
assertThat(
((ExtendedLongHistogramBuilder) meter.histogramBuilder("test").ofLongs())
.setOptInAdvice(true)
.build())
.isInstanceOf(ExtendedLongHistogram.class);
assertThat(
((ExtendedDoubleHistogramBuilder) meter.histogramBuilder("test"))
.setOptInAdvice(true)
.build())
.isInstanceOf(ExtendedDoubleHistogram.class);
assertThat(
((ExtendedLongCounterBuilder) meter.counterBuilder("test"))
.setOptInAdvice(true)
.build())
.isInstanceOf(ExtendedLongCounter.class);
assertThat(
((ExtendedDoubleCounterBuilder) meter.counterBuilder("test").ofDoubles())
.setOptInAdvice(true)
.build())
.isInstanceOf(ExtendedDoubleCounter.class);
assertThat(
((ExtendedLongUpDownCounterBuilder) meter.upDownCounterBuilder("test"))
.setOptInAdvice(true)
.build())
.isInstanceOf(ExtendedLongUpDownCounter.class);
assertThat(
((ExtendedDoubleUpDownCounterBuilder) meter.upDownCounterBuilder("test").ofDoubles())
.setOptInAdvice(true)
.build())
.isInstanceOf(ExtendedDoubleUpDownCounter.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.View;
import io.opentelemetry.sdk.metrics.ViewBuilder;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
Expand Down Expand Up @@ -147,4 +148,48 @@ void attributesAdvice() {
.hasAttributes(
Attributes.builder().put("key1", "value1").build()))));
}

@Test
void optInAdvice() {
InMemoryMetricReader reader = InMemoryMetricReader.create();
ViewBuilder enabledViewBuilder = View.builder();
SdkMeterProviderUtil.setEnabled(enabledViewBuilder, /* enabled= */ true);
SdkMeterProvider meterProvider =
SdkMeterProvider.builder()
.setResource(Resource.getDefault())
.registerMetricReader(reader)
.registerView(
InstrumentSelector.builder().setName("counter1").build(),
enabledViewBuilder.build())
.build();

Meter meter = meterProvider.get("org.foo.my-scope");

ExtendedLongCounter counter1 =
(ExtendedLongCounter)
((ExtendedLongCounterBuilder) meter.counterBuilder("counter1"))
.setOptInAdvice(true)
.build();
ExtendedLongCounter counter2 =
(ExtendedLongCounter)
((ExtendedLongCounterBuilder) meter.counterBuilder("counter2"))
.setOptInAdvice(true)
.build();

assertThat(counter1.isEnabled()).isTrue();
assertThat(counter2.isEnabled()).isFalse();

counter1.add(1);
counter2.add(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Perhaps adding an assertion to reflect the expected behavior of updating a disabled counter might be good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in e5409e0 (satisfiesExactly asserts that collectAllMetrics() only contains counter1 and that counter2's measurement was dropped; added a comment above the assertion to make that explicit).


// Verify that only counter1 is collected and measurements recorded to disabled counter2 are
// dropped
assertThat(reader.collectAllMetrics())
.satisfiesExactly(
metricData ->
assertThat(metricData)
.hasName("counter1")
.hasLongSumSatisfying(
sumAssert -> sumAssert.hasPointsSatisfying(point -> point.hasValue(1L))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@ public ExtendedDoubleCounterBuilder setAttributesAdvice(List<AttributeKey<?>> at
builder.setAdviceAttributes(attributes);
return this;
}

@Override
public ExtendedDoubleCounterBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public ExtendedDoubleGaugeBuilder setAttributesAdvice(List<AttributeKey<?>> attr
return this;
}

@Override
public ExtendedDoubleGaugeBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}

@Override
public ExtendedLongGaugeBuilder ofLongs() {
return builder.swapBuilder(ExtendedSdkLongGauge.ExtendedSdkLongGaugeBuilder::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@ public ExtendedDoubleHistogramBuilder setAttributesAdvice(List<AttributeKey<?>>
builder.setAdviceAttributes(attributes);
return this;
}

@Override
public ExtendedDoubleHistogramBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@ public ExtendedDoubleUpDownCounterBuilder setAttributesAdvice(
builder.setAdviceAttributes(attributes);
return this;
}

@Override
public ExtendedDoubleUpDownCounterBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,11 @@ public ExtendedLongCounterBuilder setAttributesAdvice(List<AttributeKey<?>> attr
builder.setAdviceAttributes(attributes);
return this;
}

@Override
public ExtendedLongCounterBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,11 @@ public ExtendedLongGaugeBuilder setAttributesAdvice(List<AttributeKey<?>> attrib
builder.setAdviceAttributes(attributes);
return this;
}

@Override
public ExtendedLongGaugeBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,11 @@ public ExtendedLongHistogramBuilder setAttributesAdvice(List<AttributeKey<?>> at
builder.setAdviceAttributes(attributes);
return this;
}

@Override
public ExtendedLongHistogramBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,11 @@ public ExtendedLongUpDownCounterBuilder setAttributesAdvice(List<AttributeKey<?>
builder.setAdviceAttributes(attributes);
return this;
}

@Override
public ExtendedLongUpDownCounterBuilder setOptInAdvice(boolean optIn) {
builder.setAdviceOptIn(optIn);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@ void setAdviceAttributes(List<AttributeKey<?>> attributes) {
void setExplicitBucketBoundaries(List<Double> bucketBoundaries) {
adviceBuilder.setExplicitBucketBoundaries(bucketBoundaries);
}

void setAdviceOptIn(boolean optIn) {
adviceBuilder.setOptIn(optIn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public SdkMeterProviderBuilder registerView(InstrumentSelector selector, View vi
view,
AttributesFilters.byKeyName(view.getAttributeFilter()),
view.getCardinalityLimit(),
SourceInfo.fromCurrentStack()));
SourceInfo.fromCurrentStack(),
view.getEnabled()));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ static View create(
@Nullable String description,
Aggregation aggregation,
Predicate<String> attributeFilter,
int cardinalityLimit) {
return new AutoValue_View(name, description, aggregation, attributeFilter, cardinalityLimit);
int cardinalityLimit,
@Nullable Boolean enabled) {
return new AutoValue_View(
name, description, aggregation, attributeFilter, cardinalityLimit, enabled);
}

View() {}
Expand Down Expand Up @@ -71,6 +73,13 @@ static View create(
*/
public abstract int getCardinalityLimit();

/**
* Returns whether the metric stream from this view is produced, or {@code null} if not explicitly
* configured.
*/
@Nullable
abstract Boolean getEnabled();

@Override
public final String toString() {
StringJoiner joiner = new StringJoiner(", ", "View{", "}");
Expand All @@ -83,6 +92,9 @@ public final String toString() {
joiner.add("aggregation=" + getAggregation());
joiner.add("attributeFilter=" + getAttributeFilter());
joiner.add("cardinalityLimit=" + getCardinalityLimit());
if (getEnabled() != null) {
joiner.add("enabled=" + getEnabled());
}
return joiner.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.sdk.common.export.MemoryMode;
import io.opentelemetry.sdk.common.internal.IncludeExcludePredicate;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.metrics.internal.aggregator.AggregatorFactory;
import io.opentelemetry.sdk.metrics.internal.state.MetricStorage;
import io.opentelemetry.sdk.metrics.internal.view.StringPredicates;
Expand All @@ -28,6 +29,7 @@ public final class ViewBuilder {
private Aggregation aggregation = Aggregation.defaultAggregation();
private Predicate<String> attributeFilter = StringPredicates.ALL;
private int cardinalityLimit = MetricStorage.DEFAULT_MAX_CARDINALITY;
@Nullable private Boolean enabled;

ViewBuilder() {}

Expand Down Expand Up @@ -125,8 +127,21 @@ public ViewBuilder setCardinalityLimit(int cardinalityLimit) {
return this;
}

/**
* Sets whether the metric stream from the view is produced.
*
* <p>This method is experimental so not public. You may reflectively call it using {@link
* SdkMeterProviderUtil#setEnabled(ViewBuilder, boolean)}.
*
* @param enabled whether the metric stream from the view is produced
*/
ViewBuilder setEnabled(boolean enabled) {
this.enabled = enabled;
return this;
}

/** Returns a {@link View} with the configuration of this builder. */
public View build() {
return View.create(name, description, aggregation, attributeFilter, cardinalityLimit);
return View.create(name, description, aggregation, attributeFilter, cardinalityLimit, enabled);
}
}
Loading
Loading