Skip to content

feat(trace): support runtime sampler updates - #8716

Open
abhiramaab wants to merge 3 commits into
open-telemetry:mainfrom
abhiramaab:feat/runtime-sampler-config
Open

feat(trace): support runtime sampler updates#8716
abhiramaab wants to merge 3 commits into
open-telemetry:mainfrom
abhiramaab:feat/runtime-sampler-config

Conversation

@abhiramaab

Copy link
Copy Markdown

Summary

Add runtime sampler updates to SdkTracerProvider so a sampler can be changed after the provider is built.

Changes

  • Add SdkTracerProvider.setSampler(Sampler)
  • Make the shared sampler reference mutable so new spans observe updates
  • Add tests for:
    • updating the sampler at runtime
    • null rejection
    • an existing tracer observing the new sampler for new spans

Verification

  • ./gradlew :sdk:trace:test --tests io.opentelemetry.sdk.trace.SdkTracerProviderTest
  • ./gradlew :sdk:trace:test
  • ./gradlew spotlessApply
  • git diff --check

Copilot AI lite review requested due to automatic review settings August 10, 2026 11:09
@abhiramaab
abhiramaab requested a review from a team as a code owner August 10, 2026 11:09
@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 10, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: abhiramaab / name: abhiramaab (213ffb5)

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Aug 10, 2026

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-08-10 12:23 UTC

Investigate required status check failures.

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.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

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.

Pull request overview

This PR adds support for updating the SdkTracerProvider sampler at runtime so that new spans (including those created from previously obtained Tracer instances) observe the updated sampler.

Changes:

  • Introduces SdkTracerProvider.setSampler(Sampler) and corresponding test coverage for runtime updates and null rejection.
  • Makes the shared sampler reference mutable (volatile) so new span creation uses the latest sampler.
  • Adds a test verifying sampler updates affect spans started from an existing Tracer.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java Adds public runtime sampler setter and keeps sampler accessible via getSampler().
sdk/trace/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java Makes sampler mutable to allow runtime updates for span creation.
sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java Adds tests for sampler updates, null rejection, and existing tracer behavior.
Suppressed comments (1)

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java:125

  • getSampler() now returns the current sampler (it can change at runtime), but its Javadoc still says "configured" which reads like an immutable build-time value.
  /** Returns the configured {@link Sampler}. */
  public Sampler getSampler() {
    return sharedState.getSampler();
  }

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +82 to +84
void setSampler(Sampler sampler) {
this.sampler = sampler;
}
Comment on lines +113 to +117
/**
* Updates the sampler used for new spans.
*
* @param sampler the sampler to use for sampling new spans.
*/

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Follow-up: added sampler lifecycle handling so replaced samplers are shut down, and samplers set after provider shutdown are immediately shut down. Added tests covering both cases. :sdk:trace:test passes.

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.

2 participants