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

Add Activity.HasRemoteParent #66489

Merged
merged 2 commits into from
Mar 11, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public string? Id
get { throw null; }
}

public bool HasRemoteParent { get { throw null; } }
public bool IsAllDataRequested { get { throw null; } set { throw null; } }
public bool IsStopped { get { throw null; } }
public System.Diagnostics.ActivityIdFormat IdFormat { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public partial class Activity : IDisposable
/// </summary>
public string? StatusDescription => _statusDescription;

/// <summary>
/// Gets whether the parent context was created from remote propagation.
/// </summary>
public bool HasRemoteParent { get; private set; }

/// <summary>
/// Sets the status code and description on the current activity object.
/// </summary>
Expand Down Expand Up @@ -1075,6 +1080,7 @@ internal static Activity Create(ActivitySource source, string name, ActivityKind

activity.ActivityTraceFlags = parentContext.TraceFlags;
activity._parentTraceFlags = (byte) parentContext.TraceFlags;
activity.HasRemoteParent = parentContext.IsRemote;
}

activity.IsAllDataRequested = request == ActivitySamplingResult.AllData || request == ActivitySamplingResult.AllDataAndRecorded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,57 @@ public void TestSamplerTagsWithMixedListenerModels()
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void TestHasRemoteParent()
{
RemoteExecutor.Invoke(() => {
using ActivitySource aSource = new ActivitySource("HasRemoteParent");
using ActivityListener listener1 = new ActivityListener();
listener1.ShouldListenTo = (activitySource) => activitySource == aSource;

listener1.SampleUsingParentId = (ref ActivityCreationOptions<string> activityOptions) => ActivitySamplingResult.AllData;
listener1.Sample = (ref ActivityCreationOptions<ActivityContext> activityOptions) => ActivitySamplingResult.AllData;

ActivitySource.AddActivityListener(listener1);

using (Activity activity = aSource.StartActivity("a1", ActivityKind.Client))
{
Assert.False(activity.HasRemoteParent);
}

using (Activity activity = aSource.StartActivity("a2", ActivityKind.Client, new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), 0, null, false)))
{
Assert.False(activity.HasRemoteParent);
}

using (Activity activity = aSource.StartActivity("a3", ActivityKind.Client, new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), 0, null, true)))
{
Assert.True(activity.HasRemoteParent);
}

using (Activity activity = aSource.CreateActivity("a2", ActivityKind.Client, new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), 0, null, false)))
{
Assert.False(activity.HasRemoteParent);
activity.Start();
Assert.False(activity.HasRemoteParent);
}

using (Activity activity = aSource.CreateActivity("a3", ActivityKind.Client, new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), 0, null, true)))
{
Assert.True(activity.HasRemoteParent);
activity.Start();
Assert.True(activity.HasRemoteParent);
}

using (Activity activity = new Activity("a4"))
{
Assert.False(activity.HasRemoteParent);
activity.Start();
Assert.False(activity.HasRemoteParent);
}
}).Dispose();
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void TestAddSamplerAndActivityCreationTags()
{
Expand Down