Skip to content

Latest commit

 

History

History
80 lines (63 loc) · 7.29 KB

cloudflare.md

File metadata and controls

80 lines (63 loc) · 7.29 KB
description How do you get per-route RED, SLIs and burn-rate alerts for an API served by a Cloudflare Worker?
icon cloudflare

Cloudflare Workers

For an API served by a Worker, Cloudflare covers the edge and server levels of the KPI tree; per-route RED and burn-rate alerts need data and a schedule you add yourself (method). Vendor docs checked: 2026-10-01.

Building blocks

Building block Cloudflare's name for it
Events and spans Workers Logs (one invocation log per request, plus your JSON logs); traces
Metric types Workers metrics: counts and quantiles per Worker; no custom metrics
Derived metrics None from logs; write Analytics Engine data points instead
Queries Query Builder, Analytics Engine SQL, GraphQL
Panels Query Builder; Custom dashboards; Grafana over SQL
Alerts Notifications (per zone); Issues automations (per error group)
Change markers Deploy markers on the memory chart; version metadata binding

Limits

  • Sampling: logs and traces follow head_sampling_rate; a per-log rate isn't documented (not verified). Analytics Engine samples per index and stores _sample_interval on each row (sampling).
  • Retention: logs and traces 7 days (Paid), Workers metrics and Analytics Engine 3 months (limits).
  • Alert windows: none to configure; a Cron Trigger runs at most every minute. Analytics Engine delay: not verified.
  • Cardinality: Analytics Engine: 20 blobs, 20 doubles, 1 index per data point, no charge for cardinality. Logs: any JSON field.
  • Pricing: Workers bill requests and CPU time; logs and spans bill per event; Analytics Engine per data point and read query (not billed yet, pricing).

Translation

Guide concept How on Workers
Key action SLI, from the edge A Tail Worker writes one data point per invocation, crashes included
What counts as an error outcome plus HTTP status (tail handler); built-in error counts are outcomes only
RED by route template Your code logs the route; spans carry only url.path
Latency percentiles quantileExactWeighted over a recorded duration (SQL)
USE per resource Workers metrics: CPU, memory, subrequests; spans per binding
Burn-rate pair Enterprise only (available notifications), per zone, preset sensitivity (traffic alerts); else a scheduled Worker
Baseline, change markers SQL shifted by INTERVAL '7' DAY; version ID as a blob
Exploring by any field Query Builder over log fields, 7 days

Gaps and fallbacks

Gap Fallback Loses
No custom error-rate or burn-rate alert Cron Worker queries SQL, posts to your pager (from a Worker) A failed cron is silent without a heartbeat check
No metrics over OpenTelemetry (export) Data points per request Sampled counts are weighted estimates
Requests blocked before the Worker (WAF) aren't counted Zone HTTP analytics Per-route context

Worked example

Example key action Save, critical-path call PUT /documents/:id (Map it); example SLO 99.9%. The API Worker logs a wide event {type: "request", key_action, route, error, error_expected}; the Tail Worker adds outcome and status (What counts as an error). Reading the object from message[0] is not verified.

{% code title="Tail Worker" overflow="wrap" %}

const w = ev.logs.map((l) => l.message[0]).find((m) => m?.type === "request") ?? {};
const status = ev.event?.response?.status ?? 0;
const canceled = ev.outcome === "canceled"; // the client went away: a timeout or a closed tab
const failed = canceled || ev.outcome !== "ok" || status >= 500 || (w.error && !w.error_expected);
env.API_SLI.writeDataPoint({ indexes: [w.key_action ?? "unknown"], blobs: [w.route ?? "unknown"], doubles: [failed ? 1 : 0, canceled ? 1 : 0] });

{% endcode %}

{% code title="Analytics Engine SQL: 30-day SLI" %}

SELECT sumIf(_sample_interval, double1 = 0) / SUM(_sample_interval) AS sli, SUM(_sample_interval) AS attempts
FROM api_sli WHERE index1 = 'save_document' AND timestamp > NOW() - INTERVAL '30' DAY

{% endcode %}

  • Canceled counts as failed, because a client that gave up on a call it waits on saw a timeout (what counts as an error); double2 keeps it as its own rate, to tell timeouts from other failures.
  • Panel (key action dashboard): the same query in Grafana, grouped per 5 minutes, success rate next to attempts.
  • Page alert: uv run src/calc.py burn --slo 99.9 gives threshold = burn rate × (1 − 0.999): 1.44% over 1 h and 5 min, 0.60% over 6 h and 30 min (burn rates). A cron Worker runs the query per window and pages when both windows of a pair exceed it.
  • Zero and low traffic: no attempts in 5 minutes is no data, not 0%; add a volume floor on the short window.

Vendor docs