Skip to Content
Docs

Log drains to Grafana Loki

Push the logs of your workspace to Grafana Cloud Logs or to a Loki server with a log drain.

The type loki pushes the records of a log drain to Grafana Loki. You need an API key with the scope log_drains:write. A log drain is a feature of the Business plan.

Prepare Grafana Cloud

  1. Open the details of your Loki service in Grafana Cloud. Copy the URL (for example https://logs-prod-012.grafana.net) and the user (a number).
  2. Make an access policy token with the scope logs:write.

Make the log drain

curl https://api.afffast.com/v1/log_drains \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "type": "loki",
    "label": "Grafana Loki",
    "config": { "url": "https://logs-prod-012.grafana.net" },
    "credentials": { "username": "123456", "password": "glc_..." },
    "sources": ["api_request", "sync_run"]
  }'
  • url must use https. It must not point to a private or internal address. Do not add /loki/api/v1/push.
  • tenant_id is optional. It sets the header X-Scope-OrgID. Grafana Cloud does not need it. A Loki with more than one tenant does.
  • username and password are for basic authentication. Give both or none. The API never returns them.

POST /v1/log_drains/{id}/test sends one sample record ("test": true).

Labels and lines

A label with many values makes Loki slow. The API uses only labels with few values:

LabelValue
jobafffast
sourceapi_request, delivery, sync_run or click
levelinfo, warning or error
livemodetrue or false
workspaceThe ID of your workspace.

The line of each value is the JSON of the record. Query the fields with | json:

{job="afffast", source="api_request", level="error"} | json | data_status >= 500

The API sorts the values of each stream by time. One request has 1000 records or 1 MiB at most. It compresses the request with gzip.

Errors

Answer of LokiWhat the API does
2xx (normally 204)The batch is done.
429The API waits and tries again.
401, 403The API stops the log drain (failing). The user or the token is wrong.
404The API stops the log drain (failing). The URL is wrong.
400 (entries that are too old or out of order)The API skips these entries and goes on. Loki stores the other entries. The log drain does not fail.
400 (any other reason)The API stops the log drain (failing). A line can be too long.
413The API sends the batch in smaller parts.
408, 5xx, other, no answerThe API tries again with a growing wait.

See Log drains for the retry wait and the resume of a log drain that failed.

On this page