Contains extensions to start OpenTelemetry in applications using Microsoft.Extensions.Hosting
$ dotnet add package OpenTelemetry.Extensions.Hostingdotnet add package OpenTelemetry.Extensions.Hosting
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.
Targeting Microsoft.Extensions.DependencyInjection.IServiceCollection:
AddOpenTelemetry: Registers an
IHostedService
to automatically start tracing and/or metric services in the supplied
IServiceCollection
and then returns an OpenTelemetryBuilder class.
<!-- This comment is to make sure the two notes above and below are not merged -->[!NOTE]
AddOpenTelemetryshould be called by application host code only. Library authors see: Registration extension method guidance for library authors.
[!NOTE] Multiple calls to
AddOpenTelemetrywill result in multiple providers. Only a single and/or will be created in the target . To establish multiple providers use the and/or methods. See and for more details.
TracerProviderMeterProviderIServiceCollectionSdk.CreateTracerProviderBuilder()Sdk.CreateMeterProviderBuilder()OpenTelemetryBuilder methods:
ConfigureResource: Registers a callback action to configure the
ResourceBuilder for tracing and metric providers.
WithTracing: Enables tracing and optionally configures the
TracerProvider.
WithMetrics: Enables metrics and optionally configures the
MeterProvider.
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.
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());
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.
TBD