Skip to content

Add ActivitySource support to DiagnosticsHandler #64753

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

Merged
merged 3 commits into from
Feb 5, 2022

Conversation

ManickaP
Copy link
Member

@ManickaP ManickaP commented Feb 3, 2022

This is an attempt to bring the logic from #54437 in main.

Description from the original PR:

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 for Activity creation if an ActivityListener is present:

  • ActivitySource is given chance to create an activity
  • if none was created, the logic falls back to original behaviour: either Activity.Current is set or DiagnosticListener is enabled for the activity name
    - 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 Activity? CreateActivity(HttpRequestMessage request)

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.


@MihaZupan could you please give it a look if I'm not missing anything? I had to adapt the PR and I tried to not to do much unnecessary churn.

Contributes to #63868

cc @denisivan0v @cijothomas

@ghost ghost assigned ManickaP Feb 3, 2022
@ghost ghost added the area-System.Net.Http label Feb 3, 2022
@ghost
Copy link

ghost commented Feb 3, 2022

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

Issue Details

This is an attempt to bring the logic from #54437 in main.

Description from the original PR:

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.


@MihaZupan could you please give it a look if I'm not missing anything? I had to adapt the PR and I tried to not to do much unnecessary churn.

Author: ManickaP
Assignees: ManickaP
Labels:

area-System.Net.Http

Milestone: -

Copy link
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

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

The behaviour changes iff all of the following hold:

Looks like I didn't update the description on the PR as things changed 😄
From what I can tell, this PR is functionally equivalent to #54437 and the difference is this:

  • ActivitySource is given the chance to create the Activity first, but if it chooses not to, one will still be created manually.

I.e. ActivitySource's sampling decision is ignored if there is already an Activity present / a diagnostic listener said otherwise. This is the same thing AspNetCore does on their side. Some more details here: #54437 (comment)

@ManickaP ManickaP force-pushed the mapichov/open-telemetry branch from 0a5eefb to 4ed0d55 Compare February 4, 2022 09:22
Copy link
Member

@CarnaViire CarnaViire left a comment

Choose a reason for hiding this comment

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

LGTM (I'm no expert though 😆)

@@ -998,6 +962,112 @@ public async Task SendAsync_CustomSocketsHttpHandlerPropagator_PropagatorIsUsed(
});
}

[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData(true, true, true)] // Activity was set and ActivitySource created an Activity
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to have a generated MemberData instead? 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to preserve some of the wisdom from the comments so it's not much shorter than the original, but it's moved in member data.

@ManickaP
Copy link
Member Author

ManickaP commented Feb 5, 2022

No failing tests, pipelines are timing out on Mac/iOS/..., other runs have similar problems ==> merging.

@ManickaP ManickaP merged commit d8c0170 into dotnet:main Feb 5, 2022
@ManickaP ManickaP deleted the mapichov/open-telemetry branch February 5, 2022 20:17
@ghost ghost locked as resolved and limited conversation to collaborators Mar 8, 2022
@karelz karelz added this to the 7.0.0 milestone Apr 8, 2022
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.

4 participants