HttpClient distributed tracing tag enrichment
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- networking, observability
Research direction
Start with System.Net.Http/DiagnosticsHandler.cs, where HttpClient creates the activity, and review Activity.AddTag in System.Diagnostics.DiagnosticSource. Use the reproduction scenario to examine how enrichment could be scoped per HttpClient or HttpRequestMessage and expose the relevant activity. Done means a practical enrichment API supports the stated configuration and activity-access requirements.
Written by the indexing model from the issue text.
Description
Description
HttpClient creates an Activity for each HTTP request. The activity is used to create a span in OTel distributed tracing.
OTel spans have attributes and HttpClient automatically adds information about the HTTP request to the span. Additional information can be added by calling Activity.AddTag. However, there isn't an easy way to enrich the span with custom information for an HTTP request. Usually the activity is accessed using Activity.Current, an async local, but HttpClient creates the activity in DiaganosticsHandler which is automatically added as the last handler in the pipeline. It's not possible to add your own handler to work with Activity.Current because it's created afterwards.
An alternative approach is to use a diagnostics listener and listen to System.Net.Http.HttpRequestOut.Start. It is called after the activity has been created. The problem with this approach:
- It's static across the app. Can't localize enrichment to one HttpClient or request.
- Accessing the
HttpRequestMessagein the raised listener callback requires reflection. - It's hard and inaccessible.
A scenario where this information is useful is this Aspire UI enhancement: https://github.com/dotnet/aspire/pull/1061
We want to identify the Browser Link HTTP request an ASP.NET Core app makes to Visual Studio. It would be great if we could enrich the activity to have an attribute to identify the call was for Browser Link.
Because that's not possible we were forced to look at the request URL to infer the request is for Browser Link.
Reproduction Steps
Try to enrich Activity for a HttpClient.
Expected behavior
There is an easy-to-use API for enriching the HttpClient. The API:
- Can be configured per HttpClient, or ideally, per HttpRequestMessage.
- Provides the Activity for HttpClient. Providing the activity is better than making people use Activity.Current, because there might be a child activity created, and using Activity.Current forces callers to check the name for each activity in the hierarchy until they find the one for the HttpClient.
Maybe something like:
var httpRequest = new HttpRequestMessage("https://contoso.com");
HttpActivityEnrichmentContext.AddCallback(httpRequest, context =>
{
context.Activity.AddTag("http.is_browser_link", true);
});
Actual behavior
No easy way to enrich activity for HttpClient.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 589
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
bug documentation frontend
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
azurenoops/spin_agent#975 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100