Skip to content

Commit

Permalink
Add ActivityChange event
Browse files Browse the repository at this point in the history
  • Loading branch information
ezsilmar authored and verdie-g committed Dec 12, 2023
1 parent be46080 commit 6ea097c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.Tracing;
using System.Threading;

namespace System.Diagnostics
{
public partial class Activity
{
private static readonly AsyncLocal<Activity?> s_current = new AsyncLocal<Activity?>();
private static readonly AsyncLocal<Activity?> s_current = new AsyncLocal<Activity?>(OnActivityChange);

/// <summary>
/// Gets or sets the current operation (Activity) for the current thread. This flows
Expand All @@ -29,5 +30,13 @@ private static void SetCurrent(Activity? activity)
{
s_current.Value = activity;
}

private static void OnActivityChange(AsyncLocalValueChangedArgs<Activity?> args)
{
if (DiagnosticSourceEventSource.Log.IsEnabled(EventLevel.Verbose, DiagnosticSourceEventSource.Keywords.ActivityChange))
{
DiagnosticSourceEventSource.Log.ActivityChange(args.CurrentValue?._spanId);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ public static class Keywords
/// </summary>
public const EventKeywords Events = (EventKeywords)0x2;

/// <summary>
/// Logs every time Activity.Current changes on any thread.
/// This allows to correlate any other event (like a CPU sample or an AllocationTick) to the activity it belongs to
/// </summary>
public const EventKeywords ActivityChange = (EventKeywords)0x4;

// Some ETW logic does not support passing arguments to the EventProvider. To get around
// this in common cases, we define some keywords that basically stand in for particular common arguments
// That way at least the common cases can be used by everyone (and it also compresses things).
Expand Down Expand Up @@ -369,6 +375,12 @@ private void ActivityStart(string SourceName, string ActivityName, IEnumerable<K
private void ActivityStop(string SourceName, string ActivityName, IEnumerable<KeyValuePair<string, string?>> Arguments) =>
WriteEvent(12, SourceName, ActivityName, Arguments);

[Event(13, Keywords = Keywords.ActivityChange, Level = EventLevel.Verbose)]
public void ActivityChange(string? SpanId)
{
WriteEvent(13, SpanId);
}

#region private

private DiagnosticSourceEventSource()
Expand Down

0 comments on commit 6ea097c

Please sign in to comment.