Interview Prep/Python for DevOps/Testing, services & metrics

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), recording last_success_timestamp_seconds, duration and findings count. Alert when time() - last_success > 2 * interval.
  • Write a .prom file 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.

All Python for DevOps questions Roadmap: Python for automation

More on Testing, services & metrics