> The index of all pages is at https://docs.afffast.com/llms.txt.

# 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](https://docs.afffast.com/docs/log-drains.md) 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

```bash
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:

| Label       | Value                                            |
| ----------- | ------------------------------------------------ |
| `job`       | `afffast`                                        |
| `source`    | `api_request`, `delivery`, `sync_run` or `click` |
| `level`     | `info`, `warning` or `error`                     |
| `livemode`  | `true` or `false`                                |
| `workspace` | The ID of your workspace.                        |

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

```text
{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 Loki                                 | What the API does                                                                                    |
| ---------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| 2xx (normally 204)                             | The batch is done.                                                                                   |
| 429                                            | The API waits and tries again.                                                                       |
| 401, 403                                       | The API stops the log drain (`failing`). The user or the token is wrong.                             |
| 404                                            | The 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.                                     |
| 413                                            | The API sends the batch in smaller parts.                                                            |
| 408, 5xx, other, no answer                     | The API tries again with a growing wait.                                                             |

See [Log drains](https://docs.afffast.com/docs/log-drains.md) for the retry wait and the resume of a log drain that failed.
