Skip to content

Draft ActivitySource Child Activity PropagationData solution #1

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -25,13 +25,12 @@
<Compile Include="System\Diagnostics\DiagnosticSource.cs" />
<Compile Include="System\Diagnostics\DiagnosticListener.cs" />
<Compile Include="System\Diagnostics\DiagnosticSourceEventSource.cs" />
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\PreserveDependencyAttribute.cs"
Link="Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs" />
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\PreserveDependencyAttribute.cs" Link="Common\System\Runtime\CompilerServices\PreserveDependencyAttribute.cs" />
<Compile Include="System\Diagnostics\ParentActivityState.cs" />
<None Include="DiagnosticSourceUsersGuide.md" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.1'">
<Compile Include="$(CommonPath)System\HexConverter.cs"
Link="Common\System\HexConverter.cs" />
<Compile Include="$(CommonPath)System\HexConverter.cs" Link="Common\System\HexConverter.cs" />
<Compile Include="System\Diagnostics\Activity.cs" />
<Compile Include="System\Diagnostics\ActivityContext.cs" />
<Compile Include="System\Diagnostics\ActivityCreationOptions.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ActivityListener()
/// <summary>
/// Set or get the callback used to decide allowing creating <see cref="Activity"/> objects with specific data state.
/// </summary>
public GetRequestedData<ActivityContext>? GetRequestedDataUsingContext { get; set; }
public GetRequestedData<ParentActivityState>? GetRequestedDataUsingContext { get; set; }

/// <summary>
/// Dispose will unregister this <see cref="ActivityListener"/> object from listeneing to <see cref="Activity"/> events.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,19 @@ public bool HasListeners()
}
else
{
ActivityContext initializedContext = context == default && Activity.Current != null ? Activity.Current.Context : context;
listeners.EnumWithFunc(listener => {
ParentActivityState parentActivityState = BuildParentActivityState(context);
listeners.EnumWithFunc(listener =>
{
var getRequestedDataUsingContext = listener.GetRequestedDataUsingContext;
if (getRequestedDataUsingContext != null)
{
ActivityCreationOptions<ActivityContext> aco = new ActivityCreationOptions<ActivityContext>(this, name, initializedContext, kind, tags, links);
ActivityCreationOptions<ParentActivityState> aco = new ActivityCreationOptions<ParentActivityState>(
this,
name,
parentActivityState,
kind,
tags,
links);
ActivityDataRequest dr = getRequestedDataUsingContext(ref aco);
if (dr > dataRequest)
{
Expand Down Expand Up @@ -242,6 +249,26 @@ internal void NotifyActivityStop(Activity activity)
listeners.EnumWithAction(listener => listener.ActivityStopped?.Invoke(activity));
}
}

private static ParentActivityState BuildParentActivityState(ActivityContext context)
{
bool isDefaultContext = context == default;

Activity? currentActivity = Activity.Current;
ActivityContext initializedContext = !isDefaultContext
? context
: currentActivity?.Context ?? context;

ActivityDataRequest parentDataRequested = isDefaultContext
? ActivityDataRequest.None
: (initializedContext.TraceFlags & ActivityTraceFlags.Recorded) == ActivityTraceFlags.Recorded
? ActivityDataRequest.AllDataAndRecorded
: currentActivity?.IsAllDataRequested == true
? ActivityDataRequest.AllData
: ActivityDataRequest.PropagationData;

return new ParentActivityState(parentDataRequested, initializedContext);
}
}

// SynchronizedList<T> is a helper collection which ensure thread safety on the collection
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Diagnostics
{
public readonly struct ParentActivityState : IEquatable<ParentActivityState>
{
public ParentActivityState(ActivityDataRequest requestedData, ActivityContext activityContext)
{
RequestedData = requestedData;
ActivityContext = activityContext;
}

public ActivityDataRequest RequestedData { get; }

public ActivityContext ActivityContext { get; }

public bool Equals(ParentActivityState other) => ActivityContext.Equals(other.ActivityContext) && RequestedData == other.RequestedData;

public override int GetHashCode() => HashCode.Combine(RequestedData, ActivityContext);

public override bool Equals(object? obj) => (obj is ParentActivityState context) && Equals(context);
public static bool operator ==(ParentActivityState left, ParentActivityState right) => left.Equals(right);
public static bool operator !=(ParentActivityState left, ParentActivityState right) => !(left == right);
}
}