OpenTelemetry .NET Contrib

repository·main·Indexed 20 days ago

https://github.com/open-telemetry/opentelemetry-dotnet-contrib

A collection of helpful libraries and standalone OpenTelemetry-based utilities for .NET that fall outside the core scope of the main OpenTelemetry .NET and Automatic Instrumentation projects. Includes instrumentation for EventCounters, gRPC Core, OWIN, StackExchange.Redis, and WCF, as well as the Geneva Exporter and Dynamic Control for runtime behavior management.

Tokens
67.2K
Snippets
196
Records
280
Agent score
71%

What's inside OpenTelemetry .NET Contrib

  1. Overview of Dynamic Control for OpenTelemetry .NET

    main

    Dynamic Control for OpenTelemetry .NET is a library designed to allow developers to dynamically control the behavior of specific OpenTelemetry SDK features and instrumentations at runtime.

    Warning: This is an incubating feature. Breaking changes may occur in new releases without notice and without backward compatibility guarantees.

  2. Use OpenTelemetry.Extensions.Enrichment.Http for HTTP telemetry enrichment

    main

    The OpenTelemetry.Extensions.Enrichment.Http package provides extensions for the OpenTelemetry .NET SDK to enrich logs, metrics, and traces for outbound HTTP requests.

    Currently, the package only supports trace enrichment.

    Instead of manually attaching metadata to every telemetry object, you can implement a custom enricher by inheriting from HttpClientTraceEnricher. Once registered with the enrichment framework, your enricher will automatically be called for every outgoing HTTP request in your application, ensuring consistent metadata attachment.

  3. Enrich ASP.NET Core traces with custom information

    main

    The OpenTelemetry.Extensions.Enrichment.AspNetCore package provides an extension framework for enriching logs, metrics, and traces generated during inbound HTTP requests in ASP.NET Core.

    Currently, the package supports trace enrichment only.

    Instead of manually attaching metadata to every telemetry object, you can implement a custom enricher by inheriting from AspNetCoreTraceEnricher. Once registered, the framework automatically invokes your enrichment logic for every incoming HTTP request, ensuring consistent metadata across your traces.

  4. Use Persistent Storage Abstractions to extend exporters

    main
    The OpenTelemetry.PersistentStorage.Abstractions package provides a set of APIs designed for exporter developers. If you are building or maintaining an OpenTelemetry exporter and want to implement persistent storage (to prevent data loss during network outages or application restarts), you can use these abstractions to define how telemetry data is buffered and stored.
  5. How trace enrichment works

    main

    The enrichment framework allows you to automatically attach custom information to all traces in your application. Instead of manually adding tags to every Activity object, you implement a TraceEnricher class. The framework ensures that the Enrich() method of your class is called exactly once when an Activity stops.

    If you need to add information at the moment an activity begins, you can also override EnrichOnActivityStart.

  6. Configure HangfireInstrumentationOptions

    main

    You can customize the behavior of the Hangfire instrumentation using HangfireInstrumentationOptions. This is useful for changing how jobs are named, filtering which jobs are recorded, or deciding whether to record exceptions.

    If you are using OpenTelemetry.Extensions.Hosting, you can configure these options via the standard .NET dependency injection services.Configure<HangfireInstrumentationOptions>(...) pattern.

    // Using Dependency Injection with OpenTelemetry.Extensions.Hosting
    services.Configure<HangfireInstrumentationOptions>(options =>
    {
        options.DisplayNameFunc = job => $"JOB {job.Id}";
        options.Filter = job => job.Id == "Filter this job";
        options.RecordException = true;
    });
    
    services.AddOpenTelemetry()
        .WithTracing(builder => builder
            .AddHangfireInstrumentation()
            .AddConsoleExporter());
  7. Specify the Redis connection for instrumentation

    main

    There are three ways to specify which IConnectionMultiplexer instances to instrument:

    1. Directly via AddRedisInstrumentation: Pass the connection instance directly to the extension method.
    2. Via IServiceProvider: If using OpenTelemetry.Extensions.Hosting, you can omit the connection parameter in AddRedisInstrumentation(). The instrumentation will automatically resolve an IConnectionMultiplexer from the application's IServiceProvider.
    3. Directly via StackExchangeRedisInstrumentation: Use ConfigureRedisInstrumentation to get a reference to the StackExchangeRedisInstrumentation object. This allows you to call .AddConnection(connection) at any time to add or remove connections dynamically.
    // Option 1: Pass connection directly
    using var tracerProvider = Sdk.CreateTracerProviderBuilder()
        .AddRedisInstrumentation(connection)
        .Build();
    
    // Option 2: Resolve from IServiceProvider
    appBuilder.Services.AddSingleton<IConnectionMultiplexer>(sp => MyRedisConnectionHelper.CreateConnection(sp));
    appBuilder.Services.AddOpenTelemetry().WithTracing(tracing => tracing.AddRedisInstrumentation());
    
    // Option 3: Direct interaction for dynamic connections
    StackExchangeRedisInstrumentation redisInstrumentation = null;
    using var tracerProvider = Sdk.CreateTracerProviderBuilder()
        .AddRedisInstrumentation()
        .ConfigureRedisInstrumentation(instrumentation => redisInstrumentation = instrumentation)
        .Build();
    
    redisInstrumentation.AddConnection(ConnectionMultiplexer.Connect("localhost:6379"));
  8. How table name resolution works in OneCollector

    main

    The OneCollectorExporter maps logs and events to specific tables in the OneCollector service.

    Default Behavior: It generates fully qualified names using the LogRecord.CategoryName and EventId.Name properties (e.g., CategoryName.EventId.Name). If EventId.Name is missing, it uses the OneCollectorLogExporterOptions.DefaultEventName (which defaults to Log).

    Table Naming Rules: The service converts these full names into table names by changing casing and replacing . characters with _ characters. Attributes provided in the log are automatically promoted to columns in the resulting table.

  9. How blob leasing and file extensions work

    main

    The FileBlobProvider uses specific file extensions to manage state and concurrency:

    • .blob: A standard data file.
    • .lock: A file that has an active lease. When TryLease is called, the extension changes from .blob to .lock and an expiration timestamp is appended to the filename (e.g., filename.blob@timestamp.lock).
    • .tmp: A file currently undergoing a write operation.

    To safely read a blob, you should first call TryLease(int leasePeriodMilliseconds) on the blob object. This prevents other processes from reading it until the lease expires.