Expand description
Parse and explain Datadog monitor queries.
A Datadog monitor query packs an aggregation window, a metric, a scope, a grouping, transforms, an evaluation function, and a threshold into one dense line:
avg(last_1d):anomalies(sum:app.service.latency{platform:macos, country:us}.as_count(), 'agile', 5, direction='below') >= 1This crate turns that string into a typed, serializable AST
(MonitorQuery) and renders it back as either a plain-language paragraph
(explain) or a structured, render-ready breakdown (summarize).
§Design
- Hand-written recursive descent + a small Pratt loop for arithmetic precedence. No parser-generator dependency: the grammar is small and stable.
- Never panics on input. Every failure path returns a
ParseErrorcarrying a byte offset and a one-line reason. - Graceful degradation.
parse_or_unparsednever fails: a query the grammar does not yet model becomesMonitorQuery::Unparsedwith the raw string preserved, so a consumer can always render something. - Bounded recursion. Pathologically nested input is rejected before the stack can grow.
- Pure. No I/O, no global state; parsing is linear in the input length.
§Dialects
| Dialect | Shape | AST |
|---|---|---|
| metric | time-agg(window): expr op threshold | MetricQuery |
| search | logs("…").rollup(…).last(…) op n | SearchQuery |
| service check | "check".over(…).last(n).count_by_status() | CheckQuery |
| slo | error_budget("id").over("7d") op n | SloQuery |
| composite | 123 && !456 || 789 | CompositeExpr |
§Examples
use ddquery_core::{explain, parse, MonitorQuery};
let query = "avg(last_5m):avg:system.cpu.user{env:production} > 90";
let ast = parse(query).unwrap();
assert!(matches!(ast, MonitorQuery::Metric(_)));
println!("{}", explain(&ast));Structs§
- Check
Query - A service-check query.
- Condition
- The alerting condition: comparison operator plus thresholds.
- Func
Param - A parameter to an evaluation function.
- KeyVal
- A scope key/value pair (a filter chip).
- Metric
Query - A metric monitor query:
time_agg(window): expr op threshold. - Modifier
- A postfix series modifier.
- Monitor
Ref - A reference to another monitor by its numeric id (kept as a string to preserve the verbatim token).
- Parse
Error - An error produced while parsing a monitor query.
- Query
Summary - A render-ready breakdown of a query for a front-end (labelled chips/rows).
- Scalar
- A numeric scalar literal (threshold or arithmetic operand).
- Search
Query - A log/event/rum/error-tracking search-source query.
- Series
- A scoped metric series.
- SloQuery
- An SLO error-budget query.
- Window
- A normalized evaluation window.
Enums§
- ArithOp
- An arithmetic operator.
- BoolOp
- A boolean operator in a composite query.
- Change
Kind - A change function kind.
- CmpOp
- A comparison operator in a condition.
- Composite
Expr - A boolean tree over monitor-id references (composite monitors).
- Filter
- A scope filter inside
{ … }. - Func
Kind - An evaluation function.
- Metric
Expr - The recursive metric expression — the tree a visualization renders.
- Monitor
Query - A parsed Datadog monitor query, tagged by dialect.
- Param
Value - The value of a
FuncParam. - Search
Source - The source of a
SearchQuery. - Space
Agg - Spatial aggregation across reporting sources.
- TimeAgg
- Evaluation-time aggregation.
Functions§
- explain
- Produce a one-paragraph, plain-language summary of a parsed query.
- parse
- Parse a Datadog monitor query into a typed AST.
- parse_
or_ unparsed - Parse a query, degrading to
MonitorQuery::Unparsedon failure. - summarize
- Produce a structured, render-ready summary of a parsed query.