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.
1. Built-In Expression Functions
Section titled “1. Built-In Expression Functions”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 |
Common Rule Patterns
Section titled “Common Rule Patterns”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:
- Auto-inference: The engine automatically inspects
expr/stagesto determine which collector instances are involved, filtering both metric curves and forensic sections. A purehttp_probe/api_prodalert never pulls in unrelated host process tables. - 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
- Automatic
trace_idcorrelation: Wheninputs.apmandinputs.log_filerun on the same host and logs containtrace_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
forbefore enteringFiring, and stay false forresolve_for(default15s) before emittingResolved. - Clean resolution on hot reload: Raising a threshold on a currently
Firingrule preserves itsfiringstate andincident_id, allowing it to naturally passresolve_forand emit a closingResolvedsnapshot.
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.mdEach 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: 1795881849alert: 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
```csvphase,ts,apm.order_svc.req_count,apm.order_svc.err_count,apm.order_svc.latency_p99_mspre,1795881840,142,0,84.2trigger,1795881845,158,19,2430.5post,1795881850,136,24,2890.1Forensic: apm:order_svc/apm_traces_detail
Section titled “Forensic: apm:order_svc/apm_traces_detail”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)Logs: log_file/order_app
Section titled “Logs: log_file/order_app”2026-11-28T16:04:05.120Z ERROR trace_id=9f8a1c7b2e04... lock wait timeout on table=inventory sku=SKU-8842Forensic: host/top_processes
Section titled “Forensic: host/top_processes”pid,name,cpu_pct,mem_pct,mem_rss_mb,state,matched_by18422,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).