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

ActivityContext Changes #37185

Merged
merged 2 commits into from
Jun 1, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@
<data name="SetParentIdOnActivityWithParent" xml:space="preserve">
<value>"Can not set ParentId on activity which has parent"</value>
</data>
<data name="SpanIdOrTraceIdInvalid" xml:space="preserve">
<value>"Invalid SpanId or TraceId"</value>
</data>
<data name="StartTimeNotUtc" xml:space="preserve">
<value>"StartTime is not UTC"</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ namespace System.Diagnostics
/// <param name="traceState"> Carries system-specific configuration data.</param>
public ActivityContext(ActivityTraceId traceId, ActivitySpanId spanId, ActivityTraceFlags traceFlags, string? traceState = null)
{
// We don't allow creating context with invalid span or trace Ids.
if (traceId == default || spanId == default)
{
throw new ArgumentException(SR.SpanIdOrTraceIdInvalid, traceId == default ? nameof(traceId) : nameof(spanId));
}

TraceId = traceId;
SpanId = spanId;
TraceFlags = traceFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ public bool HasListeners()
}
else
{
ActivityContext initializedContext = context == default && Activity.Current != null ? Activity.Current.Context : context;
listeners.EnumWithFunc(listener => {
var getRequestedDataUsingContext = listener.GetRequestedDataUsingContext;
if (getRequestedDataUsingContext != null)
{
ActivityCreationOptions<ActivityContext> aco = new ActivityCreationOptions<ActivityContext>(this, name, context, kind, tags, links);
ActivityCreationOptions<ActivityContext> aco = new ActivityCreationOptions<ActivityContext>(this, name, initializedContext, kind, tags, links);
ActivityDataRequest dr = getRequestedDataUsingContext(ref aco);
if (dr > dateRequest)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,40 @@ public void TestActivityCreationProperties()
}
}).Dispose();
}

[Fact]
public void TestDefaultParentContext()
{
RemoteExecutor.Invoke(() => {
using (ActivitySource aSource = new ActivitySource("ParentContext"))
{
using (ActivityListener listener = new ActivityListener
{
ShouldListenTo = (activitySource) => activitySource.Name == "ParentContext",
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> activityOptions) =>
{
Activity c = Activity.Current;
if (c != null)
{
Assert.Equal(c.Context, activityOptions.Parent);
}

return ActivityDataRequest.AllData;
}
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
})
{
ActivitySource.AddActivityListener(listener);

using (Activity a = aSource.StartActivity("a", ActivityKind.Server, new ActivityContext(ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom(), 0)))
using (Activity b = aSource.StartActivity("b"))
{
Assert.Equal(a.Context, b.Parent.Context);
}
}
}
}).Dispose();
}

public void Dispose() => Activity.Current = null;
}
}