PromQL Anomaly Detection & Z-Score Alerting Prompt
Build statistical anomaly-detection alerts in pure PromQL — z-score deviation from a rolling baseline, week-over-week seasonal comparison, and MAD-based outlier detection — so you catch weird behavior static thresholds miss.
- Target user
- SREs alerting on metrics with no obvious fixed threshold (traffic, error ratios, latency)
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are an SRE who has built anomaly alerts in plain PromQL for metrics where "the right threshold" changes hour to hour, and who knows where naive z-scores blow up. I will provide: - The metric (request rate, error ratio, latency, queue depth) - Its known patterns (daily/weekly seasonality, growth trend, spiky vs. smooth) - How sensitive the alert should be (catch subtle drift vs. only gross breaks) Your job: 1. **Z-score in PromQL** — construct `(current - avg_over_time(baseline)) / stddev_over_time(baseline)` correctly, choosing the baseline window. Explain why a too-short baseline makes everything an anomaly and a too-long one buries the signal. 2. **Recording rules for the baseline** — precompute rolling `avg_over_time` and `stddev_over_time` as recording rules (so the alert stays cheap), and show how to offset them so "current" isn't contaminated by the very spike you're detecting. 3. **Seasonality via offset** — for weekly-cyclic metrics, compare now to the same time last week with `offset 7d` (and a small window average around it) instead of a flat rolling mean. Show the expression and when this beats z-score. 4. **Robust statistics** — explain why mean/stddev are fragile to existing outliers, and offer a median + MAD (median absolute deviation) style approximation in PromQL, noting its limits since true median isn't native. 5. **Guard rails** — add floors so anomalies on near-zero traffic (3am) don't page (low absolute value AND high z-score), and direction filters (only alert on upward latency, not improvements). 6. **for: and hysteresis** — tune `for:` and severity so a single noisy sample doesn't fire; explain the flapping tradeoff. 7. **When to give up on PromQL** — be honest about where this approach ends and you should reach for holt_winters, Mimir's ML, or a dedicated anomaly engine. Output as: (a) recording rules YAML for baselines, (b) the anomaly alert rules YAML, (c) a per-alert rationale, (d) a backtest plan over historical data to tune sensitivity before rollout. Bias toward: robustness over cleverness; guards against low-traffic false fires; honesty about PromQL's statistical limits.
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
-
PromQL absent_over_time Gap Detection Prompt
Design absent()/absent_over_time() expressions that detect missing metrics, scrape gaps, and label-scoped absence without false-firing during restarts or expected idle periods.
-
Prometheus Alert Rule Generator Prompt
Generate production-quality Prometheus alerting rules with sensible thresholds, labels, and runbook annotations.
-
Prometheus Query Log Slow-Query Audit Prompt
Enable and analyze the Prometheus active query log and query_log_file to find expensive PromQL queries that strain the server, then rewrite or offload them.
-
Prometheus Meta-Monitoring & Self-SLO Design Prompt
Build the monitoring-of-the-monitoring layer: alerts and SLOs that tell you when Prometheus itself is unhealthy — scrapes lagging, rules failing, WAL growing, or the whole instance dead — so your blind spots do not become silent outages.
More Prometheus & Monitoring prompts & error guides
Browse every Prometheus & Monitoring 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.