Skip to content

Migrate to use ActivitySource API to create Activities in AspNetCore Hosting #29132

Closed
@cijothomas

Description

@cijothomas

Is your feature request related to a problem? Please describe.

AspNetCore uses Activity API along with DiagnosticSource/Listener API to emit telemetry/diagnostic events. https://github.com/dotnet/aspnetcore/blob/master/src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs

Its currently using the new Activity() method to create an Activity. In .NET 5.0, a new mechanism was introduced to create Activity. The new API is based on ActivitySource. It offers following advantages:

  1. ActivitySource allows setting proper Kind to be set on Activity, instead of the default Internal.

  2. ActivitySource assigns correct ActivitySource to Activity. (instead of a default ActivitySource which has "" name)

  3. ActivitySource runs through the Sampler before deciding to create the activity and creates Activity with correct values for IsAllDataRequested and Recorded. This allows connecting samplers to control Activity creation and is in alignment with OpenTelemetry specifications. Since AspNetCore does not use ActivitySource API, Activity is always created without the ability to configure Sampler.

Describe the solution you'd like

A clear and concise description of what you want to happen. Include any alternative solutions you've considered.

  1. Migrating to ActivitySource API - replacing new Activity() with ActivitySource.StartActivity() is a breaking change, as Activity may or may not be created when using ActivitySource. To avoid this breaking change, it is proposed to create a dummy ActivityListener, which will set SamplingResult to PropagationData. This ensures that activity will always be created (thus maintaining backward compatibility).

OpenTelemetry (or other telemetry systems) can setup additional Listeners and achieve its needs.

  1. Populate the necessary tags. (as per OpenTelemetry semantic conventions) - This is optional, if replace batch files with kvm #3 is continued, as a listener can listen to DiagnosticSource events, and populate Tags as needed.
  2. The DiagnosticSource events may continue to be fired.

Once the above is achieved, OpenTelemetry instrumentations can avoid the current hacks it deploys to workaround the limitations. As of now, OpenTelemetry uses reflection to set Kind, Source on Activity, and runs samplers to correctly set IsAllDataRequested and Recorded fields.

Additional context

Related issue: #28642

Metadata

Metadata

Assignees

Labels

area-hostingIncludes Hostingarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions