Grafana: Query Loki
grafana_query_lokiRun LogQL queries against Loki to fetch logs or aggregate metric series, returning time-ordered lines or stats for debugging and analysis.
Instructions
Run LogQL through a Loki datasource. A log query ({namespace="api"} |= "error") returns lines newest first, each with its time, level and the labels that differ between lines (the shared ones once, as common_labels), cut at max_line_chars. A metric query (sum by (app) (count_over_time({namespace="api"} |= "error" [5m]))) returns series on a step grid with stats. Always put a label matcher in the selector and keep the range short: Loki scans every line in it. To count, prefer a metric query over fetching lines.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end | No | Range end: now, now-1h, -30m, an RFC3339 timestamp (2026-01-31T12:00:00Z) or unix seconds/ms. Defaults to now. | |
| expr | Yes | A LogQL query, e.g. `{namespace="ingress-nginx"} |= "error" | json | status >= 500`. grafana_list_labels lists the stream labels to select on. | |
| sort | No | Order of time series, applied before `limit` cuts: labels keeps the datasource's order; max/avg/last rank highest first, so a cut keeps the biggest. | labels |
| step | No | Resolution, as a duration (30s, 5m, 1h). Defaults to a round step giving `points` points. Grafana never goes below the datasource's scrape interval. | |
| view | No | For time series. values: stats (min/max/avg/last) plus one value per step on a shared grid given by start and step, null where there was no sample. stats: the stats only, about a tenth of the size — use it to survey many series before reading a few. | values |
| limit | No | Maximum log lines, or series for a metric query (1-5000). Defaults to 50. | |
| start | No | Range start: now, now-1h, -30m, an RFC3339 timestamp (2026-01-31T12:00:00Z) or unix seconds/ms. Defaults to now-1h. | |
| points | No | Target points per series (2-1000), from which the step is picked. Defaults to 60; a dashboard in a browser asks for ~1000, which is rarely worth the tokens. | |
| instant | No | For a metric query: evaluate once at `end` instead of over the range. | |
| direction | No | backward (default): the newest `limit` lines. forward: the oldest first. | backward |
| precision | No | Significant digits to round fractional values to (whole numbers are never rounded). Defaults to 4; 0 returns the exact value. | |
| datasource | No | Datasource uid or name, e.g. "prometheus" or "Loki". Defaults to the first Loki datasource. grafana_list_datasources lists them. | |
| max_line_chars | No | Cut each log line at this many characters (50-20000). Defaults to 1000. |