Skip to content

Commit 40c9291

Browse files
committed
update measures and tags text
1 parent e8b4746 commit 40c9291

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

docs/core/extensions/httpclient-latency-extensions.md

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,61 @@ The <xref:Microsoft.Extensions.DependencyInjection.HttpClientLatencyTelemetryExt
1414
extension enables collection of detailed timing information for outgoing HTTP calls with no changes to calling code.
1515
It plugs into the existing `HttpClientFactory` pipeline to capture stage timings across the request lifecycle, record
1616
HTTP protocol details, measure garbage collection impact where the runtime exposes that data, and emit a uniform
17-
telemetry shape suitable for performance analysis and tuning.
17+
telemetry shape suitable for performance analysis and tuning.Enable them by calling `AddHttpClientLatencyTelemetry()` extension method.
18+
The built‑in handler creates an `ILatencyContext` per outbound request and populates measures by the time the inner
19+
pipeline completes. Consume them after `await base.SendAsync(...)` in a later delegating handler (added after telemetry)
20+
and export to your metrics backend. Example:
21+
22+
Register extension method:
23+
24+
```csharp
25+
using Microsoft.Extensions.DependencyInjection;
26+
using Microsoft.Extensions.Http.Diagnostics;
27+
using Microsoft.Extensions.Hosting;
28+
29+
var builder = Host.CreateApplicationBuilder(args);
30+
31+
// An example of some accessor that is able to read latency context
32+
builder.Services.AddSingleton<ILatencyContextAccessor, LatencyContextAccessor>();
33+
34+
// Register HTTP client latency telemetry first so its delegating handler runs earlier.
35+
builder.Services.AddHttpClientLatencyTelemetry();
36+
37+
// Register export handler (runs after telemetry; sees finalized ILatencyContext).
38+
builder.Services.AddTransient<HttpLatencyExportHandler>();
39+
40+
// Register an HttpClient that will emit and export latency measures.
41+
builder.Services
42+
.AddHttpClient("observed")
43+
.AddHttpMessageHandler<HttpLatencyExportHandler>();
44+
45+
var host = builder.Build();
46+
await host.RunAsync();
47+
```
48+
Access the context:
49+
50+
```csharp
51+
public sealed class HttpLatencyExportHandler : DelegatingHandler
52+
{
53+
// ILatencyContextAccessor is just an example of some accessor that is able to read latency context
54+
private readonly ILatencyContextAccessor _latency;
55+
56+
public HttpLatencyExportHandler(ILatencyContextAccessor latency) => _latency = latency;
57+
58+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct)
59+
{
60+
var rsp = await base.SendAsync(request, ct).ConfigureAwait(false);
61+
62+
var ctx = _latency.Current;
63+
if (ctx != null)
64+
{
65+
var data = ctx.LatencyData;
66+
// Record/export gc and conn with version as a dimension here.
67+
}
68+
return rsp;
69+
}
70+
}
71+
```
1872

1973
### [.NET CLI](#tab/dotnet-cli)
2074

@@ -96,10 +150,16 @@ Timestamps are recorded for key stages of the HTTP request lifecycle:
96150

97151
#### Measures (platform dependent)
98152

99-
Measures are numeric values captured during a request that quantify things your timestamp checkpoints alone
100-
don't show - like how long GC pauses stalled the thread or how often a brand-new connection had to be opened instead of
101-
reusing a pooled one. Use them when total latency is higher than the sum of visible phases, when investigating memory
102-
pressure or connection churn, or when tuning pooling and allocation patterns.
153+
Measures quantify latency contributors that raw phase checkpoints cannot (GC pause overlap, connection churn, other
154+
accumulated counts or durations). They are collected in an in‑memory latency context created when you call
155+
`AddHttpClientLatencyTelemetry()`. Nothing is emitted automatically: the context simply accumulates checkpoints, measures,
156+
and tags until the request completes. If you also enable HTTP client logging enrichment with `AddExtendedHttpClientLogging()`,
157+
the completed context is flattened into a single structured log field named `LatencyInfo` (version marker, server name if available, then tag, checkpoint, and measure name/value sequences).
158+
159+
That log field is the only built‑in output artifact; no metrics or traces are produced unless you add your own exporter.
160+
To surface them as metrics, read the context after the request pipeline returns and record (for example) GC pause overlap
161+
to a histogram and connection initiations to a counter, optionally dimensioned by protocol version.
162+
103163

104164
| Name | Description |
105165
|--------------------------|-------------------------------------------------------------------------|
@@ -108,17 +168,29 @@ pressure or connection churn, or when tuning pooling and allocation patterns.
108168

109169
#### Tags
110170

111-
Use tags to attach stable categorical dimensions to each request so you can segment, filter, and aggregate metrics and
112-
logs without reprocessing raw data. They carry classification (not duration) and remain low‑cardinality by design.
171+
Use tags to attach stable categorical dimensions to each request so you can segment, filter, and aggregate metrics
172+
and logs without reprocessing raw data. They are low‑cardinality classification labels (not timings) captured
173+
in the latency context and, if HTTP client log enrichment is enabled, serialized into a single LatencyInfo log field.
174+
Enable enrichment at application startup by adding the logging extension (for all clients or per client) along with
175+
latency telemetry, for example:
176+
177+
```csharp
178+
var builder = Host.CreateApplicationBuilder(args);
179+
builder.Services.AddHttpClientLatencyTelemetry(); // enables latency context + measures/tags
180+
builder.Services.AddExtendedHttpClientLogging();
181+
var app = builder.Build();
182+
```
183+
184+
After this, outbound requests logged through the structured logging pipeline will include the `LatencyInfo` property
185+
containing the flattened tags, checkpoints, and measures. No metrics or traces are emitted automatically for tags;
186+
export them yourself (e.g., turn tag values into metric dimensions or `Activity` tags) if you need them outside logs.
113187

114188
| Tag | Description |
115189
|--------------|-----------------------------------------------------------------|
116190
| Http.Version | HTTP protocol version negotiated/used (for example, 1.1, 2, 3). |
117191

118192
## Usage example
119193

120-
### Track HTTP request client latency
121-
122194
These components enable tracking and reporting the latency of HTTP client request processing.
123195

124196
You can register the services using the following methods:

0 commit comments

Comments
 (0)