id: akamai-cache-hit-ratio-degradation-gate
namespace: company.team
description: |
Edge cache hit ratios collapse minutes after a bad config push, origin
load spikes, and nobody connects the traffic anomaly to the change that
caused it. A keyed webhook receives per property cache metrics from your
CDN telemetry relay, a shared working directory stages a deterministic
fixture for trial runs, and Python compares the current window against the
baseline for every property. A degraded property alerts Slack with the
worst offender and its config version, then fails the run so the push can
be rolled back, while a healthy window records an all-clear log, and the
verdict, the worst property and its delta are published as flow outputs.
triggers:
- id: on_cache_metrics
type: io.kestra.plugin.core.trigger.Webhook
description: Your CDN telemetry relay POSTs per property cache hit ratio windows
here so the gate scores the estate the moment new metrics land.
key: "{{ secret('AKAMAI_CACHE_WEBHOOK_KEY') }}"
- id: morning_ratio_check
type: io.kestra.plugin.core.trigger.Schedule
description: Disabled 05:00 UTC net that rescores the staged metrics snapshot,
shipped off until the webhook path is trusted in your environment.
cron: "0 5 * * *"
disabled: true
inputs:
- id: min_degradation_pct
type: FLOAT
defaults: 10.0
description: Percentage points the current hit ratio must fall below the
baseline before a property counts as degraded.
- id: min_requests
type: INT
defaults: 10000
description: Request floor for the window, below it a property is too quiet for
a ratio swing to mean anything.
- id: window_hours
type: INT
defaults: 6
description: Hours the telemetry relay aggregated each hit ratio window over,
echoed in the Slack alert for context.
tasks:
# --- 1. cache workspace ---
- id: cache_workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
description: One shared directory so the staged cache metrics snapshot and the
hit ratio scorer read the same file for the whole run.
tasks:
# --- 2. metrics fixture ---
- id: write_cache_metrics_fixture
type: io.kestra.plugin.scripts.shell.Commands
description: Write a deterministic cache metrics fixture with a heredoc, one
property whose hit ratio fell 26.3 points after a config push, so the
scorer always has metrics to read when no relay has posted.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
entryPoint: []
containerImage: alpine:3.20
commands:
- |
set -u
cat > cache_metrics.json <<'JSON'
{
"window_hours": 6,
"collected_at": "2026-10-15T05:00:00Z",
"properties": [
{"property": "www.example.com", "host": "www.example.com", "current_hit_ratio": 61.2, "baseline_hit_ratio": 87.5, "requests": 2450000, "misses": 950600, "config_version": "v42-push-2026-10-14"},
{"property": "static.example.com", "host": "static.example.com", "current_hit_ratio": 93.4, "baseline_hit_ratio": 94.1, "requests": 820000, "misses": 54120, "config_version": "v39-push-2026-10-02"},
{"property": "api.example.com", "host": "api.example.com", "current_hit_ratio": 12.0, "baseline_hit_ratio": 13.5, "requests": 410000, "misses": 360800, "config_version": "v17-push-2026-09-28"}
]
}
JSON
echo "wrote $(wc -c < cache_metrics.json) bytes of cache metrics"
# --- 3. ratio score ---
- id: score_hit_ratio
type: io.kestra.plugin.scripts.python.Script
description: Read the posted metrics or the staged fixture, compare each
property current hit ratio against its baseline, flag properties past
the degradation threshold above the request floor, and emit the
verdict and the worst property through the outputs protocol.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: python:3.12-slim
env:
CACHE_METRICS_JSON: "{{ trigger.body ?? '' }}"
MIN_DEGRADATION_PCT: "{{ inputs.min_degradation_pct }}"
MIN_REQUESTS: "{{ inputs.min_requests }}"
script: |
import json
import os
MIN_DEGRADATION_DEFAULT_PCT = 10.0
MIN_REQUESTS_DEFAULT = 10000
def as_float(value, fallback=0.0):
try:
return float(value)
except (TypeError, ValueError):
return fallback
def as_int(value, fallback=0):
try:
return int(value)
except (TypeError, ValueError):
try:
return int(float(value))
except (TypeError, ValueError):
return fallback
def load_metrics():
raw = (os.environ.get("CACHE_METRICS_JSON") or "").strip()
if raw:
try:
return json.loads(raw)
except ValueError:
return None
path = "cache_metrics.json"
if not os.path.exists(path) or os.path.getsize(path) == 0:
return None
try:
with open(path, "r", encoding="utf-8") as handle:
return json.load(handle)
except ValueError:
return None
min_delta = as_float(os.environ.get("MIN_DEGRADATION_PCT"), MIN_DEGRADATION_DEFAULT_PCT)
min_requests = as_int(os.environ.get("MIN_REQUESTS"), MIN_REQUESTS_DEFAULT)
outputs = {
"parse_ok": False,
"verdict": "UNKNOWN",
"property_count": 0,
"degraded_count": 0,
"worst_property": "none",
"worst_delta_pct": 0.0,
"worst_current_ratio": 0.0,
"worst_config_version": "none",
}
doc = load_metrics()
if doc is not None:
props = doc.get("properties") if isinstance(doc, dict) else doc
if not isinstance(props, list):
props = []
degraded = []
worst = None
worst_delta = 0.0
counted = 0
for prop in props:
if not isinstance(prop, dict):
continue
counted += 1
name = str(prop.get("property") or prop.get("host") or "unnamed")
current = as_float(prop.get("current_hit_ratio"))
baseline = as_float(prop.get("baseline_hit_ratio"))
requests = max(0, as_int(prop.get("requests")))
delta = round(baseline - current, 1)
if requests >= min_requests and delta >= min_delta:
degraded.append(name)
if worst is None or delta > worst_delta:
worst = prop
worst_delta = delta
outputs.update({
"parse_ok": True,
"verdict": "DEGRADED" if degraded else "HEALTHY",
"property_count": counted,
"degraded_count": len(degraded),
"worst_property": str((worst or {}).get("property") or (worst or {}).get("host") or "none") if worst else "none",
"worst_delta_pct": worst_delta if worst else 0.0,
"worst_current_ratio": round(as_float((worst or {}).get("current_hit_ratio")), 1) if worst else 0.0,
"worst_config_version": str((worst or {}).get("config_version") or "unknown") if worst else "none",
})
print("verdict=%s properties=%d degraded=%d worst=%s delta=%.1f" % (
outputs["verdict"], outputs["property_count"], outputs["degraded_count"],
outputs["worst_property"], outputs["worst_delta_pct"]))
print("::" + json.dumps({"outputs": outputs}) + "::")
# --- 4. degradation gate ---
- id: degradation_gate
type: io.kestra.plugin.core.flow.If
description: Branch on the ratio verdict so only a degraded property window
reaches the Slack alert and the failing rollback gate.
condition: "{{ outputs.score_hit_ratio.vars.verdict == 'DEGRADED' }}"
then:
# --- 5. degradation escalation ---
- id: alert_degraded
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Tell the edge platform channel which property fell the furthest, by
how many points, and which config version to roll back.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Akamai cache hit ratio DEGRADED - {{ outputs.score_hit_ratio.vars.degraded_count }} property(s) fell past {{ inputs.min_degradation_pct }} point(s) in the last {{ inputs.window_hours }} hour window. Worst is {{ outputs.score_hit_ratio.vars.worst_property }} at {{ outputs.score_hit_ratio.vars.worst_delta_pct }} point(s) down, current ratio {{ outputs.score_hit_ratio.vars.worst_current_ratio }} percent, config {{ outputs.score_hit_ratio.vars.worst_config_version }}. Roll back the push and watch the ratio recover. Execution {{ execution.id }}."
}
- id: fail_gate
type: io.kestra.plugin.core.execution.Fail
description: Turn the execution red so the deploy pipeline stops and the config
push can be rolled back before origin load spikes further.
errorMessage: "Akamai hit ratio degraded for {{
outputs.score_hit_ratio.vars.worst_property }} by {{
outputs.score_hit_ratio.vars.worst_delta_pct }} point(s) against
baseline."
else:
# --- 6. healthy window ---
- id: log_healthy
type: io.kestra.plugin.core.log.Log
description: Record the healthy window so the execution history doubles as edge
performance evidence after a push.
message: "Akamai cache hit ratios healthy across {{
outputs.score_hit_ratio.vars.property_count }} property/properties in
the last {{ inputs.window_hours }} hour window, none fell past {{
inputs.min_degradation_pct }} point(s) with enough traffic to matter."
errors:
- id: notify_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Escalate any failure to the ops channel with the execution url.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "FAILED: {{ flow.id }} {{ execution.id }}"}
outputs:
- id: verdict
type: STRING
description: Ratio verdict for the window, DEGRADED when a property fell past
the threshold, HEALTHY when every property held, UNKNOWN when the metrics
could not be parsed.
value: "{{ outputs.score_hit_ratio.vars.verdict }}"
- id: degraded_count
type: INT
description: Number of properties whose hit ratio fell past the degradation
threshold with enough traffic to matter.
value: "{{ outputs.score_hit_ratio.vars.degraded_count }}"
- id: worst_property
type: STRING
description: Property whose hit ratio fell the furthest past the threshold, or
none when every property held its baseline.
value: "{{ outputs.score_hit_ratio.vars.worst_property }}"
- id: worst_delta_pct
type: FLOAT
description: Percentage points the worst property lost against its baseline hit
ratio, for example 26.3 when an 87.5 baseline fell to 61.2.
value: "{{ outputs.score_hit_ratio.vars.worst_delta_pct }}"
- id: worst_current_ratio
type: FLOAT
description: Current cache hit ratio in percent for the worst property in the
scored window.
value: "{{ outputs.score_hit_ratio.vars.worst_current_ratio }}"
- id: worst_config_version
type: STRING
description: Config version or push tag the telemetry reported for the worst
property, so the rollback target is named.
value: "{{ outputs.score_hit_ratio.vars.worst_config_version }}"