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

# Log drain to Sentry

Send the logs of your workspace to Sentry Logs.

The type `sentry` of a [log drain](https://docs.afffast.com/docs/log-drains.md) sends the records to the Sentry Logs of a project. It also works with self-hosted Sentry. You need an API key with the scope `log_drains:write`.

## Make the log drain

Copy the DSN of the project from the Sentry project settings (Client Keys). Then make the log drain. Use `filters` to send only the errors, for example:

```bash
curl https://api.afffast.com/v1/log_drains \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "type": "sentry",
    "label": "Sentry",
    "config": {},
    "credentials": { "dsn": "https://<key>@o123.ingest.sentry.io/456" },
    "sources": ["api_request", "sync_run"],
    "filters": { "api_request": { "levels": ["error"] } }
  }'
```

- The DSN must use `https`. It must not point to a private or internal address. The API checks the address at each request, after the DNS lookup. The API does not follow a redirect.
- The API never returns the DSN.

`POST /v1/log_drains/{id}/test` sends one sample record. Its body starts with `[test]`.

## What Sentry gets

The API sends one envelope with one `log` item for each batch (100 records or 512 KB at most) to `https://<host>/api/<project ID>/envelope/`. The request has the header `X-Sentry-Auth` with the public key of the DSN.

| Record level | Sentry level | Severity number |
| ------------ | ------------ | --------------- |
| `info`       | `info`       | 9               |
| `warning`    | `warn`       | 13              |
| `error`      | `error`      | 17              |

- `body` is a short line, for example `API request GET /v1/links 200`.
- `timestamp` is the time of the event of the record.
- The attributes are `afffast.record_id`, `afffast.source`, `afffast.workspace`, `afffast.livemode`, `afffast.test` and `sentry.environment` (`live` or `test`). The fields of `data` are `afffast.data.<name>`. Nested objects use a dot in the name. An array is JSON text. A text has 1024 bytes at most. A log has 64 attributes at most.
- A log has a `trace_id`. It is a hash of the record ID, because the records have no trace.

## Rate limits

The API obeys the rate limits of Sentry. On a `429` answer, and on a `200` answer that has a limit for logs (`log_item` or `log_byte`, or all categories) in the header `X-Sentry-Rate-Limits`, the API waits for the time that Sentry says (the header `Retry-After` is the second choice). It does not count this as a failure.

## Errors

| Answer of Sentry                    | What the API does                                                    |
| ----------------------------------- | -------------------------------------------------------------------- |
| 200                                 | The batch is done.                                                   |
| 429, or a limit for logs            | The API waits and tries again.                                       |
| 400, 401, 403, 404, 413, a redirect | The API stops the log drain at once (`failing`) and says the reason. |
| 408, 5xx, other, no answer          | The API tries again with a growing wait.                             |

Delivery is **at least once**. Sentry does not remove duplicates, so a log can appear two times. Use the attribute `afffast.record_id` to find them.
