Repository navigation
[OpenTelemetry] Add AlwaysRecordSampler #7695
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
martincostello
merged 5 commits into
open-telemetry:main
from
martincostello:Add-AlwaysRecordSampler
Sep 9, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03841a7
[OpenTelemetry] Add AlwaysRecordSampler
martincostello 7eeec53
[OpenTelemetry] Update CHANGELOG
martincostello 87f2aae
[OpenTelemetry] Address feedback
martincostello 5a1789b
Merge branch 'main' into Add-AlwaysRecordSampler
martincostello 891e4e9
Merge branch 'main' into Add-AlwaysRecordSampler
Kielek 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| OpenTelemetry.Trace.AlwaysRecordSampler | ||
| OpenTelemetry.Trace.AlwaysRecordSampler.AlwaysRecordSampler(OpenTelemetry.Trace.Sampler! rootSampler) -> void | ||
| override OpenTelemetry.Trace.AlwaysRecordSampler.ShouldSample(in OpenTelemetry.Trace.SamplingParameters samplingParameters) -> OpenTelemetry.Trace.SamplingResult |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Includes work from: | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using OpenTelemetry.Internal; | ||
|
|
||
| namespace OpenTelemetry.Trace; | ||
|
|
||
| /// <summary> | ||
| /// Sampler implementation which wraps another <see cref="Sampler"/> and replaces any | ||
| /// <see cref="SamplingDecision.Drop"/> decision made by that sampler with | ||
| /// <see cref="SamplingDecision.RecordOnly"/>, leaving every other decision unchanged. | ||
| /// This ensures that all spans are recorded, without changing the sampling rate. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The intended use case of this sampler is to provide a means of sending all spans to a | ||
| /// processor without having an impact on the sampling rate. This may be desirable if a user wishes | ||
| /// to count or otherwise measure all spans produced in a service, without incurring the cost of 100% | ||
| /// sampling. | ||
| /// </remarks> | ||
| public sealed class AlwaysRecordSampler : Sampler | ||
| { | ||
| private readonly Sampler rootSampler; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AlwaysRecordSampler"/> class. | ||
| /// </summary> | ||
| /// <param name="rootSampler">The <see cref="Sampler"/> whose sampling decisions should always be recorded.</param> | ||
| public AlwaysRecordSampler(Sampler rootSampler) | ||
| { | ||
| Guard.ThrowIfNull(rootSampler); | ||
|
|
||
| this.rootSampler = rootSampler; | ||
| #pragma warning disable CA1062 // Validate arguments of public methods - needed for netstandard2.1 | ||
| this.Description = "AlwaysRecordSampler{" + rootSampler.Description + "}"; | ||
| #pragma warning restore CA1062 // Validate arguments of public methods - needed for netstandard2.1 | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override SamplingResult ShouldSample(in SamplingParameters samplingParameters) | ||
| { | ||
| var result = this.rootSampler.ShouldSample(samplingParameters); | ||
|
|
||
| return result.Decision == SamplingDecision.Drop | ||
|
martincostello marked this conversation as resolved.
Resolved
|
||
| ? new SamplingResult(SamplingDecision.RecordOnly, result.AttributesOrNull, result.TraceStateString) | ||
| : result; | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
test/OpenTelemetry.Tests/Trace/AlwaysRecordSamplerTests.cs
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,47 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Includes work from: | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| using OpenTelemetry.Tests; | ||
|
|
||
| namespace OpenTelemetry.Trace.Tests; | ||
|
|
||
| public class AlwaysRecordSamplerTests | ||
| { | ||
| [Fact] | ||
| public void ConstructorThrowsOnNullRootSampler() | ||
| => Assert.Throws<ArgumentNullException>(() => new AlwaysRecordSampler(null!)); | ||
|
|
||
| [Fact] | ||
| public void DescriptionIncludesRootSamplerDescription() | ||
| { | ||
| var sampler = new AlwaysRecordSampler(new TestSampler()); | ||
|
|
||
| Assert.Equal("AlwaysRecordSampler{TestSampler}", sampler.Description); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(SamplingDecision.Drop, SamplingDecision.RecordOnly)] | ||
| [InlineData(SamplingDecision.RecordAndSample, SamplingDecision.RecordAndSample)] | ||
| [InlineData(SamplingDecision.RecordOnly, SamplingDecision.RecordOnly)] | ||
| public void ShouldSampleReplacesDropWithRecordOnly(SamplingDecision rootDecision, SamplingDecision expectedDecision) | ||
| { | ||
| var attributes = new List<KeyValuePair<string, object>> { new("key", "value") }; | ||
| var rootResult = new SamplingResult(rootDecision, attributes, "traceState"); | ||
| var testSampler = new TestSampler { SamplingAction = _ => rootResult }; | ||
| var sampler = new AlwaysRecordSampler(testSampler); | ||
|
|
||
| var samplingParameters = new SamplingParameters( | ||
| default, default, "name", ActivityKind.Client, [], []); | ||
|
|
||
| var actualResult = sampler.ShouldSample(samplingParameters); | ||
|
|
||
| Assert.Equal(expectedDecision, actualResult.Decision); | ||
| Assert.Equal(rootResult.Attributes, actualResult.Attributes); | ||
| Assert.Equal(rootResult.TraceStateString, actualResult.TraceStateString); | ||
| } | ||
| } |
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.