Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add unhealthy ECS hosts runbook (#2580)

sarayourfriendc25609445c72
diff --git a/documentation/api/reference/healthcheck.md b/documentation/api/reference/healthcheck.md
new file mode 100644
index 000000000..1a41a214b
--- /dev/null
+++ b/documentation/api/reference/healthcheck.md
@@ -0,0 +1,23 @@
+# Django Healthcheck
+
+The Django API healthcheck endpoint checks the following:
+
+- Whether the API is accessible at all (i.e., is Django returning responses)
+- Whether the Postgres database is accessible to Django, by checking if
+ connections are available to the database
+- Optionally, whether Elasticsearch is accessible to Django (only when the
+ `check_es` query parameter is present)
+ - When debugging live healthcheck requests, check the request parameters to
+ determine whether Elasticsearch health is included
+
+The healthcheck can fail for any of the following reasons:
+
+- The worker never started and therefore Django is unable to handle requests
+- The worker meant to service the healthcheck request was aborted, either
+ because it timed out when serving another or this request, or because the
+ entire task was stopped due to another failure
+- The database connection health check failed, either due to a temporary or
+ persistent database connection issue
+- The `check_es` query parameter was passed and the worker was unable to
+ establish a connection to Elasticsearch _or_ Elasticsearch reported cluster
+ health as "red"
diff --git a/documentation/api/reference/index.md b/documentation/api/reference/index.md
index 88a9498ea..8c137ee78 100644
--- a/documentation/api/reference/index.md
+++ b/documentation/api/reference/index.md
@@ -5,4 +5,5 @@
search_algorithm
authentication_and_throttling
+healthcheck
```
diff --git a/documentation/frontend/reference/healthcheck.md b/documentation/frontend/reference/healthcheck.md
new file mode 100644
index 000000000..c4a9dc324
--- /dev/null
+++ b/documentation/frontend/reference/healthcheck.md
@@ -0,0 +1,12 @@
+# Nuxt Healthcheck
+
+The Nuxt frontend's healthcheck is extremely naive. It confirms only that the
+Nuxt server is running and able to respond to requests. It does not confirm
+whether any required services are available (like the API) or whether rendering
+is possible.
+
+The Nuxt frontend's healthcheck can fail for the following reasons:
+
+- The Nuxt server is unable to start and therefore unable to service requests
+- The Nuxt server is overloaded and crashes while servicing the healthcheck
+ request
diff --git a/documentation/frontend/reference/index.md b/documentation/frontend/reference/index.md
index f67ff7b97..bfa79a228 100644
--- a/documentation/frontend/reference/index.md
+++ b/documentation/frontend/reference/index.md
@@ -8,4 +8,5 @@ testing_guidelines
playwright_tests
storybook_tests
miscellaneous
+healthcheck
```
diff --git a/documentation/meta/monitoring/index.md b/documentation/meta/monitoring/index.md
index 717770e44..c2a606e2d 100644
--- a/documentation/meta/monitoring/index.md
+++ b/documentation/meta/monitoring/index.md
@@ -8,4 +8,5 @@ applications are monitored.
traffic/index
cloudwatch_logs/index
+runbooks/index
```
diff --git a/documentation/meta/monitoring/runbooks/index.md b/documentation/meta/monitoring/runbooks/index.md
new file mode 100644
index 000000000..0fa0d6891
--- /dev/null
+++ b/documentation/meta/monitoring/runbooks/index.md
@@ -0,0 +1,16 @@
+# Runbooks
+
+This section collects runbooks for responding to Openverse infrastructure
+alarms.
+
+Please see
+[the "Run Books" section of the ECS baseline monitoring implementation plan for further details on what runbooks are and their requirements](/projects/proposals/monitoring/20230606_implementation_plan_ecs_alarms.md#run-books).
+The implementation plan also includes
+[example runbooks](/projects/proposals/monitoring/20230606_implementation_plan_ecs_alarms.md#example-run-books)
+that can be a good resource when writing a new one.
+
+```{toctree}
+:titlesonly:
+
+unhealthy_ecs_hosts
+```
diff --git a/documentation/meta/monitoring/runbooks/unhealthy_ecs_hosts.md b/documentation/meta/monitoring/runbooks/unhealthy_ecs_hosts.md
new file mode 100644
index 000000000..c35fcf331
--- /dev/null
+++ b/documentation/meta/monitoring/runbooks/unhealthy_ecs_hosts.md
@@ -0,0 +1,96 @@
+# Run Book: Unhealthy hosts for ECS service
+
+```{admonition} Metadata
+Status: **Unstable**
+
+
+Alarm links:
+- [production-api](https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#alarmsV2:alarm/production-api+has+unhealthy+hosts)
+- [staging-api](https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#alarmsV2:alarm/staging-api+has+unhealthy+hosts)
+- [productoin-api-thumbnails](https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#alarmsV2:alarm/production-api-thumbnails+has+unhealthy+hosts)
+- [staging-api-thumbnails](https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#alarmsV2:alarm/staging-api-thumbnails+has+unhealthy+hosts)
+- [production-nuxt](https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#alarmsV2:alarm/production-nuxt+has+unhealthy+hosts)
+- [staging-nuxt](https://us-east-1.console.aws.amazon.com/cloudwatch/home?region=us-east-1#alarmsV2:alarm/staging-nuxt+has+unhealthy+hosts)
+```
+
+```{note}
+This run book is shared between all unhealthy host count alarms _for ECS services_.
+Our infrastructure is slightly different for non-ECS services so do not re-use this
+run book for anything not deployed using ECS (though feel free to reference relevant
+parts of it).
+```
+
+## Triage Guide
+
+After confirming there is not a total outage, check the ECS events log for the
+service. You can find a link to the events log for the specific service in the
+additional information section of the alarm description included in the alert
+notification. The events log can help you find the general cause for the
+unhealthy hosts. Specifically, check whether the container failed to start
+altogether or whether it starts but fails to respond to the healthcheck
+requests.
+
+If the container fails to start altogether (usually only after a deployment),
+the severity is critical. Immediately
+[roll the service back to the previous version](/general/deployment.md#rollbacks).
+After kicking off the roll back,
+[query the logs for the failed tasks](/meta/monitoring/cloudwatch_logs/index.md)
+to check if there are logs that identify the problem preventing the container
+from starting.
+
+If the container fails the healthcheck request, determine whether this is a
+persistent issue or just a blip. In a multi-worker service (like the Django
+API), a single worker can fail a healthcheck without the entire service being
+broken, for example, if that worker alone has difficulty establishing a database
+connection due to DNS resolution issues. Follow the
+[log querying guide](/meta/monitoring/cloudwatch_logs/index.md) to check whether
+the cause of the timeout or failed healtcheck request is present.
+
+A healthcheck failure is persistent if the ECS service continues to replace
+tasks due to failed healthcheck requests. It is not persistent if the ECS
+service eventually stops replacing tasks without intervention.
+
+If the issue is persistent, the severity is critical. Check the logs for the
+service and use information about service healthchecks to determine if services
+checked by the healthcheck are failing:
+
+- [Django API healthcheck](/api/reference/healthcheck.md)
+- [Nuxt frontend healthcheck](/frontend/reference/healthcheck.md)
+
+For example, if the Postgres database is failing to serve connections to the
+API, the Django API service healthcheck will fail, causing the task to restart,
+even though the core issue is with a dependent service.
+
+If the persistent unhealthy hosts are caused by a related service, fix the
+related service. Do not roll back the service with the unhealthy hosts until
+you've confirmed that the healthcheck is no longer failing due to a related
+service failure. For example, do not roll back the API if the healthcheck fails
+due to a database connection issue unless the database connection issue is
+confirmed to be an issue with the API and not the database.
+
+```{hint}
+If the persistent unhealthy hosts occur right after a deployment, you might be able to
+"fast track" to the conclusion that the issue is with the newly deployed version rather
+than a downstream service. However, coincidences do happen, so it's good practice to
+consider whether downstream service changes coinciding with the new version deployment.
+```
+
+```{warning}
+Recall that new deployments may also be the first time environment variable changes
+are deployed. If a roll back does not help and it does not appear to be an issue with the
+downstream service, then check whether environment variables changed for the service.
+
+Database connection issues could be caused by a malformed connection string, for example.
+```
+
+If the unhealthy hosts are not persistent, then the severity is low. Record any
+logs you can find for the unhealthy tasks in the incident report and triage
+further investigation as usual.
+
+## Historical false positives
+
+None, yet.
+
+## Related incident reports
+
+None, yet.