Skip to content

Commit

Permalink
add the ready health check
Browse files Browse the repository at this point in the history
  • Loading branch information
rgl committed Mar 16, 2024
1 parent 325a4c9 commit f947fbb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"ASPNETCORE",
"Edsger",
"healthz",
"Heemstra",
"opentelemetry",
"opsi",
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ docker compose ps
# show logs.
docker compose logs

# show the quotes container health check output log.
docker inspect --format '{{json .State.Health }}' \
"$(docker compose ps quotes --format '{{.Name}}')" \
| jq -r '.Log[].Output'

# open a container network interface in wireshark.
./wireshark.sh quotes

Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ services:
# http api.
# http://localhost:8000
- 8000:8000
healthcheck:
test: ["CMD", "curl", "--silent", "--fail-with-body", "--max-time", "5", "http://localhost:8000/healthz/ready"]
interval: 15s
timeout: 5s
retries: 2
restart: on-failure
7 changes: 7 additions & 0 deletions quotes/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ COPY . ./
RUN dotnet publish -c Release

FROM mcr.microsoft.com/dotnet/aspnet:8.0
RUN <<'EOF'
#!/bin/bash
set -euxo pipefail
apt-get update
apt-get install -y --no-install-recommends curl
rm -rf /var/lib/apt/lists/*
EOF
WORKDIR /app
COPY --from=builder /app/bin/Release/net8.0/publish ./
ENTRYPOINT ["/app/Quotes"]
4 changes: 4 additions & 0 deletions quotes/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.ResourceDetectors.Container;
Expand All @@ -7,6 +8,7 @@

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHealthChecks();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
Expand Down Expand Up @@ -46,6 +48,8 @@

var app = builder.Build();

app.MapHealthChecks("/healthz/ready");

// enable the swagger document endpoint and swagger ui.
app.UseSwagger(); // make the swagger available at /swagger/v1/swagger.json
app.UseSwaggerUI(); // make the swagger UI available at /swagger
Expand Down

0 comments on commit f947fbb

Please sign in to comment.