Skip to main content

Crate logql

Crate logql 

Source
Expand description

§LogQL Front-End

Lexing, parsing, and syntactic validation for Grafana Loki’s LogQL. This crate recognises a query; it does not execute one. It knows nothing about columns, catalogs, tenants, or storage, and depends on nothing but thiserror — so the same query string parses identically whatever backend it is aimed at, and validity can be checked without one at all.

Published as logql-parser; the library is imported as logql.

§The two query forms

A log query starts from a stream selector ({service_name="api"}), optionally followed by a pipeline of line filters, label filters, parser stages, and formatters:

{service_name="api"} |= "error" | json | status >= 500

A metric query wraps one in a range or vector aggregation:

sum by (service_name) (rate({service_name="api"} |= "error" [5m]))

parse accepts either and returns an Expr saying which it got; parse_query and parse_selector are the narrower entry points.

§Errors carry positions

LexError and ParseError both report 1-based line and column, so a caller can underline the offending token rather than restating the query.

§Modules

  • token: token type produced by the lexer
  • lexer: hand-rolled tokenizer with line/column error reporting
  • ast: abstract syntax tree for log queries
  • metric: abstract syntax tree for metric queries
  • parser: recursive-descent parser over the token stream

Re-exports§

pub use ast::FilterOp;
pub use ast::FilterValue;
pub use ast::Grouping;
pub use ast::LabelExtraction;
pub use ast::LabelFilterExpr;
pub use ast::LabelFilterPred;
pub use ast::LabelFormat;
pub use ast::LabelFormatValue;
pub use ast::LabelMatcher;
pub use ast::LabelPredicate;
pub use ast::LineFilter;
pub use ast::LineFilterOp;
pub use ast::LogQuery;
pub use ast::LogfmtStage;
pub use ast::MatchOp;
pub use ast::PipelineStage;
pub use ast::StreamSelector;
pub use ast::Unwrap;
pub use lexer::LexError;
pub use lexer::tokenize;
pub use metric::AggregationFunction;
pub use metric::BinOp;
pub use metric::BinaryExpr;
pub use metric::LabelReplace;
pub use metric::MetricQuery;
pub use metric::RangeAggregation;
pub use metric::RangeFunction;
pub use metric::VectorAggregation;
pub use metric::VectorMatch;
pub use parser::Expr;
pub use parser::ParseError;
pub use parser::parse;
pub use parser::parse_metric_query;
pub use parser::parse_query;
pub use parser::parse_selector;
pub use token::SpannedToken;
pub use token::Token;

Modules§

ast
LogQL AST
lexer
LogQL Lexer
metric
LogQL Metric Query AST
parser
LogQL Parser
token
LogQL Tokens