Skip to content

Commit 336a89d

Browse files
authored
[Sampler.Aws] perf improvements for AWS Xray sampler (open-telemetry#2046)
1 parent a21a307 commit 336a89d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/OpenTelemetry.Sampler.AWS/AWSXRayRemoteSampler.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public sealed class AWSXRayRemoteSampler : Trace.Sampler, IDisposable
1515
internal static readonly TimeSpan DefaultTargetInterval = TimeSpan.FromSeconds(10);
1616

1717
private static readonly Random Random = new Random();
18+
private bool isFallBackEventToWriteSwitch = true;
1819

1920
[SuppressMessage("Performance", "CA5394: Do not use insecure randomness", Justification = "Secure random is not required for jitters.")]
2021
internal AWSXRayRemoteSampler(Resource resource, TimeSpan pollingInterval, string endpoint, Clock clock)
@@ -82,10 +83,18 @@ public override SamplingResult ShouldSample(in SamplingParameters samplingParame
8283
{
8384
if (this.RulesCache.Expired())
8485
{
85-
AWSSamplerEventSource.Log.InfoUsingFallbackSampler();
86+
if (this.isFallBackEventToWriteSwitch)
87+
{
88+
this.isFallBackEventToWriteSwitch = false;
89+
90+
// could be expensive operation, conditionally call once
91+
AWSSamplerEventSource.Log.InfoUsingFallbackSampler();
92+
}
93+
8694
return this.FallbackSampler.ShouldSample(in samplingParameters);
8795
}
8896

97+
this.isFallBackEventToWriteSwitch = true;
8998
return this.RulesCache.ShouldSample(in samplingParameters);
9099
}
91100

src/OpenTelemetry.Sampler.AWS/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
## 0.1.0-alpha.2
6+
7+
* Performance problem fix for calling event source when required.
8+
([#2046](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/2046))
9+
510
## 0.1.0-alpha.1
611

712
Released 2024-Jun-20

src/OpenTelemetry.Sampler.AWS/RulesCache.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class RulesCache : IDisposable
1111
private const int CacheTTL = 60 * 60; // cache expires 1 hour after the refresh (in sec)
1212

1313
private readonly ReaderWriterLockSlim rwLock;
14+
private bool isFallBackEventToWriteSwitch = true;
1415

1516
public RulesCache(Clock clock, string clientId, Resource resource, Trace.Sampler fallbackSampler)
1617
{
@@ -85,13 +86,19 @@ public SamplingResult ShouldSample(in SamplingParameters samplingParameters)
8586
{
8687
if (ruleApplier.Matches(samplingParameters, this.Resource))
8788
{
89+
this.isFallBackEventToWriteSwitch = true;
8890
return ruleApplier.ShouldSample(in samplingParameters);
8991
}
9092
}
9193

9294
// ideally the default rule should have matched.
9395
// if we are here then likely due to a bug.
94-
AWSSamplerEventSource.Log.InfoUsingFallbackSampler();
96+
if (this.isFallBackEventToWriteSwitch)
97+
{
98+
this.isFallBackEventToWriteSwitch = false;
99+
AWSSamplerEventSource.Log.InfoUsingFallbackSampler();
100+
}
101+
95102
return this.FallbackSampler.ShouldSample(in samplingParameters);
96103
}
97104

0 commit comments

Comments
 (0)