Reading — step 1 of 5
Read
~1 min readQuery
LogQL — Query Language
LogQL (Loki) is PromQL-inspired:
{service="api",level="error"} # stream selector
{service="api"} |= "timeout" # filter for substring
{service="api"} |~ "user_id=\d+" # regex
{service="api"} != "healthcheck" # exclude
{service="api"} | json # parse JSON, extract fields
{service="api"} | logfmt | duration > 1s # parse + filter
# Metric queries (aggregates):
sum by (service) (rate({level="error"}[5m])) # error rate per service
count_over_time({service="api"}[1h]) # log count last hour
Building blocks:
- Stream selector:
{label=value,...}— filters streams by label. - Line filters:
|=substring,|~regex match,!=not contains,!~not regex match. - Parsers:
| json,| logfmt,| pattern— extract structured fields. - Label filters:
| level="error"— filter on extracted fields. - Aggregations:
count_over_time,rate,sum by, etc.
Parsing pipeline:
chunks -> filter by label selector -> stream of entries
-> filter by line filters
-> extract fields (parser)
-> filter by extracted fields
-> aggregate (if metric query)
Each filter step happens lazily — we can stop early when LIMIT is hit.
Implementation tip: break query into pipeline stages, each a generator. Compose them.
python
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…