Skip to content

Commit

Permalink
Merge pull request #57 from wemogy/fix/manual-setup
Browse files Browse the repository at this point in the history
Fix manual setup
  • Loading branch information
robinmanuelthiel authored Jan 5, 2024
2 parents ce00a42 + aef4d36 commit e63835a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["CQRS", "dapr", "Dapr", "dtos", "Dtos", "wemogy", "Xunit"]
"cSpell.words": ["CQRS", "dapr", "Dapr", "dtos", "Dtos", "oltp", "Otlp", "wemogy", "Xunit"]
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
Expand Down
48 changes: 30 additions & 18 deletions src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
Expand All @@ -26,7 +33,7 @@ public static IServiceCollection AddDefaultMonitoring(
builder.AddHttpClientInstrumentation();
builder.AddAspNetCoreInstrumentation();

if (environment.UsePrometheus)
if (options.MonitoringEnvironment.UsePrometheus)
{
builder.AddPrometheusExporter();
}
Expand All @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions src/Wemogy.AspNet/Startup/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
20 changes: 16 additions & 4 deletions src/Wemogy.AspNet/Swagger/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
Expand All @@ -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)
Expand Down

0 comments on commit e63835a

Please sign in to comment.