diff --git a/.vscode/settings.json b/.vscode/settings.json index c7ec07e..0d017a0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "cSpell.words": ["CQRS", "dapr", "Dapr", "dtos", "Dtos", "wemogy", "Xunit"] + "cSpell.words": ["CQRS", "dapr", "Dapr", "dtos", "Dtos", "oltp", "Otlp", "wemogy", "Xunit"] } diff --git a/README.md b/README.md index cbdad81..82f2272 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,9 @@ public void ConfigureServices(IServiceCollection services) // Register the options manually services.AddDefaultCors(); - services.AddDefaultSwagger(_options.OpenApiEnvironment); - services.AddDefaultMonitoring(_options.MonitoringEnvironment); - services.AddDefaultControllers(_options.DaprEnvironment != null, _options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes); + services.AddDefaultSwagger(_options); + services.AddDefaultMonitoring(_options); + services.AddDefaultControllers(_options, _options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes); services.AddDefaultHealthChecks(_options); services.AddDefaultRouting(); } @@ -77,8 +77,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } app.UseDefaultCors(); - app.UseDefaultSwagger(_options.OpenApiEnvironment); - app.UseDefaultMonitoring(_options.MonitoringEnvironment); + app.UseDefaultSwagger(_options); + app.UseDefaultMonitoring(_options); app.UseDefaultRouting(); app.UseCloudEvents(); // when using Dapr app.UseAuthentication(); diff --git a/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs b/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs index 4fde8d0..399d585 100644 --- a/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs +++ b/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs @@ -1,9 +1,11 @@ +using System; using Azure.Monitor.OpenTelemetry.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; +using Wemogy.AspNet.Startup; using Wemogy.Core.Monitoring; namespace Wemogy.AspNet.Monitoring @@ -12,12 +14,17 @@ public static class MonitoringExtensions { public static IServiceCollection AddDefaultMonitoring( this IServiceCollection services, - MonitoringEnvironment environment) + StartupOptions options) { + if (options.MonitoringEnvironment == null) + { + throw new ArgumentException("MonitoringEnvironment is not configured."); + } + // Metrics services.AddOpenTelemetry().WithMetrics(builder => { - foreach (var meterName in environment.MeterNames) + foreach (var meterName in options.MonitoringEnvironment.MeterNames) { builder.AddMeter(meterName); } @@ -26,7 +33,7 @@ public static IServiceCollection AddDefaultMonitoring( builder.AddHttpClientInstrumentation(); builder.AddAspNetCoreInstrumentation(); - if (environment.UsePrometheus) + if (options.MonitoringEnvironment.UsePrometheus) { builder.AddPrometheusExporter(); } @@ -38,48 +45,53 @@ public static IServiceCollection AddDefaultMonitoring( builder.ConfigureResource((resource) => { resource.AddService( - serviceName: environment.ServiceName, - serviceNamespace: environment.ServiceNamespace, - serviceInstanceId: environment.ServiceInstanceId, - serviceVersion: environment.ServiceVersion); + serviceName: options.MonitoringEnvironment.ServiceName, + serviceNamespace: options.MonitoringEnvironment.ServiceNamespace, + serviceInstanceId: options.MonitoringEnvironment.ServiceInstanceId, + serviceVersion: options.MonitoringEnvironment.ServiceVersion); }); builder.AddAspNetCoreInstrumentation(); builder.AddEntityFrameworkCoreInstrumentation(); - foreach (var activitySourceName in environment.ActivitySourceNames) + foreach (var activitySourceName in options.MonitoringEnvironment.ActivitySourceNames) { builder.AddSource(activitySourceName); } - if (environment.UseOtlpExporter) + if (options.MonitoringEnvironment.UseOtlpExporter) { - builder.AddOtlpExporter(options => + builder.AddOtlpExporter(oltpOptions => { - options.Endpoint = environment.OtlpExportEndpoint; + oltpOptions.Endpoint = options.MonitoringEnvironment.OtlpExportEndpoint; }); } }); // Azure - if (environment.UseApplicationInsights) + if (options.MonitoringEnvironment.UseApplicationInsights) { - services.AddOpenTelemetry().UseAzureMonitor(options => + services.AddOpenTelemetry().UseAzureMonitor(azureMonitorOptions => { - options.ConnectionString = environment.ApplicationInsightsConnectionString; - options.SamplingRatio = environment.ApplicationInsightsSamplingRatio; + azureMonitorOptions.ConnectionString = options.MonitoringEnvironment.ApplicationInsightsConnectionString; + azureMonitorOptions.SamplingRatio = options.MonitoringEnvironment.ApplicationInsightsSamplingRatio; }); } - services.AddSingleton(environment); + services.AddSingleton(options.MonitoringEnvironment); return services; } public static void UseDefaultMonitoring( this IApplicationBuilder applicationBuilder, - MonitoringEnvironment environment) + StartupOptions options) { - if (environment.UsePrometheus) + if (options.MonitoringEnvironment == null) + { + throw new ArgumentException("OpenApiEnvironment is not configured."); + } + + if (options.MonitoringEnvironment.UsePrometheus) { applicationBuilder.UseOpenTelemetryPrometheusScrapingEndpoint(); } diff --git a/src/Wemogy.AspNet/Startup/StartupExtensions.cs b/src/Wemogy.AspNet/Startup/StartupExtensions.cs index a6a43dc..9d73236 100644 --- a/src/Wemogy.AspNet/Startup/StartupExtensions.cs +++ b/src/Wemogy.AspNet/Startup/StartupExtensions.cs @@ -29,12 +29,12 @@ public static void GetWemogyDefaultControllerOptions(MvcOptions options, bool su options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = suppressImplicitRequiredAttributeForNonNullableReferenceTypes; } - public static void AddDefaultControllers(this IServiceCollection serviceCollection, bool addDapr = false, bool suppressImplicitRequiredAttributeForNonNullableReferenceTypes = false) + public static void AddDefaultControllers(this IServiceCollection serviceCollection, StartupOptions options, bool suppressImplicitRequiredAttributeForNonNullableReferenceTypes = false) { var builder = serviceCollection.AddControllers(options => GetWemogyDefaultControllerOptions(options, suppressImplicitRequiredAttributeForNonNullableReferenceTypes)); builder.AddWemogyJsonOptions(); - if (addDapr) + if (options.DaprEnvironment != null) { builder.AddDapr(); } @@ -46,15 +46,15 @@ public static void AddDefaultSetup(this IServiceCollection serviceCollection, St if (options.OpenApiEnvironment != null) { - serviceCollection.AddDefaultSwagger(options.OpenApiEnvironment); + serviceCollection.AddDefaultSwagger(options); } if (options.MonitoringEnvironment != null) { - serviceCollection.AddDefaultMonitoring(options.MonitoringEnvironment); + serviceCollection.AddDefaultMonitoring(options); } - serviceCollection.AddDefaultControllers(options.DaprEnvironment != null, options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes); + serviceCollection.AddDefaultControllers(options, options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes); serviceCollection.AddDefaultHealthChecks(options); @@ -100,12 +100,12 @@ public static void UseDefaultSetup(this IApplicationBuilder applicationBuilder, if (options.OpenApiEnvironment != null) { - applicationBuilder.UseDefaultSwagger(options.OpenApiEnvironment); + applicationBuilder.UseDefaultSwagger(options); } if (options.MonitoringEnvironment != null) { - applicationBuilder.UseDefaultMonitoring(options.MonitoringEnvironment); + applicationBuilder.UseDefaultMonitoring(options); } applicationBuilder.UseDefaultRouting(); diff --git a/src/Wemogy.AspNet/Swagger/SwaggerExtensions.cs b/src/Wemogy.AspNet/Swagger/SwaggerExtensions.cs index eb8e618..546519d 100644 --- a/src/Wemogy.AspNet/Swagger/SwaggerExtensions.cs +++ b/src/Wemogy.AspNet/Swagger/SwaggerExtensions.cs @@ -1,8 +1,10 @@ +using System; using System.Collections.Generic; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; +using Wemogy.AspNet.Startup; using Wemogy.Core.Extensions; namespace Wemogy.AspNet.Swagger @@ -11,11 +13,16 @@ public static class SwaggerExtensions { public static IServiceCollection AddDefaultSwagger( this IServiceCollection services, - OpenApiEnvironment environment) + StartupOptions options) { + if (options.OpenApiEnvironment == null) + { + throw new ArgumentException("OpenApiEnvironment is not configured."); + } + services.AddSwaggerGen(c => { - foreach (var configure in environment.SwaggerGenOptions) + foreach (var configure in options.OpenApiEnvironment.SwaggerGenOptions) { configure(c); } @@ -24,12 +31,17 @@ public static IServiceCollection AddDefaultSwagger( return services; } - public static void UseDefaultSwagger(this IApplicationBuilder applicationBuilder, OpenApiEnvironment environment) + public static void UseDefaultSwagger(this IApplicationBuilder applicationBuilder, StartupOptions options) { + if (options.OpenApiEnvironment == null) + { + throw new ArgumentException("OpenApiEnvironment is not configured."); + } + applicationBuilder.UseSwagger(); applicationBuilder.UseSwaggerUI(c => { - foreach (var group in environment.OpenApiGroups) + foreach (var group in options.OpenApiEnvironment.OpenApiGroups) { // Only publish groups that are marked as publishable if (group.Value.Publish)