#region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.Telemetry; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using OpenTelemetry; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; #endregion namespace Lumina.Presentation.Api.Common.DependencyInjection; /// /// Utility class for registering the OpenTelemetry services of the Presentation Api layer into the Dependency Injection container. /// [ExcludeFromCodeCoverage] public static class PresentationApiTelemetryServices { private const string SERVICE_NAME = "Lumina.Api"; private const string ENTITY_FRAMEWORK_CORE_METER_NAME = "Microsoft.EntityFrameworkCore"; /// /// Registers the OpenTelemetry services of the Presentation Api layer into the Dependency Injection container. /// /// The service collection to add the services to. /// The configuration used to read the application configuration. /// The hosting environment of the application. /// The updated . public static IServiceCollection AddPresentationApiTelemetryServices(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment) { bool isTelemetryEnabled = configuration.GetValue("Telemetry:Enabled") ?? true; if (!isTelemetryEnabled) return services; bool isConsoleExporterEnabled = configuration.GetValue("Telemetry:ConsoleExporterEnabled") ?? environment.IsDevelopment(); string? otlpEndpoint = GetOtlpEndpoint(configuration); double traceSampleRatio = configuration.GetValue("Telemetry:TraceSampleRatio") ?? 1.0; string serviceVersion = typeof(Program).Assembly.GetName().Version?.ToString() ?? "unknown"; services.AddOpenTelemetry() .ConfigureResource(resourceBuilder => resourceBuilder .AddService(serviceName: SERVICE_NAME, serviceVersion: serviceVersion) .AddEnvironmentVariableDetector() .AddAttributes(new Dictionary { ["deployment.environment"] = environment.EnvironmentName })) .WithTracing(tracingBuilder => { tracingBuilder.SetSampler(CreateSampler(traceSampleRatio)); tracingBuilder.AddSource(ApplicationTelemetry.SOURCE_NAME); tracingBuilder.AddAspNetCoreInstrumentation(); if (isConsoleExporterEnabled) tracingBuilder.AddConsoleExporter(); if (otlpEndpoint is not null) tracingBuilder.AddOtlpExporter(otlpExporterOptions => otlpExporterOptions.Endpoint = new Uri(otlpEndpoint)); }) .WithMetrics(metricsBuilder => { metricsBuilder.AddMeter(ApplicationTelemetry.SOURCE_NAME); metricsBuilder.AddMeter(ENTITY_FRAMEWORK_CORE_METER_NAME); metricsBuilder.AddAspNetCoreInstrumentation(); metricsBuilder.AddRuntimeInstrumentation(); if (isConsoleExporterEnabled) metricsBuilder.AddConsoleExporter(); if (otlpEndpoint is not null) metricsBuilder.AddOtlpExporter(otlpExporterOptions => otlpExporterOptions.Endpoint = new Uri(otlpEndpoint)); }); return services; } /// /// Gets the OTLP exporter endpoint from configuration, falling back to the standard OTEL_EXPORTER_OTLP_ENDPOINT environment variable. /// /// Application configuration properties. /// The OTLP endpoint, or when no endpoint is configured. private static string? GetOtlpEndpoint(IConfiguration configuration) { string? endpoint = configuration.GetValue("Telemetry:Otlp:Endpoint"); if (string.IsNullOrWhiteSpace(endpoint)) endpoint = Environment.GetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT"); return string.IsNullOrWhiteSpace(endpoint) ? null : endpoint; } /// /// Creates the trace sampler used by the API, sampling every trace by default and honoring the configured ratio for production traffic. /// /// The ratio of traces to sample, in the range of zero to one. /// The configured trace sampler. private static Sampler CreateSampler(double traceSampleRatio) { return traceSampleRatio < 1.0 ? new ParentBasedSampler(new TraceIdRatioBasedSampler(traceSampleRatio)) : new AlwaysOnSampler(); } }