Python Prometheus Pushgateway Batch-Job Metrics Reporter Prompt
Build a Python module that reports batch-job and cron metrics (success, duration, records processed, last-success timestamp) to a Prometheus Pushgateway with correct grouping keys and failure-safe pushing
- Target user
- Engineers instrumenting batch jobs and cron automation in Python
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are an observability engineer who instruments short-lived batch jobs — which Prometheus can't scrape directly — by pushing their metrics to a Pushgateway, and you do it without the classic footguns. I will provide: - The batch job to instrument and the metrics that matter (success/failure, duration, records processed, bytes, last-success time) - The Pushgateway URL and the grouping labels that identify this job instance (job name, instance, env) - Whether the job runs on a schedule and how stale metrics should be handled Your job: 1. **Choose metric types correctly** — use a Gauge for duration, records processed, and a `last_success_timestamp_seconds`; use a success/failure Gauge (0/1) rather than a Counter, since Pushgateway-pushed counters don't reset semantics the way scraped ones do. Explain the choice. 2. **Set grouping keys deliberately** — push with a stable `grouping_key` (job + instance) so re-runs overwrite the previous push instead of accumulating orphan series, and document how to delete a group when a job is retired. 3. **Instrument success and failure paths** — wrap the job so a crash still records `job_last_failure` and duration; use a `try/finally` (or context manager) so metrics are pushed whether the job succeeds or raises. 4. **Push failure-safe** — never let a Pushgateway outage fail the actual job: time-box the push, catch push errors, log them, and continue. The metric push is best-effort telemetry, not job-critical. 5. **Enable staleness alerting** — always emit `last_success_timestamp_seconds` so an alert can fire on "job hasn't succeeded in N hours" even if the job stops running entirely. 6. **Test** — pytest with the push client mocked, asserting correct metrics on success and failure and that a push error doesn't propagate. Output as: (a) the metrics-reporting context manager/decorator, (b) an example job wired through it, (c) guidance on the alerting rules that consume these metrics, (d) the pytest suite. Bias toward: `last_success_timestamp_seconds` for staleness alerting, stable grouping keys to avoid orphan series, and best-effort pushes that never fail the underlying job.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python Multi-Log Merge and Timeline Correlator Prompt
Build a Python tool that merges several log files with different timestamp formats into one time-ordered timeline, tags each line with its source, and correlates events across services for incident analysis
-
Python Environment Config Drift Detector Prompt
Build a Python tool that compares configuration or environment-variable sets across environments (dev/staging/prod) and reports missing, extra, and mismatched keys while masking secret values
-
Python SIGHUP Config Hot-Reload Daemon Prompt
Add safe config hot-reloading to a long-running Python daemon so operators can apply new settings with a SIGHUP (or systemctl reload) instead of a full restart that drops in-flight work.
-
Python argparse Parent Parsers and Shared-Flag Layering Prompt
Compose argparse parsers with parents=[...] to share common flags across subcommands and layer config-file, env, and flag precedence
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.