From 624194bb0161749281d1a3072ba7a93b7743a08e Mon Sep 17 00:00:00 2001 From: meysamhadeli Date: Tue, 3 Jan 2023 22:59:47 +0330 Subject: [PATCH] feat: Add health-options --- src/BuildingBlocks/HealthCheck/Extensions.cs | 12 +++++++++++- .../HealthCheck/HealthOptions.cs | 6 ++++++ src/BuildingBlocks/Logging/ElasticOptions.cs | 4 ++-- src/BuildingBlocks/Logging/Extensions.cs | 6 +++--- src/BuildingBlocks/Logging/FileOptions.cs | 4 ++-- src/BuildingBlocks/Logging/SentryOptions.cs | 2 +- .../Booking/src/Booking.Api/appsettings.json | 19 +++++++++++-------- .../Booking/src/Booking/Booking.csproj | 2 +- .../Flight/src/Flight.Api/appsettings.json | 19 +++++++++++-------- src/Services/Flight/src/Flight/Flight.csproj | 6 +++--- .../src/Identity.Api/appsettings.json | 19 +++++++++++-------- .../src/Passenger.Api/appsettings.json | 19 +++++++++++-------- .../Passenger/src/Passenger/Passenger.csproj | 2 +- 13 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 src/BuildingBlocks/HealthCheck/HealthOptions.cs diff --git a/src/BuildingBlocks/HealthCheck/Extensions.cs b/src/BuildingBlocks/HealthCheck/Extensions.cs index 86f14ee..fb43c9c 100644 --- a/src/BuildingBlocks/HealthCheck/Extensions.cs +++ b/src/BuildingBlocks/HealthCheck/Extensions.cs @@ -16,6 +16,10 @@ public static class Extensions { public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services) { + var healthOptions = services.GetOptions(nameof(HealthOptions)); + + if (!healthOptions.Enabled) return services; + var appOptions = services.GetOptions(nameof(AppOptions)); var sqlOptions = services.GetOptions(nameof(DatabaseOptions)); var rabbitMqOptions = services.GetOptions(nameof(RabbitMqOptions)); @@ -23,7 +27,9 @@ public static class Extensions var logOptions = services.GetOptions(nameof(LogOptions)); var healthChecksBuilder = services.AddHealthChecks() - .AddRabbitMQ(rabbitConnectionString: $"amqp://{rabbitMqOptions.UserName}:{rabbitMqOptions.Password}@{rabbitMqOptions.HostName}") + .AddRabbitMQ( + rabbitConnectionString: + $"amqp://{rabbitMqOptions.UserName}:{rabbitMqOptions.Password}@{rabbitMqOptions.HostName}") .AddElasticsearch(logOptions.Elastic.ElasticServiceUrl); if (mongoOptions.ConnectionString is not null) @@ -43,6 +49,10 @@ public static class Extensions public static WebApplication UseCustomHealthCheck(this WebApplication app) { + var healthOptions = app.Configuration.GetOptions(nameof(HealthOptions)); + + if (!healthOptions.Enabled) return app; + app.UseHealthChecks("/healthz", new HealthCheckOptions { diff --git a/src/BuildingBlocks/HealthCheck/HealthOptions.cs b/src/BuildingBlocks/HealthCheck/HealthOptions.cs new file mode 100644 index 0000000..7b98301 --- /dev/null +++ b/src/BuildingBlocks/HealthCheck/HealthOptions.cs @@ -0,0 +1,6 @@ +namespace BuildingBlocks.HealthCheck; + +public class HealthOptions +{ + public bool Enabled { get; set; } = true; +} diff --git a/src/BuildingBlocks/Logging/ElasticOptions.cs b/src/BuildingBlocks/Logging/ElasticOptions.cs index 26fbc87..5c57758 100644 --- a/src/BuildingBlocks/Logging/ElasticOptions.cs +++ b/src/BuildingBlocks/Logging/ElasticOptions.cs @@ -2,7 +2,7 @@ public class ElasticOptions { - public bool Enable { get; set; } + public bool Enabled { get; set; } public string ElasticServiceUrl { get; set; } public string ElasticSearchIndex { get; set; } -} \ No newline at end of file +} diff --git a/src/BuildingBlocks/Logging/Extensions.cs b/src/BuildingBlocks/Logging/Extensions.cs index c47a47b..5fa1288 100644 --- a/src/BuildingBlocks/Logging/Extensions.cs +++ b/src/BuildingBlocks/Logging/Extensions.cs @@ -38,7 +38,7 @@ namespace BuildingBlocks.Logging .Enrich.FromLogContext() .ReadFrom.Configuration(context.Configuration); - if (logOptions.Elastic is { Enable: true }) + if (logOptions.Elastic is { Enabled: true }) { loggerConfiguration.WriteTo.Elasticsearch( new ElasticsearchSinkOptions(new Uri(logOptions.Elastic.ElasticServiceUrl)) @@ -49,7 +49,7 @@ namespace BuildingBlocks.Logging } - if (logOptions?.Sentry is {Enable: true}) + if (logOptions?.Sentry is {Enabled: true}) { var minimumBreadcrumbLevel = Enum.TryParse(logOptions.Level, true, out var minBreadcrumbLevel) ? minBreadcrumbLevel @@ -67,7 +67,7 @@ namespace BuildingBlocks.Logging }); } - if (logOptions.File is { Enable: true }) + if (logOptions.File is { Enabled: true }) { var root = env.ContentRootPath; Directory.CreateDirectory(Path.Combine(root, "logs")); diff --git a/src/BuildingBlocks/Logging/FileOptions.cs b/src/BuildingBlocks/Logging/FileOptions.cs index d3021a8..223e824 100644 --- a/src/BuildingBlocks/Logging/FileOptions.cs +++ b/src/BuildingBlocks/Logging/FileOptions.cs @@ -2,7 +2,7 @@ public class FileOptions { - public bool Enable { get; set; } + public bool Enabled { get; set; } public string Path { get; set; } public string Interval { get; set; } -} \ No newline at end of file +} diff --git a/src/BuildingBlocks/Logging/SentryOptions.cs b/src/BuildingBlocks/Logging/SentryOptions.cs index 6eb4b4e..3b036d2 100644 --- a/src/BuildingBlocks/Logging/SentryOptions.cs +++ b/src/BuildingBlocks/Logging/SentryOptions.cs @@ -2,7 +2,7 @@ public class SentryOptions { - public bool Enable { get; set; } + public bool Enabled { get; set; } public string Dsn { get; set; } public string MinimumBreadcrumbLevel { get; set; } public string MinimumEventLevel { get; set; } diff --git a/src/Services/Booking/src/Booking.Api/appsettings.json b/src/Services/Booking/src/Booking.Api/appsettings.json index bb84c2d..f034e8b 100644 --- a/src/Services/Booking/src/Booking.Api/appsettings.json +++ b/src/Services/Booking/src/Booking.Api/appsettings.json @@ -6,19 +6,19 @@ "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", "Elastic": { - "Enable": true, + "Enabled": true, "ElasticServiceUrl": "http://localhost:9200" }, "File": { - "enable": false, - "path": "logs/logs.txt", - "interval": "day" + "Enabled": false, + "Path": "logs/logs.txt", + "Interval": "day" }, "Sentry": { - "enable": false, - "dsn": "", - "minimumBreadcrumbLevel": "information", - "minimumEventLevel":"error" + "Enabled": false, + "Dsn": "", + "MinimumBreadcrumbLevel": "information", + "MinimumEventLevel":"error" } }, "Jwt": { @@ -43,6 +43,9 @@ "ConnectionString": "mongodb://localhost:27017", "DatabaseName": "booking-db" }, + "HealthOptions": { + "Enabled": false + }, "PersistMessageOptions": { "Interval": 30, "Enabled": true, diff --git a/src/Services/Booking/src/Booking/Booking.csproj b/src/Services/Booking/src/Booking/Booking.csproj index 704b1db..856bc39 100644 --- a/src/Services/Booking/src/Booking/Booking.csproj +++ b/src/Services/Booking/src/Booking/Booking.csproj @@ -6,7 +6,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Services/Flight/src/Flight.Api/appsettings.json b/src/Services/Flight/src/Flight.Api/appsettings.json index be45ae2..e66bb16 100644 --- a/src/Services/Flight/src/Flight.Api/appsettings.json +++ b/src/Services/Flight/src/Flight.Api/appsettings.json @@ -6,19 +6,19 @@ "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", "Elastic": { - "Enable": true, + "Enabled": true, "ElasticServiceUrl": "http://localhost:9200" }, "File": { - "enable": false, - "path": "logs/logs.txt", - "interval": "day" + "Enabled": false, + "Path": "logs/logs.txt", + "Interval": "day" }, "Sentry": { - "enable": false, - "dsn": "", - "minimumBreadcrumbLevel": "information", - "minimumEventLevel":"error" + "Enabled": false, + "Dsn": "", + "MinimumBreadcrumbLevel": "information", + "MinimumEventLevel": "error" } }, "DatabaseOptions": { @@ -44,5 +44,8 @@ "Enabled": true, "ConnectionString": "Server=.\\sqlexpress;Database=PersistMessageDB;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True" }, + "HealthOptions": { + "Enabled": false + }, "AllowedHosts": "*" } diff --git a/src/Services/Flight/src/Flight/Flight.csproj b/src/Services/Flight/src/Flight/Flight.csproj index 23644c6..7523afd 100644 --- a/src/Services/Flight/src/Flight/Flight.csproj +++ b/src/Services/Flight/src/Flight/Flight.csproj @@ -5,9 +5,9 @@ - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/Services/Identity/src/Identity.Api/appsettings.json b/src/Services/Identity/src/Identity.Api/appsettings.json index 2ffcf3c..da83ed8 100644 --- a/src/Services/Identity/src/Identity.Api/appsettings.json +++ b/src/Services/Identity/src/Identity.Api/appsettings.json @@ -20,21 +20,24 @@ "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", "Elastic": { - "Enable": true, + "Enabled": true, "ElasticServiceUrl": "http://localhost:9200" }, "File": { - "enable": false, - "path": "logs/logs.txt", - "interval": "day" + "Enabled": false, + "Path": "logs/logs.txt", + "Interval": "day" }, "Sentry": { - "enable": false, - "dsn": "", - "minimumBreadcrumbLevel": "information", - "minimumEventLevel":"error" + "Enabled": false, + "Dsn": "", + "MinimumBreadcrumbLevel": "information", + "MinimumEventLevel":"error" } }, + "HealthOptions": { + "Enabled": false + }, "PersistMessageOptions": { "Interval": 30, "Enabled": true, diff --git a/src/Services/Passenger/src/Passenger.Api/appsettings.json b/src/Services/Passenger/src/Passenger.Api/appsettings.json index c26cda4..bfbb06d 100644 --- a/src/Services/Passenger/src/Passenger.Api/appsettings.json +++ b/src/Services/Passenger/src/Passenger.Api/appsettings.json @@ -24,21 +24,24 @@ "Level": "information", "LogTemplate": "{Timestamp:HH:mm:ss} [{Level:u4}] {Message:lj}{NewLine}{Exception}", "Elastic": { - "Enable": true, + "Enabled": true, "ElasticServiceUrl": "http://localhost:9200" }, "File": { - "enable": false, - "path": "logs/logs.txt", - "interval": "day" + "Enabled": false, + "Path": "logs/logs.txt", + "Interval": "day" }, "Sentry": { - "enable": false, - "dsn": "", - "minimumBreadcrumbLevel": "information", - "minimumEventLevel":"error" + "Enabled": false, + "Dsn": "", + "MinimumBreadcrumbLevel": "information", + "MinimumEventLevel":"error" } }, + "HealthOptions": { + "Enabled": false + }, "PersistMessageOptions": { "Interval": 30, "Enabled": true, diff --git a/src/Services/Passenger/src/Passenger/Passenger.csproj b/src/Services/Passenger/src/Passenger/Passenger.csproj index 5080fe6..8e10d56 100644 --- a/src/Services/Passenger/src/Passenger/Passenger.csproj +++ b/src/Services/Passenger/src/Passenger/Passenger.csproj @@ -6,7 +6,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive