Skip to content

Add more elaborate health check endpoint that optionally hits the database#72

Merged
brandur merged 2 commits intomasterfrom
brandur-health-check
Jun 28, 2024
Merged

Add more elaborate health check endpoint that optionally hits the database#72
brandur merged 2 commits intomasterfrom
brandur-health-check

Conversation

@brandur
Copy link
Collaborator

@brandur brandur commented Jun 28, 2024

An alternative to #68 that adds a pair of health check endpoints:

  • GET /api/health-checks/complete
  • GET /api/health-checks/minimal

The minimal endpoint just returns an OK response regardless of anything
else so it'll return 200 as long as the Go process is up. The complete
endpoint runs the additional check of pinging the database, verifying its
liveliness as well. This is useful because if the database is totally down
then River UI will be totally non-functional, but the minimal endpoint
would still return OK, and it's nice to have an alternative that'll return
an unhealthy status. Based on something I wrote about last year here [1].

Fixes #67.

[1] https://brandur.org/fragments/database-health-check

SetBundle(bundle *apiBundle)
}

type statusResponse struct {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A lot of endpoints return {status: "ok"}, so I made a common struct that they can share so we don't have to write a new response struct for each. statusResponseOK below is also a shortcut for making an OK response.

// InternalError is an additional error that might be associated with the
// API error. It's not returned in the API error response, but is logged in
// API endpoint execution to provide extra information for operators.
InternalError error `json:"-"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a possible internal error that an API error can be tagged with and which will be added to the logs by api_endpoint.go above. This is nice because it lets some additional context be added for operators without the possibility of leaking anything sensitive from an internal error via HTTP.

Copy link
Contributor

Choose a reason for hiding this comment

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

Love it!

…abase

An alternative to #68 that adds a pair of health check endpoints:

* `GET /api/health-checks/complete`
* `GET /api/health-checks/minimal`

The minimal endpoint just returns an OK response regardless of anything
else so it'll return 200 as long as the Go process is up. The complete
endpoint runs the additional check of pinging the database, verifying
its liveliness as well. This is useful because if the database is
totally down then River UI will be totally non-functional, but the
minimal endpoint would still return OK, and it's nice to have an
alternative that'll return an unhealthy status. Based on something I
wrote about last year here [1].

Fixes #67.

[1] https://brandur.org/fragments/database-health-check
@brandur brandur force-pushed the brandur-health-check branch from 6792d5f to 29a4eed Compare June 28, 2024 03:05
@brandur brandur mentioned this pull request Jun 28, 2024
// InternalError is an additional error that might be associated with the
// API error. It's not returned in the API error response, but is logged in
// API endpoint execution to provide extra information for operators.
InternalError error `json:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Love it!

@bgentry
Copy link
Contributor

bgentry commented Jun 28, 2024

I had the same thoughts when seeing the health check PR so I love that you just went for it, thank you!

@brandur
Copy link
Collaborator Author

brandur commented Jun 28, 2024

Wicked. Thanks!

@brandur brandur merged commit 8521ddd into master Jun 28, 2024
@brandur brandur deleted the brandur-health-check branch June 28, 2024 04:23
brandur added a commit that referenced this pull request Jun 28, 2024
Oops, I forgot to add a changelog entry for #72, but given it's a
feature somewhat meant to be used by users, it should probably get one.
brandur added a commit that referenced this pull request Jun 28, 2024
Oops, I forgot to add a changelog entry for #72, but given it's a
feature somewhat meant to be used by users, it should probably get one.
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.

Add healthcheck route

2 participants