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>
|
@EasyL0ver please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
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. |
| internal const string DataVersion = "v1.0"; | ||
|
|
||
| private readonly ObjectPool<StringBuilder> _builderPool = PoolFactory.CreateStringBuilderPool(); | ||
|
|
| builder.Services.AddRequestLatencyTelemetry(); | ||
| builder.Services.AddRequestCheckpoint(); | ||
| builder.Services.AddHttpLatencyTelemetry(); | ||
| builder.Services.AddHttpLoggingRedaction(); | ||
|
|
| public string SerializedLatencyData { get; private set; } | ||
|
|
||
| public LatencyData LatencyData { get; private set; } |
| request.Setup(a => a.Headers).Returns(headers); | ||
| var enricher = new HttpLatencyLogEnricher(); | ||
| Mock<IEnrichmentTagCollector> mockEnrichmentPropertyBag = new Mock<IEnrichmentTagCollector>(); | ||
| Assert.Throws<NullReferenceException>(() => enricher.Enrich(mockEnrichmentPropertyBag.Object, httpContext!)); | ||
| mockEnrichmentPropertyBag.Verify(m => m.Add(It.Is<string>(s => true), It.Is<string>(s => true)), Times.Never); |
| private static void AppendClientName(HttpRequest request, StringBuilder stringBuilder) | ||
| { | ||
| if (request.Headers.TryGetValue(TelemetryConstants.ClientApplicationNameHeader, out var values)) | ||
| { | ||
| _ = stringBuilder.Append(values); | ||
| } | ||
| } |
| _ = stringBuilder.Append(','); | ||
| FormatLatencyData(stringBuilder, latencyContext.LatencyData); | ||
| collector.Add("latencyInfo", stringBuilder.ToString()); | ||
| } |
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