OpenTelemetry.Extensions.Hosting 1.12.0

OpenTelemetry.Extensions.Hosting

NuGet NuGet

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 an OpenTelemetryBuilder 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 single TracerProvider and/or MeterProvider will be created in the target IServiceCollection. To establish multiple providers use the Sdk.CreateTracerProviderBuilder() and/or Sdk.CreateMeterProviderBuilder() methods. See TracerProvider configuration and Building a MeterProvider for more details.

    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.

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:

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
Aspire.Confluent.Kafka
Confluent.Kafka based Kafka generic consumer and producer that integrates with Aspire, including healthchecks and metrics.
7
Aspire.NATS.Net
A NATS client that integrates with Aspire, including health checks and logging.
7
Aspire.MongoDB.Driver
A generic MongoDB client that integrates with Aspire.
7
Aspire.StackExchange.Redis.DistributedCaching
A Redis® implementation for IDistributedCache that integrates with Aspire, including health checks, logging, and telemetry.
5
Aspire.Confluent.Kafka
Confluent.Kafka based Kafka generic consumer and producer that integrates with Aspire, including healthchecks and metrics.
5
Aspire.StackExchange.Redis
A generic Redis® client that integrates with Aspire, including health checks, logging, and telemetry.
4
Aspire.Confluent.Kafka
Confluent.Kafka based Kafka generic consumer and producer that integrates with Aspire, including healthchecks and metrics.
4
Aspire.NATS.Net
A NATS client that integrates with Aspire, including health checks and logging.
4
Aspire.MongoDB.Driver
A generic MongoDB client that integrates with Aspire.
4
Aspire.MongoDB.Driver
A generic MongoDB client that integrates with Aspire.
3

For highlights and announcements see: https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.12.0/RELEASENOTES.md. For detailed changes see: https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.12.0/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md.

Version Downloads Last updated
1.12.0 12 05/01/2025
1.11.2 17 03/08/2025
1.11.1 22 01/28/2025
1.11.0 25 01/18/2025
1.11.0-rc.1 25 12/16/2024
1.10.0 26 11/14/2024
1.10.0-rc.1 31 11/03/2024
1.10.0-beta.1 30 10/01/2024
1.9.0 40 06/15/2024
1.9.0-rc.1 35 06/08/2024
1.9.0-alpha.1 34 05/24/2024
1.8.1 42 04/20/2024
1.8.0 46 04/06/2024
1.8.0-rc.1 36 04/03/2024
1.8.0-beta.1 36 03/22/2024
1.7.0 4,155 12/10/2023
1.7.0-rc.1 42 12/06/2023
1.7.0-alpha.1 42 10/24/2023
1.6.0 45 09/07/2023
1.6.0-rc.1 43 08/25/2023
1.6.0-alpha.1 51 08/16/2023
1.5.1 509 06/28/2023
1.5.0 47 08/17/2023
1.5.0-rc.1 39 08/09/2023
1.5.0-alpha.2 41 05/24/2023
1.5.0-alpha.1 41 08/06/2023
1.4.0 49 08/06/2023
1.4.0-rc.4 40 08/10/2023
1.4.0-rc.3 44 03/29/2023
1.4.0-rc.2 44 06/04/2023
1.4.0-rc.1 36 08/15/2023
1.0.0-rc9.9 42 08/18/2023
1.0.0-rc9.8 42 05/07/2023
1.0.0-rc9.7 42 10/17/2022
1.0.0-rc9.6 40 08/11/2023
1.0.0-rc9.5 44 05/26/2023
1.0.0-rc9.4 42 05/24/2023
1.0.0-rc9.3 45 06/15/2022
1.0.0-rc9.2 45 08/25/2023
1.0.0-rc9.1 46 08/15/2023
1.0.0-rc9 44 07/30/2023
1.0.0-rc8 47 08/21/2023
1.0.0-rc7 42 08/25/2023
1.0.0-rc6 43 11/19/2022
1.0.0-rc5 44 07/04/2023
1.0.0-rc4 45 08/24/2023
1.0.0-rc3 40 08/22/2023
1.0.0-rc2 42 08/22/2023
1.0.0-rc10 41 08/24/2023
1.0.0-rc1.1 43 08/05/2023
0.8.0-beta.1 41 08/06/2023
0.7.0-beta.1 54 11/24/2022
0.6.0-beta.1 40 09/24/2023
0.5.0-beta.2 40 03/02/2023
0.4.0-beta.2 44 08/05/2023
0.3.0-beta.1 37 08/08/2023
0.2.0-alpha.275 50 08/07/2023