From 6d8a039b667bff27d9dbc44fd9608b955894697d Mon Sep 17 00:00:00 2001 From: Phong Nguyen Date: Wed, 8 Jan 2025 13:13:25 +0700 Subject: [PATCH] OpenTelemetry --- .../ConfigurationOptions/AppSettings.cs | 5 ++- .../ClassifiedAds.Background/Program.cs | 3 ++ .../appsettings.Development.json | 10 ++++++ .../ClassifiedAds.Background/appsettings.json | 13 ++++++- .../OpenTelemetry/OpenTelemetryExtensions.cs | 35 +++++++++++++------ .../OpenTelemetry/OpenTelemetryOptions.cs | 4 +++ .../ClassifiedAds.WebAPI/appsettings.json | 2 ++ .../ClassifiedAds.WebMVC/appsettings.json | 2 ++ 8 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/Monolith/ClassifiedAds.Background/ConfigurationOptions/AppSettings.cs b/src/Monolith/ClassifiedAds.Background/ConfigurationOptions/AppSettings.cs index e65b9c76..72c0ac57 100644 --- a/src/Monolith/ClassifiedAds.Background/ConfigurationOptions/AppSettings.cs +++ b/src/Monolith/ClassifiedAds.Background/ConfigurationOptions/AppSettings.cs @@ -2,6 +2,7 @@ using ClassifiedAds.Infrastructure.IdentityProviders.Azure; using ClassifiedAds.Infrastructure.Logging; using ClassifiedAds.Infrastructure.MessageBrokers; +using ClassifiedAds.Infrastructure.Monitoring; using ClassifiedAds.Infrastructure.Notification; using ClassifiedAds.Infrastructure.Storages; using Microsoft.Extensions.Options; @@ -14,9 +15,7 @@ public class AppSettings public LoggingOptions Logging { get; set; } - public string AllowedHosts { get; set; } - - public string CurrentUrl { get; set; } + public MonitoringOptions Monitoring { get; set; } public StorageOptions Storage { get; set; } diff --git a/src/Monolith/ClassifiedAds.Background/Program.cs b/src/Monolith/ClassifiedAds.Background/Program.cs index 25df75fd..7f32a740 100644 --- a/src/Monolith/ClassifiedAds.Background/Program.cs +++ b/src/Monolith/ClassifiedAds.Background/Program.cs @@ -11,6 +11,7 @@ using ClassifiedAds.Infrastructure.IdentityProviders.Auth0; using ClassifiedAds.Infrastructure.IdentityProviders.Azure; using ClassifiedAds.Infrastructure.Logging; +using ClassifiedAds.Infrastructure.Monitoring; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; @@ -42,6 +43,8 @@ services.Configure(configuration); + services.AddMonitoringServices(appSettings.Monitoring); + services.AddScoped(); services.AddDateTimeProvider(); diff --git a/src/Monolith/ClassifiedAds.Background/appsettings.Development.json b/src/Monolith/ClassifiedAds.Background/appsettings.Development.json index fca7ea73..9c65f068 100644 --- a/src/Monolith/ClassifiedAds.Background/appsettings.Development.json +++ b/src/Monolith/ClassifiedAds.Background/appsettings.Development.json @@ -13,5 +13,15 @@ "Endpoint": "https://localhost:21052" } } + }, + "Monitoring": { + "OpenTelemetry": { + "IsEnabled": true, + "ServiceName": "ClassifiedAds.Background", + "Otlp": { + "IsEnabled": true, + "Endpoint": "https://localhost:21052" + } + } } } diff --git a/src/Monolith/ClassifiedAds.Background/appsettings.json b/src/Monolith/ClassifiedAds.Background/appsettings.json index 884264d8..0e656b75 100644 --- a/src/Monolith/ClassifiedAds.Background/appsettings.json +++ b/src/Monolith/ClassifiedAds.Background/appsettings.json @@ -15,7 +15,18 @@ "MinimumLogEventLevel": "Information" } }, - "AllowedHosts": "*", + "Monitoring": { + "OpenTelemetry": { + "IsEnabled": false, + "ServiceName": "ClassifiedAds.Background", + "TraceEnabled": true, + "MetricEnabled": true, + "Otlp": { + "IsEnabled": false, + "Endpoint": "http://localhost:4317" + } + } + }, "MessageBroker": { "Provider": "RabbitMQ", "RabbitMQ": { diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryExtensions.cs b/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryExtensions.cs index bb1afb42..230fb313 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryExtensions.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryExtensions.cs @@ -17,15 +17,23 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl return services; } - services.AddOpenTelemetry() - .ConfigureResource(configureResource => - { - configureResource.AddService( - serviceName: options.ServiceName, - serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown", - serviceInstanceId: options.ServiceName + "-" + Environment.MachineName); - }) - .WithTracing(builder => + if (!options.TraceEnabled && !options.MetricEnabled) + { + return services; + } + + var openTelemetry = services.AddOpenTelemetry() + .ConfigureResource(configureResource => + { + configureResource.AddService( + serviceName: options.ServiceName, + serviceVersion: Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown", + serviceInstanceId: options.ServiceName + "-" + Environment.MachineName); + }); + + if (options.TraceEnabled) + { + openTelemetry.WithTracing(builder => { builder .SetSampler(new AlwaysOnSampler()) @@ -56,8 +64,12 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl opts.ConnectionString = options.AzureMonitor.ConnectionString; }); } - }) - .WithMetrics(builder => + }); + } + + if (options.MetricEnabled) + { + openTelemetry.WithMetrics(builder => { builder .AddRuntimeInstrumentation() @@ -81,6 +93,7 @@ public static IServiceCollection AddClassifiedAdsOpenTelemetry(this IServiceColl }); } }); + } return services; } diff --git a/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryOptions.cs b/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryOptions.cs index 0bcef84d..c0da9903 100644 --- a/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryOptions.cs +++ b/src/Monolith/ClassifiedAds.Infrastructure/Monitoring/OpenTelemetry/OpenTelemetryOptions.cs @@ -6,6 +6,10 @@ public class OpenTelemetryOptions public string ServiceName { get; set; } + public bool TraceEnabled { get; set; } + + public bool MetricEnabled { get; set; } + public ZipkinOptions Zipkin { get; set; } public OtlpOptions Otlp { get; set; } diff --git a/src/Monolith/ClassifiedAds.WebAPI/appsettings.json b/src/Monolith/ClassifiedAds.WebAPI/appsettings.json index 703412a3..3ee5f4ab 100644 --- a/src/Monolith/ClassifiedAds.WebAPI/appsettings.json +++ b/src/Monolith/ClassifiedAds.WebAPI/appsettings.json @@ -88,6 +88,8 @@ "OpenTelemetry": { "IsEnabled": false, "ServiceName": "ClassifiedAds.WebAPI", + "TraceEnabled": true, + "MetricEnabled": true, "Otlp": { "IsEnabled": false, "Endpoint": "http://localhost:4317" diff --git a/src/Monolith/ClassifiedAds.WebMVC/appsettings.json b/src/Monolith/ClassifiedAds.WebMVC/appsettings.json index 718c384e..097c015d 100644 --- a/src/Monolith/ClassifiedAds.WebMVC/appsettings.json +++ b/src/Monolith/ClassifiedAds.WebMVC/appsettings.json @@ -84,6 +84,8 @@ "OpenTelemetry": { "IsEnabled": false, "ServiceName": "ClassifiedAds.WebMVC", + "TraceEnabled": true, + "MetricEnabled": true, "Otlp": { "IsEnabled": false, "Endpoint": "http://localhost:4317"