---
name: obistack-alerts
description: Turn a postmortem, an incident write-up or a plain description of a failure into ObiStack alert rules written in ObiQL, backtested against real history before anything is saved. Use when the user asks to create, tune or review an alert, or asks how to catch a problem next time.
license: MIT
metadata:
  version: "1.0.0"
---

# Write ObiStack alert rules

An ObiStack alert rule is an ObiQL query plus a condition: fire when the query
matches more than N events in a window. Your job is to find the query that
catches the failure the user describes, check how often it would have fired,
and only then propose the rule.

Use the ObiStack MCP tools when they are connected, and the `obi` command line
otherwise. Never invent a field name: look it up first.

## 1. Pin down the failure

From the postmortem or description, write down:

- the service, or services, involved,
- what the logs said when it went wrong (an error message, a status code, a
  missing heartbeat),
- how fast someone needed to know,
- a time when it happened, so you can check the query finds it.

One failure mode is one rule. A postmortem with three contributing causes
usually wants two or three rules, not one rule with every condition ORed
together.

## 2. Find the fields

MCP: call `get_schema`, or `search_fields` with a word from the description
(for example `status` or `payment`). Command line:

```bash
obi get query-schema -o yaml
```

Then look at real events around the time it happened:

```bash
obi search 'service="payments" level="error"' --from 2026-11-03T14:00:00Z --to 2026-11-03T15:00:00Z --limit 20
```

Group noisy messages by shape to find the one that matters:

```obiql
service="payments" level="error" | stats count() by _pattern
```

## 3. Write the query

Start narrow and widen only if you miss the incident.

| The failure | Query |
|---|---|
| A specific error | `service="payments" "card declined"` |
| Errors from one service | `service="payments" level="error"` |
| Server errors | `service="checkout-api" status>=500` |
| A service gone quiet | `service="billing-worker"`, with the condition `<` |

Some complete examples:

```obiql
service="payments" level="error" -"retrying"
```

```obiql
service="checkout-api" status>=500 | stats count() by route
```

Run the query over the incident window. It must return the events you
expected, and few you did not. With MCP, use `run_query` and show the user
the Explorer link it returns.

## 4. Choose the condition

| Field | Meaning |
|---|---|
| `conditionType` | `log_volume` (count of matches), `error_rate`, or `pattern_match` |
| `conditionOperator` | `>`, `>=`, `<`, `<=` or `==` |
| `conditionThreshold` | The number the count is compared with |
| `windowSeconds` | How far back each evaluation looks (10 to 86400) |
| `intervalSeconds` | How often the rule runs (10 to 86400) |

A short window with a low threshold pages on every blip. Prefer a window long
enough that one retry storm does not fire the rule on its own.

## 5. Backtest before you propose

MCP: call `backtest_alert` with the query and condition. Command line, with
the rule in a file:

```bash
obi api POST /v1/alerts/backtest -d @rule.json
```

where `rule.json` holds the query, the condition and `"window": "7d"` (one of
`1d`, `3d`, `7d`, `14d` or `30d`).

Read the result with the user:

- It must fire during the incident you are writing it for.
- It should fire rarely otherwise. A rule that would have fired every day last
  week will be silenced and then ignored.
- If it misses the incident, the query is too narrow. If it fires all the
  time, raise the threshold, lengthen the window, or narrow the query.

## 6. Propose the rule

MCP: call `create_alert_rule` (or `update_alert_rule` for an existing rule).
It only proposes the change. The user approves it in their client or in
ObiStack. If the approval comes back as still pending, ask the user, then
record their answer with `decide_change`. Never decide for them, and never
say a rule exists until the result says it was approved.

Command line, after the user agrees, since this creates the rule at once:

```bash
obi create alert payments-card-declines --query 'service="payments" "card declined"' --threshold 20 --window 10m --description "Card declines above normal after a provider outage"
```

To keep rules in version control, export them and apply the file:

```bash
obi get alerts --export > alerts.yaml
obi apply -f alerts.yaml --dry-run
```

## Writing descriptions that help

The rule's description is what the on-call person reads at 3am. Say what
broke, why it matters and where to look first, in plain words. Name the
service and the user impact, not the query.

## Reviewing existing rules

```bash
obi get alerts -o wide
obi get alert-health
obi describe alert payments-card-declines
```

Rules that fire often and resolve within minutes are noise: raise their
threshold or window, or delete them, with the user's agreement.
