Skip to content

Commit

Permalink
Sample.AWS.Tests - nullable (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Sep 5, 2023
1 parent 75d7622 commit ae05c91
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 84 deletions.
9 changes: 2 additions & 7 deletions src/OpenTelemetry.Sampler.AWS/AWSXRayRemoteSamplerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public AWSXRayRemoteSamplerBuilder SetPollingInterval(TimeSpan pollingInterval)

/// <summary>
/// Sets the endpoint for the TCP proxy to connect to. This is the address to the port on the
/// OpenTelemetry Collector configured for proxying X-Ray sampling requests.If unset, defaults to
/// OpenTelemetry Collector configured for proxying X-Ray sampling requests. If unset, defaults to
/// <see cref="DefaultEndpoint"/>.
/// </summary>
/// <param name="endpoint">the endpoint for the TCP proxy.</param>
Expand Down Expand Up @@ -90,12 +90,7 @@ public AWSXRayRemoteSampler Build()
// Should not be exposed to public.
internal AWSXRayRemoteSamplerBuilder SetClock(Clock clock)
{
if (clock == null)
{
throw new ArgumentNullException(nameof(clock));
}

this.clock = clock;
this.clock = clock ?? throw new ArgumentNullException(nameof(clock));

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Sampler.AWS/SamplingRuleApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace OpenTelemetry.Sampler.AWS;

internal class SamplingRuleApplier
{
public SamplingRuleApplier(string clientId, Clock clock, SamplingRule rule, Statistics statistics)
public SamplingRuleApplier(string clientId, Clock clock, SamplingRule rule, Statistics? statistics)
{
this.ClientId = clientId;
this.Clock = clock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);$(NetFrameworkMinimumSupportedVersion)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void TestSamplerUpdateAndSample()
// create sampler
AWSXRayRemoteSampler sampler = AWSXRayRemoteSampler.Builder(ResourceBuilder.CreateEmpty().Build())
.SetPollingInterval(TimeSpan.FromMilliseconds(10))
.SetEndpoint(mockServer.Url)
.SetEndpoint(mockServer.Url!)
.SetClock(clock)
.Build();

Expand Down Expand Up @@ -117,9 +117,9 @@ private SamplingDecision DoSample(Trace.Sampler sampler, string serviceName)
ActivityTraceId.CreateRandom(),
"myActivityName",
ActivityKind.Server,
new List<KeyValuePair<string, object>>()
new List<KeyValuePair<string, object?>>
{
new KeyValuePair<string, object>("test", serviceName),
new("test", serviceName),
},
null);

Expand Down
22 changes: 11 additions & 11 deletions test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRaySamplerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ namespace OpenTelemetry.Sampler.AWS.Tests;

public class TestAWSXRaySamplerClient : IDisposable
{
private WireMockServer mockServer;
private readonly WireMockServer mockServer;

private AWSXRaySamplerClient client;
private readonly AWSXRaySamplerClient client;

public TestAWSXRaySamplerClient()
{
this.mockServer = WireMockServer.Start();
this.client = new AWSXRaySamplerClient(this.mockServer.Url);
this.client = new AWSXRaySamplerClient(this.mockServer.Url!);
}

public void Dispose()
Expand Down Expand Up @@ -116,23 +116,23 @@ public void TestGetSamplingTargets()

this.CreateResponse("/SamplingTargets", "Data/GetSamplingTargetsResponse.json");

var request = new GetSamplingTargetsRequest(new List<SamplingStatisticsDocument>()
var request = new GetSamplingTargetsRequest(new List<SamplingStatisticsDocument>
{
new SamplingStatisticsDocument(
new(
"clientId",
"rule1",
100,
50,
10,
clock.ToDouble(clock.Now())),
new SamplingStatisticsDocument(
new(
"clientId",
"rule2",
200,
100,
20,
clock.ToDouble(clock.Now())),
new SamplingStatisticsDocument(
new(
"clientId",
"rule3",
20,
Expand All @@ -144,7 +144,7 @@ public void TestGetSamplingTargets()
var responseTask = this.client.GetSamplingTargets(request);
responseTask.Wait();

GetSamplingTargetsResponse targetsResponse = responseTask.Result;
GetSamplingTargetsResponse targetsResponse = responseTask.Result!;

Assert.Equal(2, targetsResponse.SamplingTargetDocuments.Count);
Assert.Single(targetsResponse.UnprocessedStatistics);
Expand Down Expand Up @@ -175,9 +175,9 @@ public void TestGetSamplingTargetsWithMalformed()
.RespondWith(
Response.Create().WithStatusCode(200).WithHeader("Content-Type", "application/json").WithBody("notJson"));

var request = new GetSamplingTargetsRequest(new List<SamplingStatisticsDocument>()
var request = new GetSamplingTargetsRequest(new List<SamplingStatisticsDocument>
{
new SamplingStatisticsDocument(
new(
"clientId",
"rule1",
100,
Expand All @@ -189,7 +189,7 @@ public void TestGetSamplingTargetsWithMalformed()
var responseTask = this.client.GetSamplingTargets(request);
responseTask.Wait();

GetSamplingTargetsResponse targetsResponse = responseTask.Result;
GetSamplingTargetsResponse? targetsResponse = responseTask.Result;

Assert.Null(targetsResponse);
}
Expand Down
4 changes: 2 additions & 2 deletions test/OpenTelemetry.Sampler.AWS.Tests/TestClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace OpenTelemetry.Sampler.AWS.Tests;

internal class TestClock : Clock
{
private static readonly DateTime EpochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly DateTime EpochStart = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private DateTime nowTime;

public TestClock()
Expand Down Expand Up @@ -55,7 +55,7 @@ public override double ToDouble(DateTime dateTime)
return timestamp;
}

// Advnaces the clock by a given time span.
// Advances the clock by a given time span.
public void Advance(TimeSpan time)
{
this.nowTime = this.nowTime.Add(time);
Expand Down
22 changes: 11 additions & 11 deletions test/OpenTelemetry.Sampler.AWS.Tests/TestMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public void TestWildcardDoesNotMatch(string input, string pattern)
[Fact]
public void TestAttributeMatching()
{
var tags = new List<KeyValuePair<string, object>>()
var tags = new List<KeyValuePair<string, object?>>
{
new KeyValuePair<string, object>("dog", "bark"),
new KeyValuePair<string, object>("cat", "meow"),
new KeyValuePair<string, object>("cow", "mooo"),
new("dog", "bark"),
new("cat", "meow"),
new("cow", "mooo"),
};

var ruleAttributes = new Dictionary<string, string>()
var ruleAttributes = new Dictionary<string, string>
{
{ "dog", "bar?" },
{ "cow", "mooo" },
Expand All @@ -67,11 +67,11 @@ public void TestAttributeMatching()
[Fact]
public void TestAttributeMatchingWithoutRuleAttributes()
{
var tags = new List<KeyValuePair<string, object>>()
var tags = new List<KeyValuePair<string, object?>>
{
new KeyValuePair<string, object>("dog", "bark"),
new KeyValuePair<string, object>("cat", "meow"),
new KeyValuePair<string, object>("cow", "mooo"),
new("dog", "bark"),
new("cat", "meow"),
new("cow", "mooo"),
};

var ruleAttributes = new Dictionary<string, string>();
Expand All @@ -82,13 +82,13 @@ public void TestAttributeMatchingWithoutRuleAttributes()
[Fact]
public void TestAttributeMatchingWithoutSpanTags()
{
var ruleAttributes = new Dictionary<string, string>()
var ruleAttributes = new Dictionary<string, string>
{
{ "dog", "bar?" },
{ "cow", "mooo" },
};

Assert.False(Matcher.AttributeMatch(new List<KeyValuePair<string, object>>(), ruleAttributes));
Assert.False(Matcher.AttributeMatch(new List<KeyValuePair<string, object?>>(), ruleAttributes));
Assert.False(Matcher.AttributeMatch(null, ruleAttributes));
}
}
2 changes: 1 addition & 1 deletion test/OpenTelemetry.Sampler.AWS.Tests/TestRateLimiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public async Task TestRateLimiterConcurrencyAsync()
TestClock clock = new TestClock();
RateLimiter limiter = new RateLimiter(1, numWorkers * creditsPerWorker, clock);
int count = 0;
List<Task> tasks = new List<Task>(numWorkers);
List<Task> tasks = new(numWorkers);

for (int w = 0; w < numWorkers; ++w)
{
Expand Down
42 changes: 21 additions & 21 deletions test/OpenTelemetry.Sampler.AWS.Tests/TestRulesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void TestUpdateRules()

// set up rules cache with a default rule
var defaultRule = this.CreateDefaultRule(1, 0.05);
var stats = new Statistics()
var stats = new Statistics
{
RequestCount = 10,
SampleCount = 5,
Expand All @@ -51,9 +51,9 @@ public void TestUpdateRules()

var cache = new RulesCache(clock, "test", ResourceBuilder.CreateEmpty().Build(), new AlwaysOffSampler())
{
RuleAppliers = new List<SamplingRuleApplier>()
RuleAppliers = new List<SamplingRuleApplier>
{
{ new SamplingRuleApplier("testId", clock, defaultRule, stats) },
{ new("testId", clock, defaultRule, stats) },
},
};

Expand All @@ -80,10 +80,10 @@ public void TestUpdateRulesRemovesOlderRule()
// set up rule cache with 2 rules
var rulesCache = new RulesCache(clock, "test", ResourceBuilder.CreateEmpty().Build(), new AlwaysOffSampler())
{
RuleAppliers = new List<SamplingRuleApplier>()
RuleAppliers = new List<SamplingRuleApplier>
{
{ new SamplingRuleApplier("testId", clock, this.CreateDefaultRule(1, 0.05), null) },
{ new SamplingRuleApplier("testId", clock, this.CreateRule("Rule1", 5, 0.20, 1), null) },
{ new("testId", clock, this.CreateDefaultRule(1, 0.05), null) },
{ new("testId", clock, this.CreateRule("Rule1", 5, 0.20, 1), null) },
},
};

Expand All @@ -103,10 +103,10 @@ public void TestShouldSampleMatchesExactRule()
var clock = new TestClock();
var rulesCache = new RulesCache(clock, "clientId", ResourceBuilder.CreateEmpty().Build(), new AlwaysOffSampler())
{
RuleAppliers = new List<SamplingRuleApplier>()
RuleAppliers = new List<SamplingRuleApplier>
{
{ new SamplingRuleApplier("clientId", clock, this.CreateRule("ruleWillMatch", 1, 0.0, 1), new Statistics()) }, // higher priority rule will sample
{ new SamplingRuleApplier("clientId", clock, this.CreateRule("ruleWillNotMatch", 0, 0.0, 2), new Statistics()) }, // this rule will not sample
{ new("clientId", clock, this.CreateRule("ruleWillMatch", 1, 0.0, 1), new Statistics()) }, // higher priority rule will sample
{ new("clientId", clock, this.CreateRule("ruleWillNotMatch", 0, 0.0, 2), new Statistics()) }, // this rule will not sample
},
};

Expand Down Expand Up @@ -144,26 +144,26 @@ public void TestUpdateTargets()
var clock = new TestClock();
var rulesCache = new RulesCache(clock, "clientId", ResourceBuilder.CreateEmpty().Build(), new AlwaysOffSampler())
{
RuleAppliers = new List<SamplingRuleApplier>()
RuleAppliers = new List<SamplingRuleApplier>
{
{ new SamplingRuleApplier("clientId", clock, this.CreateRule("rule1", 1, 0.0, 1), new Statistics()) }, // this rule will sample 1 req/sec
{ new SamplingRuleApplier("clientId", clock, this.CreateRule("rule2", 0, 0.0, 2), new Statistics()) },
{ new("clientId", clock, this.CreateRule("rule1", 1, 0.0, 1), new Statistics()) }, // this rule will sample 1 req/sec
{ new("clientId", clock, this.CreateRule("rule2", 0, 0.0, 2), new Statistics()) },
},
};

Assert.Equal(SamplingDecision.RecordAndSample, rulesCache.ShouldSample(default).Decision);
Assert.Equal(SamplingDecision.Drop, rulesCache.ShouldSample(default).Decision);

// update targets
var targetForRule1 = new SamplingTargetDocument()
var targetForRule1 = new SamplingTargetDocument
{
FixedRate = 0.0,
Interval = 0,
ReservoirQuota = 2,
ReservoirQuotaTTL = clock.ToDouble(clock.Now().AddMinutes(5)),
RuleName = "rule1",
};
var targetForRule2 = new SamplingTargetDocument()
var targetForRule2 = new SamplingTargetDocument
{
FixedRate = 0.0,
Interval = 0,
Expand All @@ -172,7 +172,7 @@ public void TestUpdateTargets()
RuleName = "rule2",
};

var targets = new Dictionary<string, SamplingTargetDocument>()
var targets = new Dictionary<string, SamplingTargetDocument>
{
{ "rule1", targetForRule1 },
{ "rule2", targetForRule2 },
Expand All @@ -192,23 +192,23 @@ public void TestNextTargetFetchTime()
var clock = new TestClock();
var rulesCache = new RulesCache(clock, "clientId", ResourceBuilder.CreateEmpty().Build(), new AlwaysOffSampler())
{
RuleAppliers = new List<SamplingRuleApplier>()
RuleAppliers = new List<SamplingRuleApplier>
{
{ new SamplingRuleApplier("clientId", clock, this.CreateRule("rule1", 1, 0.0, 1), new Statistics()) },
{ new SamplingRuleApplier("clientId", clock, this.CreateRule("rule2", 0, 0.0, 2), new Statistics()) },
{ new("clientId", clock, this.CreateRule("rule1", 1, 0.0, 1), new Statistics()) },
{ new("clientId", clock, this.CreateRule("rule2", 0, 0.0, 2), new Statistics()) },
},
};

// update targets
var targetForRule1 = new SamplingTargetDocument()
var targetForRule1 = new SamplingTargetDocument
{
FixedRate = 0.0,
Interval = 10, // next target poll after 10s
ReservoirQuota = 2,
ReservoirQuotaTTL = clock.ToDouble(clock.Now().Add(TimeSpan.FromMinutes(5))),
RuleName = "rule1",
};
var targetForRule2 = new SamplingTargetDocument()
var targetForRule2 = new SamplingTargetDocument
{
FixedRate = 0.0,
Interval = 5, // next target poll after 5s
Expand All @@ -217,7 +217,7 @@ public void TestNextTargetFetchTime()
RuleName = "rule2",
};

var targets = new Dictionary<string, SamplingTargetDocument>()
var targets = new Dictionary<string, SamplingTargetDocument>
{
{ "rule1", targetForRule1 },
{ "rule2", targetForRule2 },
Expand Down
Loading

0 comments on commit ae05c91

Please sign in to comment.