Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health checks module #77

Closed
wants to merge 4 commits into from
Closed

Conversation

jongalloway
Copy link
Contributor

@jongalloway jongalloway commented Feb 27, 2025

Fixes #76

Add health checks module to the application.

  • Add Health Check Packages: Add Microsoft.Extensions.Diagnostics.HealthChecks, AspNetCore.HealthChecks.Npgsql, and AspNetCore.HealthChecks.Redis package references to complete/Api/Api.csproj.
  • Add Health Check Services: Add health check services for database, redis cache, and external service in complete/Api/Program.cs.
  • Add Health Check Endpoints: Add health check endpoints for /health and /alive in complete/Api/Program.cs.
  • Considerations for Non-Development Environments: Add considerations for non-development environments in MapDefaultEndpoints method in complete/ServiceDefaults/Extensions.cs.
  • Documentation: Add workshop/10-health-checks.md to explain the health checks module and include instructions for students to update the application with health checks. Reference relevant documentation and include information on the HealthChecksUI sample.

For more details, open the Copilot Workspace session.

Fixes #76

Add health checks module to the application.

* **Add Health Check Packages**: Add `Microsoft.Extensions.Diagnostics.HealthChecks`, `AspNetCore.HealthChecks.Npgsql`, and `AspNetCore.HealthChecks.Redis` package references to `complete/Api/Api.csproj`.
* **Add Health Check Services**: Add health check services for database, redis cache, and external service in `complete/Api/Program.cs`.
* **Add Health Check Endpoints**: Add health check endpoints for `/health` and `/alive` in `complete/Api/Program.cs`.
* **Considerations for Non-Development Environments**: Add considerations for non-development environments in `MapDefaultEndpoints` method in `complete/ServiceDefaults/Extensions.cs`.
* **Documentation**: Add `workshop/10-health-checks.md` to explain the health checks module and include instructions for students to update the application with health checks. Reference relevant documentation and include information on the HealthChecksUI sample.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/dotnet-presentations/dotnet-aspire-workshop/issues/76?shareId=XXXX-XXXX-XXXX-XXXX).
@jongalloway jongalloway requested a review from Copilot February 27, 2025 00:19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR adds a comprehensive health checks module to the application to monitor critical dependencies and expose health endpoints for both development and non-development environments.

  • Added documentation for health checks setup.
  • Updated Api.csproj and Program.cs to register health check services and endpoints.
  • Modified MapDefaultEndpoints in ServiceDefaults/Extensions.cs with distinct logic for non-development environments.

Reviewed Changes

File Description
workshop/10-health-checks.md Documentation outlining steps to add health checks to the application
complete/Api/Program.cs Registers health check services and maps health check endpoints in Program.cs
complete/ServiceDefaults/Extensions.cs Updates the MapDefaultEndpoints method to handle non-development environments

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

complete/Api/Program.cs:41

  • Health check endpoints are being mapped in both MapDefaultEndpoints and again explicitly in Program.cs, which may lead to duplicate route mapping. Consider removing the redundant mapping in Program.cs if MapDefaultEndpoints already handles these endpoints.
app.MapHealthChecks("/health");
jongalloway and others added 3 commits February 26, 2025 16:41
- Added `AspNetCore.HealthChecks.Uris` package.
- Removed several outdated package references, including Entity Framework Core and health checks for Npgsql and Redis.
- Updated `Microsoft.EntityFrameworkCore.Design` and health check packages to version `9.0.2` and `9.0.0`, respectively.
- Removed `using Microsoft.EntityFrameworkCore;` from `NwsManager.cs`.
- Updated `Program.cs` to reflect changes in health check services, focusing on Redis and external services.
- Restructured `MyWeatherHub.csproj` while maintaining existing properties and added new health check packages.
- Modified error handling in `Program.cs` to ensure database context creation outside of development mode.
- Updated `Extensions.cs` to include health checks and JSON serialization.
@jongalloway
Copy link
Contributor Author

I am leaning towards closing this PR and potentially revisiting this module in a future release of .NET Aspire.

  • .NET Aspire currently creates /health and /alive health check endpoints for all web app projects in Service Defaults. It's possible to add additional health checks via the standard ASP.NET Core Health Checks API, but they're not directly integrated into the .NET Aspire health checks dashboard or dependency management.
  • There is a sample to add the HealthChecksUI dashboard via docker image, but this carries other caveats since it shouldn't be run in production without security consideration, and is kind of a side scenario.
  • The .NET Aspire docs don't advocate dependency health checks, and there's no direct guidance in the docs or samples.

I had previously discussed with Jeff and he pointed me to this stackoverflow issue describing the above: How to make Health Checks in NET Aspire right way?

I've written and rewritten this several times, and it seems like a poor use of workshop time to cover this currently. There are some open issues to extend health check functionality, at which time it would make sense to create a new PR based on the updated API.

@jongalloway jongalloway marked this pull request as draft March 9, 2025 06:57
@jongalloway
Copy link
Contributor Author

Closing the PR per #77 (comment)

@jongalloway jongalloway closed this Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Health Checks module
2 participants