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 ActivitySource support to DiagnosticsHandler #54437

Merged
merged 6 commits into from
Jun 24, 2021

Conversation

MihaZupan
Copy link
Member

Today, DiagnosticsHandler will create a new Activity if either is true:

  • An Activity was already set (Activity.Current is not null)
  • A DiagnosticListener requested one (was enabled for the activity name)

This PR preserves the same behaviour when no ActivityListener is present.

The behaviour changes iff all of the following hold:

  • An Activity was already set (Activity.Current was not null)
  • No DiagnosticListener was enabled for the activity name
  • ActivitySource had listeners present
  • Listeners decided not to sample the request

In this case, no Activity is created (and so we also won't inject context headers).
If a DiagnosticListener is enabled for the activity, one is always created (regardless of sampling decisions).

The PR moves the decision of whether to log events/create an activity out of the SendAsync call into

// The interesting part of the PR - this method decides on the actual behaviour
static bool ShouldLogDiagnostics(HttpRequestMessage request, out Activity? activity)

I also removed the internal Settings and LoggingStrings classes - am I missing something and they actually provide value?

Note that the PR is only a slight refactor + adding ActivitySource. It does not address other known DiagnosticsHandler issues.
This is runtime's equivalent of what dotnet/aspnetcore#30089 was for AspNet.

Opening as a draft so the diagnostics team can confirm whether the above is the desired behaviour with ActivitySource.
cc: @shirhatti @tarekgh @noahfalk

@ghost
Copy link

ghost commented Jun 18, 2021

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

Issue Details

Today, DiagnosticsHandler will create a new Activity if either is true:

  • An Activity was already set (Activity.Current is not null)
  • A DiagnosticListener requested one (was enabled for the activity name)

This PR preserves the same behaviour when no ActivityListener is present.

The behaviour changes iff all of the following hold:

  • An Activity was already set (Activity.Current was not null)
  • No DiagnosticListener was enabled for the activity name
  • ActivitySource had listeners present
  • Listeners decided not to sample the request

In this case, no Activity is created (and so we also won't inject context headers).
If a DiagnosticListener is enabled for the activity, one is always created (regardless of sampling decisions).

The PR moves the decision of whether to log events/create an activity out of the SendAsync call into

// The interesting part of the PR - this method decides on the actual behaviour
static bool ShouldLogDiagnostics(HttpRequestMessage request, out Activity? activity)

I also removed the internal Settings and LoggingStrings classes - am I missing something and they actually provide value?

Note that the PR is only a slight refactor + adding ActivitySource. It does not address other known DiagnosticsHandler issues.
This is runtime's equivalent of what dotnet/aspnetcore#30089 was for AspNet.

Opening as a draft so the diagnostics team can confirm whether the above is the desired behaviour with ActivitySource.
cc: @shirhatti @tarekgh @noahfalk

Author: MihaZupan
Assignees: -
Labels:

area-System.Net.Http

Milestone: 6.0.0

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@MihaZupan MihaZupan marked this pull request as ready for review June 21, 2021 10:05
@MihaZupan MihaZupan requested a review from a team June 21, 2021 10:06
@MihaZupan
Copy link
Member Author

@dotnet/ncl please take a look at this PR - the behaviour should be unchanged, but there is a considerable refactor to the underlying class.

private static readonly DiagnosticListener s_diagnosticListener = new("HttpHandlerDiagnosticListener");
private static readonly ActivitySource s_activitySource = new(Namespace);

public static readonly bool IsGloballyEnabled = GetEnableActivityPropagationValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be static property or it will regress feature switch setting and increases size duo to static ctor dependency

Copy link
Member Author

@MihaZupan MihaZupan Jun 23, 2021

Choose a reason for hiding this comment

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

Changed to

public static bool IsGloballyEnabled { get; } = GetEnableActivityPropagationValue();

@marek-safar Is this the pattern you had in mind?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better but it will still all the unnecessary dependencies like DiagnosticListener, ActivitySource and others where were not there before. You can check the size regression to see how much it adds

Copy link
Member Author

Choose a reason for hiding this comment

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

Is there a linker switch for DiagnosticsHandler?
Or was it capable of removing the code before (it would have had to remove Activity and DiagnosticListener)?

You can check the size regression to see how much it adds

Do you mean just the size of the System.Net.Http assembly? Or some other way of looking at trimmed regressions?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a linker switch for DiagnosticsHandler?

Yes, see HttpActivityPropagationSupport at https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md

Do you mean just the size of the System.Net.Http assembly? Or some other way of looking at trimmed regressions?

System.Net.Http will probably show the biggest increase if there is some. You can publish trimmed release build with HttpActivityPropagationSupport property set to false to compare.

Copy link
Member Author

@MihaZupan MihaZupan Jun 23, 2021

Choose a reason for hiding this comment

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

I updated the ILLink.Substitutions.xml to get_IsGloballyEnabled().

Looking at .\build.cmd -projects .\src\libraries\System.Net.Http\src\System.Net.Http.csproj -os Browser -c Release
the size of System.Net.Http.dll in browser-wasm\lib\net6.0 dropped from 247.296 to 246.272 bytes after this PR.

I don't know how to test the runtime change with trimming and the feature switch.

Copy link
Member

@noahfalk noahfalk left a comment

Choose a reason for hiding this comment

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

I had a few questions around what our desired behavior should be inline. Other than that all looked good : )

@karelz karelz requested a review from geoffkizer June 22, 2021 19:25
Copy link
Contributor

@geoffkizer geoffkizer left a comment

Choose a reason for hiding this comment

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

These changes look reasonable to me. I'd suggest changing the "when" usage (as I commented above), but it's not a huge deal.

Copy link
Member

@noahfalk noahfalk left a comment

Choose a reason for hiding this comment

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

LGTM!

@MihaZupan MihaZupan merged commit c88da29 into dotnet:main Jun 24, 2021
antonfirsov added a commit that referenced this pull request Jun 25, 2021
MihaZupan added a commit to MihaZupan/runtime that referenced this pull request Jun 30, 2021
@MihaZupan MihaZupan mentioned this pull request Jun 30, 2021
MihaZupan added a commit to MihaZupan/runtime that referenced this pull request Jul 1, 2021
stephentoub pushed a commit that referenced this pull request Jul 1, 2021
antonfirsov added a commit to antonfirsov/runtime that referenced this pull request Jul 1, 2021
MihaZupan added a commit to MihaZupan/runtime that referenced this pull request Jul 8, 2021
dotnet#54437 but without the ActivitySource support
@ghost ghost locked as resolved and limited conversation to collaborators Jul 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants