Skip to content
DuckDB community extension

loki

Query Grafana Loki from DuckDB in plain SQL, with label/time/line-filter pushdown into LogQL

01 / SETUP

Install and load

Run these commands in a Haybarn SQL session. The extension is available from the community repository.

Haybarn 2.0.0 · SQL
INSTALL loki FROM community;
LOAD loki FROM community;

Using upstream DuckDB? Follow the maintainer’s installation instructions.

02 / QUERY

Usage example

Store the Loki endpoint and credentials once with the Secret Manager.

SQL
-- Store the Loki endpoint and credentials once with the Secret Manager.
CREATE SECRET my_loki (
    TYPE loki,
    ENDPOINT 'https://logs-prod.example.net',
    TOKEN 'glc_...'            -- or USERNAME + PASSWORD for basic auth
);

-- Query logs in plain SQL. WHERE predicates on promoted label columns, the
-- timestamp and the log line are translated into a LogQL stream selector,
-- start/end bounds and line filters; anything else DuckDB applies as a residual.
SELECT timestamp, level, line
FROM loki(selector := '{job="api"}', labels := ['level'], secret := 'my_loki')
WHERE level = 'error'
  AND timestamp >= now() - INTERVAL 2 HOUR
  AND line LIKE '%timeout%';

-- Or send raw LogQL through untouched with loki_scan():
SELECT * FROM loki_scan('{job="api"} |= `timeout`', secret := 'my_loki');

-- Or ATTACH the whole instance and reach logs by name. Loki requires a stream
-- selector, so always filter on at least one label column.
ATTACH 'my_loki' AS lk (TYPE loki);
SELECT timestamp, line FROM lk.public.logs WHERE job = 'api';

Provided by prochac. Not yet verified on Haybarn 2.0.0.

Sample data & setup

About this extension

loki turns a Grafana Loki instance into a first-class, queryable DuckDB data source, so you can join your logs against Parquet, CSV, Postgres, or anything else DuckDB reads — without ever hand-writing an HTTP request.

Connection and auth (bearer token, basic auth, X-Scope-OrgID tenant, or arbitrary headers) come from DuckDB's Secret Manager with per-call overrides. A CREATE SECRET (TYPE loki, PROVIDER env) seeds the connection from the standard logcli environment variables (LOKI_ADDR, LOKI_BEARER_TOKEN, LOKI_USERNAME/LOKI_PASSWORD, LOKI_ORG_ID), with inline options overriding the environment. Results page automatically over Loki's per-request entry cap, and projection pushdown skips materialising the columns a query doesn't select. Timestamps keep Loki's nanosecond resolution (TIMESTAMP_NS). See the repository for full documentation.

Project overview from the community descriptor.

Documentation and resources

Project and build details
Repository
prochac/duckdb-loki
License
MIT
Maintainers
prochac
Written in
C++
Build source
c936b0faaf78
Build definition
Community descriptor