diff --git a/src/Application/Extensions.cs b/src/Application/Extensions.cs index f8e027f67..120a6881c 100644 --- a/src/Application/Extensions.cs +++ b/src/Application/Extensions.cs @@ -32,53 +32,41 @@ public static TBuilder AddServiceDefaults(this TBuilder builder) where } public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder - { - builder.Logging.AddOpenTelemetry(logging => - { - logging.IncludeFormattedMessage = true; - logging.IncludeScopes = true; - }); - - builder.Services.AddOpenTelemetry() - .WithMetrics(metrics => - { - metrics.AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation() - .AddRuntimeInstrumentation() - .SetExemplarFilter(ExemplarFilterType.TraceBased); - }) - .WithTracing(tracing => - { - tracing.AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation() - .AddOtlpExporter(); - }); - - builder.AddOpenTelemetryExporters(); - - return builder; - } - - private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder { var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); if (useOtlpExporter) { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + var telemetryBuilder = builder.Services.AddOpenTelemetry(); + + telemetryBuilder + .WithLogging(logging => logging.AddOtlpExporter()) + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation() + .SetExemplarFilter(ExemplarFilterType.TraceBased) + .AddOtlpExporter(); + }) + .WithTracing(tracing => + { + tracing.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddOtlpExporter(); + }); if (string.Equals(Environment.GetEnvironmentVariable("YARP_UNSAFE_OLTP_CERT_ACCEPT_ANY_SERVER_CERTIFICATE"), "true", StringComparison.InvariantCultureIgnoreCase)) { // We cannot use UseOtlpExporter() since it doesn't support configuration via OtlpExporterOptions // https://github.com/open-telemetry/opentelemetry-dotnet/issues/5802 builder.Services.Configure(ConfigureOtlpExporterOptions); - builder.Services.AddOpenTelemetry() - .WithLogging(logging => logging.AddOtlpExporter()) - .WithMetrics(metrics => metrics.AddOtlpExporter()) - .WithTracing(tracing => tracing.AddOtlpExporter()); - } - else - { - builder.Services.AddOpenTelemetry().UseOtlpExporter(); } } diff --git a/src/Application/Program.cs b/src/Application/Program.cs index 292fc9e74..45a74ae9e 100644 --- a/src/Application/Program.cs +++ b/src/Application/Program.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; + // Load configuration if (args.Length != 1) {