OpenTelemetry.Extensions.Hosting 1.11.0-rc.1
OpenTelemetry.Extensions.Hosting
Installation
dotnet add package OpenTelemetry.Extensions.Hosting
Overview
The OpenTelemetry.Extensions.Hosting package provides extension methods for
automatically starting (and stopping) OpenTelemetry tracing (TracerProvider
)
and metrics (MeterProvider
) in ASP.NET
Core and
.NET Generic
hosts. These are completely optional extensions meant to simplify the
management of the OpenTelemetry SDK lifecycle.
Extension method reference
Targeting Microsoft.Extensions.DependencyInjection.IServiceCollection
:
AddOpenTelemetry
: Registers an IHostedService to automatically start tracing and/or metric services in the supplied IServiceCollection and then returns anOpenTelemetryBuilder
class.[!NOTE]
AddOpenTelemetry
should be called by application host code only. Library authors see: Registration extension method guidance for library authors.[!NOTE] Multiple calls to
AddOpenTelemetry
will NOT result in multiple providers. Only a singleTracerProvider
and/orMeterProvider
will be created in the targetIServiceCollection
. To establish multiple providers use theSdk.CreateTracerProviderBuilder()
and/orSdk.CreateMeterProviderBuilder()
methods. See TracerProvider configuration and Building a MeterProvider for more details.OpenTelemetryBuilder
methods:ConfigureResource
: Registers a callback action to configure theResourceBuilder
for tracing and metric providers.WithTracing
: Enables tracing and optionally configures theTracerProvider
.WithMetrics
: Enables metrics and optionally configures theMeterProvider
.
Usage
The following example shows how to register OpenTelemetry tracing & metrics in an ASP.NET Core host using the OpenTelemetry.Extensions.Hosting extensions.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
var appBuilder = WebApplication.CreateBuilder(args);
appBuilder.Services.AddOpenTelemetry()
.ConfigureResource(builder => builder.AddService(serviceName: "MyService"))
.WithTracing(builder => builder.AddConsoleExporter())
.WithMetrics(builder => builder.AddConsoleExporter());
var app = appBuilder.Build();
app.Run();
A fully functional example can be found here.
Resources
To dynamically add resources at startup from the dependency injection you can
provide an IResourceDetector
.
To make use of it add it to the dependency injection and then you can use the
IServiceProvider
to add it to OpenTelemetry:
public class MyResourceDetector : IResourceDetector
{
private readonly IWebHostEnvironment webHostEnvironment;
public MyResourceDetector(IWebHostEnvironment webHostEnvironment)
{
this.webHostEnvironment = webHostEnvironment;
}
public Resource Detect()
{
return ResourceBuilder.CreateEmpty()
.AddService(serviceName: this.webHostEnvironment.ApplicationName)
.AddAttributes(new Dictionary<string, object> { ["host.environment"] = this.webHostEnvironment.EnvironmentName })
.Build();
}
}
services.AddSingleton<MyResourceDetector>();
services.AddOpenTelemetry()
.ConfigureResource(builder =>
builder.AddDetector(sp => sp.GetRequiredService<MyResourceDetector>()))
.WithTracing(builder => builder.AddConsoleExporter())
.WithMetrics(builder => builder.AddConsoleExporter());
Migrating from pre-release versions of OpenTelemetry.Extensions.Hosting
Pre-release versions (all versions prior to 1.4.0) of
OpenTelemetry.Extensions.Hosting
contained signal-specific methods for
configuring tracing and metrics:
AddOpenTelemetryTracing
: Configure OpenTelemetry and register an IHostedService to automatically start tracing services in the supplied IServiceCollection.AddOpenTelemetryMetrics
: Configure OpenTelemetry and register an IHostedService to automatically start metric services in the supplied IServiceCollection.
These methods were marked obsolete and later removed. You should migrate your
code to the new AddOpenTelemetry
method documented above. Refer the
old
and
new
versions of the example application to assist you in your migration.
Hosted Service Ordering and Telemetry Capture
TBD
References
Showing the top 20 packages that depend on OpenTelemetry.Extensions.Hosting.
Packages | Downloads |
---|---|
Honeycomb.OpenTelemetry
Honeycomb's OpenTelemetry SDK
|
4 |
Honeycomb.OpenTelemetry
Honeycomb's OpenTelemetry SDK
|
7 |
Honeycomb.OpenTelemetry
Honeycomb's OpenTelemetry SDK
|
8 |
Honeycomb.OpenTelemetry
Honeycomb's OpenTelemetry SDK
|
9 |
Honeycomb.OpenTelemetry
Honeycomb's OpenTelemetry SDK
|
10 |
.NET Framework 4.6.2
- OpenTelemetry (>= 1.11.0-rc.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
.NET 8.0
- OpenTelemetry (>= 1.11.0-rc.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
.NET 9.0
- OpenTelemetry (>= 1.11.0-rc.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
.NET Standard 2.0
- OpenTelemetry (>= 1.11.0-rc.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
Version | Downloads | Last updated |
---|---|---|
1.11.0-rc.1 | 2 | 12/12/2024 |
1.10.0 | 3 | 11/14/2024 |
1.10.0-rc.1 | 3 | 12/04/2024 |
1.10.0-beta.1 | 3 | 12/08/2024 |
1.9.0 | 5 | 12/02/2024 |
1.9.0-rc.1 | 3 | 12/02/2024 |
1.9.0-alpha.1 | 8 | 05/28/2024 |
1.8.1 | 9 | 06/01/2024 |
1.8.0 | 6 | 06/01/2024 |
1.8.0-rc.1 | 7 | 05/28/2024 |
1.8.0-beta.1 | 5 | 05/28/2024 |
1.7.0 | 8 | 06/01/2024 |
1.7.0-rc.1 | 3 | 05/28/2024 |
1.7.0-alpha.1 | 4 | 05/28/2024 |
1.6.0 | 4 | 12/02/2024 |
1.6.0-rc.1 | 7 | 05/28/2024 |
1.6.0-alpha.1 | 7 | 05/28/2024 |
1.5.1 | 9 | 07/20/2023 |
1.5.0 | 6 | 06/01/2024 |
1.5.0-rc.1 | 7 | 05/28/2024 |
1.5.0-alpha.2 | 3 | 05/28/2024 |
1.5.0-alpha.1 | 4 | 05/28/2024 |
1.4.0 | 7 | 06/01/2024 |
1.4.0-rc.4 | 8 | 05/28/2024 |
1.4.0-rc.3 | 5 | 05/28/2024 |
1.4.0-rc.2 | 7 | 05/28/2024 |
1.4.0-rc.1 | 5 | 05/28/2024 |
1.0.0-rc9.9 | 10 | 05/28/2024 |
1.0.0-rc9.8 | 10 | 05/28/2024 |
1.0.0-rc9.7 | 7 | 05/28/2024 |
1.0.0-rc9.6 | 6 | 05/28/2024 |
1.0.0-rc9.5 | 6 | 05/28/2024 |
1.0.0-rc9.4 | 8 | 05/28/2024 |
1.0.0-rc9.3 | 8 | 05/28/2024 |
1.0.0-rc9.2 | 5 | 05/28/2024 |
1.0.0-rc9.1 | 10 | 05/28/2024 |
1.0.0-rc9 | 6 | 05/28/2024 |
1.0.0-rc8 | 4 | 05/28/2024 |
1.0.0-rc7 | 5 | 12/04/2024 |
1.0.0-rc6 | 5 | 05/28/2024 |
1.0.0-rc5 | 8 | 05/28/2024 |
1.0.0-rc4 | 7 | 05/28/2024 |
1.0.0-rc3 | 9 | 05/28/2024 |
1.0.0-rc2 | 8 | 05/28/2024 |
1.0.0-rc10 | 6 | 05/28/2024 |
1.0.0-rc1.1 | 4 | 05/28/2024 |
0.8.0-beta.1 | 6 | 05/28/2024 |
0.7.0-beta.1 | 8 | 05/28/2024 |
0.6.0-beta.1 | 8 | 05/16/2023 |
0.5.0-beta.2 | 5 | 05/28/2024 |
0.4.0-beta.2 | 4 | 05/28/2024 |
0.3.0-beta.1 | 2 | 05/28/2024 |
0.2.0-alpha.275 | 5 | 05/28/2024 |