Skip to content

Add OTEL semantic conventions tags to HTTP server activity#64851

Merged
JamesNK merged 10 commits intomainfrom
jamesnk/activity-telemetry-tags
Jan 30, 2026
Merged

Add OTEL semantic conventions tags to HTTP server activity#64851
JamesNK merged 10 commits intomainfrom
jamesnk/activity-telemetry-tags

Conversation

@JamesNK
Copy link
Copy Markdown
Member

@JamesNK JamesNK commented Dec 23, 2025

This PR adds OTEL telemetry tags to the HTTP activity:

  • Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryData now defaults to false. Telemetry is added to activity by default in .NET 11.
  • All required attributes are added to telemetry from HTTP server span spec.
  • Fixed HTTP request duration metric to include error.type attribute for 500+ response status codes

The goal is to match the metadata set on the activity by OpenTelemetry.Instrumentation.AspNetCore.

Fixes #52439

@JamesNK JamesNK added the area-hosting Includes Hosting label Dec 23, 2025
@JamesNK JamesNK requested a review from halter73 as a code owner December 23, 2025 02:16
Copilot AI review requested due to automatic review settings December 23, 2025 02:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds OpenTelemetry semantic convention tags to HTTP server activities and metrics in ASP.NET Core, aligning with OTEL specifications. The key change is that telemetry is now enabled by default in .NET 11.

  • Changes the default value of Microsoft.AspNetCore.Hosting.SuppressActivityOpenTelemetryData from true to false, enabling OTEL data by default
  • Adds required OTEL semantic convention attributes to HTTP activities including http.response.status_code, network.protocol.version, http.route, and error.type
  • Implements error.type attribute for HTTP server metrics with 5xx status codes, following OTEL specifications

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Shared/Diagnostics/RouteDiagnosticsHelpers.cs Updates comment to reference "telemetry attributes" instead of just "metrics"
src/Hosting/Hosting/src/Internal/HostingTelemetryHelpers.cs Adds new attribute constants for OTEL semantic conventions and implements IsErrorStatusCode helper method
src/Hosting/Hosting/src/Internal/HostingMetrics.cs Updates metrics to use attribute constants and adds error.type tag for 5xx status codes
src/Hosting/Hosting/src/Internal/HostingApplicationDiagnostics.cs Changes default to enable OTEL data, adds end tags to activities including error handling logic
src/Hosting/Hosting/test/HostingMetricsTests.cs Adds comprehensive tests for error.type behavior in metrics with various status codes
src/Hosting/Hosting/test/HostingApplicationDiagnosticsTests.cs Adds extensive tests for activity tags, error handling, and sampling scenarios

@ghost
Copy link
Copy Markdown

ghost commented Dec 23, 2025

There might be an issue with forwarded headers handling in the current implementation. The attributes server.address, server.port, and url.scheme are currently set at the start of the activity, before the middleware pipeline is executed and the forwarded headers have been processed. This results in incorrect values for these attributes.

The specification recommends setting these attributes based on the processed forwarded headers:

HTTP server instrumentations SHOULD do the best effort when populating server.address and server.port attributes and SHOULD determine them by using the first of the following that applies:

  • The original host which may be passed by the reverse proxy in the Forwarded#host, X-Forwarded-Host, or a similar header.
  • The :authority pseudo-header in case of HTTP/2 or HTTP/3
  • The Host header.

Should these attributes also be set when the activity stops?

@ghost
Copy link
Copy Markdown

ghost commented Dec 23, 2025

Do you plan to add specific support for activity enrichment based on the HttpRequest and HttpResponse , similar to what is available in OpenTelemetry.Instrumentation.AspNetCore? https://github.com/open-telemetry/opentelemetry-dotnet-contrib/blob/c938a14321ebf64e3ef4f10a679bc3c3d71690e6/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs#L68-L86
Or is the current recommendation to use a middleware (or similar) to enrich the activity?

@JamesNK
Copy link
Copy Markdown
Member Author

JamesNK commented Dec 23, 2025

Do you plan to add specific support for activity enrichment based on the HttpRequest and HttpResponse , similar to what is available in OpenTelemetry.Instrumentation.AspNetCore? open-telemetry/opentelemetry-dotnet-contrib@c938a14/src/OpenTelemetry.Instrumentation.AspNetCore/AspNetCoreTraceInstrumentationOptions.cs#L68-L86 Or is the current recommendation to use a middleware (or similar) to enrich the activity?

No plans for special events.

@JamesNK
Copy link
Copy Markdown
Member Author

JamesNK commented Dec 23, 2025

Should these attributes also be set when the activity stops?

I don't know. I looked at what OpenTelemetry.Instrumentation.AspNetCore does and it doesn't appear to have any special logic. Those tags are set at the same of the request. The guiding principal of this improvement is to match OpenTelemetry.Instrumentation.AspNetCore tag output.

@ghost
Copy link
Copy Markdown

ghost commented Dec 24, 2025

Should these attributes also be set when the activity stops?

I don't know. I looked at what OpenTelemetry.Instrumentation.AspNetCore does and it doesn't appear to have any special logic. Those tags are set at the same of the request. The guiding principal of this improvement is to match OpenTelemetry.Instrumentation.AspNetCore tag output.

There is an open issue in OTel repo because of this behavior: open-telemetry/opentelemetry-dotnet-contrib#2231

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Dec 31, 2025
@JamesNK
Copy link
Copy Markdown
Member Author

JamesNK commented Jan 14, 2026

@BrennanConroy @halter73 Please review when you have some time.

@JamesNK
Copy link
Copy Markdown
Member Author

JamesNK commented Jan 14, 2026

/azp run

@dotnet-policy-service dotnet-policy-service bot removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jan 14, 2026
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jan 21, 2026
@JamesNK
Copy link
Copy Markdown
Member Author

JamesNK commented Jan 22, 2026

/azp run

@JamesNK JamesNK enabled auto-merge (squash) January 22, 2026 00:21
@dotnet-policy-service dotnet-policy-service bot removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jan 22, 2026
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@JamesNK
Copy link
Copy Markdown
Member Author

JamesNK commented Jan 27, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

@JamesNK
Copy link
Copy Markdown
Member Author

JamesNK commented Jan 29, 2026

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-hosting Includes Hosting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native Trace Instrumentation Support for ASP.NET Core as per OTel specification

4 participants