Skip to content

Health check builds a new Elasticsearch client on every request #146

Description

@saqibmanan

api/views/health.py constructs a new Elasticsearch client on every call:

es_settings = settings.ELASTICSEARCH_DSL["default"]
es = Elasticsearch(hosts=es_settings["hosts"], http_auth=es_settings["http_auth"])
if es.ping():

Visible in the logs as a new TCP connection each time:

{"event": "Starting new HTTP connection (1): elasticsearch:9200"}
{"event": "http://elasticsearch:9200 \"HEAD / HTTP/1.1\" 200 0"}

Why it is worth fixing

/health/ is called by the Docker healthcheck, the deploy gate in scripts/ci-deploy.sh, and any external monitoring — so this is a fresh connection several times a minute, forever, to send one HEAD /.

Measured cost is small (the ping itself is ~0.14s, and the endpoint answers in ~0.04s on-box), so this is efficiency and log noise rather than a performance problem.

It also emits a DeprecationWarning on every call:

The 'http_auth' parameter is deprecated. Use 'basic_auth' or 'bearer_auth' parameters instead

Suggested

  • Reuse a module-level client, or the one django-elasticsearch-dsl already configures, instead of building one per request.
  • Move http_auth to basic_auth while there.

Worth keeping the check itself: it is what makes /health/ return 503 when Elasticsearch is down, which the deploy gate depends on. Only the per-request construction should change.

Low priority. Related: the health check's database probe was fixed in #140 to open a fresh connection deliberately — that one is intentional and should stay.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions