Exporters

You are viewing the English version of this page because it has not yet been fully translated. Interested in helping out? See Contributing.

OpenTelemetryコレクターにテレメトリーを送信し、正しくエクスポートされることを確認してください。 本番環境でコレクターを使用することはベストプラクティスです。 テレメトリーを可視化するために、JaegerZipkinPrometheus、またはベンダー固有のようなバックエンドにエクスポートしてください。

使用可能なエクスポーター

レジストリには、.NET 用のエクスポーターのリストが含まれています。

エクスポーターの中でも、OpenTelemetry Protocol (OTLP)エクスポーターは、OpenTelemetryのデータモデルを考慮して設計されており、OTelデータを情報の損失なく出力します。 さらに、多くのテレメトリーデータを扱うツールがOTLPに対応しており(たとえば、PrometheusJaegerやほとんどのベンダー)、必要なときに高い柔軟性を提供します。 OTLPについて詳細に学習したい場合は、OTLP仕様を参照してください。

このページでは、主要なOpenTelemetry .NET エクスポーターとその設定方法について説明します。

OTLP

コレクターのセットアップ

OTLPエクスポーターを試し、検証するために、テレメトリーを直接コンソールに書き込むDockerコンテナでコレクターを実行できます。 空のディレクトリで、以下の内容でcollector-config.yamlというファイルを作成します。

receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318
exporters:
  debug:
    verbosity: detailed
service:
  pipelines:
    traces:
      receivers: [otlp]
      exporters: [debug]
    metrics:
      receivers: [otlp]
      exporters: [debug]
    logs:
      receivers: [otlp]
      exporters: [debug]

次に、Docker コンテナでコレクターを実行します。

docker run -p 4317:4317 -p 4318:4318 --rm -v $(pwd)/collector-config.yaml:/etc/otelcol/config.yaml otel/opentelemetry-collector

このコレクターは、OTLPを介してテレメトリーを受け取ることができるようになりました。後で、テレメトリーを監視バックエンドに送信するためにコレクターを設定することもできます。

Dependencies

If you want to send telemetry data to an OTLP endpoint (like the OpenTelemetry Collector, Jaeger or Prometheus), you can choose between two different protocols to transport your data:

  • HTTP/protobuf
  • gRPC

Start by installing the OpenTelemetry.Exporter.OpenTelemetryProtocol package as a dependency for your project:

dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol

If you’re using ASP.NET Core install the OpenTelemetry.Extensions.Hosting package as well:

dotnet add package OpenTelemetry.Extensions.Hosting

Usage

ASP.NET Core

Configure the exporters in your ASP.NET Core services:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithTracing(tracing => tracing
        // The rest of your setup code goes here
        .AddOtlpExporter())
    .WithMetrics(metrics => metrics
        // The rest of your setup code goes here
        .AddOtlpExporter());

builder.Logging.AddOpenTelemetry(logging => {
    // The rest of your setup code goes here
    logging.AddOtlpExporter();
});

This will, by default, send telemetry using gRPC to http://localhost:4317, to customize this to use HTTP and the protobuf format, you can add options like this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithTracing(tracing => tracing
        // The rest of your setup code goes here
        .AddOtlpExporter(options =>
        {
            options.Endpoint = new Uri("your-endpoint-here/v1/traces");
            options.Protocol = OtlpExportProtocol.HttpProtobuf;
        }))
    .WithMetrics(metrics => metrics
        // The rest of your setup code goes here
        .AddOtlpExporter(options =>
        {
            options.Endpoint = new Uri("your-endpoint-here/v1/metrics");
            options.Protocol = OtlpExportProtocol.HttpProtobuf;
        }));

builder.Logging.AddOpenTelemetry(logging => {
    // The rest of your setup code goes here
    logging.AddOtlpExporter(options =>
    {
        options.Endpoint = new Uri("your-endpoint-here/v1/logs");
        options.Protocol = OtlpExportProtocol.HttpProtobuf;
    });
});

Non-ASP.NET Core

Configure the exporter when creating a TracerProvider, MeterProvider or LoggerFactory:

var tracerProvider = Sdk.CreateTracerProviderBuilder()
    // Other setup code, like setting a resource goes here too
    .AddOtlpExporter(options =>
    {
        options.Endpoint = new Uri("your-endpoint-here/v1/traces");
        options.Protocol = OtlpExportProtocol.HttpProtobuf;
    })
    .Build();

var meterProvider = Sdk.CreateMeterProviderBuilder()
    // Other setup code, like setting a resource goes here too
    .AddOtlpExporter(options =>
    {
        options.Endpoint = new Uri("your-endpoint-here/v1/metrics");
        options.Protocol = OtlpExportProtocol.HttpProtobuf;
    })
    .Build();

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddOpenTelemetry(logging =>
    {
        logging.AddOtlpExporter(options =>
        {
            options.Endpoint = new Uri("your-endpoint-here/v1/logs");
            options.Protocol = OtlpExportProtocol.HttpProtobuf;
        })
    });
});

Use environment variables to set values like headers and an endpoint URL for production.

Console

Dependencies

The console exporter is useful for development and debugging tasks, and is the simplest to set up. Start by installing the OpenTelemetry.Exporter.Console package as a dependency for your project:

dotnet add package OpenTelemetry.Exporter.Console

If you’re using ASP.NET Core install the OpenTelemetry.Extensions.Hosting package as well:

dotnet add package OpenTelemetry.Extensions.Hosting

Usage

ASP.NET Core

Configure the exporter in your ASP.NET Core services:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithTracing(tracing => tracing
        // The rest of your setup code goes here
        .AddConsoleExporter()
    )
    .WithMetrics(metrics => metrics
        // The rest of your setup code goes here
        .AddConsoleExporter()
    );

builder.Logging.AddOpenTelemetry(logging => {
    // The rest of your setup code goes here
    logging.AddConsoleExporter();
});

Non-ASP.NET Core

Configure the exporter when creating a TracerProvider, MeterProvider or LoggerFactory:

var tracerProvider = Sdk.CreateTracerProviderBuilder()
    // The rest of your setup code goes here
    .AddConsoleExporter()
    .Build();

var meterProvider = Sdk.CreateMeterProviderBuilder()
    // The rest of your setup code goes here
    .AddConsoleExporter()
    .Build();

var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddOpenTelemetry(logging =>
    {
        logging.AddConsoleExporter();
    });
});

Jaeger

バックエンドのセットアップ

Jaegerは、トレースデータを受信するためにOTLPをネイティブでサポートしています。UIがポート16686でアクセス可能で、OTLPがポート4317と4318で有効になったDockerコンテナでJaegerを実行できます。

docker run --rm \
  -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
  -p 16686:16686 \
  -p 4317:4317 \
  -p 4318:4318 \
  -p 9411:9411 \
  jaegertracing/all-in-one:latest

使用方法

OTLPエクスポーターをセットアップするための手順に従ってください。

Prometheus

メトリクスデータをPrometheusに送信するには、PrometheusのOTLPレシーバーを有効にしてOTLPエクスポーターを使用するか、Prometheusエクスポーターを使用できます。 Prometheusエクスポーターは、メトリクスを収集しリクエストに応じてPrometheusテキスト形式にシリアライズするHTTPサーバーを起動するMetricReaderです。

バックエンドのセットアップ

PrometheusをDockerコンテナで実行し、ポート9090でアクセスできるようにするには、以下の手順に従ってください。

以下の内容でprometheus.ymlというファイルを作成します。

scrape_configs:
  - job_name: dice-service
    scrape_interval: 5s
    static_configs:
      - targets: [host.docker.internal:9464]

UIがポート9090でアクセス可能なDockerコンテナでPrometheusを実行します。

docker run --rm -v ${PWD}/prometheus.yml:/prometheus/prometheus.yml -p 9090:9090 prom/prometheus --web.enable-otlp-receiver

The following sections provide detailed, .NET-specific instructions for configuring the Prometheus exporter.

There are two approaches for exporting metrics to Prometheus:

  1. Using OTLP Exporter (Push): Push metrics to Prometheus using the OTLP protocol. This requires Prometheus’ OTLP Receiver to be enabled. This is the recommended approach for production environments as it supports exemplars and is stable.

  2. Using Prometheus Exporter (Pull/Scrape): Expose a scraping endpoint in your application that Prometheus can scrape. This is the traditional Prometheus approach.

Using OTLP Exporter (Push)

This approach uses the OTLP exporter to push metrics directly to Prometheus' OTLP receiver endpoint. This is recommended for production environments because it supports exemplars and uses the stable OTLP protocol.

Dependencies

Install the OpenTelemetry.Exporter.OpenTelemetryProtocol package as a dependency for your project:

dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol

If you’re using ASP.NET Core install the OpenTelemetry.Extensions.Hosting package as well:

dotnet add package OpenTelemetry.Extensions.Hosting
Usage
ASP.NET Core

Configure the OTLP exporter to send metrics to Prometheus OTLP receiver:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics => metrics
        // The rest of your setup code goes here
        .AddOtlpExporter(options =>
        {
            options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
            options.Protocol = OtlpExportProtocol.HttpProtobuf;
        }));
Non-ASP.NET Core

Configure the exporter when creating a MeterProvider:

var meterProvider = Sdk.CreateMeterProviderBuilder()
    // Other setup code, like setting a resource goes here too
    .AddOtlpExporter(options =>
    {
        options.Endpoint = new Uri("http://localhost:9090/api/v1/otlp/v1/metrics");
        options.Protocol = OtlpExportProtocol.HttpProtobuf;
    })
    .Build();

Using Prometheus Exporter (Pull/Scrape)

This approach exposes a metrics endpoint in your application (e.g., /metrics) that Prometheus scrapes at regular intervals.

Dependencies

Install the exporter package as a dependency for your application:

dotnet add package OpenTelemetry.Exporter.Prometheus.AspNetCore --version 1.15.0-beta.1

If you’re using ASP.NET Core install the OpenTelemetry.Extensions.Hosting package as well:

dotnet add package OpenTelemetry.Extensions.Hosting
Usage
ASP.NET Core

Configure the exporter in your ASP.NET Core services:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics => metrics.AddPrometheusExporter());

You’ll then need to register the Prometheus scraping middleware so that Prometheus can scrape your application. Use the UseOpenTelemetryPrometheusScrapingEndpoint extension method on IApplicationBuilder:

var builder = WebApplication.CreateBuilder(args);

// ... Setup

var app = builder.Build();

app.UseOpenTelemetryPrometheusScrapingEndpoint();

await app.RunAsync();

By default, this exposes the metrics endpoint at /metrics. You can customize the endpoint path or use a predicate function for more advanced configuration:

app.UseOpenTelemetryPrometheusScrapingEndpoint(
    context => context.Request.Path == "/internal/metrics"
        && context.Connection.LocalPort == 5067);
Non-ASP.NET Core

For applications not using ASP.NET Core, you can use the HttpListener version which is available in a different package:

dotnet add package OpenTelemetry.Exporter.Prometheus.HttpListener --version 1.15.0-beta.1

Then this is setup directly on the MeterProviderBuilder:

var meterProvider = Sdk.CreateMeterProviderBuilder()
    .AddMeter(MyMeter.Name)
    .AddPrometheusHttpListener(
        options => options.UriPrefixes = new string[] { "http://localhost:9464/" })
    .Build();
Prometheus Configuration (Scrape)

When using the Prometheus exporter (pull/scrape approach), you need to configure Prometheus to scrape your application. Add the following to your prometheus.yml:

scrape_configs:
  - job_name: 'your-app-name'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:5000'] # Your application's host:port

For more details on configuring the Prometheus exporter, see OpenTelemetry.Exporter.Prometheus.AspNetCore.

Zipkin

バックエンドのセットアップ

以下のコマンドを実行して、ZipkinをDockerコンテナで実行できます。

docker run --rm -d -p 9411:9411 --name zipkin openzipkin/zipkin

Dependencies

To send your trace data to Zipkin, install the exporter package as a dependency for your application:

dotnet add package OpenTelemetry.Exporter.Zipkin

If you’re using ASP.NET Core install the OpenTelemetry.Extensions.Hosting package as well:

dotnet add package OpenTelemetry.Extensions.Hosting

Usage

ASP.NET Core

Configure the exporter in your ASP.NET Core services:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithTracing(tracing => tracing
        // The rest of your setup code goes here
        .AddZipkinExporter(options =>
        {
            options.Endpoint = new Uri("your-zipkin-uri-here");
        }));

Non-ASP.NET Core

Configure the exporter when creating a tracer provider:

var tracerProvider = Sdk.CreateTracerProviderBuilder()
    // The rest of your setup code goes here
    .AddZipkinExporter(options =>
    {
        options.Endpoint = new Uri("your-zipkin-uri-here");
    })
    .Build();