You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Libraries like HttpClient use legacy activity creation, requiring hacks in OpenTelemetry SDK #63868
Distributed tracing can be supported by .NET applications by instrumenting them using System.Diagnostics.Activity API. In addition, there are two APIs to be used to produce and consume events generated by instrumented application:
Currently, HttpClientstill leveragesSystem.Diagnostics.DiagnosticSource for distributed tracing instrumentation which requires OpenTelemetry SDK to use hacks and reflection. Going forward, this may also make it harder to stay up-to-date with progress done in OpenTelemetry specifications bringing even more complexity to OpenTelemetry SDK. For example, HTTP retries and redirects implementation require SpanLink objects (as per proposal here) that can only be supported by System.Diagnostics.ActivitySource.
We have tried adding ActivitySource support in .NET 6.0 (#54437), but the PR was reverted as it caused networking test failures (#55006, #54778) - the why is currently unclear.
Allowing more functionality that is only available to ActivitySource is a good reason to add it.
HttpClient still leverages System.Diagnostics.DiagnosticSource for distributed tracing instrumentation which requires OpenTelemetry SDK to use hacks and reflection
My understanding is that instrumentations like OpenTelemetry and AppInsights would still rely on DiagnosticSource and such hacks even if ActivitySource support were added.
They currently rely on inspecting the HttpRequestMessage object that comes with DiagnosticSource events, and as far as I am aware, there is currently no way for ActivitySource to expose such context to its listeners. Is that not the case?
@denisivan0v: follow this change in OT implementation and see how it can be useful
After that, we can see what other API changes we'd need to support Activity creation with links (for the redirects scenario)
a. Changes to ActivitySource to be able to access HttpRequestMessage associated with the activity - @MihaZupan did I get this right? Is this what is missing and why we still need DiagnosticSource?
b. Changes to be able to access HttpResponseMessage associated with the request - @denisivan0v what scenarios need the response object, do we have examples?
c. Potentially: changes to SocketsHttpHandler/DistributedContextPropagator to be able to create Activity manually and override default DiagnosticsHandler behavior, i.e. fully customize Activity creation.
a. Changes to ActivitySource to be able to access HttpRequestMessage associated with the activity - @MihaZupan did I get this right? Is this what is missing and why we still need DiagnosticSource?
b. Changes to be able to access HttpResponseMessage associated with the request - @denisivan0v what scenarios need the response object, do we have examples?
There are a few steps here:
Activity creation
This is where ActivitySource would fit in.
To be more useful, it would need access to the HttpRequestMessage and have full control over the Activity creation. Currently it does not have access to the request - that is only available to DiagnosticSource events.Activity has already been created.
Injecting headers into the request
This is done by either the call to DistributedContextPropagator.Inject or as part of creating the Activity in step 1.
At this point the propagator has access to the HttpRequestMessage, but is by definition too late to influence Activity creation.
Reporting the result based on the response
DiagnosticSource is currently the only mechanism providing a hook/callback when the response is received. It also gives access to the HttpResponseMessage and Exception.
So with the current API shape, DiagnosticSource is still required for:
Providing access to the HttpRequestMessage, HttpResponseMessage and Exception.
Providing a hook before the request is sent & after the response is received.
Just by adding ActivitySource, we don't eliminate any of the above.
All of this context is available to a custom DelegatingHandler.
The reason instrumentations currently prefer using events instead is that injecting a handler into the HttpClient pipeline requires user interaction (on top of just calling 1 method to register in DI).
If the existing events are not sufficient, should we instead look at providing a callback that allows instrumentations to inject a custom handler?
That would decouple instrumentations from internal implementation details of DiagnosticsHandler, allowing them to simplify the logic and remove all reflection hacks. They would also have as much context as DiagnosticsHandler can have when it comes to creating the activity.
So with the current API shape, DiagnosticSource is still required for:
Providing access to the HttpRequestMessage, HttpResponseMessage and Exception.
Providing a hook before the request is sent & after the response is received.
Just by adding ActivitySource, we don't eliminate any of the above.
DiagnosticSource callbacks are still required for OpenTelemetry (and for other scenarios as well)
Shifting from creating activity using legacy way (i.e new Activity("name")), to the ActivitySource way (i.e activitySource.startActivity(Kind.Client,..)) is the only ask. This would allow instrumentations in OpenTelemetry to avoid current hacks its doing to:
Activity
MihaZupan commentedon Jan 17, 2022
We have tried adding
ActivitySourcesupport in .NET 6.0 (#54437), but the PR was reverted as it caused networking test failures (#55006, #54778) - the why is currently unclear.Allowing more functionality that is only available to
ActivitySourceis a good reason to add it.My understanding is that instrumentations like OpenTelemetry and AppInsights would still rely on
DiagnosticSourceand such hacks even ifActivitySourcesupport were added.They currently rely on inspecting the
HttpRequestMessageobject that comes withDiagnosticSourceevents, and as far as I am aware, there is currently no way forActivitySourceto expose such context to its listeners. Is that not the case?cc: @dotnet/ncl as areas overlap here
ManickaP commentedon Jan 17, 2022
Follow up from the meeting:
Activitycreation with links (for the redirects scenario)a. Changes to
ActivitySourceto be able to accessHttpRequestMessageassociated with the activity - @MihaZupan did I get this right? Is this what is missing and why we still needDiagnosticSource?b. Changes to be able to access
HttpResponseMessageassociated with the request - @denisivan0v what scenarios need the response object, do we have examples?c. Potentially: changes to
SocketsHttpHandler/DistributedContextPropagatorto be able to createActivitymanually and override defaultDiagnosticsHandlerbehavior, i.e. fully customizeActivitycreation.MihaZupan commentedon Jan 18, 2022
There are a few steps here:
ActivitySourcewould fit in.HttpRequestMessageand have full control over theActivitycreation. Currently it does not have access to the request - that is only available toDiagnosticSourceevents.Activityhas already been created.DistributedContextPropagator.Injector as part of creating theActivityin step 1.HttpRequestMessage, but is by definition too late to influenceActivitycreation.DiagnosticSourceis currently the only mechanism providing a hook/callback when the response is received. It also gives access to theHttpResponseMessageandException.So with the current API shape,
DiagnosticSourceis still required for:HttpRequestMessage,HttpResponseMessageandException.Just by adding
ActivitySource, we don't eliminate any of the above.All of this context is available to a custom
DelegatingHandler.The reason instrumentations currently prefer using events instead is that injecting a handler into the
HttpClientpipeline requires user interaction (on top of just calling 1 method to register in DI).If the existing events are not sufficient, should we instead look at providing a callback that allows instrumentations to inject a custom handler?
That would decouple instrumentations from internal implementation details of
DiagnosticsHandler, allowing them to simplify the logic and remove all reflection hacks. They would also have as much context asDiagnosticsHandlercan have when it comes to creating the activity.cijothomas commentedon Jan 24, 2022
DiagnosticSource callbacks are still required for OpenTelemetry (and for other scenarios as well)
Shifting from creating activity using legacy way (i.e new Activity("name")), to the ActivitySource way (i.e activitySource.startActivity(Kind.Client,..)) is the only ask. This would allow instrumentations in OpenTelemetry to avoid current hacks its doing to:
This hack means Samplers don't always get the right info it is supposed to get (Instrumentations based on legacy activity creation provide default Kind to sampler open-telemetry/opentelemetry-dotnet#1947)
tarekgh commentedon Feb 15, 2022
@denisivan0v I think we can close this now as it is addressed by @ManickaP PR?
tarekgh commentedon Feb 18, 2022
@denisivan0v can we close this issue? or are we waiting for something more?
ManickaP commentedon Feb 21, 2022
I'll close this since the PR is in. We can reopen and revisit if something turns out to be missing.