|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Includes work from: |
| 5 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 |
| 7 | + |
| 8 | +using System.Diagnostics; |
| 9 | +using OpenTelemetry.Tests; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace OpenTelemetry.Trace.Tests; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// AlwaysRecordSamplerTest test class. |
| 16 | +/// </summary> |
| 17 | +public class AlwaysRecordSamplerTests |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Tests Description is set properly with AlwaysRecordSampler keyword. |
| 21 | + /// </summary> |
| 22 | + [Fact] |
| 23 | + public void TestGetDescription() |
| 24 | + { |
| 25 | + var testSampler = new TestSampler(); |
| 26 | + var sampler = AlwaysRecordSampler.Create(testSampler); |
| 27 | + Assert.Equal("AlwaysRecordSampler{TestSampler}", sampler.Description); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Test RECORD_AND_SAMPLE sampling decision. |
| 32 | + /// </summary> |
| 33 | + [Fact] |
| 34 | + public void TestRecordAndSampleSamplingDecision() |
| 35 | + { |
| 36 | + ValidateShouldSample(SamplingDecision.RecordAndSample, SamplingDecision.RecordAndSample); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Test RECORD_ONLY sampling decision. |
| 41 | + /// </summary> |
| 42 | + [Fact] |
| 43 | + public void TestRecordOnlySamplingDecision() |
| 44 | + { |
| 45 | + ValidateShouldSample(SamplingDecision.RecordOnly, SamplingDecision.RecordOnly); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Test DROP sampling decision. |
| 50 | + /// </summary> |
| 51 | + [Fact] |
| 52 | + public void TestDropSamplingDecision() |
| 53 | + { |
| 54 | + ValidateShouldSample(SamplingDecision.Drop, SamplingDecision.RecordOnly); |
| 55 | + } |
| 56 | + |
| 57 | + private static SamplingResult BuildRootSamplingResult(SamplingDecision samplingDecision) |
| 58 | + { |
| 59 | + ActivityTagsCollection? attributes = new ActivityTagsCollection |
| 60 | + { |
| 61 | + { "key", samplingDecision.GetType().Name }, |
| 62 | + }; |
| 63 | + string traceState = samplingDecision.GetType().Name; |
| 64 | +#pragma warning disable CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 65 | + return new SamplingResult(samplingDecision, attributes, traceState); |
| 66 | +#pragma warning restore CS8620 // Argument cannot be used for parameter due to differences in the nullability of reference types. |
| 67 | + } |
| 68 | + |
| 69 | + private static void ValidateShouldSample( |
| 70 | + SamplingDecision rootDecision, SamplingDecision expectedDecision) |
| 71 | + { |
| 72 | + SamplingResult rootResult = BuildRootSamplingResult(rootDecision); |
| 73 | + var testSampler = new TestSampler { SamplingAction = _ => rootResult }; |
| 74 | + var sampler = AlwaysRecordSampler.Create(testSampler); |
| 75 | + |
| 76 | + SamplingParameters samplingParameters = new SamplingParameters( |
| 77 | + default, default, "name", ActivityKind.Client, new ActivityTagsCollection(), new List<ActivityLink>()); |
| 78 | + |
| 79 | + SamplingResult actualResult = sampler.ShouldSample(samplingParameters); |
| 80 | + |
| 81 | + if (rootDecision.Equals(expectedDecision)) |
| 82 | + { |
| 83 | + Assert.True(actualResult.Equals(rootResult)); |
| 84 | + Assert.True(actualResult.Decision.Equals(rootDecision)); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + Assert.False(actualResult.Equals(rootResult)); |
| 89 | + Assert.True(actualResult.Decision.Equals(expectedDecision)); |
| 90 | + } |
| 91 | + |
| 92 | + Assert.Equal(rootResult.Attributes, actualResult.Attributes); |
| 93 | + Assert.Equal(rootDecision.GetType().Name, actualResult.TraceStateString); |
| 94 | + } |
| 95 | +} |
0 commit comments