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

Create an activity even when SamplingDecision is Drop #1715

Closed
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
18 changes: 2 additions & 16 deletions src/OpenTelemetry/Trace/TracerProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ internal TracerProviderSdk(
else if (sampler is AlwaysOffSampler)
{
listener.Sample = (ref ActivityCreationOptions<ActivityContext> options) =>
!Sdk.SuppressInstrumentation ? PropagateOrIgnoreData(options.Parent.TraceId) : ActivitySamplingResult.None;
!Sdk.SuppressInstrumentation ? ActivitySamplingResult.PropagationData : ActivitySamplingResult.None;
}
else
{
Expand Down Expand Up @@ -287,23 +287,9 @@ private static ActivitySamplingResult ComputeActivitySamplingResult(
{
options.SamplingTags.Add(att.Key, att.Value);
}

return activitySamplingResult;
}

return PropagateOrIgnoreData(options.Parent.TraceId);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ActivitySamplingResult PropagateOrIgnoreData(ActivityTraceId traceId)
{
var isRootSpan = traceId == default;

// If it is the root span select PropagationData so the trace ID is preserved
// even if no activity of the trace is recorded (sampled per OpenTelemetry parlance).
return isRootSpan
? ActivitySamplingResult.PropagationData
: ActivitySamplingResult.None;
return activitySamplingResult;
}
}
}
12 changes: 6 additions & 6 deletions test/OpenTelemetry.Tests/Trace/TracerProviderSdkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ public void TracerSdkSetsActivitySamplingResultBasedOnSamplingDecision()

using (var activity = activitySource.StartActivity("root"))
{
// Even if sampling returns false, for root activities,
// activity is still created with PropagationOnly.
Assert.NotNull(activity);
Assert.True(activity.IsAllDataRequested);
Assert.False(activity.Recorded);
Expand All @@ -176,17 +174,19 @@ public void TracerSdkSetsActivitySamplingResultBasedOnSamplingDecision()

using (var activity = activitySource.StartActivity("root"))
{
// Even if sampling returns false, for root activities,
// Even if sampling returns false,
// activity is still created with PropagationOnly.
Assert.NotNull(activity);
Assert.False(activity.IsAllDataRequested);
Assert.False(activity.Recorded);

using (var innerActivity = activitySource.StartActivity("inner"))
{
// This is not a root activity.
// If sampling returns false, no activity is created at all.
Assert.Null(innerActivity);
// If sampling returns false, activity is created with PropagationOnly regardless
// of whether the activity is root or not.
Assert.NotNull(activity);
Assert.False(activity.IsAllDataRequested);
Assert.False(activity.Recorded);
}
}
}
Expand Down