-
Notifications
You must be signed in to change notification settings - Fork 208
Better sampling override behavior #4014
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8ce7b0b
Improve sampling override behavior
trask 11b4832
up
trask bc7348b
up
trask 70579e2
up
trask 0ec9de1
up
trask 208cabd
up
trask 4488b2d
Merge remote-tracking branch 'origin/main' into sampling-override-beh…
trask a839f97
extract method
trask ba26600
hide static inner class
trask 3c50114
comment explaining test assertion
trask f1d672e
Merge remote-tracking branch 'origin/main' into sampling-override-beh…
trask 32b41c8
hasSamplingOverride
trask 4c98fff
Add test
trask 51db4b1
some renaming
trask 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
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
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
105 changes: 105 additions & 0 deletions
105
...a/com/microsoft/applicationinsights/agent/internal/sampling/AiFixedPercentageSampler.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,105 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.microsoft.applicationinsights.agent.internal.sampling; | ||
|
|
||
| import com.azure.monitor.opentelemetry.autoconfigure.implementation.AiSemanticAttributes; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.api.trace.SpanKind; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.sdk.trace.ReadableSpan; | ||
| import io.opentelemetry.sdk.trace.data.LinkData; | ||
| import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
| import io.opentelemetry.sdk.trace.samplers.SamplingResult; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| public class AiFixedPercentageSampler implements Sampler { | ||
|
|
||
| private final double percentage; | ||
|
|
||
| public static AiFixedPercentageSampler create(double percentage) { | ||
| return new AiFixedPercentageSampler(percentage); | ||
| } | ||
|
|
||
| private AiFixedPercentageSampler(double percentage) { | ||
| this.percentage = percentage; | ||
| } | ||
|
|
||
| @Override | ||
| public SamplingResult shouldSample( | ||
| Context parentContext, | ||
| String traceId, | ||
| String name, | ||
| SpanKind spanKind, | ||
| Attributes attributes, | ||
| List<LinkData> parentLinks) { | ||
|
|
||
| Span parentSpan = Span.fromContext(parentContext); | ||
| SpanContext parentSpanContext = parentSpan.getSpanContext(); | ||
| Double parentSpanSampleRate = null; | ||
| if (parentSpan instanceof ReadableSpan) { | ||
| parentSpanSampleRate = | ||
| ((ReadableSpan) parentSpan).getAttribute(AiSemanticAttributes.SAMPLE_RATE); | ||
| } | ||
|
|
||
| return internalShouldSample(parentSpanContext, parentSpanSampleRate, traceId); | ||
| } | ||
|
|
||
| public SamplingResult shouldSampleLog(SpanContext spanContext, @Nullable Double spanSampleRate) { | ||
| return internalShouldSample(spanContext, spanSampleRate, spanContext.getTraceId()); | ||
| } | ||
|
|
||
| private SamplingResult internalShouldSample( | ||
| SpanContext parentSpanContext, @Nullable Double parentSpanSampleRate, String traceId) { | ||
|
|
||
| SamplingResult samplingResult = | ||
| useLocalParentDecisionIfPossible(parentSpanContext, parentSpanSampleRate); | ||
| if (samplingResult != null) { | ||
| return samplingResult; | ||
| } | ||
|
|
||
| return SamplerUtil.shouldSample(traceId, percentage); | ||
| } | ||
|
|
||
| @Nullable | ||
| private SamplingResult useLocalParentDecisionIfPossible( | ||
| SpanContext parentSpanContext, @Nullable Double parentSpanSampleRate) { | ||
|
|
||
| // remote parent-based sampling messes up item counts since item count is not propagated in | ||
| // tracestate (yet), but local parent-based sampling doesn't have this issue since we are | ||
| // propagating item count locally | ||
|
|
||
| if (!parentSpanContext.isValid() || parentSpanContext.isRemote()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!parentSpanContext.isSampled()) { | ||
| if (percentage < 100) { | ||
| // only 100% sampling override will override an unsampled parent!! | ||
| return SamplingResult.drop(); | ||
| } else { | ||
| // falls back in this case to percentage | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| if (parentSpanSampleRate == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (percentage < parentSpanSampleRate || percentage == 100) { | ||
| // falls back in this case to percentage | ||
| return null; | ||
| } | ||
| // don't sample more dependencies than parent in this case | ||
| return SamplerUtil.createSamplingResultWithSampleRateAndItemCount(parentSpanSampleRate); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "FixedPercentageSampler"; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Because the scenarios are not super simple, the code comments could maybe try to refer to tests checking this scenario
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.
I totally agree that the scenarios are complex, this particular comment though I think it pretty clear (relatively, at least, compared to some of the other code in this PR)? maybe you had in mind other places?
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.
My suggestion was to refer to tests in the comments, in addition to the current comment explanations.
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.
I'd prefer to just improve the comments if they are unclear. Or if you make specific suggestions linking these to tests I'm happy to hit accept.