Skip to content

Commit

Permalink
[Sampler.AWS] Tested and Updated X-Ray Sampler (#1887)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsakerMohd committed Jun 17, 2024
1 parent 7652931 commit 7e93019
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/OpenTelemetry.Sampler.AWS/RulesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ public void UpdateRules(List<SamplingRule> newRules)
List<SamplingRuleApplier> newRuleAppliers = new List<SamplingRuleApplier>();
foreach (var rule in newRules)
{
var currentStatistics = this.RuleAppliers
.FirstOrDefault(currentApplier => currentApplier.RuleName == rule.RuleName)
?.Statistics ?? new Statistics();
// If the ruleApplier already exists in the current list of appliers, then we reuse it.
var ruleApplier = this.RuleAppliers
.FirstOrDefault(currentApplier => currentApplier.RuleName == rule.RuleName) ??
new SamplingRuleApplier(this.ClientId, this.Clock, rule, new Statistics());

// update the rule in the applier in case rule attributes have changed
ruleApplier.Rule = rule;

var ruleApplier = new SamplingRuleApplier(this.ClientId, this.Clock, rule, currentStatistics);
newRuleAppliers.Add(ruleApplier);
}

Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry.Sampler.AWS/SamplingRuleApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public SamplingRuleApplier(string clientId, Clock clock, SamplingRule rule, Stat
this.FixedRateSampler = new ParentBasedSampler(new TraceIdRatioBasedSampler(rule.FixedRate));

// We either have no reservoir sampling or borrow until we get a quota so have no end time.
this.ReservoirEndTime = DateTime.MaxValue;
this.ReservoirEndTime = DateTimeOffset.MaxValue;

// We don't have a SamplingTarget so are ready to report a snapshot right away.
this.NextSnapshotTime = this.Clock.Now();
Expand Down Expand Up @@ -97,15 +97,15 @@ public bool Matches(SamplingParameters samplingParameters, Resource resource)
{
foreach (var tag in samplingParameters.Tags)
{
if (tag.Key.Equals(SemanticConventions.AttributeHttpTarget, StringComparison.Ordinal))
if (tag.Key.Equals(SemanticConventions.AttributeUrlPath, StringComparison.Ordinal))
{
httpTarget = (string?)tag.Value;
}
else if (tag.Key.Equals(SemanticConventions.AttributeHttpUrl, StringComparison.Ordinal))
else if (tag.Key.Equals(SemanticConventions.AttributeUrlFull, StringComparison.Ordinal))
{
httpUrl = (string?)tag.Value;
}
else if (tag.Key.Equals(SemanticConventions.AttributeHttpMethod, StringComparison.Ordinal))
else if (tag.Key.Equals(SemanticConventions.AttributeHttpRequestMethod, StringComparison.Ordinal))
{
httpMethod = (string?)tag.Value;
}
Expand Down
14 changes: 7 additions & 7 deletions test/OpenTelemetry.Sampler.AWS.Tests/TestSamplingRuleApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void TestRuleMatchesWithAllAttributes()
var activityTags = new Dictionary<string, string>
{
{ "http.host", "localhost" },
{ "http.method", "GET" },
{ "http.url", @"http://127.0.0.1:5000/helloworld" },
{ "http.request.method", "GET" },
{ "url.full", @"http://127.0.0.1:5000/helloworld" },
{ "faas.id", "arn:aws:lambda:us-west-2:123456789012:function:my-function" },
};

Expand Down Expand Up @@ -59,8 +59,8 @@ public void TestRuleMatchesWithWildcardAttributes()
var activityTags = new Dictionary<string, string>
{
{ "http.host", "localhost" },
{ "http.method", "GET" },
{ "http.url", @"http://127.0.0.1:5000/helloworld" },
{ "http.request.method", "GET" },
{ "url.full", @"http://127.0.0.1:5000/helloworld" },
};

var applier = new SamplingRuleApplier("clientId", new TestClock(), rule, new Statistics());
Expand Down Expand Up @@ -132,7 +132,7 @@ public void TestRuleMatchesWithHttpTarget()

var activityTags = new Dictionary<string, string>
{
{ "http.target", "/helloworld" },
{ "url.path", "/helloworld" },
};

var applier = new SamplingRuleApplier("clientId", new TestClock(), rule, new Statistics());
Expand Down Expand Up @@ -164,7 +164,7 @@ public void TestAttributeMatching()

var activityTags = new Dictionary<string, string>
{
{ "http.target", "/helloworld" },
{ "url.path", "/helloworld" },
{ "dog", "bark" },
{ "cat", "meow" },
};
Expand Down Expand Up @@ -198,7 +198,7 @@ public void TestAttributeMatchingWithLessActivityTags()

var activityTags = new Dictionary<string, string>
{
{ "http.target", "/helloworld" },
{ "url.path", "/helloworld" },
{ "dog", "bark" },
};

Expand Down

0 comments on commit 7e93019

Please sign in to comment.