From 8d332e4515093b7aa997dec883bb9bb673cbee8c Mon Sep 17 00:00:00 2001 From: Melo Y Guan <62586556+Meloyg@users.noreply.github.com> Date: Fri, 30 Aug 2024 03:18:16 +1200 Subject: [PATCH] chore: Remove unused OpenTelemetry imports and fix formatting in StaticLogger.cs and Extensions.cs (#1021) --- .../Logging/Serilog/StaticLogger.cs | 3 +- src/aspire/service-defaults/Extensions.cs | 41 +++++++++---------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/api/framework/Infrastructure/Logging/Serilog/StaticLogger.cs b/src/api/framework/Infrastructure/Logging/Serilog/StaticLogger.cs index 7e11f8ab8..1809893b5 100644 --- a/src/api/framework/Infrastructure/Logging/Serilog/StaticLogger.cs +++ b/src/api/framework/Infrastructure/Logging/Serilog/StaticLogger.cs @@ -1,9 +1,8 @@ using Serilog; using Serilog.Core; -using Serilog.OpenTelemetry; -using Serilog.Sinks.OpenTelemetry; namespace FSH.Framework.Infrastructure.Logging.Serilog; + public static class StaticLogger { public static void EnsureInitialized() diff --git a/src/aspire/service-defaults/Extensions.cs b/src/aspire/service-defaults/Extensions.cs index e7147830e..f74bf3359 100644 --- a/src/aspire/service-defaults/Extensions.cs +++ b/src/aspire/service-defaults/Extensions.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using OpenTelemetry; -using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; @@ -39,7 +38,6 @@ public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBu public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) { - #region OpenTelemetry // Configure OpenTelemetry service resource details @@ -54,7 +52,8 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati var attributes = new Dictionary { ["host.name"] = Environment.MachineName, - ["service.names"] = "FSH.Starter.WebApi.Host", //builder.Configuration["OpenTelemetrySettings:ServiceName"]!, //It's a WA Fix because the service.name tag is not completed automatically by Resource.Builder()...AddService(serviceName) https://github.com/open-telemetry/opentelemetry-dotnet/issues/2027 + ["service.names"] = + "FSH.Starter.WebApi.Host", //builder.Configuration["OpenTelemetrySettings:ServiceName"]!, //It's a WA Fix because the service.name tag is not completed automatically by Resource.Builder()...AddService(serviceName) https://github.com/open-telemetry/opentelemetry-dotnet/issues/2027 ["os.description"] = System.Runtime.InteropServices.RuntimeInformation.OSDescription, ["deployment.environment"] = builder.Environment.EnvironmentName.ToLowerInvariant() }; @@ -78,12 +77,12 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati .WithMetrics(metrics => { metrics.SetResourceBuilder(resourceBuilder) - .AddAspNetCoreInstrumentation() - .AddHttpClientInstrumentation() - .AddRuntimeInstrumentation() - .AddProcessInstrumentation() - .AddMeter(MetricsConstants.Todos) - .AddMeter(MetricsConstants.Catalog); + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation() + .AddProcessInstrumentation() + .AddMeter(MetricsConstants.Todos) + .AddMeter(MetricsConstants.Catalog); //.AddConsoleExporter(); }) .WithTracing(tracing => @@ -94,9 +93,9 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati } tracing.SetResourceBuilder(resourceBuilder) - .AddAspNetCoreInstrumentation(nci => nci.RecordException = true) - .AddHttpClientInstrumentation() - .AddEntityFrameworkCoreInstrumentation(); + .AddAspNetCoreInstrumentation(nci => nci.RecordException = true) + .AddHttpClientInstrumentation() + .AddEntityFrameworkCoreInstrumentation(); //.AddConsoleExporter(); }); @@ -116,11 +115,11 @@ private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostAppli // The following lines enable the Prometheus exporter (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) builder.Services.AddOpenTelemetry() - // BUG: Part of the workaround for https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1617 - .WithMetrics(metrics => metrics.AddPrometheusExporter(options => - { - options.DisableTotalNameSuffixForCounters = true; - })); + // BUG: Part of the workaround for https://github.com/open-telemetry/opentelemetry-dotnet-contrib/issues/1617 + .WithMetrics(metrics => metrics.AddPrometheusExporter(options => + { + options.DisableTotalNameSuffixForCounters = true; + })); return builder; } @@ -143,14 +142,12 @@ public static WebApplication MapDefaultEndpoints(this WebApplication app) if (context.Request.Path != "/metrics") return false; return true; }); - + // All health checks must pass for app to be considered ready to accept traffic after starting app.MapHealthChecks("/health").AllowAnonymous(); // Only health checks tagged with the "live" tag must pass for app to be considered alive - app.MapHealthChecks("/alive", new HealthCheckOptions - { - Predicate = r => r.Tags.Contains("live") - }).AllowAnonymous(); + app.MapHealthChecks("/alive", new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") }) + .AllowAnonymous(); return app; }