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

# Log drains to Datadog

Send the logs of your workspace to Datadog Logs with a log drain.

The type `datadog` sends the records of a [log drain](https://docs.afffast.com/docs/log-drains.md) to the Datadog Logs intake API. You need an API key with the scope `log_drains:write` and a Datadog API key. A log drain is a feature of the Business plan.

## Make the log drain

Use a Datadog **API key**. An application key does not work. Choose the site of your Datadog account:

| `site`    | Datadog site        |
| --------- | ------------------- |
| `us1`     | `datadoghq.com`     |
| `us3`     | `us3.datadoghq.com` |
| `us5`     | `us5.datadoghq.com` |
| `eu1`     | `datadoghq.eu`      |
| `ap1`     | `ap1.datadoghq.com` |
| `ap2`     | `ap2.datadoghq.com` |
| `uk1`     | `uk1.datadoghq.com` |
| `us1-fed` | `ddog-gov.com`      |
| `us2-fed` | `us2.ddog-gov.com`  |

```bash
curl https://api.afffast.com/v1/log_drains \
  -H "Authorization: Bearer sk_live_..." \
  -H "Content-Type: application/json" \
  -d '{
    "type": "datadog",
    "label": "Datadog",
    "config": { "site": "eu1", "service": "affiliate", "tags": "env:prod,team:growth" },
    "credentials": { "api_key": "..." },
    "sources": ["api_request", "sync_run"]
  }'
```

- `site` is required.
- `service` is optional. The default is `afffast`.
- `tags` is optional. Write tags that are separated by commas (200 characters at most). Use letters, digits and the characters `_ . / : -`.
- `api_key` is write-only. The API never returns it.

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

## What Datadog gets

Each record is one log entry:

| Field      | Value                                                                                               |
| ---------- | --------------------------------------------------------------------------------------------------- |
| `ddsource` | `afffast`                                                                                           |
| `service`  | The `service` of the config.                                                                        |
| `ddtags`   | `source`, `level`, `livemode` and `workspace`, then your extra tags. A test record has `test:true`. |
| `status`   | `info`, `warn` or `error`.                                                                          |
| `message`  | A short line, for example `POST /v1/links 500 80 ms`.                                               |
| `date`     | The time of the record.                                                                             |
| `afffast`  | The full record. Each field is an attribute, for example `@afffast.data.path`.                      |

The API sends up to 1000 records in one request, and the request is smaller than the limit of Datadog (5 MB). It compresses the request with gzip.

## Errors

| Answer of Datadog          | What the API does                                                                         |
| -------------------------- | ----------------------------------------------------------------------------------------- |
| 202                        | The batch is done.                                                                        |
| 429                        | The API waits and tries again.                                                            |
| 401, 403                   | The API stops the log drain (`failing`). The API key is wrong, or it is for another site. |
| 400, 404                   | The API stops the log drain (`failing`).                                                  |
| 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.
