Skip to content

Commit

Permalink
[repo] Fix tests reliant on bugged sampling behavior (#6097)
Browse files Browse the repository at this point in the history
Co-authored-by: Rajkumar Rangaraj <[email protected]>
  • Loading branch information
CodeBlanch and rajkumar-rangaraj authored Jan 27, 2025
1 parent cd8cb41 commit e5a7095
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,32 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

public class OtlpHttpTraceExportClientTests
public sealed class OtlpHttpTraceExportClientTests : IDisposable
{
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();

private readonly ActivityListener activityListener;

static OtlpHttpTraceExportClientTests()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
}

var listener = new ActivityListener
public OtlpHttpTraceExportClientTests()
{
this.activityListener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllDataAndRecorded,
};

ActivitySource.AddActivityListener(listener);
ActivitySource.AddActivityListener(this.activityListener);
}

public void Dispose()
{
this.activityListener.Dispose();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.Implementation.Serializer;

public class OtlpArrayTagWriterTests : IDisposable
public sealed class OtlpArrayTagWriterTests : IDisposable
{
private readonly ProtobufOtlpTagWriter.OtlpArrayTagWriter arrayTagWriter;
private readonly ActivityListener activityListener;

static OtlpArrayTagWriterTests()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;

var listener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
};

ActivitySource.AddActivityListener(listener);
}

public OtlpArrayTagWriterTests()
{
this.arrayTagWriter = new ProtobufOtlpTagWriter.OtlpArrayTagWriter();
this.activityListener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllDataAndRecorded,
};

ActivitySource.AddActivityListener(this.activityListener);
}

[Fact]
Expand Down Expand Up @@ -265,6 +265,7 @@ public void Dispose()
{
// Clean up the thread buffer after each test
ProtobufOtlpTagWriter.OtlpArrayTagWriter.ThreadBuffer = null;
this.activityListener.Dispose();
}

private static OtlpTrace.Span? ToOtlpSpan(SdkLimitOptions sdkOptions, Activity activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,35 @@
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;

[Collection("xUnitCollectionPreventingTestsThatDependOnSdkConfigurationFromRunningInParallel")]
public class OtlpTraceExporterTests
public sealed class OtlpTraceExporterTests : IDisposable
{
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();

private static readonly ExperimentalOptions DefaultExperimentalOptions = new();

private readonly ActivityListener activityListener;

static OtlpTraceExporterTests()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
}

var listener = new ActivityListener
public OtlpTraceExporterTests()
{
this.activityListener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => options.Parent.TraceFlags.HasFlag(ActivityTraceFlags.Recorded)
? ActivitySamplingResult.AllDataAndRecorded
: ActivitySamplingResult.AllData,
};

ActivitySource.AddActivityListener(listener);
ActivitySource.AddActivityListener(this.activityListener);
}

public void Dispose()
{
this.activityListener.Dispose();
}

[Fact]
Expand Down
23 changes: 13 additions & 10 deletions test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace OpenTelemetry.Exporter.Zipkin.Tests;

public class ZipkinExporterTests : IDisposable
public sealed class ZipkinExporterTests : IDisposable
{
private const string TraceId = "e8ea7e9ac72de94e91fabc613f9686b2";
private static readonly ConcurrentDictionary<Guid, string> Responses = new();
Expand All @@ -32,14 +32,6 @@ static ZipkinExporterTests()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;

var listener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
};

ActivitySource.AddActivityListener(listener);
}

public ZipkinExporterTests()
Expand Down Expand Up @@ -68,7 +60,6 @@ static void ProcessServerRequest(HttpListenerContext context)
public void Dispose()
{
this.testServer.Dispose();
GC.SuppressFinalize(this);
}

[Fact]
Expand Down Expand Up @@ -455,6 +446,16 @@ internal static Activity CreateTestActivity(
string? statusDescription = null,
DateTime? dateTime = null)
{
using var activityListener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => options.Parent.TraceFlags.HasFlag(ActivityTraceFlags.Recorded)
? ActivitySamplingResult.AllDataAndRecorded
: ActivitySamplingResult.AllData,
};

ActivitySource.AddActivityListener(activityListener);

var startTimestamp = DateTime.UtcNow;
var endTimestamp = startTimestamp.AddSeconds(60);
var eventTimestamp = DateTime.UtcNow;
Expand Down Expand Up @@ -532,6 +533,8 @@ internal static Activity CreateTestActivity(
links,
startTime: startTimestamp)!;

Assert.NotNull(activity);

if (addEvents)
{
foreach (var evnt in events)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Fixture()
this.listener = new ActivityListener
{
ShouldListenTo = _ => true,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData,
Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllDataAndRecorded,
};

ActivitySource.AddActivityListener(this.listener);
Expand Down

0 comments on commit e5a7095

Please sign in to comment.