OpenTelemetry.Instrumentation.AspNet
1.13.0-beta.1
Prefix Reserved
dotnet add package OpenTelemetry.Instrumentation.AspNet --version 1.13.0-beta.1
NuGet\Install-Package OpenTelemetry.Instrumentation.AspNet -Version 1.13.0-beta.1
<PackageReference Include="OpenTelemetry.Instrumentation.AspNet" Version="1.13.0-beta.1" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNet" Version="1.13.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNet" />
paket add OpenTelemetry.Instrumentation.AspNet --version 1.13.0-beta.1
#r "nuget: OpenTelemetry.Instrumentation.AspNet, 1.13.0-beta.1"
#:package OpenTelemetry.Instrumentation.AspNet@1.13.0-beta.1
#addin nuget:?package=OpenTelemetry.Instrumentation.AspNet&version=1.13.0-beta.1&prerelease
#tool nuget:?package=OpenTelemetry.Instrumentation.AspNet&version=1.13.0-beta.1&prerelease
ASP.NET Instrumentation for OpenTelemetry
Status | |
---|---|
Stability | Beta |
Code Owners | @open-telemetry/dotnet-contrib-maintainers |
This is an Instrumentation Library, which instruments ASP.NET and collect metrics and traces about incoming web requests.
This package is a pre-release. Until a stable version is released, there can be breaking changes.
Steps to enable OpenTelemetry.Instrumentation.AspNet
Step 1: Install Package
Add a reference to the
OpenTelemetry.Instrumentation.AspNet
package. Also, add any other instrumentations & exporters you will need.
dotnet add package OpenTelemetry.Instrumentation.AspNet
Step 2: Modify Web.config
OpenTelemetry.Instrumentation.AspNet
requires adding an additional HttpModule
to your web server. This additional HttpModule is shipped as part of
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule
which is implicitly brought by OpenTelemetry.Instrumentation.AspNet
. The
following shows changes required to your Web.config
when using IIS web server.
<system.webServer>
<modules>
<add
name="TelemetryHttpModule"
type="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule,
OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule"
preCondition="integratedMode,managedHandler" />
</modules>
</system.webServer>
Step 3: Enable ASP.NET Instrumentation at application startup
ASP.NET instrumentation must be enabled at application startup. This is
typically done in the Global.asax.cs
.
Traces
The following example demonstrates adding ASP.NET instrumentation with the
extension method .AddAspNetInstrumentation()
on TracerProviderBuilder
to
an application. This example also sets up
the OTLP (OpenTelemetry Protocol) exporter, which requires adding the package
OpenTelemetry.Exporter.OpenTelemetryProtocol
to the application.
using OpenTelemetry;
using OpenTelemetry.Trace;
public class WebApiApplication : HttpApplication
{
private TracerProvider tracerProvider;
protected void Application_Start()
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation()
.AddOtlpExporter()
.Build();
}
protected void Application_End()
{
this.tracerProvider?.Dispose();
}
}
Metrics
The following example demonstrates adding ASP.NET instrumentation with the
extension method .AddAspNetInstrumentation()
on MeterProviderBuilder
to
an application. This example also sets up
the OTLP (OpenTelemetry Protocol) exporter, which requires adding the package
OpenTelemetry.Exporter.OpenTelemetryProtocol
to the application.
using OpenTelemetry;
using OpenTelemetry.Metrics;
public class WebApiApplication : HttpApplication
{
private MeterProvider meterProvider;
protected void Application_Start()
{
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddAspNetInstrumentation()
.AddOtlpExporter()
.Build();
}
protected void Application_End()
{
this.meterProvider?.Dispose();
}
}
List of metrics produced
The instrumentation is implemented based on metrics semantic conventions. Currently, the instrumentation supports the following metric.
Name | Instrument Type | Unit | Description |
---|---|---|---|
http.server.request.duration |
Histogram | s |
Duration of HTTP server requests. |
Advanced trace configuration
This instrumentation can be configured to change the default behavior by using
AspNetTraceInstrumentationOptions
, which allows configuring Filter
as explained
below.
Trace Filter
OpenTelemetry has the concept of a
Sampler.
When using OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule
the
url.path
tag is supplied automatically to samplers when telemetry is started
for incoming requests. It is recommended to use a sampler which inspects
url.path
(as opposed to the Filter
option described below) in order to
perform filtering as it will prevent child spans from being created and bypass
data collection for anything NOT recorded by the sampler. The sampler approach
will reduce the impact on the process being instrumented for all filtered
requests.
This instrumentation by default collects all the incoming http requests. It
allows filtering of requests by using the Filter
function in
AspNetTraceInstrumentationOptions
. This defines the condition for allowable
requests. The Filter receives the HttpContextBase
of the incoming request, and
does not collect telemetry about the request if the Filter returns false or
throws exception.
The following code snippet shows how to use Filter
to only allow GET requests.
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation(
(options) => options.Filter =
(httpContext) =>
{
// only collect telemetry about HTTP GET requests
return httpContext.Request.HttpMethod.Equals("GET");
})
.Build();
Trace Enrich
This instrumentation library provides EnrichWithHttpRequest
,
EnrichWithHttpResponse
and EnrichWithException
options that can be used to
enrich the activity with additional information from the raw HttpRequestBase
,
HttpResponseBase
and Exception
objects respectively. These actions are called
only when activity.IsAllDataRequested
is true
. It contains the activity
itself (which can be enriched) and the actual raw object.
The following code snippet shows how to enrich the activity using all 3 different options.
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation(o =>
{
o.EnrichWithHttpRequest = (activity, httpRequest) =>
{
activity.SetTag("physicalPath", httpRequest.PhysicalPath);
};
o.EnrichWithHttpResponse = (activity, httpResponse) =>
{
activity.SetTag("responseType", httpResponse.ContentType);
};
o.EnrichWithException = (activity, exception) =>
{
if (exception.Source != null)
{
activity.SetTag("exception.source", exception.Source);
}
};
})
.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 HttpRequestBase
and HttpResponseBase
.
RecordException
This instrumentation automatically sets Activity Status to Error if an unhandled
exception is thrown. Additionally, RecordException
feature may be turned on,
to store the exception to the Activity itself as ActivityEvent.
Advanced metric configuration
This instrumentation can be configured to change the default behavior by using
AspNetMetricsInstrumentationOptions
as explained below.
Metric Enrich
This option allows one to enrich the metric with additional information from
the HttpContextBase
. The Enrich
action is always called unless the metric was
filtered. The callback allows for modifying the tag list. If the callback
throws an exception the metric will still be recorded.
this.meterProvider = Sdk.CreateMeterProviderBuilder()
.AddAspNetInstrumentation(options => options.Enrich =
(HttpContextBase context, ref TagList tags) =>
{
// Add request content type to the metric tags.
if (!string.IsNullOrEmpty(context.Request.ContentType))
{
tags.Add("custom.content.type", context.Request.ContentType);
}
})
.Build();
References
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 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)
- OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule (>= 1.13.0-beta.1)
NuGet packages (11)
Showing the top 5 NuGet packages that depend on OpenTelemetry.Instrumentation.AspNet:
Package | Downloads |
---|---|
OpenTelemetry.AutoInstrumentation.Runtime.Managed
Managed components used by the OpenTelemetry.AutoInstrumentation project. |
|
Grafana.OpenTelemetry
Full Grafana distribution of OpenTelemetry .NET |
|
Honeycomb.OpenTelemetry.CommonInstrumentations
Honeycomb's OpenTelemetry common instrumentations package. Adds support for many common instrumentation libraries for you. |
|
Splunk.OpenTelemetry.AutoInstrumentation
Splunk Distribution of OpenTelemetry .NET package with all required components to enable automatic instrumentation. |
|
AWS.Distro.OpenTelemetry.AutoInstrumentation
Package Description |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on OpenTelemetry.Instrumentation.AspNet:
Repository | Stars |
---|---|
dotnet/systemweb-adapters
|
Version | Downloads | Last Updated | |
---|---|---|---|
1.13.0-beta.1 | 845 | 10/15/2025 | |
1.12.0-beta.2 | 8,122 | 9/18/2025 | |
1.12.0-beta.1 | 69,570 | 5/5/2025 | |
1.11.0-beta.2 | 65,401 | 3/5/2025 | |
1.11.0-beta.1 | 42,485 | 1/27/2025 | |
1.10.0-beta.1 | 59,976 | 12/9/2024 | |
1.9.0-beta.1 | 497,488 | 6/18/2024 | |
1.8.0-beta.3 | 153,298 | 5/23/2024 | |
1.8.0-beta.2 | 71,774 | 4/17/2024 | |
1.8.0-beta.1 | 34,236 | 4/5/2024 | |
1.7.0-beta.2 | 123,049 | 2/7/2024 | |
1.7.0-beta.1 | 66,065 | 12/20/2023 | |
1.6.0-beta.2 | 133,884 | 11/6/2023 | |
1.6.0-beta.1 | 31,018 | 10/12/2023 | |
1.0.0-rc9.9 | 229,713 | 6/9/2023 | |
1.0.0-rc9.8 | 272,369 | 2/28/2023 | |
1.0.0-rc9.7 | 141,877 | 11/28/2022 | |
1.0.0-rc9.6 | 64,735 | 9/28/2022 | |
1.0.0-rc9.5 | 107,347 | 6/21/2022 | |
1.0.0-rc9.4 | 62,442 | 6/3/2022 | |
1.0.0-rc9.3 | 10,569 | 4/20/2022 | |
1.0.0-rc9.2 | 28,515 | 4/13/2022 | |
1.0.0-rc9.1 | 83,491 | 3/30/2022 | |
1.0.0-rc9 | 98,440 | 2/3/2022 | |
1.0.0-rc8 | 94,681 | 10/8/2021 | |
1.0.0-rc7 | 142,857 | 7/13/2021 | |
1.0.0-rc6 | 58,433 | 6/26/2021 | |
1.0.0-rc5 | 6,047 | 6/9/2021 | |
1.0.0-rc4 | 11,746 | 4/23/2021 | |
1.0.0-rc3 | 59,412 | 3/19/2021 | |
1.0.0-rc2 | 25,530 | 1/30/2021 | |
1.0.0-rc1.1 | 15,813 | 11/18/2020 | |
0.8.0-beta.1 | 530 | 11/5/2020 | |
0.7.0-beta.1 | 511 | 10/16/2020 | |
0.6.0-beta.1 | 34,942 | 9/16/2020 | |
0.5.0-beta.2 | 531 | 8/28/2020 | |
0.4.0-beta.2 | 1,360 | 7/25/2020 | |
0.3.0-beta.1 | 532 | 7/23/2020 |