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

Prototypes Context-scoped attributes (OTEP #4931, #8532), allowing Traces, Logs, and synchronous Metrics to automatically inherit attributes from the active Context without manually adding them at every call site.


Key Decisions (Spec Gaps Resolved)

  • Bound Instruments ignore Context Attributes:
    • Gap: OTEP Prepare release 1.20.0 #4931 omitted bound instruments because core specs removed bind(), but Java incubator retains it.
    • Resolution: Bound handles (BoundStorageHandle) record only to their pre-bound timeseries with zero runtime attribute merging. This guarantees zero allocations on hot paths and prevents cardinality explosion on shared infrastructure (e.g., connection pools). Unbound instruments continue to merge context attributes dynamically.
  • setNoParent() preserves Context Attributes:
    • Gap: setNoParent() historically reset context to Context.root(), unintentionally deleting context attributes and baggage.
    • Resolution: Replaced Context.root() with current.with(Span.getInvalid()). This detaches the parent span while keeping context attributes and baggage available for Sampler and SpanProcessor. Use setParent(Context.root()) for full isolation.

Signal Implementation Summary

  • API (api:incubator): ContextAttributes.add(Context, Attributes) (merges into context) and get(Context) (SDK reader).
  • Precedence: Attributes set directly on a span, log, or metric measurement always override context attributes on key collisions.
  • Trace (sdk:trace): Merges context attributes before Sampler.shouldSample(); unifies context across sampling, span creation, and SpanProcessor.onStart().
  • Logs (sdk:logs): Merges on emit() into an isolated record attributes map to prevent attribute leakage when reusing LogRecordBuilder.
  • Metrics (sdk:metrics): Merges context attributes on unbound instruments before view filtering; bypasses canUseRootContext optimization when active.
  • Config: Disabled by default. Enabled programmatically via Sdk*ProviderUtil or declaratively via context_scoped_attributes/development.

Verification

  • Trace: ContextScopedAttributesSpanTest (attributes, precedence, limits, and setNoParent() context preservation) & SdkSpanBuilderTest (legacy compatibility).
  • Metrics: ContextScopedAttributesMetricTest (dynamic unbound enrichment, bound handle isolation, same-context co-existence).
  • Logs: ContextScopedAttributesLogTest (merging, collision precedence, builder reuse).
  • Integration & Static Analysis: ContextScopedAttributesIntegrationTest (multi-signal E2E); clean runs across spotless, checkstyle, jApiCmp, and animalsniffer.

@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-24 16:31 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 91.02564% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.45%. Comparing base (877820b) to head (ef1468f).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ava/io/opentelemetry/sdk/trace/SdkSpanBuilder.java 76.47% 1 Missing and 3 partials ⚠️
...metry/sdk/logs/internal/SdkLoggerProviderUtil.java 75.00% 2 Missing ⚠️
...try/sdk/metrics/internal/SdkMeterProviderUtil.java 75.00% 2 Missing ⚠️
...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 ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #8947      +/-   ##
============================================
+ Coverage     90.41%   90.45%   +0.03%     
- Complexity    10981    11033      +52     
============================================
  Files          1019     1021       +2     
  Lines         30016    30160     +144     
  Branches       3917     3937      +20     
============================================
+ Hits          27140    27282     +142     
+ Misses         1923     1915       -8     
- Partials        953      963      +10     

☔ 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 3 times, most recently from 3b2ce44 to e7872d7 Compare October 14, 2026 21:45
@rooks-ns
rooks-ns marked this pull request as draft October 18, 2026 12:22
@rooks-ns
rooks-ns force-pushed the feat/context-scoped-attributes branch from e7872d7 to 657d862 Compare October 19, 2026 23:48
@rooks-ns
rooks-ns marked this pull request as ready for review October 20, 2026 14:20
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
@rooks-ns
rooks-ns force-pushed the feat/context-scoped-attributes branch from 657d862 to ef1468f Compare October 22, 2026 16:49
@rooks-ns

Copy link
Copy Markdown
Contributor Author

@jack-berg Please take a look . Thank you.

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