diff --git a/src/OpenTelemetry.Sampler.AWS/.publicApi/PublicAPI.Unshipped.txt b/src/OpenTelemetry.Sampler.AWS/.publicApi/PublicAPI.Unshipped.txt index 063bdbf96d..96770ae2fa 100644 --- a/src/OpenTelemetry.Sampler.AWS/.publicApi/PublicAPI.Unshipped.txt +++ b/src/OpenTelemetry.Sampler.AWS/.publicApi/PublicAPI.Unshipped.txt @@ -2,7 +2,7 @@ OpenTelemetry.Sampler.AWS.AWSXRayRemoteSampler OpenTelemetry.Sampler.AWS.AWSXRayRemoteSampler.Dispose() -> void OpenTelemetry.Sampler.AWS.AWSXRayRemoteSamplerBuilder -OpenTelemetry.Sampler.AWS.AWSXRayRemoteSamplerBuilder.Build() -> OpenTelemetry.Sampler.AWS.AWSXRayRemoteSampler! +OpenTelemetry.Sampler.AWS.AWSXRayRemoteSamplerBuilder.Build() -> OpenTelemetry.Trace.Sampler! OpenTelemetry.Sampler.AWS.AWSXRayRemoteSamplerBuilder.SetEndpoint(string! endpoint) -> OpenTelemetry.Sampler.AWS.AWSXRayRemoteSamplerBuilder! OpenTelemetry.Sampler.AWS.AWSXRayRemoteSamplerBuilder.SetPollingInterval(System.TimeSpan pollingInterval) -> OpenTelemetry.Sampler.AWS.AWSXRayRemoteSamplerBuilder! override OpenTelemetry.Sampler.AWS.AWSXRayRemoteSampler.ShouldSample(in OpenTelemetry.Trace.SamplingParameters samplingParameters) -> OpenTelemetry.Trace.SamplingResult diff --git a/src/OpenTelemetry.Sampler.AWS/AWSXRayRemoteSamplerBuilder.cs b/src/OpenTelemetry.Sampler.AWS/AWSXRayRemoteSamplerBuilder.cs index 7d64254b7b..7f2ad35bee 100644 --- a/src/OpenTelemetry.Sampler.AWS/AWSXRayRemoteSamplerBuilder.cs +++ b/src/OpenTelemetry.Sampler.AWS/AWSXRayRemoteSamplerBuilder.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 using OpenTelemetry.Resources; +using OpenTelemetry.Trace; namespace OpenTelemetry.Sampler.AWS; @@ -66,10 +67,11 @@ public AWSXRayRemoteSamplerBuilder SetEndpoint(string endpoint) /// /// Returns a with configuration of this builder. /// - /// an instance of . - public AWSXRayRemoteSampler Build() + /// an instance of . + public Trace.Sampler Build() { - return new AWSXRayRemoteSampler(this.resource, this.pollingInterval, this.endpoint, this.clock); + var rootSampler = new AWSXRayRemoteSampler(this.resource, this.pollingInterval, this.endpoint, this.clock); + return new ParentBasedSampler(rootSampler); } // This is intended for testing with a mock clock. diff --git a/src/OpenTelemetry.Sampler.AWS/CHANGELOG.md b/src/OpenTelemetry.Sampler.AWS/CHANGELOG.md index 6c3514b50c..6a78f967c5 100644 --- a/src/OpenTelemetry.Sampler.AWS/CHANGELOG.md +++ b/src/OpenTelemetry.Sampler.AWS/CHANGELOG.md @@ -17,6 +17,10 @@ * Updated OpenTelemetry core component version(s) to `1.10.0`. ([#2317](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2317)) +* `AWSXRayRemoteSamplerBuilder.Build()` now returns `ParentBasedSampler` which + which is of type `Sampler` instead of `AWSXRayRemoteSampler`. + ([#2188](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2188)) + ## 0.1.0-alpha.2 Released 2024-Sep-09 diff --git a/src/OpenTelemetry.Sampler.AWS/FallbackSampler.cs b/src/OpenTelemetry.Sampler.AWS/FallbackSampler.cs index 356ecf4570..08d271cc49 100644 --- a/src/OpenTelemetry.Sampler.AWS/FallbackSampler.cs +++ b/src/OpenTelemetry.Sampler.AWS/FallbackSampler.cs @@ -12,8 +12,8 @@ internal class FallbackSampler : Trace.Sampler public FallbackSampler(Clock clock) { - this.reservoirSampler = new ParentBasedSampler(new RateLimitingSampler(1, clock)); - this.fixedRateSampler = new ParentBasedSampler(new TraceIdRatioBasedSampler(0.05)); + this.reservoirSampler = new RateLimitingSampler(1, clock); + this.fixedRateSampler = new TraceIdRatioBasedSampler(0.05); } public override SamplingResult ShouldSample(in SamplingParameters samplingParameters) diff --git a/src/OpenTelemetry.Sampler.AWS/SamplingRuleApplier.cs b/src/OpenTelemetry.Sampler.AWS/SamplingRuleApplier.cs index 73a59c17bf..2deb456fdd 100644 --- a/src/OpenTelemetry.Sampler.AWS/SamplingRuleApplier.cs +++ b/src/OpenTelemetry.Sampler.AWS/SamplingRuleApplier.cs @@ -20,7 +20,7 @@ public SamplingRuleApplier(string clientId, Clock clock, SamplingRule rule, Stat { // Until calling GetSamplingTargets, the default is to borrow 1/s if reservoir size is // positive. - this.ReservoirSampler = new ParentBasedSampler(new RateLimitingSampler(1, this.Clock)); + this.ReservoirSampler = new RateLimitingSampler(1, this.Clock); this.Borrowing = true; } else @@ -30,7 +30,7 @@ public SamplingRuleApplier(string clientId, Clock clock, SamplingRule rule, Stat this.Borrowing = false; } - this.FixedRateSampler = new ParentBasedSampler(new TraceIdRatioBasedSampler(rule.FixedRate)); + this.FixedRateSampler = new TraceIdRatioBasedSampler(rule.FixedRate); // We either have no reservoir sampling or borrow until we get a quota so have no end time. this.ReservoirEndTime = DateTimeOffset.MaxValue; @@ -191,7 +191,7 @@ public SamplingStatisticsDocument Snapshot(DateTimeOffset now) public SamplingRuleApplier WithTarget(SamplingTargetDocument target, DateTimeOffset now) { var newFixedRateSampler = target.FixedRate != null - ? new ParentBasedSampler(new TraceIdRatioBasedSampler(target.FixedRate.Value)) + ? new TraceIdRatioBasedSampler(target.FixedRate.Value) : this.FixedRateSampler; Trace.Sampler newReservoirSampler = new AlwaysOffSampler(); @@ -199,7 +199,7 @@ public SamplingRuleApplier WithTarget(SamplingTargetDocument target, DateTimeOff if (target.ReservoirQuota != null && target.ReservoirQuotaTTL != null) { newReservoirSampler = target.ReservoirQuota > 0 - ? new ParentBasedSampler(new RateLimitingSampler(target.ReservoirQuota.Value, this.Clock)) + ? new RateLimitingSampler(target.ReservoirQuota.Value, this.Clock) : new AlwaysOffSampler(); newReservoirEndTime = this.Clock.ToDateTime(target.ReservoirQuotaTTL.Value); diff --git a/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs b/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs index e61cb76d31..5d0c0bbc04 100644 --- a/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs +++ b/test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 using System.Diagnostics; +using System.Reflection; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using WireMock.RequestBuilders; @@ -18,27 +19,34 @@ public void TestSamplerWithConfiguration() { var pollingInterval = TimeSpan.FromSeconds(5); var endpoint = "http://localhost:3000"; - - var sampler = AWSXRayRemoteSampler.Builder(ResourceBuilder.CreateEmpty().Build()) + var parentBasedSampler = AWSXRayRemoteSampler.Builder(ResourceBuilder.CreateEmpty().Build()) .SetPollingInterval(pollingInterval) .SetEndpoint(endpoint) .Build(); - Assert.Equal(pollingInterval, sampler.PollingInterval); - Assert.Equal(endpoint, sampler.Endpoint); - Assert.NotNull(sampler.RulePollerTimer); - Assert.NotNull(sampler.Client); + FieldInfo? rootSamplerFieldInfo = typeof(ParentBasedSampler).GetField("rootSampler", BindingFlags.NonPublic | BindingFlags.Instance); + + var xraySampler = (AWSXRayRemoteSampler?)rootSamplerFieldInfo?.GetValue(parentBasedSampler); + + Assert.Equal(pollingInterval, xraySampler?.PollingInterval); + Assert.Equal(endpoint, xraySampler?.Endpoint); + Assert.NotNull(xraySampler?.RulePollerTimer); + Assert.NotNull(xraySampler?.Client); } [Fact] public void TestSamplerWithDefaults() { - var sampler = AWSXRayRemoteSampler.Builder(ResourceBuilder.CreateEmpty().Build()).Build(); + var parentBasedSampler = AWSXRayRemoteSampler.Builder(ResourceBuilder.CreateEmpty().Build()).Build(); + + FieldInfo? rootSamplerFieldInfo = typeof(ParentBasedSampler).GetField("rootSampler", BindingFlags.NonPublic | BindingFlags.Instance); + + var xraySampler = (AWSXRayRemoteSampler?)rootSamplerFieldInfo?.GetValue(parentBasedSampler); - Assert.Equal(TimeSpan.FromMinutes(5), sampler.PollingInterval); - Assert.Equal("http://localhost:2000", sampler.Endpoint); - Assert.NotNull(sampler.RulePollerTimer); - Assert.NotNull(sampler.Client); + Assert.Equal(TimeSpan.FromMinutes(5), xraySampler?.PollingInterval); + Assert.Equal("http://localhost:2000", xraySampler?.Endpoint); + Assert.NotNull(xraySampler?.RulePollerTimer); + Assert.NotNull(xraySampler?.Client); } [Fact(Skip = "Flaky test. Related issue: https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1219")]