Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow suppressing nested activities without dependency on OTel #4642

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 15 additions & 2 deletions src/OpenTelemetry/SuppressInstrumentationScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#nullable enable

using System.Diagnostics;
using System.Runtime.CompilerServices;
using OpenTelemetry.Context;

Expand All @@ -29,17 +30,18 @@ public sealed class SuppressInstrumentationScope : IDisposable
// * Depth = [1, int.MaxValue]: instrumentation is suppressed in a reference-counting mode
private static readonly RuntimeContextSlot<SuppressInstrumentationScope?> Slot = RuntimeContext.RegisterSlot<SuppressInstrumentationScope?>("otel.suppress_instrumentation");

private static readonly string SuppressActivityProperty = "otel.suppress_instrumentation";
private readonly SuppressInstrumentationScope? previousScope;
private bool disposed;

internal SuppressInstrumentationScope(bool value = true)
{
this.previousScope = Slot.Get();
this.Depth = value ? -1 : 0;
this.Depth = (value || (this.previousScope == null && IsSuppressedWithActivityProperty())) ? -1 : 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need perf numbers like #1049

Slot.Set(this);
}

internal static bool IsSuppressed => (Slot.Get()?.Depth ?? 0) != 0;
internal static bool IsSuppressed => (Slot.Get()?.Depth ?? 0) != 0 || IsSuppressedWithActivityProperty();

internal int Depth { get; private set; }

Expand Down Expand Up @@ -159,5 +161,16 @@ internal static int DecrementIfTriggered()

return currentDepth;
}

private static bool IsSuppressedWithActivityProperty()
{
if (Activity.Current?.GetCustomProperty(SuppressActivityProperty) is bool suppressed && suppressed)
{
Enter();
return true;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,61 @@ public async Task RespectsSuppress()
}
}

[Fact]
public async Task RespectsSuppressWithActivityProperty()
{
try
{
var propagator = new Mock<TextMapPropagator>();
propagator.Setup(m => m.Inject(It.IsAny<PropagationContext>(), It.IsAny<HttpRequestMessage>(), It.IsAny<Action<HttpRequestMessage, string, string>>()))
.Callback<PropagationContext, HttpRequestMessage, Action<HttpRequestMessage, string, string>>((context, message, action) =>
{
action(message, "custom_traceparent", $"00/{context.ActivityContext.TraceId}/{context.ActivityContext.SpanId}/01");
action(message, "custom_tracestate", Activity.Current.TraceStateString);
});

var processor = new Mock<BaseProcessor<Activity>>();

using var request = new HttpRequestMessage
{
RequestUri = new Uri(this.url),
Method = new HttpMethod("GET"),
};

using var parent = new Activity("parent")
.SetIdFormat(ActivityIdFormat.W3C)
.Start();
parent.TraceStateString = "k1=v1,k2=v2";
parent.ActivityTraceFlags = ActivityTraceFlags.Recorded;
parent.SetCustomProperty("otel.suppress_instrumentation", true);

Sdk.SetDefaultTextMapPropagator(propagator.Object);

using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddProcessor(processor.Object)
.Build())
{
using var c = new HttpClient();
await c.SendAsync(request).ConfigureAwait(false);
}

// If suppressed, activity is not emitted and
// propagation is also not performed.
Assert.Equal(3, processor.Invocations.Count); // SetParentProvider/OnShutdown/Dispose called.
Assert.False(request.Headers.Contains("custom_traceparent"));
Assert.False(request.Headers.Contains("custom_tracestate"));
}
finally
{
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new BaggagePropagator(),
}));
}
}

[Fact]
public async Task ExportsSpansCreatedForRetries()
{
Expand Down