feat: prometheus metrics endpoint on SN and DN nodes#447
Conversation
|
Thanks for your contribution! Having a way to plug in tools like Grafana will be helpful for bother operators of the service and developers (e.g. to assist in determining where bottlenecks are). Also, the original intent of the /info endpoint was to be the path for fetching metrics, but you added a /metrics path. Can /info just be re-purposed? Or would it be better to have just /metrics and deprecate /info? Finally, how is(should) security handled? Some admins would prefer not to expose HSDS metric data to the outside world. Can the access be restricted to (say) just admin users? |
Motivation
When running HSDS on Kubernetes there is currently no machine-readable health signal beyond the
/infoJSON endpoint. This PR adds a standard Prometheus/metricsendpoint to every node so operators can alert on service health, saturation, and storage-backend problems using the usual Prometheus/Grafana stack.What changed
New
/metricsendpoint (hsds/metrics.py), built on the officialprometheus_clientlibrary:hsds_http_requests_total{method,status}and anhsds_http_request_duration_secondshistogram for every request.hsds_node_ready), known DN count (hsds_active_dn_count), asyncio task saturation (hsds_tasks_active/hsds_tasks_max), WARN/ERROR log counts, LRU cache stats (items, dirty count, memory used/target per cache), and storage counters (errors, bytes read/written, labelled by backend).process_*/python_gc_*metrics for free./metricsis served regardless of node state, so unhealthy nodes remain observable.Storage stats bug fixes (found while validating the metrics live):
fileClient._file_stats_incrementwas missing its final increment statement, so every POSIX storage counter stayed at zero permanently (s3/azure clients have the line; the file client validated the input and returned without counting).bytes_inwas initialized but never incremented in any backend; it is now recorded onget_objectin the file, s3, and azure clients.Kubernetes manifests:
prometheus.io/scrapeannotations and named container ports (sn,dn) in the three deployment manifests so annotation-based discovery scrapes the SN and a PodMonitor can target both containers.Docs:
docs/prometheus_metrics.mdwith the metric reference, the rationale for each signal, a PodMonitor example, and suggested alert rules.Dependency:
prometheus-client(pure Python, no transitive deps) added topyproject.tomlandrequirements.txt.Testing
tests/unit/metrics_test.py(middleware counting, exposition rendering, cache/storage/node metrics, handler response, and a regression test for the fileClient stats bug), registered intestall.pyand collected by pytest in CI.python testall.py(unit + integration against a standalone server with 4 DNs, posix storage) passes./metricson SN and DN nodes during the integration run; request counters, latency buckets, cache gauges, and storage byte counters all advance as expected.flake8clean under both CI configurations.