Skip to content

Commit

Permalink
Merge pull request #54 from wemogy/feature/automatic-app-insights-det…
Browse files Browse the repository at this point in the history
…ection-otel

Make Application Insights Optional
  • Loading branch information
robinmanuelthiel authored Dec 10, 2023
2 parents 0bb0d24 + 4e8ba07 commit ce00a42
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
37 changes: 32 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,55 @@ public Startup(IConfiguration configuration)
}
```

Add the `StartupOptions` and register the default setup.
Add the `StartupOptions` and register the default setup using the `AddDefaultSetup` extension method. Alternatively, you can also register our defaults manually in case you need to tweak in some of your own adjustments.

```csharp
public void ConfigureServices(IServiceCollection services)
{
// Register all options automatically
services.AddDefaultSetup(_options);

// ...
// or
// Register the options manually
services.AddDefaultCors();
services.AddDefaultSwagger(_options.OpenApiEnvironment);
services.AddDefaultMonitoring(_options.MonitoringEnvironment);
services.AddDefaultControllers(_options.DaprEnvironment != null, _options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes);
services.AddDefaultHealthChecks(_options);
services.AddDefaultRouting();
}
```

Make sure, the default setup gets used.
Make sure, the default setup is getting used. Again, you can either use the `UseDefaultSetup` extension method or use the options manually, in case you need to tweak in some of your own adjustments.

```csharp

```csharp
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDefaultSetup(env, _options);

// ...
// or
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseDefaultCors();
app.UseDefaultSwagger(_options.OpenApiEnvironment);
app.UseDefaultMonitoring(_options.MonitoringEnvironment);
app.UseDefaultRouting();
app.UseCloudEvents(); // when using Dapr
app.UseAuthentication();
app.UseAuthorization();
app.UseErrorHandlerMiddleware();
app.UseDefaultEndpoints(_options);
}
```

For modern or Minimal API services, this would look similar to this. In the `Program.cs`, add the `StartupOptions` and register the default setup.
For modern or Minimal API services (introduced in .NET 6), this would look similar to this. In the `Program.cs`, add the `StartupOptions` and register the default setup.

```csharp
var options = new StartupOptions();
Expand All @@ -75,6 +101,7 @@ options

// ...
app.AddDefaultSetup(options);
app.UseDefaultSetup(app.Environment, options);
```

Expand Down
1 change: 1 addition & 0 deletions src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static IServiceCollection AddDefaultMonitoring(
}
});

// Azure
if (environment.UseApplicationInsights)
{
services.AddOpenTelemetry().UseAzureMonitor(options =>
Expand Down
31 changes: 14 additions & 17 deletions src/Wemogy.AspNet/Startup/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public static void AddDefaultSetup(this IServiceCollection serviceCollection, St

serviceCollection.AddDefaultControllers(options.DaprEnvironment != null, options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes);

// Health checks
var healthChecksBuilder = serviceCollection.AddHealthChecks();
options.ConfigureHealthCheckBuilder?.Invoke(healthChecksBuilder);
serviceCollection.AddDefaultHealthChecks(options);

serviceCollection.AddDefaultRouting();
}
Expand All @@ -77,6 +75,12 @@ public static void AddDefaultCors(this IServiceCollection serviceCollection)
});
}

public static void AddDefaultHealthChecks(this IServiceCollection serviceCollection, StartupOptions options)
{
var healthChecksBuilder = serviceCollection.AddHealthChecks();
options.ConfigureHealthCheckBuilder?.Invoke(healthChecksBuilder);
}

public static void AddDefaultRouting(this IServiceCollection serviceCollection)
{
// Enforce lowercase routes
Expand Down Expand Up @@ -120,19 +124,7 @@ public static void UseDefaultSetup(this IApplicationBuilder applicationBuilder,
applicationBuilder.UseMiddleware(middleware);
}

if (options.DaprEnvironment != null)
{
applicationBuilder.UseEndpoints(endpoints =>
{
endpoints.MapSubscribeHandler(); // Register endpoints for Dapr PubSub
endpoints.MapControllers();
endpoints.MapHealthChecks("/healthz");
});
}
else
{
applicationBuilder.UseDefaultEndpoints();
}
applicationBuilder.UseDefaultEndpoints(options);
}

public static void UseDefaultRouting(this IApplicationBuilder applicationBuilder)
Expand All @@ -145,12 +137,17 @@ public static void UseDefaultCors(this IApplicationBuilder applicationBuilder)
applicationBuilder.UseCors();
}

public static void UseDefaultEndpoints(this IApplicationBuilder applicationBuilder)
public static void UseDefaultEndpoints(this IApplicationBuilder applicationBuilder, StartupOptions options)
{
applicationBuilder.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHealthChecks("/healthz");

if (options.DaprEnvironment != null)
{
endpoints.MapSubscribeHandler(); // Register endpoints for Dapr PubSub
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/Wemogy.AspNet/Wemogy.AspNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<PackageReference Include="RestSharp" Version="107.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.4.0" />
<PackageReference Include="Wemogy.Core" Version="3.1.2" />
<PackageReference Include="Wemogy.Core" Version="3.2.0" />
<PackageReference Include="Dapr.AspNetCore" Version="1.10.0" />
</ItemGroup>
</Project>

0 comments on commit ce00a42

Please sign in to comment.