Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions test/OpenTelemetry.Sampler.AWS.Tests/MockServerRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,29 @@ public void SetResponse(string path, string responseBody)

public void Handle(HttpListenerContext context)
{
var path = context.Request.Url?.AbsolutePath;
if (path != null && this.responses.TryGetValue(path, out var responseBody))
try
{
context.Response.StatusCode = 200;
context.Response.ContentType = "application/json";
using var writer = new StreamWriter(context.Response.OutputStream);
writer.Write(responseBody);
var path = context.Request.Url?.AbsolutePath;
if (path != null && this.responses.TryGetValue(path, out var responseBody))
{
Comment thread
martincostello marked this conversation as resolved.
context.Response.StatusCode = 200;
context.Response.ContentType = "application/json";

#if NET
using var writer = new StreamWriter(context.Response.OutputStream, leaveOpen: true);
#else
using var writer = new StreamWriter(context.Response.OutputStream, new System.Text.UTF8Encoding(false), 4096, leaveOpen: true);
#endif
writer.Write(responseBody);
}
else
{
context.Response.StatusCode = 404;
}
}
else
finally
{
context.Response.StatusCode = 404;
context.Response.Close();
}

context.Response.Close();
}
}
35 changes: 23 additions & 12 deletions test/OpenTelemetry.Sampler.AWS.Tests/TestAWSXRayRemoteSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ public void TestSamplerWithDefaults()
Assert.NotNull(xraySampler?.Client);
}

#if NETFRAMEWORK
[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1219")]
#else
[Fact]
#endif
public void TestSamplerUpdateAndSample()
public async Task TestSamplerUpdateAndSample()
{
// setup mock server
var clock = new TestClock();
Expand All @@ -76,22 +72,37 @@ public void TestSamplerUpdateAndSample()
// GetSamplingRules mock response
requestHandler.SetResponse("/GetSamplingRules", File.ReadAllText("Data/GetSamplingRulesResponseOptionalFields.json"));

// rules will be polled in 10 milliseconds
Thread.Sleep(2000);
await Task.Delay(TimeSpan.FromSeconds(2));

// sampler will drop because rule has 0 reservoir and 0 fixed rate
Assert.Equal(SamplingDecision.Drop, this.DoSample(sampler, "cat-service"));

// GetSamplingTargets mock response
requestHandler.SetResponse("/SamplingTargets", File.ReadAllText("Data/GetSamplingTargetsResponseOptionalFields.json"));

// targets will be polled in 10 seconds
Thread.Sleep(13000);
var decision = SamplingDecision.Drop;
var expected = SamplingDecision.RecordAndSample;

// targets should be polled in 10 seconds
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20)))
{
while (!cts.IsCancellationRequested)
{
decision = this.DoSample(sampler, "cat-service");

if (decision == expected)
{
break;
}

await Task.Delay(TimeSpan.FromSeconds(1), cts.Token);
}
}

Comment thread
martincostello marked this conversation as resolved.
// sampler will always sampler since target has 100% fixed rate
Assert.Equal(SamplingDecision.RecordAndSample, this.DoSample(sampler, "cat-service"));
Assert.Equal(SamplingDecision.RecordAndSample, this.DoSample(sampler, "cat-service"));
Assert.Equal(SamplingDecision.RecordAndSample, this.DoSample(sampler, "cat-service"));
Assert.Equal(expected, this.DoSample(sampler, "cat-service"));
Assert.Equal(expected, this.DoSample(sampler, "cat-service"));
Assert.Equal(expected, this.DoSample(sampler, "cat-service"));
}

private SamplingDecision DoSample(Trace.Sampler sampler, string serviceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TestAWSXRaySamplerClient()
{
this.requestHandler = new MockServerRequestHandler();
this.mockServer = TestHttpServer.RunServer(
ctx => this.requestHandler.Handle(ctx),
this.requestHandler.Handle,
out var host,
out var port);
this.client = new AWSXRaySamplerClient($"http://{host}:{port}");
Expand Down
Loading