1. Burn rate alert for SLO budget
Use case. A single minute of elevated errors should not page. Compare multi-window burn rate against budget to page only when reliability is actually consumed fast enough to breach the SLO.
# short_rate and long_rate are error-rate ratios.
const BURN_FAST = 14.4
const BURN_SLOW = 6.0
const MIN_TRAFFIC = 0.05
function burn_alert(short_rate, long_rate, traffic_share) {
if traffic_share < MIN_TRAFFIC {
return {"action": "IGNORE", "reason": "LOW_TRAFFIC"}
}
if short_rate >= BURN_FAST and long_rate >= BURN_SLOW {
return {"action": "PAGE", "reason": "BUDGET_BURNING_FAST"}
}
return {"action": "MONITOR", "reason": "WITHIN_BUDGET"}
}
print "SLO={burn_alert(16.2, 7.1, 0.4).action}"
Save as observability-sre_01.pea; check pea fmt --check observability-sre_01.pea && pea check observability-sre_01.pea; run pea run observability-sre_01.pea.
2. Alert deduplication grouping key
Use case. Five pods failing for the same dependency should create one incident, not five pages. Group by service, dependency and signature; preserve raw events for investigation but suppress noise.
# signature fields are normalised by collector.
function alert_key(service, dependency, signature) {
if service == "" or dependency == "" or signature == "" {
return {"key": null, "ok": false, "reason": "INCOMPLETE"}
}
return {"key": service + "|" + dependency + "|" + signature, "ok": true}
}
print "GROUP={alert_key("payments", "postgres", "p99-latency").key}"
Save as observability-sre_02.pea; check pea fmt --check observability-sre_02.pea && pea check observability-sre_02.pea; run pea run observability-sre_02.pea.
3. Canary abort rule for progressive delivery
Use case. A canary gets 5% of traffic; if errors or latency increase meaningfully, abort before full rollout. Use precomputed deltas; the gate must not depend on dashboards being read manually.
# delta_* are relative changes vs baseline.
const MAX_ERROR_DELTA = 0.01
const MAX_P95_DELTA = 0.15
function canary_verdict(error_delta, p95_delta, sample_size) {
if sample_size < 500 {
return {"action": "HOLD", "reason": "INSUFFICIENT_DATA"}
}
if error_delta > MAX_ERROR_DELTA {
return {"action": "ABORT", "reason": "ERROR_REGRESSION"}
}
if p95_delta > MAX_P95_DELTA {
return {"action": "ABORT", "reason": "LATENCY_REGRESSION"}
}
return {"action": "PROMOTE", "reason": "WITHIN_GUARDS"}
}
print "CANARY={canary_verdict(0.02, 0.08, 2000).action}"
Save as observability-sre_03.pea; check pea fmt --check observability-sre_03.pea && pea check observability-sre_03.pea; run pea run observability-sre_03.pea.
4. Autoscale trigger from durable queue delay
Use case. CPU percent lies for queue-backlog workloads. Scale when age of oldest message grows past threshold for sustained minutes, not on a single spike.
# oldest age in seconds; consecutive breaches count windows.
const MAX_AGE_S = 90
const MIN_BREACHES = 3
function scale_action(oldest_age_s, breaches) {
if oldest_age_s > MAX_AGE_S and breaches >= MIN_BREACHES {
return {"action": "SCALE_OUT", "reason": "QUEUE_DELAY"}
}
if oldest_age_s < 30 {
return {"action": "SCALE_IN", "reason": "QUEUE_CLEAR"}
}
return {"action": "HOLD", "reason": "MIXED"}
}
print "AUTOSCALE={scale_action(120, 4).action}"
Save as observability-sre_04.pea; check pea fmt --check observability-sre_04.pea && pea check observability-sre_04.pea; run pea run observability-sre_04.pea.
5. Health check critical dependency isolation
Use case. Returning 500 when Redis is degraded but Postgres is healthy confuses load balancers. Model critical, degraded and healthy dependency states independently and build a small response object the frontend understands.
# redis and postgres are booleans.
function health_mode(redis_ok, postgres_ok) {
if not postgres_ok {
return {"status": "CRITICAL", "code": "DB_DOWN"}
}
if not redis_ok {
return {"status": "DEGRADED", "code": "CACHE_DOWN"}
}
return {"status": "HEALTHY", "code": "OK"}
}
print "HEALTH={health_mode(true, true).status}"
Save as observability-sre_05.pea; check pea fmt --check observability-sre_05.pea && pea check observability-sre_05.pea; run pea run observability-sre_05.pea.
6. Span latency tail alert independent of average
Use case. Average latency can stay low while the p99 tail burns budget for key customers. Alert when tail delta exceeds baseline shape, not just mean, or you will miss the outage users feel.
# deltas precomputed by metrics.
const P99_DELTA = 0.4
function tail_alert(p99_delta, sample_count) {
if sample_count < 1000 {
return {"action": "WAIT", "reason": "LOW_N"}
}
if p99_delta > P99_DELTA {
return {"action": "INVESTIGATE", "reason": "P99_REGRESSION"}
}
return {"action": "OK", "reason": "WITHIN_BAND"}
}
print "TAIL={tail_alert(0.55, 5000).action}"
Save as observability-sre_06.pea; check pea fmt --check observability-sre_06.pea && pea check observability-sre_06.pea; run pea run observability-sre_06.pea.
7. Capacity forecast against hard volume envelope
Use case. Traffic is projected to exceed provisioned headroom in under two weeks. Decide before cascading autoscaling failures when growth crosses the envelope.
# headroom_days from BI forecast; capacity_pct current utilisation.
const URGENT_DAYS = 14
const CAPACITY_LIMIT = 80
function capacity_action(headroom_days, capacity_pct) {
if headroom_days < URGENT_DAYS and capacity_pct >= CAPACITY_LIMIT {
return {"action": "ORDER_SCALE", "reason": "HEADROOM_BREACH"}
}
if headroom_days < 30 {
return {"action": "WATCH", "reason": "NEARING_LIMIT"}
}
return {"action": "OK", "reason": "CAPACITY_FINE"}
}
print "CAPACITY={capacity_action(10, 85).action}"
Save as observability-sre_07.pea; check pea fmt --check observability-sre_07.pea && pea check observability-sre_07.pea; run pea run observability-sre_07.pea.
8. Sampled trace policy for payment requests
Use case. Every request cannot carry full trace due to overhead, but payment failures must always be retained. Keep successes sampled and force trace retention for non-2xx statuses.
# sampled fraction is host policy, e.g. 0.1.
function should_trace(status, sampled) {
if status >= 400 {
return {"trace": true, "reason": "ERROR"}
}
if sampled {
return {"trace": true, "reason": "SAMPLED"}
}
return {"trace": false, "reason": "SKIP"}
}
print "TRACE={should_trace(503, false).trace}"
Save as observability-sre_08.pea; check pea fmt --check observability-sre_08.pea && pea check observability-sre_08.pea; run pea run observability-sre_08.pea.
9. Error budget burndown projection gate
Use case. If the team keeps shipping features, how much error budget remains? A controller can freeze non-critical changes when runway is short, protecting reliability without stopping every deploy.
# budget_remaining_pct and days_left in sprint.
const FREEZE_PCT = 25
function release_decision(budget_remaining_pct, days_left) {
if budget_remaining_pct <= FREEZE_PCT and days_left >= 3 {
return {"decision": "FREEZE_NONESSENTIALS", "reason": "LOW_BUDGET"}
}
return {"decision": "ALLOW_CRITICAL", "reason": "BUDGET_OK"}
}
print "RELEASE={release_decision(12, 5).decision}"
Save as observability-sre_09.pea; check pea fmt --check observability-sre_09.pea && pea check observability-sre_09.pea; run pea run observability-sre_09.pea.
10. Safe log redaction at collector boundary
Use case. Collectors often forward everything from applications that print tokens by mistake. Enforce redaction at ingestion so downstream stores never see secrets, even if app logs are sloppy.
# The collector can parse the log message but not the whole JSON tree.
const SENSITIVE = ["authorization", "cookie", "set-cookie", "x-api-key"]
let key = "x-api-key"
let emitted_key = key
if key in SENSITIVE {
emitted_key = "[REDACTED]"
}
print "KEY={emitted_key}"
Save as observability-sre_10.pea; check pea fmt --check observability-sre_10.pea && pea check observability-sre_10.pea; run pea run observability-sre_10.pea.
← Back to the Code Library