Skip to content

feat(context): prototype context-scoped attributes (#8532) - #8947

Open
rooks-ns wants to merge 2 commits into
open-telemetry:mainfrom
rooks-ns:feat/context-scoped-attributes
Open

rooks-ns wants to merge 2 commits into
open-telemetry:mainfrom
rooks-ns:feat/context-scoped-attributes

Conversation

@rooks-ns

@rooks-ns rooks-ns commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Implements a prototype of Context-scoped attributes as defined in OTEP #4931 and Issue #8532.
Context-scoped attributes attach OpenTelemetry Attributes directly to in-process Context (via ThreadLocal), allowing Traces, Logs, and synchronous Metrics to automatically inherit them .


Changes

  • API (api:incubator):
    • ContextAttributes.add(Context, Attributes): Stores non-propagating attributes in Context (null/empty safe).
    • ContextAttributes.get(Context): Pipeline/SDK reader; applications treat context attributes as write-only.
  • Attribute Precedence: Attributes passed directly to a span, log, or measurement call always override conflicting keys from the context.
  • Trace (sdk:trace):
    • Merges before Sampler.shouldSample, enabling attribute-based sampling decisions.
    • setNoParent() preserves ambient context attributes while decoupling the parent span (parentContext = Context.root()).
  • Logs (sdk:logs):
    • Merges on emit() into a separate record-level map to prevent attribute leakage across reused LogRecordBuilder calls.
  • Metrics (sdk:metrics):
    • Merges synchronously per measurement before view filtering and cardinality limits.
    • Supports bound and unbound instruments across both Cumulative and Delta temporality (BoundLongCounter, BoundDoubleCounter).
    • Disables the canUseRootContext bypass when active to ensure thread-local context lookup.
  • Configuration:
    • Disabled by default across all signals.
    • Configured programmatically via Sdk*ProviderUtil.setContextScopedAttributesEnabled and declaratively via the experimental context_scoped_attributes/development flag.

Verification

  • Unit test suites (ContextScopedAttributesSpanTest, ContextScopedAttributesLogTest, ContextScopedAttributesMetricTest) verifying key collision precedence, limits, builder reuse isolation, bound instruments, and setNoParent().
  • End-to-end integration test (ContextScopedAttributesIntegrationTest).

Implements prototype for Context-scoped attributes per OTEP open-telemetry#4931 and Issue open-telemetry#8532:
- Add ContextAttributes API in :api:incubator for setting/getting/extracting attributes on Context
- Support opt-in context-scoped attributes configuration across SdkTracerProvider, SdkLoggerProvider, and SdkMeterProvider
- Inherit context attributes in SdkSpanBuilder, SdkLogRecordBuilder, and synchronous metric storage (Delta & Cumulative)
- Expose configuration via declarative-config (tracer_provider, logger_provider, meter_provider)
- Add comprehensive unit, incubating, and multi-signal integration tests
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-10-14 22:22 UTC

Review the latest changes.

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 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.32584% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.67%. Comparing base (387f5c5) to head (e7872d7).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
.../internal/state/DeltaSynchronousMetricStorage.java 62.50% 3 Missing and 3 partials ⚠️
...metry/sdk/logs/internal/SdkLoggerProviderUtil.java 75.00% 2 Missing ⚠️
...try/sdk/metrics/internal/SdkMeterProviderUtil.java 75.00% 2 Missing ⚠️
...rnal/state/CumulativeSynchronousMetricStorage.java 84.61% 1 Missing and 1 partial ⚠️
...etry/sdk/trace/internal/SdkTracerProviderUtil.java 75.00% 2 Missing ⚠️
...io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java 92.30% 0 Missing and 1 partial ⚠️
.../opentelemetry/sdk/metrics/AbstractInstrument.java 50.00% 0 Missing and 1 partial ⚠️
...in/java/io/opentelemetry/sdk/metrics/SdkMeter.java 50.00% 0 Missing and 1 partial ⚠️
...trics/internal/state/SynchronousMetricStorage.java 0.00% 1 Missing ⚠️
...ava/io/opentelemetry/sdk/trace/SdkSpanBuilder.java 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8947      +/-   ##
============================================
+ Coverage     90.65%   90.67%   +0.01%     
- Complexity    10887    10939      +52     
============================================
  Files          1015     1017       +2     
  Lines         29731    29894     +163     
  Branches       3876     3904      +28     
============================================
+ Hits          26953    27106     +153     
+ Misses         1860     1859       -1     
- Partials        918      929      +11     

☔ 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.

@rooks-ns
rooks-ns force-pushed the feat/context-scoped-attributes branch from 403a57c to 7d8f465 Compare October 13, 2026 22:24
@rooks-ns
rooks-ns marked this pull request as ready for review October 14, 2026 18:25
@rooks-ns
rooks-ns requested a review from a team as a code owner October 14, 2026 18:25
@rooks-ns
rooks-ns force-pushed the feat/context-scoped-attributes branch 2 times, most recently from 810c7b0 to 3b2ce44 Compare October 14, 2026 20:03
@rooks-ns
rooks-ns force-pushed the feat/context-scoped-attributes branch from 3b2ce44 to e7872d7 Compare October 14, 2026 21:45

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.

1 participant