Trigger icon
Webhook icon
Get icon
Script icon
Docker icon
Set icon
If icon
SlackIncomingWebhook icon
Pause icon
OutputValues icon
Loop icon
Request icon
Sleep icon
Query icon
Fail icon

Prometheus Cardinality Explosion Purge with Regrowth Check

Contain a Prometheus cardinality explosion. Detect growth across runs, find the exploding label, purge after approval and check that the source stopped.

Categories
Infrastructure

A new release puts a request id into a metric label and Prometheus gains thousands of series a minute until it runs out of memory. Deleting the series is a one-line admin call, but it is destructive, it is useless while the source keeps emitting the label, and /api/v1/series still lists series after they were deleted. This blueprint decides from a trend, asks a person, and then checks the result the right way.

How it works

  1. alertmanager_webhook (or the disabled series_churn_detected trigger, a PromQL poll on scrape_series_added) starts a run. load_state reads the snapshot of the previous run from KV.
  2. inspect (io.kestra.plugin.scripts.node.Script) reads /api/v1/status/tsdb, whose per-metric counts Prometheus refreshes only about every 40 seconds, so run the flow every few minutes and not more often. It compares each metric with the snapshot. A run counts as growing when the series count grew by growth_pct and is at least min_series. A metric needs strikes_required growing runs in a row, so one spike changes nothing, and it must not match protected_metrics.
  3. For each exploding metric it samples /api/v1/series and picks the label with the most distinct values as the culprit, then builds the selector {__name__="m",culprit=~".+"}. It also sends delete_series without a matcher, which Prometheus rejects before deleting anything: a 400 means the admin API is on, a 500 "admin APIs disabled" means it is off.
  4. save_state stores the new snapshot on every run, also a dry run. announce_findings lists each growing metric, the culprit label and the selector.
  5. approve_purge (io.kestra.plugin.core.flow.Pause) waits for approval. The default is false, and exclude leaves metrics out.
  6. purge_loop posts delete_series for each metric, from the start of the explosion to a fixed purge time, then clean_tombstones frees the disk and record_purge writes an audit record.
  7. After regrowth_wait, verify_loop runs check_old_data, which must find no series with samples before the purge time, and check_new_series, which must find at most regrowth_tolerance series after it. New series mean the source still emits the label, and the run fails with that advice.

What you get

  • A purge triggered by a trend, not by one number, with protected metrics that are never touched.
  • The exploding label named, with how many distinct values it has.
  • Verification from samples, which honour deletions, and a regrowth check that tells you the purge cannot hold yet.
  • An audit record of every purge and a snapshot that expires after two days.

Who it's for

  • Platform and SRE teams that run Prometheus and own its memory and disk.
  • Teams whose applications can start emitting unbounded labels.

Why orchestrate this with Kestra

Prometheus keeps no history of what its series counts were an hour ago and has no approval step. Kestra keeps the snapshot in KV, pauses for a person, runs the delete, the wait and the verification in order, and starts from an Alertmanager webhook.

Prerequisites

  • Prometheus started with --web.enable-admin-api (tested on 3.15.0), reachable from the Kestra worker and the Docker task runner at prometheus_url.
  • If Prometheus sits behind authentication, add the headers to the HTTP and Query tasks and to the script.

Secrets

  • PROMETHEUS_URL: base URL for the polling trigger, which cannot read inputs.
  • PROM_CARDINALITY_WEBHOOK_KEY: key for the webhook trigger.
  • SLACK_WEBHOOK_URL: Slack incoming webhook for findings and results.

Quick start

  1. Start an application that can explode: docker run -d -p 9101:9101 node:22-alpine node -e "let n=0,on=0;require('http').createServer((q,r)=>{if(q.url=='/on')on=1;if(q.url=='/off')on=0;let o='x_total 1\n';if(on)for(let i=0;i<400;i++)o+='req_seconds_count{request_id=\"r'+(++n)+'\"} 1\n';r.end(o)}).listen(9101)".
  2. Start Prometheus with a prometheus.yml that has scrape_interval: 5s and a job for host.docker.internal:9101: docker run -d -p 9090:9090 -v ./prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus:v3.15.0 --config.file=/etc/prometheus/prometheus.yml --web.enable-admin-api.
  3. Add the three secrets, set min_series to 1000 and run with dry_run: true. The first run only saves a baseline.
  4. Open http://localhost:9101/on, then run the flow twice, a minute apart. The second dry run names req_seconds_count, the label request_id and the selector.
  5. Run with dry_run: false and tick Approve. The purge is verified, and the run fails with the regrowth advice because the source is still on.
  6. Open http://localhost:9101/off, wait for a scrape, and run again. The metric is contained and verified.

How to extend

  • Add a task that opens a ticket for the team that owns the job when a regrowth failure is reported.
  • Add a metric_relabel_configs rule to your Prometheus config in Git once the culprit label is known.
  • Run one flow per Prometheus by changing state_scope and prometheus_url.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.