Skip to content

Commit

Permalink
[Extensions.AzureMonitor.ApplicationInsightsSampler] fix unit test (#896
Browse files Browse the repository at this point in the history
)

* fix test

* update comment

* cleanup

* comments
  • Loading branch information
TimothyMothra authored Jan 13, 2023
1 parent 4b8e1d1 commit 358dda8
Showing 1 changed file with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,41 @@ public class ApplicationInsightsSamplerTests
[Fact]
public void VerifyHashAlgorithmCorrectness()
{
byte[] testBytes = new byte[]
byte[] testBytes1 = new byte[] // hex string: 8fffffffffffffff0000000000000000
{
0x8F, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
};
byte[] testBytes2 = new byte[]
byte[] testBytes2 = new byte[] // hex string: 0f1f2f3f4f5f6f7f8f9fafbfcfdfefff
{
0x0F, 0x1F, 0x2F, 0x3F,
0x4F, 0x5F, 0x6F, 0x7F,
0x8F, 0x9F, 0xAF, 0xBF,
0xCF, 0xDF, 0xEF, 0xFF,
};
ActivityTraceId testId = ActivityTraceId.CreateFromBytes(testBytes);
ActivityTraceId testId1 = ActivityTraceId.CreateFromBytes(testBytes1);
ActivityTraceId testId2 = ActivityTraceId.CreateFromBytes(testBytes2);

ActivityContext parentContext = default(ActivityContext);
SamplingParameters testParams = new SamplingParameters(parentContext, testId, "TestActivity", ActivityKind.Internal);
ActivityContext parentContext = default;
SamplingParameters testParams1 = new SamplingParameters(parentContext, testId1, "TestActivity", ActivityKind.Internal);
SamplingParameters testParams2 = new SamplingParameters(parentContext, testId2, "TestActivity", ActivityKind.Internal);

var zeroSampler = new ApplicationInsightsSampler(0);
ApplicationInsightsSampler oneSampler = new ApplicationInsightsSampler(1);

// 0.86 is below the sample score for testId1, but strict enough to drop testId2
ApplicationInsightsSampler ratioSampler = new ApplicationInsightsSampler(0.86f);

Assert.Equal(SamplingDecision.Drop, zeroSampler.ShouldSample(testParams).Decision);
// Verify sample ratio: 0
ApplicationInsightsSampler zeroSampler = new ApplicationInsightsSampler(samplingRatio: 0);
Assert.Equal(SamplingDecision.Drop, zeroSampler.ShouldSample(testParams1).Decision);
Assert.Equal(SamplingDecision.Drop, zeroSampler.ShouldSample(testParams2).Decision);

Assert.Equal(SamplingDecision.RecordAndSample, oneSampler.ShouldSample(testParams).Decision);
// Verify sample ratio: 1
ApplicationInsightsSampler oneSampler = new ApplicationInsightsSampler(samplingRatio: 1);
Assert.Equal(SamplingDecision.RecordAndSample, oneSampler.ShouldSample(testParams1).Decision);
Assert.Equal(SamplingDecision.RecordAndSample, oneSampler.ShouldSample(testParams2).Decision);

Assert.Equal(SamplingDecision.Drop, ratioSampler.ShouldSample(testParams).Decision);
// Verify sample ratio: 0.5.
// This is below the sample score for testId2, but strict enough to drop testId1
ApplicationInsightsSampler ratioSampler = new ApplicationInsightsSampler(samplingRatio: 0.5f);
Assert.Equal(SamplingDecision.Drop, ratioSampler.ShouldSample(testParams1).Decision);
Assert.Equal(SamplingDecision.RecordAndSample, ratioSampler.ShouldSample(testParams2).Decision);
}

Expand Down

0 comments on commit 358dda8

Please sign in to comment.