CivicDataLab / CivicDataLab/DataSpaceBackend

Health check builds a new Elasticsearch client on every request

Open
#146 0 comments 0 reactions 2 assignees View on GitHub

@amit0539 is already working on this.

Since Sep 8, 2026.

enhancement
Dominant language
Python
Stars
2
Forks
1
Avg merge
52m
Merged PRs (30d)
40

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.