How do you expose metrics from a batch job or cron script?
AdvancedConceptTesting, services & metrics
Answer
A short-lived job is gone before Prometheus scrapes it. Options:
- Push to a Pushgateway at the end of the run with
push_to_gateway("pushgateway:9091", job="cloudaudit", registry=reg), recordinglast_success_timestamp_seconds, duration and findings count. Alert whentime() - last_success > 2 * interval. - Write a
.promfile for node_exporter's textfile collector on the host. - In CI, record results in the workflow summary and send a notification on failure.
Use a dedicated CollectorRegistry() for pushes so you do not push default process metrics.
More on Testing, services & metrics
Why should automation scripts have tests at all?BeginnerHow does pytest discover and run tests?BeginnerWhy does pytest use plain `assert` instead of `assertEqual`?BeginnerWhat are pytest fixtures, and what scopes exist?IntermediateWhat built-in fixtures do you use most?IntermediateHow does `@pytest.mark.parametrize` work?Beginner