-
Notifications
You must be signed in to change notification settings - Fork 6k
[Http Client Latency] Add new page describing the HttpClientLatencyExtension methods #48770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rainsxng
wants to merge
5
commits into
main
Choose a base branch
from
dbohdanov/latency-tags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−0
Draft
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
--- | ||
title: Use the HttpClientLatency | ||
description: Learn how to use the HttpClientLatency with dependency injection in your .NET workloads. | ||
author: IEvangelist | ||
ms.author: dapine | ||
ms.date: 09/29/2025 | ||
--- | ||
|
||
# HTTP client latency telemetry in .NET | ||
|
||
### Get started | ||
|
||
When building applications that communicate with other services over HTTP, understanding the performance characteristics | ||
of those HTTP operations is essential. The `AddHttpClientLatencyTelemetry` extension method provides a way to collect | ||
detailed timing information about HTTP requests without requiring changes to your application code. | ||
HTTP client latency telemetry integrates with the existing IHttpClientFactory system to: | ||
|
||
* Collect timing data for different stages of HTTP requests | ||
* Track HTTP protocol information used for requests | ||
* Measure garbage collection impact during HTTP operations (on .NET platforms that support it) | ||
* Provide consistent telemetry data for performance analysis | ||
|
||
### [.NET CLI](#tab/dotnet-cli) | ||
|
||
```dotnetcli | ||
dotnet add package Microsoft.Extensions.Http.Diagnostics --version 10.0.0 | ||
``` | ||
|
||
### [PackageReference](#tab/package-reference) | ||
|
||
```xml | ||
<PackageReference Include="Microsoft.Extensions.Http.Diagnostics" Version="10.0.0" /> | ||
``` | ||
|
||
--- | ||
|
||
For more information, see [dotnet package add](../tools/dotnet-package-add.md) or [Manage package dependencies in .NET applications](../tools/dependencies.md). | ||
|
||
### Register HTTP client latency telemetry | ||
|
||
To add HTTP client latency telemetry to your application, call the `AddHttpClientLatencyTelemetry` extension method when configuring your services: | ||
|
||
```csharp | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add HTTP client factory | ||
builder.Services.AddHttpClient(); | ||
|
||
// Add HTTP client latency telemetry | ||
builder.Services.AddHttpClientLatencyTelemetry(); | ||
``` | ||
|
||
This registration adds a DelegatingHandler to all HTTP clients created through IHttpClientFactory, collecting detailed latency information for each request. | ||
|
||
### Configure telemetry options | ||
|
||
You can customize how telemetry is collected by configuring the `HttpClientLatencyTelemetryOptions`: | ||
|
||
```csharp | ||
// Configure with delegate | ||
builder.Services.AddHttpClientLatencyTelemetry(options => | ||
{ | ||
options.EnableDetailedLatencyBreakdown = true; | ||
}); | ||
|
||
// Or configure from configuration | ||
builder.Services.AddHttpClientLatencyTelemetry( | ||
builder.Configuration.GetSection("HttpClientTelemetry")); | ||
``` | ||
|
||
### Configuration options | ||
|
||
The `HttpClientLatencyTelemetryOptions` class offers the following settings: | ||
|
||
### Collected telemetry data | ||
|
||
When HTTP client latency telemetry is enabled, the following information is collected: | ||
|
||
#### Timing checkpoints | ||
|
||
Timestamps are recorded for key stages of the HTTP request lifecycle: | ||
|
||
* DNS resolution (Http.NameResolutionStart, Http.NameResolutionEnd) | ||
* Socket connection (Http.SocketConnectStart, Http.SocketConnectEnd) | ||
* Connection establishment (Http.ConnectionEstablished) | ||
* Request headers (Http.RequestHeadersStart, Http.RequestHeadersEnd) | ||
* Request content (Http.RequestContentStart, Http.RequestContentEnd) | ||
* Response headers (Http.ResponseHeadersStart, Http.ResponseHeadersEnd) | ||
* Response content (Http.ResponseContentStart, Http.ResponseContentEnd) | ||
|
||
#### Measures | ||
|
||
On supported platforms: | ||
|
||
* Http.GCPauseTime - Records garbage collection pause duration during HTTP operations | ||
* Http.ConnectionInitiated - Indicates when a new connection is established | ||
|
||
#### Tags | ||
|
||
* Http.Version - Records the HTTP protocol version used for the request | ||
|
||
### Accessing telemetry data | ||
|
||
The collected telemetry data can be accessed through the standard `ILatencyContextAccessor`: | ||
|
||
```csharp | ||
public class ApiService | ||
{ | ||
private readonly IHttpClientFactory _httpClientFactory; | ||
private readonly ILatencyContextAccessor _latencyContextAccessor; | ||
|
||
public ApiService( | ||
IHttpClientFactory httpClientFactory, | ||
ILatencyContextAccessor latencyContextAccessor) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
_latencyContextAccessor = latencyContextAccessor; | ||
} | ||
|
||
public async Task<object> GetDataAsync() | ||
{ | ||
var client = _httpClientFactory.CreateClient(); | ||
var response = await client.GetAsync("https://api.example.com/data"); | ||
|
||
// Access latency information | ||
var context = _latencyContextAccessor.Current; | ||
if (context != null) | ||
{ | ||
// Calculate DNS resolution time | ||
var dnsStart = context.GetCheckpoint(HttpCheckpoints.NameResolutionStart); | ||
var dnsEnd = context.GetCheckpoint(HttpCheckpoints.NameResolutionEnd); | ||
if (dnsStart != null && dnsEnd != null) | ||
{ | ||
var dnsTime = dnsEnd.Value - dnsStart.Value; | ||
Console.WriteLine($"DNS resolution took: {dnsTime.TotalMilliseconds}ms"); | ||
} | ||
|
||
// Get HTTP version used | ||
var httpVersion = context.GetTag(HttpTags.HttpVersion); | ||
if (httpVersion != null) | ||
{ | ||
Console.WriteLine($"HTTP protocol version: {httpVersion}"); | ||
} | ||
} | ||
|
||
return await response.Content.ReadFromJsonAsync<object>(); | ||
} | ||
} | ||
``` | ||
|
||
### Platform considerations | ||
|
||
HTTP client latency telemetry works across all supported .NET platforms with the following considerations: | ||
|
||
* Core timing metrics are available on all platforms (.NET 9, .NET 8, .NET Standard 2.0, .NET Framework 4.6.2) | ||
* Garbage collection metrics (Http.GCPauseTime) are only available on .NET 8 and .NET 9 | ||
* The implementation automatically adapts to the capabilities of the target platform |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
passive voice tends to be less clear