Skip to content

[draft] Trim ActivitySource via System.Diagnostics.ActivitySource.IsSupported feature #114636

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 5 commits into
base: main
Choose a base branch
from

Conversation

pavelsavara
Copy link
Member

@pavelsavara pavelsavara commented Apr 14, 2025

  • feature System.Diagnostics.ActivitySource.IsSupported

Contribute to #114635
On top of #114326

@@ -105,7 +105,7 @@ internal static string GetAsyncStateMachineDescription(IAsyncStateMachine stateM
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void LogTraceOperationBegin(Task t, Type stateMachineType)
{
TplEventSource.Log.TraceOperationBegin(t.Id, "Async: " + stateMachineType.Name, 0);
if (EventSource.IsSupported) TplEventSource.Log.TraceOperationBegin(t.Id, "Async: " + stateMachineType.Name, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to this method is under if (TplEventSource.Log.IsEnabled()) condition. Why do we need to duplicate the condition here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Dtto for all other places that have this duplication.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be done by telling the trimmer to replace EventSource.IsEnabled() with false when EventSource is not supported. It does not make sense to duplicate the EventSource.IsSupported condition everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm unhappy about size of that diff too.
I need to investigate more, this is "stash it for later, it's too late tonight" .

I suspect that TplEventSource.Log.IsEnabled() is holding TplEventSource.Log alive even after trimming.

Maybe I could introduce TplEventSource.IsEnabled() instead which would call TplEventSource.Log.IsEnabled() and trim/substitute that for false. But it must not trim when EventSource.IsSupported is true. And so it must be XML and not an attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TplEventSource.Log alive even after trimming.

It is expected to be only a small dummy artifact. All EventSource methods that do any actual work have if (IsSupported) return; or similar, so all heavy lifting should be trimmed already.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I could introduce TplEventSource.IsEnabled()

I would focus on fixes in the EventSource itself. I would try to avoid changing every actual event source out there, even if it means leaving a tiny bit of size on the table.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trimmer is not that smart. Any mention of some class keeps the class alive and the class bodies are usually not trimmed. For example DiagLinkedList is static of Activity class. And ball of dead code keeps rolling.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

activity1 is always null after trimming.

      Activity activity1 = (Activity) null;
      if (!SocketsTelemetry.IsActivitySourceSupported)
        ;
      if (activity1 != null)
      {
               ... bloat
      }

image

@sbomer is this bug that would be considered for fixing, or I should seek workarounds ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #114730 with simple repro

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe it's same as dotnet/linker#1284

@pavelsavara pavelsavara added size-reduction Issues impacting final app size primary for size sensitive workloads area-System.Diagnostics and removed area-System.Net.Http labels Apr 15, 2025
Copy link
Contributor

Tagging subscribers to this area: @tommcdon
See info in area-owners.md if you want to be subscribed.

- MSBuild property ActivitySourceSupport
private const string ActivitySourceName = "Experimental.System.Net.NameResolution";
private const string ActivityName = ActivitySourceName + ".DnsLookup";
private static readonly ActivitySource s_activitySource = new ActivitySource(ActivitySourceName);
private static readonly ActivitySource? s_activitySource;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static readonly ActivitySource? s_activitySource;
private static readonly ActivitySource? s_activitySource = IsActivitySourceSupported ? new ActivitySource(ActivitySourceName) : null;

Instead of the explicit constructor and warning disable?


private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe";
public static readonly NetSecurityTelemetry Log = new NetSecurityTelemetry();

#pragma warning disable CA1810 // remove the explicit static constructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Diagnostics size-reduction Issues impacting final app size primary for size sensitive workloads
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants