-
Notifications
You must be signed in to change notification settings - Fork 937
Emit warning when TraceIdRatioBasedSampler is used as child sampler #7937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jkwatson
merged 7 commits into
open-telemetry:main
from
Gosling-dude:warn-child-traceidratio
Dec 27, 2025
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49677d9
Emit warning when TraceIdRatioBasedSampler is used as child sampler
Gosling-dude 4af9078
Format ParentBasedSamplerBuilder with Spotless
Gosling-dude ef83131
Remove child-sampler warnings from ParentBasedSampler
Gosling-dude db30494
Add test for child TraceIdRatioBasedSampler warning
Gosling-dude 64e09bc
Increase test coverage for ParentBasedSamplerBuilder warnings
Gosling-dude e944463
test: cover all ParentBasedSamplerBuilder warning branches
Gosling-dude efdcb79
Simplify child sampler warning logic
Gosling-dude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...race/src/test/java/io/opentelemetry/sdk/trace/samplers/ParentBasedSamplerBuilderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.sdk.trace.samplers; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.logging.Handler; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.LogRecord; | ||
| import java.util.logging.Logger; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ParentBasedSamplerBuilderTest { | ||
|
|
||
| @Test | ||
| void emitsWarningWhenTraceIdRatioBasedUsedAsChildSampler() { | ||
| Logger logger = Logger.getLogger(ParentBasedSamplerBuilder.class.getName()); | ||
| TestLogHandler handler = new TestLogHandler(); | ||
| logger.addHandler(handler); | ||
|
|
||
| try { | ||
| Sampler ratioSampler = Sampler.traceIdRatioBased(0.5); | ||
| ParentBasedSamplerBuilder builder = Sampler.parentBasedBuilder(Sampler.alwaysOn()); | ||
|
|
||
| builder.setRemoteParentSampled(ratioSampler); | ||
| builder.setRemoteParentSampled(ratioSampler); | ||
| builder.build(); | ||
|
|
||
| assertTrue(handler.warnings.stream().anyMatch(msg -> msg.contains("remoteParentSampled"))); | ||
| } finally { | ||
| logger.removeHandler(handler); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void emitsWarningForAllChildSamplerSetters() { | ||
| Logger logger = Logger.getLogger(ParentBasedSamplerBuilder.class.getName()); | ||
| TestLogHandler handler = new TestLogHandler(); | ||
| logger.addHandler(handler); | ||
|
|
||
| try { | ||
| Sampler ratioSampler = Sampler.traceIdRatioBased(0.5); | ||
| ParentBasedSamplerBuilder builder = Sampler.parentBasedBuilder(Sampler.alwaysOn()); | ||
|
|
||
| builder.setRemoteParentNotSampled(ratioSampler); | ||
| builder.setRemoteParentNotSampled(ratioSampler); | ||
|
|
||
| builder.setLocalParentSampled(ratioSampler); | ||
| builder.setLocalParentSampled(ratioSampler); | ||
|
|
||
| builder.setLocalParentNotSampled(ratioSampler); | ||
| builder.setLocalParentNotSampled(ratioSampler); | ||
|
|
||
| builder.build(); | ||
|
|
||
| assertTrue(handler.warnings.stream().anyMatch(msg -> msg.contains("remoteParentNotSampled"))); | ||
| assertTrue(handler.warnings.stream().anyMatch(msg -> msg.contains("localParentSampled"))); | ||
| assertTrue(handler.warnings.stream().anyMatch(msg -> msg.contains("localParentNotSampled"))); | ||
| } finally { | ||
| logger.removeHandler(handler); | ||
| } | ||
| } | ||
|
|
||
| static final class TestLogHandler extends Handler { | ||
|
|
||
| final List<String> warnings = new ArrayList<>(); | ||
|
|
||
| @Override | ||
| public void publish(LogRecord record) { | ||
| if (Level.WARNING.equals(record.getLevel())) { | ||
| warnings.add(record.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() {} | ||
|
|
||
| @Override | ||
| public void close() {} | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is generally only going to be done once for a given builder instance. I don't think we need the extra checks to make sure it only gets logged once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense - agreed.
I’ll remove the
warned*guards and emit the warning unconditionally when aTraceIdRatioBasedSampleris set as a child sampler. That keeps the builder simpler and aligns with typical usage.I’ll push a small follow-up update shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like one Windows CI job (windows-latest, 8) failed while the rest passed. This may be transient. would you mind re-running that job when convenient?