OpenTelemetry.Instrumentation.Http
1.13.0
Prefix Reserved
dotnet add package OpenTelemetry.Instrumentation.Http --version 1.13.0
NuGet\Install-Package OpenTelemetry.Instrumentation.Http -Version 1.13.0
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.13.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.13.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
paket add OpenTelemetry.Instrumentation.Http --version 1.13.0
#r "nuget: OpenTelemetry.Instrumentation.Http, 1.13.0"
#:package OpenTelemetry.Instrumentation.Http@1.13.0
#addin nuget:?package=OpenTelemetry.Instrumentation.Http&version=1.13.0
#tool nuget:?package=OpenTelemetry.Instrumentation.Http&version=1.13.0
HttpClient and HttpWebRequest instrumentation for OpenTelemetry
Status | |
---|---|
Stability | Stable |
Code Owners | @open-telemetry/dotnet-contrib-maintainers |
This is an Instrumentation Library, which instruments System.Net.Http.HttpClient and System.Net.HttpWebRequest and collects metrics and traces about outgoing HTTP requests.
This component is based on the v1.23 of http semantic conventions. For details on the default set of attributes that are added, checkout Traces and Metrics sections below.
Steps to enable OpenTelemetry.Instrumentation.Http
Step 1: Install Package
Add a reference to the
OpenTelemetry.Instrumentation.Http
package. Also, add any other instrumentations & exporters you will need.
dotnet add package OpenTelemetry.Instrumentation.Http
Step 2: Enable HTTP Instrumentation at application startup
HTTP instrumentation must be enabled at application startup.
Traces
Starting with .NET 9, trace instrumentation is natively implemented, and the HttpClient library emits attributes defined in the OpenTelemetry Specification. When running on .NET 9+ this instrumentation library will not add/change/override any attributes set by the native instrumentation but it is still required for performing context propagation using the OpenTelemetry SDK and supports additional features not available in runtime (enrichment, filtering, etc.).
The following example demonstrates adding HttpClient
instrumentation with the
extension method .AddHttpClientInstrumentation()
on TracerProviderBuilder
to
a console application. This example also sets up the OpenTelemetry Console
Exporter, which requires adding the package
OpenTelemetry.Exporter.Console
to the application.
using OpenTelemetry;
using OpenTelemetry.Trace;
public class Program
{
public static void Main(string[] args)
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.Build();
}
}
Following list of attributes are added by default on activity. See http-spans for more details about each individual attribute:
error.type
http.request.method
http.request.method_original
http.response.status_code
network.protocol.version
server.address
server.port
url.full
- By default, the values in the query component of the url are replaced with the textRedacted
. For example,?key1=value1&key2=value2
becomes?key1=Redacted&key2=Redacted
. You can disable this redaction by setting the environment variableOTEL_DOTNET_EXPERIMENTAL_HTTPCLIENT_DISABLE_URL_QUERY_REDACTION
totrue
.
Enrich Api can be used if any additional attributes are required on activity.
Metrics
The following example demonstrates adding HttpClient
instrumentation with the
extension method .AddHttpClientInstrumentation()
on MeterProviderBuilder
to
a console application. This example also sets up the OpenTelemetry Console
Exporter, which requires adding the package
OpenTelemetry.Exporter.Console
to the application.
using OpenTelemetry;
using OpenTelemetry.Metrics;
public class Program
{
public static void Main(string[] args)
{
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddHttpClientInstrumentation()
.AddConsoleExporter()
.Build();
}
}
Refer to this example to see how to enable this instrumentation in an ASP.NET Core application.
Refer to this example to see how to enable this instrumentation in an ASP.NET application.
Following list of attributes are added by default on
http.client.request.duration
metric. See
http-metrics
for more details about each individual attribute. .NET8.0
and above supports
additional metrics, see list of metrics produced for
more details.
error.type
http.request.method
http.response.status_code
network.protocol.version
server.address
server.port
url.scheme
List of metrics produced
When the application targets NETFRAMEWORK
, .NET6.0
or .NET7.0
, the
instrumentation emits the following metric:
Name | Details |
---|---|
http.client.request.duration |
Specification |
Starting from .NET8.0
, metrics instrumentation is natively implemented, and
the HttpClient library has incorporated support for built-in
metrics
following the OpenTelemetry semantic conventions. The library includes additional
metrics beyond those defined in the
specification,
covering additional scenarios for HttpClient users. When the application targets
.NET8.0
and newer versions, the instrumentation library automatically enables
all built-in
metrics by default.
Note that the AddHttpClientInstrumentation()
extension simplifies the process
of enabling all built-in metrics via a single line of code. Alternatively, for
more granular control over emitted metrics, you can utilize the AddMeter()
extension on MeterProviderBuilder
for meters listed in
built-in-metrics-system-net.
Using AddMeter()
for metrics activation eliminates the need to take dependency
on the instrumentation library package and calling
AddHttpClientInstrumentation()
.
If you utilize AddHttpClientInstrumentation()
and wish to exclude unnecessary
metrics, you can utilize
Views
to achieve this.
There is no difference in features or emitted metrics when enabling metrics
using AddMeter()
or AddHttpClientInstrumentation()
on .NET8.0
and newer
versions.
The http.client.request.duration
metric is emitted in seconds
as per the
semantic convention. While the convention recommends using custom histogram
buckets
, this feature is not yet available via .NET Metrics API. A
workaround
has been included in OTel SDK starting version 1.6.0
which applies recommended
buckets by default for http.client.request.duration
. This applies to all
targeted frameworks.
Advanced configuration
Tracing
This instrumentation can be configured to change the default behavior by using
HttpClientTraceInstrumentationOptions
. It is important to note that there are
differences between .NET Framework and newer .NET/.NET Core runtimes which
govern what options are used. On .NET Framework, HttpClient
uses the
HttpWebRequest
API. On .NET & .NET Core, HttpWebRequest
uses the
HttpClient
API. As such, depending on the runtime, only one half of the
"filter" & "enrich" options are used.
.NET & .NET Core
Filter HttpClient API
This instrumentation by default collects all the outgoing HTTP requests. It
allows filtering of requests by using the FilterHttpRequestMessage
function
option. This defines the condition for allowable requests. The filter function
receives the request object (HttpRequestMessage
) representing the outgoing
request and does not collect telemetry about the request if the filter function
returns false
or throws an exception.
The following code snippet shows how to use FilterHttpRequestMessage
to only
allow GET requests.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(
// Note: Only called on .NET & .NET Core runtimes.
(options) => options.FilterHttpRequestMessage =
(httpRequestMessage) =>
{
// Example: Only collect telemetry about HTTP GET requests.
return httpRequestMessage.Method.Equals(HttpMethod.Get);
})
.AddConsoleExporter()
.Build();
It is important to note that this FilterHttpRequestMessage
option is specific
to this instrumentation. OpenTelemetry has a concept of a
Sampler,
and the FilterHttpRequestMessage
option does the filtering after the Sampler
is invoked.
Enrich HttpClient API
This instrumentation library provides options that can be used to
enrich the activity with additional information. These actions are called
only when activity.IsAllDataRequested
is true
. It contains the activity
itself (which can be enriched) and the actual raw object.
HttpClientTraceInstrumentationOptions
provides 3 enrich options:
EnrichWithHttpRequestMessage
, EnrichWithHttpResponseMessage
and
EnrichWithException
. These are based on the raw object that is passed in to
the action to enrich the activity.
Example:
using System.Net.Http;
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation((options) =>
{
// Note: Only called on .NET & .NET Core runtimes.
options.EnrichWithHttpRequestMessage = (activity, httpRequestMessage) =>
{
activity.SetTag("requestVersion", httpRequestMessage.Version);
};
// Note: Only called on .NET & .NET Core runtimes.
options.EnrichWithHttpResponseMessage = (activity, httpResponseMessage) =>
{
activity.SetTag("responseVersion", httpResponseMessage.Version);
};
// Note: Called for all runtimes.
options.EnrichWithException = (activity, exception) =>
{
activity.SetTag("stackTrace", exception.StackTrace);
};
})
.Build();
.NET Framework
Filter HttpWebRequest API
This instrumentation by default collects all the outgoing HTTP requests. It
allows filtering of requests by using the FilterHttpWebRequest
function
option. This defines the condition for allowable requests. The filter function
receives the request object (HttpWebRequest
) representing the outgoing request
and does not collect telemetry about the request if the filter function returns
false
or throws an exception.
The following code snippet shows how to use FilterHttpWebRequest
to only allow
GET requests.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(
// Note: Only called on .NET Framework.
(options) => options.FilterHttpWebRequest =
(httpWebRequest) =>
{
// Example: Only collect telemetry about HTTP GET requests.
return httpWebRequest.Method.Equals(HttpMethod.Get.Method);
})
.AddConsoleExporter()
.Build();
It is important to note that this FilterHttpWebRequest
option is specific to
this instrumentation. OpenTelemetry has a concept of a
Sampler,
and the FilterHttpWebRequest
option does the filtering after the Sampler is
invoked.
Enrich HttpWebRequest API
This instrumentation library provides options that can be used to
enrich the activity with additional information. These actions are called
only when activity.IsAllDataRequested
is true
. It contains the activity
itself (which can be enriched) and the actual raw object.
HttpClientTraceInstrumentationOptions
provides 3 enrich options:
EnrichWithHttpWebRequest
, EnrichWithHttpWebResponse
and
EnrichWithException
. These are based on the raw object that is passed in to
the action to enrich the activity.
Example:
using System.Net;
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation((options) =>
{
// Note: Only called on .NET Framework.
options.EnrichWithHttpWebRequest = (activity, httpWebRequest) =>
{
activity.SetTag("requestVersion", httpWebRequest.Version);
};
// Note: Only called on .NET Framework.
options.EnrichWithHttpWebResponse = (activity, httpWebResponse) =>
{
activity.SetTag("responseVersion", httpWebResponse.Version);
};
// Note: Called for all runtimes.
options.EnrichWithException = (activity, exception) =>
{
activity.SetTag("stackTrace", exception.StackTrace);
};
})
.Build();
Processor,
is the general extensibility point to add additional properties to any activity.
The Enrich
option is specific to this instrumentation, and is provided to get
access to raw request, response, and exception objects.
When overriding the default settings provided by instrumentation or adding additional telemetry, it is important to consider the sequence of callbacks.
It is generally recommended to use EnrichWithHttpResponseMessage
or
EnrichWithHttpWebResponse
for any activity enrichment that does not require
access to exceptions or request object in case of .NET Framework, as the
instrumentation library populates all telemetry following the OTel
specification
before this callback. The following is the sequence in which these callbacks are
executed:
- Processor
OnStart
EnrichWithHttpRequestMessage
(.NET) /EnrichWithHttpWebRequest
(.NET Framework)EnrichWithException
bothEnrichWithHttpResponseMessage
(.NET) /EnrichWithHttpWebResponse
(.NET Framework)- Processor
OnEnd
As an example, if you need to override the default DisplayName set by the library you can do so as follows:
.AddHttpClientInstrumentation(o =>
{
o.EnrichWithHttpResponseMessage = (activity, response) =>
{
// .NET only, access request object if needed.
// response.RequestMessage
activity.DisplayName = "CustomDisplayName";
// Overrides the value
activity.SetTag("url.full", "CustomUrl");
// Removes the tag
activity.SetTag("network.protocol.version", null);
};
});
RecordException
This instrumentation automatically sets Activity Status to Error if the Http
StatusCode is >= 400. Additionally, RecordException
feature may be turned on,
to store the exception to the Activity itself as ActivityEvent.
Activity duration and http.client.request.duration metric calculation
Activity.Duration
and http.client.request.duration
values represents the
time the underlying client handler takes to complete the request. Completing the
request includes the time up to reading response headers from the network
stream. It doesn't include the time spent reading the response body.
Troubleshooting
This component uses an EventSource with the name "OpenTelemetry-Instrumentation-Http" for its internal logging. Please refer to SDK troubleshooting for instructions on seeing these internal logs.
References
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- OpenTelemetry.Api.ProviderBuilderExtensions (>= 1.13.1 && < 2.0.0)
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- OpenTelemetry.Api.ProviderBuilderExtensions (>= 1.13.1 && < 2.0.0)
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- OpenTelemetry.Api.ProviderBuilderExtensions (>= 1.13.1 && < 2.0.0)
NuGet packages (340)
Showing the top 5 NuGet packages that depend on OpenTelemetry.Instrumentation.Http:
Package | Downloads |
---|---|
Azure.Monitor.OpenTelemetry.AspNetCore
An OpenTelemetry .NET distro that exports to Azure Monitor |
|
Sitko.Core.App
Sitko.Core is a set of libraries to help build .NET Core applications fast |
|
OpenTelemetry.AutoInstrumentation.Runtime.Managed
Managed components used by the OpenTelemetry.AutoInstrumentation project. |
|
Grafana.OpenTelemetry.Base
Minimal Grafana distribution of OpenTelemetry .NET |
|
Elvia.Telemetry
Common logging/telemetry functionality to used by all services within the Elvia ecosystem. |
GitHub repositories (146)
Showing the top 20 popular GitHub repositories that depend on OpenTelemetry.Instrumentation.Http:
Repository | Stars |
---|---|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
ardalis/CleanArchitecture
Clean Architecture Solution Template: A proven Clean Architecture Template for ASP.NET Core 9
|
|
dotnet/AspNetCore.Docs
Documentation for ASP.NET Core
|
|
JustArchiNET/ArchiSteamFarm
C# application with primary purpose of farming Steam cards from multiple accounts simultaneously.
|
|
dotnet/orleans
Cloud Native application framework for .NET
|
|
dodyg/practical-aspnetcore
Practical samples of ASP.NET Core 10 RC 1, 9, 8.0, 7.0, 6.0, 5.0, 3.1, 2.2, and 2.1,projects you can use. Readme contains explanations on all projects.
|
|
dotnet/eShop
A reference .NET application implementing an eCommerce site
|
|
dotnet/yarp
A toolkit for developing high-performance HTTP reverse proxy applications.
|
|
quartznet/quartznet
Quartz Enterprise Scheduler .NET
|
|
fullstackhero/dotnet-starter-kit
Production Grade Cloud-Ready .NET 9 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
|
|
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
|
|
dotnet/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
|
|
microsoft/fluentui-blazor
Microsoft Fluent UI Blazor components library. For use with ASP.NET Core Blazor applications
|
|
grpc/grpc-dotnet
gRPC for .NET
|
|
oskardudycz/EventSourcing.NetCore
Examples and Tutorials of Event Sourcing in .NET
|
|
open-telemetry/opentelemetry-dotnet
The OpenTelemetry .NET Client
|
|
microsoft/kiota
OpenAPI based HTTP Client code generator
|
|
thomhurst/TUnit
A modern, fast and flexible .NET testing framework
|
|
modelcontextprotocol/csharp-sdk
The official C# SDK for Model Context Protocol servers and clients. Maintained in collaboration with Microsoft.
|
|
JasperFx/marten
.NET Transactional Document DB and Event Store on PostgreSQL
|
Version | Downloads | Last Updated | |
---|---|---|---|
1.13.0 | 1,514 | 10/22/2025 | |
1.12.0 | 16,131,497 | 5/5/2025 | |
1.11.1 | 8,573,929 | 3/5/2025 | |
1.11.0 | 4,406,484 | 1/27/2025 | |
1.10.0 | 5,455,761 | 11/27/2024 | |
1.9.0 | 40,732,802 | 6/17/2024 | |
1.8.1 | 24,462,225 | 4/12/2024 | |
1.8.0 | 482,260 | 4/4/2024 | |
1.7.1 | 4,137,139 | 2/10/2024 | |
1.7.0 | 6,523,348 | 12/14/2023 | |
1.6.0 | 653,573 | 12/14/2023 | |
1.6.0-rc.1 | 259,201 | 12/2/2023 | |
1.6.0-beta.3 | 1,364,380 | 11/17/2023 | |
1.6.0-beta.2 | 745,938 | 10/27/2023 | |
1.5.1-beta.1 | 6,729,181 | 7/21/2023 | |
1.5.0-beta.1 | 2,400,005 | 6/6/2023 | |
1.0.0-rc9.14 | 10,358,989 | 2/24/2023 | |
1.0.0-rc9.13 | 782,728 | 2/11/2023 | |
1.0.0-rc9.12 | 537,271 | 2/2/2023 | |
1.0.0-rc9.11 | 731,823 | 1/9/2023 | |
1.0.0-rc9.10 | 973,121 | 12/12/2022 | |
1.0.0-rc9.9 | 1,630,627 | 11/7/2022 | |
1.0.0-rc9.8 | 493,922 | 10/17/2022 | |
1.0.0-rc9.7 | 295,998 | 9/30/2022 | |
1.0.0-rc9.6 | 1,458,587 | 8/18/2022 | |
1.0.0-rc9.5 | 1,299,708 | 8/3/2022 | |
1.0.0-rc9.4 | 7,109,027 | 6/3/2022 | |
1.0.0-rc9.3 | 2,625,364 | 4/20/2022 | |
1.0.0-rc9.2 | 2,223,599 | 4/13/2022 | |
1.0.0-rc9.1 | 700,731 | 3/30/2022 | |
1.0.0-rc9 | 2,884,001 | 2/3/2022 | |
1.0.0-rc8 | 6,389,085 | 10/8/2021 | |
1.0.0-rc7 | 4,678,027 | 7/13/2021 | |
1.0.0-rc6 | 327,484 | 6/26/2021 | |
1.0.0-rc5 | 614,620 | 6/9/2021 | |
1.0.0-rc4 | 321,638 | 4/23/2021 | |
1.0.0-rc3 | 453,662 | 3/19/2021 | |
1.0.0-rc2 | 749,872 | 1/30/2021 | |
1.0.0-rc1.1 | 482,012 | 11/18/2020 | |
0.8.0-beta.1 | 95,536 | 11/5/2020 | |
0.7.0-beta.1 | 14,285 | 10/16/2020 | |
0.6.0-beta.1 | 132,562 | 9/16/2020 | |
0.5.0-beta.2 | 9,899 | 8/28/2020 | |
0.4.0-beta.2 | 116,392 | 7/25/2020 | |
0.3.0-beta.1 | 879 | 7/23/2020 |