Skip to content

Add experimental OptIn instrument advice and View enabled configuration - #8979

Draft
dashpole wants to merge 1 commit into
open-telemetry:mainfrom
dashpole:feat/metrics-opt-in-and-view-enabled
Draft

dashpole wants to merge 1 commit into
open-telemetry:mainfrom
dashpole:feat/metrics-opt-in-and-view-enabled

Conversation

@dashpole

Copy link
Copy Markdown
Contributor

Summary

Implements the experimental OptIn instrument advisory parameter and View enabled stream configuration defined in open-telemetry/opentelemetry-specification#4809:

  • Incubating Metrics API (opentelemetry-api-incubator):
    • Adds setOptInAdvice(boolean) (default no-op) to all 8 Extended*Builder interfaces (ExtendedDoubleCounterBuilder, ExtendedDoubleGaugeBuilder, ExtendedDoubleHistogramBuilder, ExtendedDoubleUpDownCounterBuilder, ExtendedLongCounterBuilder, ExtendedLongGaugeBuilder, ExtendedLongHistogramBuilder, ExtendedLongUpDownCounterBuilder).
  • Metrics SDK (opentelemetry-sdk-metrics):
    • Adds isOptIn() / setOptIn(boolean) to internal Advice / AdviceBuilder and wires setOptInAdvice(boolean) across all ExtendedSdk*Builder implementations (preserving optIn across .ofDoubles() / .ofLongs() builder swaps).
    • Adds package-private ViewBuilder#setEnabled(boolean) and View#getEnabled(), exposed experimentally via SdkMeterProviderUtil#setEnabled(ViewBuilder, boolean).
    • Updates ViewRegistry#findViews so that:
      • Instruments with OptIn=true use Aggregation.drop() by default when no view matches or when a matching view does not set enabled=true.
      • Matching views with enabled=false use Aggregation.drop() regardless of the view's configured aggregation.
      • Matching views with enabled=true enable OptIn=true instruments while still respecting other instrument advisory parameters (Attributes and ExplicitBucketBoundaries) unless overridden by the view.

@dashpole
dashpole requested a balanced review from Copilot October 27, 2026 15:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-10-27 15:42 UTC

Move out of draft to request review.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Oct 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.94118% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.35%. Comparing base (d97e4af) to head (7bbc2e1).

Files with missing lines Patch % Lines
...emetry/sdk/metrics/internal/view/ViewRegistry.java 90.24% 2 Missing and 2 partials ⚠️
...try/sdk/metrics/internal/SdkMeterProviderUtil.java 71.42% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8979      +/-   ##
============================================
- Coverage     90.35%   90.35%   -0.01%     
- Complexity    10984    11006      +22     
============================================
  Files          1019     1019              
  Lines         30072    30148      +76     
  Branches       3927     3937      +10     
============================================
+ Hits          27173    27241      +68     
- Misses         1939     1943       +4     
- Partials        960      964       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

Static review found no blocking issues in the localized API additions, SDK enablement logic, or associated regression coverage.

No open issues 🎉

Review effort: Balanced

@psx95 psx95 left a comment

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.

When registering the associated storage for metrics in SdkMeter, if a view has drop aggregation, then the storage for that is skipped. This could probably cause an issue in the intended functionality because opentelemetry-java evaluates each matching RegisteredView independently in SdkMeter and simply skips (continue) any view whose aggregation is Aggregation.drop(), registering both:

  1. InstrumentSelector("*") → enabled = true
  2. InstrumentSelector("my.unwanted.instrument") → enabled = false and this will not disable "my.unwanted.instrument" since the second view turns into Aggregation.drop() and is skipped by SdkMeter, while the first view (*) still registers a live MetricStorage for "my.unwanted.instrument".

IIUC, this is a key use-case of the spec, right?

UnaryOperator<Attributes> attributesFilter,
int cardinalityLimit,
SourceInfo viewSourceInfo,
@Nullable Boolean enabled) {

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.

Why is this Nullable? We already have a constructor which can be used when the user does not wish to explicitly provide enabled.

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.


private static RegisteredView applyAdviceToDefaultView(
RegisteredView instrumentDefaultView, Advice advice) {
RegisteredView registeredView, Advice advice) {

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.

Just curious: Why the name change?

if (matchesSelector(entry.getInstrumentSelector(), descriptor, meterScope)) {
if (Boolean.FALSE.equals(entry.getEnabled())) {
result.add(applyDropAggregation(entry));
continue;

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.

This would force the next iteration without checking isCompatibleWithInstrument verification.

Comment on lines +121 to +128
if (descriptor.getAdvice().isOptIn() && !Boolean.TRUE.equals(entry.getEnabled())) {
result.add(applyDropAggregation(entry));
} else if (Boolean.TRUE.equals(entry.getEnabled())
&& descriptor.getAdvice().hasAttributes()
&& entry.getView().getAttributeFilter().equals(StringPredicates.ALL)) {
result.add(applyAdviceToDefaultView(entry, descriptor.getAdvice()));
} else {
result.add(entry);

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.

Can we add minor comments on these if-else conditions to clarify the intent here?

Currently, it seems like on a standard instrument (OptIn = false) with AttributesAdvice would be treated differently depending on enabled being unset or true.

Setting enabled = true on a standard instrument would cause the advice to be applied, but leaving enabled unset would not. But it looks like this is a side effect of the current implementation rather than intention.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants