Skip to main content

parse

Function parse 

Source
pub fn parse(input: &str) -> Result<Expr, ParseError>
Expand description

Parse a top-level LogQL expression, dispatching between a log query and a metric query. A query beginning with { is a log query; any other start (a range/vector function, a number, or a parenthesis) is a metric query.

ยงExamples

use logql::Expr;

assert!(matches!(
    logql::parse(r#"{service_name="api"} |= "error""#)?,
    Expr::Log(_),
));
assert!(matches!(
    logql::parse(r#"sum by (service_name) (rate({service_name="api"}[5m]))"#)?,
    Expr::Metric(_),
));

Errors carry a 1-based position, so a caller can underline the offending token rather than restating the query:

let err = logql::parse("{service_name=}").unwrap_err();
assert_eq!(err.line, 1);
assert!(err.col > 1);