Skip to content

Alerts & Full-Context Format

Ketos Agent includes a lightweight edge rule engine supporting cross-metric aggregations, log/trace counters, and causal sequence matching. When a rule fires, the collector automatically scopes the capture to relevant instances and emits a single AI-ready Markdown incident context.


All functions evaluate directly against bounded local memory buffers with minimal overhead:

Function Signature Description
last last("metric") Most recent sample (NaN if no data)
avg avg("metric", winSec) Arithmetic mean over the last winSec seconds
max / min max("metric", winSec) Maximum / minimum value over the last winSec seconds
sum sum("metric", winSec) Sum of samples over the last winSec seconds
rate rate("metric", winSec) Per-second rate of change (with counter-reset protection)
delta delta("metric", winSec) Difference between latest and earliest sample (last - first)
count count("metric", winSec) Sample count within winSec seconds
count_above count_above("metric", winSec, th) Count of samples strictly greater than th
count_below count_below("metric", winSec, th) Count of samples strictly less than th
exists exists("metric") Whether at least one sample exists
age age("metric") Seconds elapsed since the latest sample (detects collection stalls)
log_count log_count("source", "regex", winSec) Count of log lines matching regex within winSec seconds
log_exists log_exists("source", "regex", winSec) Whether any log line matched regex within winSec seconds
trace_count trace_count("source", "regex", winSec) Count of APM spans matching regex within winSec seconds
trace_exists trace_exists("source", "regex", winSec) Whether any APM span matched regex within winSec seconds
trace_slow_count trace_slow_count("source", ms, winSec) Count of APM spans with duration >= ms within winSec seconds
alerts:
# 1. Compound threshold across metrics and windows
- id: cpu_mem_pressure
severity: critical
for: 30s
expr: 'avg("cpu.usage_pct", 60) > 85 && last("mem.usage_pct") >= 85'
# 2. Sudden spike detection (disk usage jumps > 5% within 5 minutes)
- id: disk_spike
every: 10s
expr: 'delta("disk.usage_pct", 300) > 5'
# 3. Causal sequence (CPU spike followed by memory pressure within 30s)
- id: cpu_then_mem
severity: critical
type: sequence
within: 30s
stages:
- 'last("cpu.usage_pct") > 90'
- 'last("mem.usage_pct") > 85'
# 4. Cross-pillar trigger (APM P99 or log error burst + host forensics)
- id: order_service_degraded
severity: critical
for: 15s
expr: 'last("apm.order_svc.latency_p99_ms") > 1000 || log_count("log_file.order_app", "ERROR|panic", 60) >= 5'
attach: ["host/default"]

2. Smart Context Scoping (Auto-Inference + attach)

Section titled “2. Smart Context Scoping (Auto-Inference + attach)”

To keep signal-to-noise ratio high for both humans and AI Agents:

  1. Auto-inference: The engine automatically inspects expr / stages to determine which collector instances are involved, filtering both metric curves and forensic sections. A pure http_probe/api_prod alert never pulls in unrelated host process tables.
  2. Explicit cross-instance attachment: Add one line to pull in host or sibling context whenever needed:
    attach: ["host/default"] # Attach specific instances, or ["*"] for all active instances
  3. Automatic trace_id correlation: When inputs.apm and inputs.log_file run on the same host and logs contain trace_id, slow/error traces automatically pull in matching application log lines.

3. Anti-Flapping State Machine & Hot Reload

Section titled “3. Anti-Flapping State Machine & Hot Reload”
  • Debounce: A rule must stay true for for before entering Firing, and stay false for resolve_for (default 15s) before emitting Resolved.
  • Clean resolution on hot reload: Raising a threshold on a currently Firing rule preserves its firing state and incident_id, allowing it to naturally pass resolve_for and emit a closing Resolved snapshot.

4. Single-File Markdown Incident Context {#full-context-example}

Section titled “4. Single-File Markdown Incident Context {#full-context-example}”

Snapshots are grouped by rule ID—one capture equals one self-contained .md file:

data/snapshots/
├── order_service_degraded/
│ ├── apm_order_svc_20261128_160409_firing.md
│ └── apm_order_svc_20261128_160445_resolved.md
└── api_health_failed/
└── http_probe_api_health_20261128_161000_firing.md

Each file uses YAML Frontmatter + structured Markdown sections for high information density and low token overhead:

---
id: "snap-1795881849-1"
incident_id: "inc-order_service_degraded-1795881849"
status: "complete"
source: "alert:order_service_degraded"
reason: "last(\"apm.order_svc.latency_p99_ms\") > 1000"
captured_at: "2026-11-28T16:04:09Z"
captured_at_unix: 1795881849
alert:
rule_id: "order_service_degraded"
severity: "critical"
state: "firing"
metrics_instances:
- "apm/order_svc"
- "host/default"
forensic_sections:
- "apm:order_svc/apm_traces_summary"
- "apm:order_svc/apm_traces_detail"
- "log_file:order_app/logs"
- "host/top_processes"
---
## Metrics: apm/order_svc
```csv
phase,ts,apm.order_svc.req_count,apm.order_svc.err_count,apm.order_svc.latency_p99_ms
pre,1795881840,142,0,84.2
trigger,1795881845,158,19,2430.5
post,1795881850,136,24,2890.1
Trace 9f8a1c7b2e04... (2418ms, ERROR)
├─ POST /v1/orders [order-service] 2418ms (http.status_code=500)
│ └─ SELECT * FROM inventory FOR UPDATE [mysql] 2385ms (Lock wait timeout exceeded)
2026-11-28T16:04:05.120Z ERROR trace_id=9f8a1c7b2e04... lock wait timeout on table=inventory sku=SKU-8842
pid,name,cpu_pct,mem_pct,mem_rss_mb,state,matched_by
18422,mysqld,78.40,42.10,3410.50,running,cpu
### Two-Stage Power-Loss Safe Persistence
1. **Stage 1 (`T=0`)**: Immediately writes pre-trigger metrics and T0 forensics via atomic temp-file replacement (`status: "partial"`), ensuring first-scene evidence survives sudden power loss.
2. **Stage 2 (`T=+after`)**: Once the post-trigger window finishes, appends cascading metrics, correlated logs, and late-arriving child spans, atomically updating the file to `status: "complete"`.
> [!TIP]
> For per-metric sample limits, dual hard FIFO disk quotas, and degradation behavior, see [Security & Resource Limits](/en/guide/security-and-limits).