Add HTTP request latency log enricher (experimental) - #7602
Conversation
Migrates the incoming-request latency log enricher from the R9 SDK into Microsoft.AspNetCore.Diagnostics.Middleware. Adds the public AddHttpLatencyTelemetry() extension (registered via AddHttpLogEnricher) and the internal HttpLatencyLogEnricher that emits a latencyInfo tag with checkpoints, measures, tags and total duration on incoming HTTP request logs. The new API ships behind [Experimental] (EXTEXP0013) on both the class and the method, mirroring the sibling HttpClientLatency enricher. Tests are faithfully ported from the R9 suite (golden-string serialization plus null/edge cases). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Removes PerfPanel branding from MockLatencyData helper class and renames the property to better reflect its purpose as serialized latency data. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The MockLatencyData class was nested in HttpLatencyLogEnricherTests. Extract it to a dedicated file following extensions repo convention (e.g., CustomHttpLogEnricher.cs, TestHttpLogEnricher.cs) for better test organization. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an incoming HTTP request latency log enricher to Microsoft.AspNetCore.Diagnostics.Middleware, exposing a new experimental DI extension (AddHttpLatencyTelemetry) and wiring up serialization of ILatencyContext.LatencyData into HTTP log enrichment output.
Changes:
- Introduces
AddHttpLatencyTelemetry()(experimental) to register the incoming-request latency log enricher. - Adds
HttpLatencyLogEnricherthat readsILatencyContextfrom request services and emits a serialized latency payload. - Ports/creates tests and updates the library README and API baseline JSON.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Latency/Internal/MockLatencyData.cs | Adds a test helper that builds deterministic LatencyData + expected serialized string. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Latency/Internal/HttpLatencyLogEnricherTests.cs | Adds unit tests for the new incoming-request log enricher behavior. |
| test/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware.Tests/Latency/HttpLatencyTelemetryExtensionsTests.cs | Adds DI registration tests for AddHttpLatencyTelemetry. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/README.md | Documents how to register the new enricher. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Microsoft.AspNetCore.Diagnostics.Middleware.json | Adds the new experimental public API to the baseline. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Latency/Internal/HttpLatencyLogEnricher.cs | Implements the incoming-request latency enrichment + serialization format. |
| src/Libraries/Microsoft.AspNetCore.Diagnostics.Middleware/Latency/HttpLatencyTelemetryExtensions.cs | Adds the experimental DI extension method for registering the enricher. |
System.Globalization and System.Linq became unnecessary when MockLatencyData was extracted to a separate file. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@dotnet-policy-service agree company="Microsoft" |
1 similar comment
|
@dotnet-policy-service agree company="Microsoft" |
|
Thanks — noted the automated suggestions. These behaviors were intentionally preserved to maintain parity with the original production implementation:
If maintainers prefer these changes, I can follow up with a targeted PR. Parity diffs can be provided on request (internal review only). |
…ant AddHttpLoggingRedaction() from example Make test helper properties get-only to communicate immutability. Remove redundant AddHttpLoggingRedaction() from README example because AddHttpLatencyTelemetry already wires the logging plumbing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Rename the incoming enricher's log dimension key from lowercase 'latencyInfo' to 'LatencyInfo' to match the logging-dimension naming convention established in dotnet#4545 and used by the sibling HttpClientLatencyLogEnricher. Updated test assertions accordingly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Use PoolFactory.SharedStringBuilderPool (static) instead of a per-instance pool. - Append only the first client-application header value instead of the whole StringValues. Both match the established pattern in the sibling outgoing-request enricher. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The null-context and without-latency-context tests set up HeaderDictionary and HttpRequest mocks that were never exercised. Remove the dead setup for clarity. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
🎉 Good job! The coverage increased 🎉
Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1499794&view=codecoverage-tab |
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #if NET8_0_OR_GREATER |
There was a problem hiding this comment.
why is it not going to work on .NET Framework?
| /// Extensions for enriching incoming HTTP request logs with latency telemetry. | ||
| /// </summary> | ||
| [Experimental(diagnosticId: DiagnosticIds.Experiments.HttpLogging, UrlFormat = DiagnosticIds.UrlFormat)] | ||
| public static class HttpLatencyTelemetryExtensions |
There was a problem hiding this comment.
| public static class HttpLatencyTelemetryExtensions | |
| public static class HttpLatencyTelemetryServiceCollectionExtensions |
…uard (#7645) Rename HttpLatencyTelemetryExtensions and drop redundant TFM guard Address post-merge review feedback on #7602: - Rename HttpLatencyTelemetryExtensions to HttpLatencyTelemetryServiceCollectionExtensions to match the *ServiceCollectionExtensions naming convention used by sibling extension classes (RequestLatencyTelemetryServiceCollectionExtensions, HttpLoggingServiceCollectionExtensions). - Remove the always-true #if NET8_0_OR_GREATER guard from the enricher and its extensions. The project only targets net8.0+, so the guard never excluded any target framework. Copilot-Session: d705cd71-1bda-4572-88e7-0bbfda83c3d6 Co-authored-by: Pawel Kudlacik <pkudlacik@microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Completes the open-sourcing of the incoming-request latency log enricher that was originally intended for public release but was orphaned. Migrates it from the R9 SDK into Microsoft.AspNetCore.Diagnostics.Middleware.
Changes
Design notes
latencyInfo, while sibling client enricher uses uppercaseLatencyInfo. This inconsistency should be addressed at API review.Precedent
Follows the pattern established by @rainsxng (#6783) and @mariamgerges (#7380) for other migration enrichers. Baseline JSON, attribute placement, test porting all consistent with that precedent.
Microsoft Reviewers: Open in CodeFlow