-
Notifications
You must be signed in to change notification settings - Fork 330
feat(web): add ?strict=true mode to /api/health/ready for empty-shard detection #1512
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
Open
Harsh23Kashyap
wants to merge
20
commits into
sourcebot-dev:main
Choose a base branch
from
Harsh23Kashyap:feat/strict-readiness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8668e52
feat(web): extract lazy Zoekt gRPC client into a shared module
Harsh23Kashyap 8e90c6f
feat(web): add /api/health/ready endpoint with dependency health checks
Harsh23Kashyap 0f04b34
test(web): cover /api/health/ready healthy and degraded paths
Harsh23Kashyap e29ed6b
docs(health): document liveness vs readiness split
Harsh23Kashyap 375f2dd
chore: changelog entry for /api/health/ready
Harsh23Kashyap 2a47d11
fix(web): only cache successful Zoekt client builds
Harsh23Kashyap 7a4ddc5
fix(web): bound the readiness route by its own timeout
Harsh23Kashyap 56e336b
test(web): cover unhandled-rejection suppression in readiness probe
Harsh23Kashyap 3b1ebe9
chore: changelog link to PR #1507 instead of issue #1506
Harsh23Kashyap f5193ae
fix(web): Zoekt readiness probe uses empty List options (max_wall_tim…
Harsh23Kashyap 3335633
feat(web): add ?strict=true mode to /api/health/ready
Harsh23Kashyap 8ff1624
test(web): cover strict mode and invalid-param paths in readiness probe
Harsh23Kashyap a238ee3
docs(health): document the ?strict=true readiness mode
Harsh23Kashyap 6166565
chore: changelog entry for ?strict=true readiness mode
Harsh23Kashyap aed1a21
style(web): tighten comments and pull a repeated test type alias
Harsh23Kashyap 3fa3c63
fix(web): handle undefined Zoekt List response in the readiness probe
Harsh23Kashyap a92eceb
fix(web): set 500MB gRPC message size limits on the readiness probe Z…
Harsh23Kashyap 4ef628d
fix(web): do not leak upstream error details on the unauthenticated r…
Harsh23Kashyap 2b8b833
docs(navigation): drop the duplicate GET /api/health entry from docs.…
Harsh23Kashyap 44e43bf
chore: link the changelog entry to PR #1512 instead of issue #1511
Harsh23Kashyap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| --- | ||
| title: "Health Endpoints" | ||
| description: "Liveness and readiness probes for orchestrators and monitoring systems." | ||
| --- | ||
|
|
||
| Sourcebot exposes two public health endpoints that follow the standard Kubernetes liveness / readiness split. Both are unauthenticated and return no user data. | ||
|
|
||
| ## Liveness: `GET /api/health` | ||
|
|
||
| Returns `200 OK` with `{ "status": "ok" }` whenever the Next.js process is running and able to handle a request. Does not touch the database, Redis, or Zoekt. Use this for Kubernetes `livenessProbe` or Docker Compose `healthcheck.test`. A failing liveness probe means the process must be restarted. | ||
|
|
||
| ```bash | ||
| curl -fsS https://sourcebot.example.com/api/health | ||
| # {"status":"ok"} | ||
| ``` | ||
|
|
||
| ## Readiness: `GET /api/health/ready` | ||
|
|
||
| Returns `200 OK` with `{"status":"ok", "checks":{...}}` when Postgres, Redis, and Zoekt are all reachable. Returns `503 Service Unavailable` with `{"status":"degraded", "checks":{...}}` if any dependency is unreachable. Each check runs in parallel with a 2-second per-check timeout, so the worst-case request time is bounded even when a dependency hangs. | ||
|
|
||
| Use this for Kubernetes `readinessProbe` or a load balancer health check. A failing readiness probe means the pod should be removed from the load-balancer rotation but not restarted. | ||
|
|
||
| ### Strict mode | ||
|
|
||
| Add `?strict=true` to require the Zoekt shard set to be non-empty as well. The default mode only confirms the dependencies are reachable; strict mode confirms the instance can actually serve a search that returns results. A freshly-started instance, or one whose connection sync has silently failed, returns `503` until at least one repository is indexed. | ||
|
|
||
| ```bash | ||
| curl -fsS 'https://sourcebot.example.com/api/health/ready?strict=true' | jq .status | ||
| # "ok" Postgres, Redis, and Zoekt (with at least one indexed repo) are healthy | ||
| # "degraded" Something failed, see `checks` for which | ||
| ``` | ||
|
|
||
| The response always includes a top-level `strict` field so the operator can confirm the mode that was applied: | ||
|
|
||
| ```json | ||
| { | ||
| "status": "degraded", | ||
| "strict": true, | ||
| "checks": { | ||
| "postgres": { "status": "ok", "latencyMs": 3 }, | ||
| "redis": { "status": "ok", "latencyMs": 1 }, | ||
| "zoekt": { "status": "empty", "latencyMs": 18, "error": "no repositories indexed (strict mode)" } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The Zoekt check distinguishes the two failure modes in its `status` field: | ||
| - `error`. The gRPC call itself failed (Zoekt unreachable, timeout, etc.). | ||
| - `empty`. Strict mode is on and the shard set is empty. The dependency is healthy; the instance is just not yet useful. | ||
|
|
||
| ### Response shape | ||
|
|
||
| ```json | ||
| { | ||
| "status": "ok", | ||
| "strict": false, | ||
| "checks": { | ||
| "postgres": { "status": "ok", "latencyMs": 3 }, | ||
| "redis": { "status": "ok", "latencyMs": 1 }, | ||
| "zoekt": { "status": "ok", "latencyMs": 12 } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| When degraded, each failed check carries an `error` field with the underlying message: | ||
|
|
||
| ```json | ||
| { | ||
| "status": "degraded", | ||
| "strict": false, | ||
| "checks": { | ||
| "postgres": { "status": "ok", "latencyMs": 4 }, | ||
| "redis": { "status": "ok", "latencyMs": 1 }, | ||
| "zoekt": { "status": "error", "latencyMs": 2003, "error": "zoekt check timed out after 2000ms" } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| | Check | What it probes | | ||
| |-------|---------------| | ||
| | `postgres` | `SELECT 1` via Prisma | | ||
| | `redis` | `PING` (rejects non-`PONG` responses) | | ||
| | `zoekt` | Empty `List` RPC (proves the gRPC channel is alive; bounded by the 2s per-check timeout) | | ||
|
|
||
| ### Example probes | ||
|
|
||
| <Tabs> | ||
| <Tab title="Docker Compose"> | ||
| ```yaml | ||
| services: | ||
| sourcebot: | ||
| image: sourcebot/sourcebot:latest | ||
| healthcheck: | ||
| test: ["CMD", "wget", "-qO-", "http://localhost:3000/api/health"] | ||
| interval: 30s | ||
| timeout: 5s | ||
| retries: 3 | ||
| # For dependency-aware probes, point the orchestrator at /api/health/ready | ||
| # instead. Sourcebot's example compose file does this via a sidecar. | ||
| ``` | ||
| </Tab> | ||
| <Tab title="Kubernetes"> | ||
| ```yaml | ||
| livenessProbe: | ||
| httpGet: | ||
| path: /api/health | ||
| port: 3000 | ||
| initialDelaySeconds: 30 | ||
| periodSeconds: 30 | ||
| timeoutSeconds: 5 | ||
| failureThreshold: 3 | ||
| readinessProbe: | ||
| httpGet: | ||
| path: /api/health/ready | ||
| port: 3000 | ||
| initialDelaySeconds: 10 | ||
| periodSeconds: 10 | ||
| timeoutSeconds: 5 | ||
| successThreshold: 1 | ||
| failureThreshold: 3 | ||
| # For deployments that should not receive traffic until the shard set | ||
| # is non-empty, switch to the strict readiness probe: | ||
| # path: /api/health/ready?strict=true | ||
| ``` | ||
| </Tab> | ||
| </Tabs> | ||
|
|
||
| <Note> | ||
| The readiness probe hits the database on every call. On large deployments with many pods, a high-frequency probe interval (sub-5s) can produce noticeable background load. A 10s interval with `failureThreshold: 3` is a good starting point. | ||
| </Note> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.