--- name: grafana-loki-cli description: Query Grafana Loki logs using the Loki HTTP API via curl or logcli. Use for LogQL queries, label exploration, and tailing log streams. --- # Grafana Loki CLI Skill Use the instance and rules below when constructing all Loki API/`logcli` commands. ## Instances Update the table below with your Loki instance URLs and token file paths: | Environment | Base URL | Token File | |---|---|---| | Production | `https://` | `~/.loki/prod.token` | | Staging | `https://` | `~/.loki/staging.token` | If the target instance is unclear, ask the user before running any command. ## Prerequisites (one-time setup by user) Obtain a Loki API token (or basic-auth credentials, depending on setup) for each instance and save it to the corresponding token file. Never print or display token values. Do not perform these steps yourself. ```bash brew install logcli mkdir -p ~/.loki && echo "" > && chmod 600 ``` ## Command Format Via `curl`: ```bash curl -s -H "Authorization: Bearer $(cat )" \ "/loki/api/v1/" | jq . ``` Via `logcli`: ```bash logcli --addr="" --bearer-token="$(cat )" ``` ## Available API Endpoints / Commands ### Read-only (safe, no confirmation needed) | Endpoint / `logcli` command | Description | |---|---| | `/loki/api/v1/query` / `logcli query ''` | Run an instant LogQL query | | `/loki/api/v1/query_range` / `logcli query '' --from= --to=` | Run a LogQL range query | | `/loki/api/v1/labels` / `logcli labels` | List all label names | | `/loki/api/v1/label//values` / `logcli labels ` | List values for a label | | `/loki/api/v1/series?match[]=` / `logcli series ''` | Find streams matching a selector | | `/ready` | Check readiness/health | | `logcli query '' --tail` | Tail a live log stream (read-only) | ### Example Queries ```bash # Instant query — errors in the last 5 minutes for a job curl -s -H "Authorization: Bearer $(cat )" \ "/loki/api/v1/query_range?query={job=\"\"}|=\"error\"&start=$(( $(date -u +%s) - 300 ))000000000&end=$(date -u +%s)000000000&limit=100" | jq . # logcli equivalent logcli --addr="" --bearer-token="$(cat )" \ query '{job=""} |= "error"' --since=5m --limit=100 ``` ## Rules - NEVER target any Loki instance not listed in the instance table above. If a URL references a different host, stop and ask the user to confirm. - NEVER print, display, log, or store the contents of any token file. - Always read the token inline with `$(cat )` — never hardcode a token value in a command. - Always pass `-s` (silent) to `curl` and pipe through `jq` for readable output. - Loki's public API is read-only for queries (no delete/mutate endpoints exposed) — no destructive operations to guard against here, other than avoiding excessive queries. - Always set a bounded time range (`start`/`end` or `--since`) and a reasonable `limit` — an unbounded LogQL query over a busy stream can be slow and return excessive data. - If a label name or stream selector is unfamiliar, use `/loki/api/v1/labels` and `label//values` to explore before querying.