Skip to content

Cloudflare Workers — alert when 5xx error rate spikes

A Worker auto-deploys from main. Once prod 5xx rate jumped to 12% — but dashboards get checked once a day. Want a per-minute probe.

Recipe

bash
#!/usr/bin/env bash
# Cloudflare GraphQL Analytics — last 5 min error fraction.
CF_TOKEN="${CF_TOKEN:?must be set}"
CF_ACCOUNT="${CF_ACCOUNT:?must be set}"
WORKER="${WORKER:?must be set}"
THRESHOLD_PCT="${THRESHOLD_PCT:-2}"

NOW=$(date -u -d '5 min ago' +%FT%TZ)
RESP=$(curl -sS https://api.cloudflare.com/client/v4/graphql \
  -H "Authorization: Bearer $CF_TOKEN" -H "Content-Type: application/json" \
  -d "{\"query\":\"{viewer{accounts(filter:{accountTag:\\\"$CF_ACCOUNT\\\"}){workersInvocationsAdaptive(filter:{datetime_geq:\\\"$NOW\\\",scriptName:\\\"$WORKER\\\"},limit:1){sum{requests errors}}}}}\"}")

REQ=$(echo "$RESP" | python3 -c 'import json,sys;d=json.load(sys.stdin);print(d["data"]["viewer"]["accounts"][0]["workersInvocationsAdaptive"][0]["sum"]["requests"])' 2>/dev/null)
ERR=$(echo "$RESP" | python3 -c 'import json,sys;d=json.load(sys.stdin);print(d["data"]["viewer"]["accounts"][0]["workersInvocationsAdaptive"][0]["sum"]["errors"])' 2>/dev/null)

[ -z "$REQ" ] || [ "$REQ" = "0" ] && { echo "no-data"; exit 0; }
PCT=$(( ERR * 100 / REQ ))
[ "$PCT" -ge "$THRESHOLD_PCT" ] && echo "high $PCT% ($ERR/$REQ)" || echo "ok $PCT%"

Same thing in Enterno.io

Wrap in an endpoint, put an Enterno HTTP monitor with "ok" keyword on a 1-min interval. Pro+ correlation with the site-health view tells Worker stalls from upstream stalls.

Set up HTTP monitor → ← All recipes

Related recipes

The server starts returning 503/504 — but a plain uptime check misses it because the homepage is 200 while the API path is on fire.

The CDN cache_status header (cf-cache-status or x-cache) suddenly returns MISS on more than 30% of requests — origin load + bandwidth bills both spike.