Skip to content

Commit dd5cffe

Browse files
CopilotDamianEdwardsdavidfowl
authored
Add comprehensive AppHost resource health checks documentation with enhanced overview (#4310)
* Initial plan * Add AppHost resource health checks section to health checks fundamentals Co-authored-by: DamianEdwards <[email protected]> * Add reference to ASP.NET Core health checks documentation Co-authored-by: DamianEdwards <[email protected]> * Add screenshot reference for health check dashboard status Co-authored-by: DamianEdwards <[email protected]> * Add overview clarification that health checks run in AppHost and applications Co-authored-by: davidfowl <[email protected]> * Add health checks status on dashboard screenshot --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: DamianEdwards <[email protected]> Co-authored-by: davidfowl <[email protected]> Co-authored-by: Damian Edwards <[email protected]>
1 parent c1bdce7 commit dd5cffe

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

docs/fundamentals/health-checks.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ uid: dotnet/aspire/health-checks
88

99
# Health checks in .NET Aspire
1010

11-
Health checks provide availability and state information about an app. Health checks are often exposed as HTTP endpoints, but can also be used internally by the app to write logs or perform other tasks based on the current health. Health checks are typically used in combination with an external monitoring service or container orchestrator to check the status of an app. The data reported by health checks can be used for various scenarios:
11+
Health checks provide availability and state information about an app. Health checks are often exposed as HTTP endpoints, but can also be used internally by the app to write logs or perform other tasks based on the current health. Health checks are typically used in combination with an external monitoring service or container orchestrator to check the status of an app.
12+
13+
In .NET Aspire, health checks operate in two main contexts:
14+
15+
- **AppHost resource health checks** - Run in the AppHost project to determine resource readiness for orchestration and dependency management. These checks control when dependent resources start and are displayed in the Aspire dashboard.
16+
- **Application health check endpoints** - Run within individual applications and services to expose `/health` and `/alive` endpoints for monitoring and load balancing decisions.
17+
18+
The data reported by health checks can be used for various scenarios:
1219

1320
- Influence decisions made by container orchestrators, load balancers, API gateways, and other management services. For instance, if the health check for a containerized app fails, it might be skipped by a load balancer routing traffic.
1421
- Verify that underlying dependencies are available, such as a database or cache, and return an appropriate status message.
@@ -110,6 +117,76 @@ builder.AddNpgsqlDbContext<MyDbContext>(
110117
static settings => settings.DisableHealthChecks = true);
111118
```
112119

120+
## AppHost resource health checks
121+
122+
AppHost resource health checks are different from the health check endpoints described earlier. These health checks are configured in the AppHost project and determine the readiness of resources from the orchestrator's perspective. They're particularly important for controlling when dependent resources start via the [`WaitFor`](orchestrate-resources.md#waiting-for-resources) functionality and are displayed in the Aspire dashboard.
123+
124+
### Resource readiness with health checks
125+
126+
When a resource has health checks configured, the AppHost uses them to determine if the resource is ready before starting dependent resources. If no health checks are registered for a resource, the AppHost waits for the resource to be in the <xref:Aspire.Hosting.ApplicationModel.KnownResourceStates.Running> state.
127+
128+
### HTTP health checks for resources
129+
130+
For resources that expose HTTP endpoints, you can add health checks that poll specific paths:
131+
132+
```csharp
133+
var builder = DistributedApplication.CreateBuilder(args);
134+
135+
var catalogApi = builder.AddContainer("catalog-api", "catalog-api")
136+
.WithHttpEndpoint(targetPort: 8080)
137+
.WithHttpHealthCheck("/health");
138+
139+
builder.AddProject<Projects.WebApp>("webapp")
140+
.WithReference(catalogApi)
141+
.WaitFor(catalogApi); // Waits for /health to return HTTP 200
142+
```
143+
144+
The `WithHttpHealthCheck` method can also be applied to project resources:
145+
146+
```csharp
147+
var backend = builder.AddProject<Projects.Backend>("backend")
148+
.WithHttpHealthCheck("/health");
149+
150+
builder.AddProject<Projects.Frontend>("frontend")
151+
.WithReference(backend)
152+
.WaitFor(backend);
153+
```
154+
155+
### Custom resource health checks
156+
157+
You can create custom health checks for more complex readiness scenarios. Start by defining the health check in the AppHost's service collection, then associate it with resources:
158+
159+
```csharp
160+
var builder = DistributedApplication.CreateBuilder(args);
161+
162+
var startAfter = DateTime.Now.AddSeconds(30);
163+
164+
builder.Services.AddHealthChecks().AddCheck("mycheck", () =>
165+
{
166+
return DateTime.Now > startAfter
167+
? HealthCheckResult.Healthy()
168+
: HealthCheckResult.Unhealthy();
169+
});
170+
171+
var pg = builder.AddPostgres("pg")
172+
.WithHealthCheck("mycheck");
173+
174+
builder.AddProject<Projects.MyApp>("myapp")
175+
.WithReference(pg)
176+
.WaitFor(pg); // Waits for both the Postgres container to be running
177+
// AND the custom "mycheck" health check to be healthy
178+
```
179+
180+
The <xref:Microsoft.Extensions.DependencyInjection.HealthChecksBuilderAddCheckExtensions.AddCheck*> method registers the health check, and <xref:Aspire.Hosting.ResourceBuilderExtensions.WithHealthCheck*> associates it with specific resources. For more details about creating and registering custom health checks, see [Create health checks](/aspnet/core/host-and-deploy/health-checks#create-health-checks).
181+
182+
### Dashboard integration
183+
184+
Resource health check status is displayed in the Aspire dashboard, providing real-time visibility into resource readiness. When resources are waiting for health checks to pass, the dashboard shows the current status and any failure details.
185+
186+
:::image type="content" source="media/health-checks-dashboard-status.png" alt-text="Screenshot of the Aspire dashboard showing health check status for resources" lightbox="media/health-checks-dashboard-status.png":::
187+
188+
For more information about using health checks with resource dependencies, see [Waiting for resources](orchestrate-resources.md#waiting-for-resources).
189+
113190
## See also
114191

115192
- [.NET app health checks in C#](/dotnet/core/diagnostics/diagnostic-health-checks)
150 KB
Loading

0 commit comments

Comments
 (0)