Argon.Extensions.OpenTelemetry 8.5.0

Argon.Extensions.OpenTelemetry

OpenTelemetry extensions for simplified configuration of metrics, tracing, and logging in .NET applications.

📦 Installation

dotnet add package Argon.Extensions.OpenTelemetry

✨ Features

  • Configuration-based setup: Configure OpenTelemetry using appsettings.json or other configuration sources
  • Conditional enablement: Enable/disable metrics, tracing, and logging independently
  • OTLP Exporter support: Built-in OTLP exporter configuration with customizable options
  • Fluent API extensions: Extension methods for easy integration with OpenTelemetryBuilder, MeterProviderBuilder, TracerProviderBuilder, and LoggerProviderBuilder
  • Metrics reader configuration: Customizable metric reader options for periodic exporting

🚀 Usage

Configuration

Add OpenTelemetry configuration to your appsettings.json:

{
  "OpenTelemetry": {
    "Enabled": true,
    "Exporter": {
      "Endpoint": "http://localhost:4317",
      "Protocol": "Grpc",
      "ExportProcessorType": "Batch",
      "TimeoutMilliseconds": 30000,
      "Headers": "key1=value1,key2=value2",
      "BatchExportProcessorOptions": {
        "MaxQueueSize": 2048,
        "ScheduledDelayMilliseconds": 5000,
        "ExporterTimeoutMilliseconds": 30000,
        "MaxExportBatchSize": 512
      },
      "MetricsReader": {
        "ExportIntervalMilliseconds": 60000,
        "ExportTimeoutMilliseconds": 30000,
        "TemporalityPreference": "Cumulative"
      }
    },
    "Metrics": {
      "Enabled": true
    },
    "Tracing": {
      "Enabled": true
    },
    "Logging": {
      "Enabled": true,
      "IncludeFormattedMessage": true,
      "IncludeScopes": true,
      "ParseStateValues": true
    }
  }
}

Basic Setup with Configuration

using Argon.Extensions.OpenTelemetry;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateDefaultBuilder(args);

builder.ConfigureServices((context, services) =>
{
    var otlpConfig = context.Configuration.GetOpenTelemetryConfiguration();
    
    services.AddOpenTelemetry()
        .WithMetrics(otlpConfig, (metrics, config) =>
        {
            metrics.AddMeter("MyApp.*");
            metrics.AddOtlpExporter(config.Exporter!);
        })
        .WithTracing(otlpConfig, (tracing, config) =>
        {
            tracing.AddSource("MyApp.*");
            tracing.AddOtlpExporter(config.Exporter!);
        })
        .WithLogging(otlpConfig, (logging, config) =>
        {
            logging.AddOtlpExporter(config.Exporter!);
        });
});

Logging Setup

using Argon.Extensions.OpenTelemetry;
using Microsoft.Extensions.Logging;

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddArgonOpenTelemetry();

Advanced Configuration

using Argon.Extensions.OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);

var otlpConfig = builder.Configuration.GetOpenTelemetryConfiguration();

builder.Services.AddOpenTelemetry()
    .WithMetrics(otlpConfig, (metrics, config) =>
    {
        metrics
            .AddMeter("MyApp.Metrics")
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddOtlpExporter(config.Exporter!);
    })
    .WithTracing(otlpConfig, (tracing, config) =>
    {
        tracing
            .AddSource("MyApp.Tracing")
            .AddAspNetCoreInstrumentation()
            .AddHttpClientInstrumentation()
            .AddEntityFrameworkCoreInstrumentation()
            .AddOtlpExporter(config.Exporter!);
    });

📝 API Reference

Configuration Extensions

GetOpenTelemetryConfiguration(this IConfiguration configuration, string? sectionName = null)

Retrieves the OpenTelemetry configuration from the configuration source.

Parameters:

  • configuration: The configuration instance
  • sectionName: Optional section name (defaults to "OpenTelemetry")

Returns: OpenTelemetryConfiguration instance or null if not found

OpenTelemetryBuilder Extensions

Note: These extension methods use the "Argon" prefix to avoid conflicts with native OpenTelemetry methods.

WithMetrics(this OpenTelemetryBuilder builder, OpenTelemetryConfiguration configuration, Action<MeterProviderBuilder, OpenTelemetryMetricsConfiguration>? configure = null)

Configures OpenTelemetry Metrics if enabled in the configuration.

Parameters:

  • builder: OpenTelemetryBuilder instance
  • configuration: OpenTelemetry configuration
  • configure: Optional configuration action for MeterProviderBuilder

WithTracing(this OpenTelemetryBuilder builder, OpenTelemetryConfiguration configuration, Action<TracerProviderBuilder, OpenTelemetryTracingConfiguration>? configure = null)

Configures OpenTelemetry Tracing if enabled in the configuration.

Parameters:

  • builder: OpenTelemetryBuilder instance
  • configuration: OpenTelemetry configuration
  • configure: Optional configuration action for TracerProviderBuilder

WithLogging(this OpenTelemetryBuilder builder, OpenTelemetryConfiguration configuration, Action<LoggerProviderBuilder, OpenTelemetryLoggingConfiguration>? configure = null)

Configures OpenTelemetry Logging if enabled in the configuration.

Parameters:

  • builder: OpenTelemetryBuilder instance
  • configuration: OpenTelemetry configuration
  • configure: Optional configuration action for LoggerProviderBuilder

OTLP Exporter Extensions

Note: These overloads of AddOtlpExporter accept OpenTelemetryOtlpExporterConfiguration objects to simplify configuration from settings files.

AddOtlpExporter(this MeterProviderBuilder builder, OpenTelemetryOtlpExporterConfiguration configuration)

Adds OTLP exporter to MeterProvider using the provided configuration.

AddOtlpExporter(this TracerProviderBuilder builder, OpenTelemetryOtlpExporterConfiguration configuration)

Adds OTLP exporter to TracerProvider using the provided configuration.

AddOtlpExporter(this LoggerProviderBuilder builder, OpenTelemetryOtlpExporterConfiguration configuration)

Adds OTLP exporter to LoggerProvider using the provided configuration.

LoggingBuilder Extensions

Note: This method uses the "Argon" prefix (AddArgonOpenTelemetry) to avoid conflicts with the native AddOpenTelemetry method.

AddArgonOpenTelemetry(this ILoggingBuilder builder, OpenTelemetryConfiguration? configuration = null, Action<OpenTelemetryLoggerOptions, OpenTelemetryLoggingConfiguration>? configure = null)

Adds OpenTelemetry logging with the specified configuration.

Parameters:

  • builder: ILoggingBuilder instance
  • configuration: Optional OpenTelemetry configuration
  • configure: Optional configuration action for OpenTelemetryLoggerOptions

🔧 Configuration Classes

OpenTelemetryConfiguration

Main configuration class for OpenTelemetry.

Properties:

  • Enabled: Whether OpenTelemetry is enabled globally
  • Exporter: OTLP exporter configuration
  • Metrics: Metrics configuration
  • Tracing: Tracing configuration
  • Logging: Logging configuration

OpenTelemetryOtlpExporterConfiguration

Configuration for OTLP exporter.

Properties:

  • Endpoint: OTLP endpoint URI
  • Protocol: Export protocol (Grpc, HttpProtobuf)
  • ExportProcessorType: Processor type (Batch, Simple)
  • TimeoutMilliseconds: Timeout in milliseconds
  • Headers: Custom headers
  • BatchExportProcessorOptions: Batch processor options
  • MetricsReader: Metrics reader configuration

OpenTelemetryMetricsConfiguration

Configuration for metrics.

Properties:

  • Enabled: Whether metrics are enabled
  • Exporter: Metrics-specific exporter configuration

OpenTelemetryTracingConfiguration

Configuration for tracing.

Properties:

  • Enabled: Whether tracing is enabled
  • Exporter: Tracing-specific exporter configuration

OpenTelemetryLoggingConfiguration

Configuration for logging.

Properties:

  • Enabled: Whether logging is enabled
  • IncludeFormattedMessage: Include formatted message in logs
  • IncludeScopes: Include scopes in logs
  • ParseStateValues: Parse state values in logs
  • Exporter: Logging-specific exporter configuration

No packages depend on Argon.Extensions.OpenTelemetry.

Version Downloads Last updated
8.5.0 15 11/30/2025
8.4.0 25 11/17/2025