# Datadog

A `datadog` notification endpoint sends each Insights alert to Datadog as an alert event through the [Events API v2](https://docs.datadoghq.com/api/latest/events/#post-an-event), for [Datadog On-Call](https://docs.datadoghq.com/incident_response/on-call/) to page on. An event names the team to page, On-Call opens one page per alert, and the event Insights sends when the finding clears resolves it. Insights keeps no Datadog state of its own.

## Configuration

```
notifications:

  datadog:

    - name: "datadog"

      api-key: "${DD_API_KEY}"

      site: "datadoghq.eu"

      subscriptions: ["*"]

      severities: [warning, critical]

      annotations:

        oncall_team: "nats-platform"
```

* **`api-key`** (required): a Datadog [API key](https://docs.datadoghq.com/account_management/api-app-keys/#api-keys), sent in the `DD-API-KEY` header. No application key is needed. `${NAME}` reads it from the environment; see [Secrets from the environment](/insights/1.3/guides/notifications.md#secrets-from-the-environment).
* **`site`**: your [Datadog site](https://docs.datadoghq.com/getting_started/site/), spelled as Datadog's `DD_SITE`, for example `datadoghq.eu` or `us3.datadoghq.com`; it is the hostname you log in at, without the `app.` prefix. Defaults to `datadoghq.com`. `${NAME}` reads it from the environment, so one config can send each environment to its own site. It picks the intake URL, `https://event-management-intake.<site>/api/v2/events`. It must be `datadoghq.com`, `datadoghq.eu` or `ddog-gov.com`, or a subdomain of one, so a typo cannot send the API key to someone else's host; a site Datadog adds in those domains needs no Insights release. An `app.` or `api.` hostname is refused with the site to use instead.
* **`url`**: set it in place of `site` only to send through a proxy. Setting both fails startup.
* **`auth-type`** is not accepted: the `api-key` authenticates the request.

Every other field is the same as for any endpoint type; see [Notifications → Configuration](/insights/1.3/guides/notifications.md#configuration). The key goes in a header, so the integrations page and the delivery log show the URL and never the key.

## Choosing the team to page

The `oncall_team` annotation names the On-Call team an alert pages, by its handle; the `@oncall-<handle>` mention copied from Datadog works too, and wins over a bare `@<handle>`, so a handle that itself starts with `oncall-` must be written without the `@`. Insights starts the event's message with `@oncall-<handle>`, which is how On-Call [routes a third-party alert](https://docs.datadoghq.com/incident_response/on-call/integrations/). An alert without one, and any info alert, still reaches Event Management but pages no one.

It is an ordinary [custom annotation](/insights/1.3/guides/notifications.md#custom-labels-and-annotations), so the global `notifications.labels` rules can route by check, severity, scope, category, entity type or system. Rules apply in order and a later one wins, so put an unmatched rule first as the default:

```
notifications:

  labels:

    - annotations:

        oncall_team: "nats-platform"

    - match:

        checks: ["jetstream-*", "stream-*", "consumer-*"]

      annotations:

        oncall_team: "jetstream-team"

    - match:

        systems: ["prod-eu"]

      annotations:

        oncall_team: "eu-oncall"
```

An endpoint's own `annotations` block is applied after the rules and wins over them, so set `oncall_team` there only when the whole endpoint pages one team; an empty value there, or in a later rule, pages no one. The value is a template like any annotation (`"{{ .System }}-oncall"`), and surrounding whitespace is ignored. A fixed value in a `datadog` endpoint's own block that is not a handle (letters, digits, `-`, `_` and `.`) fails startup. A rule's value may serve other endpoint types too, so it is checked when a `datadog` endpoint renders it: one that is not a handle pages no one and is logged as `webhook alerts sent with warnings`, and the event is still sent.

While a `datadog` endpoint is configured, `oncall_team` may not depend on the entity name: a rule matching `entity-name` may not set it, and no value may use `{{ .EntityName }}`. An entity's name resolves after its finding first fires, so the fire could page one team and the resolve, chosen by name, go to another, leaving the page open. For the same reason a rule whose `systems` selects by label, rather than by `id`, may not set it: a remote system's labels can change while its page is open.

`oncall_team` is an annotation rather than a label so routing never changes an alert's fingerprint for the other endpoint types that share the rules.

On-Call resolves a page only with an `ok` that mentions the page's own team, so the resolve carries the team as it is when the finding clears. Change `oncall_team` for an alert while its page is open and the `ok` goes to the new team: the open page stays open and has to be resolved by hand. Change routing while the affected pages are closed. The key also includes the endpoint, so renaming a `datadog` endpoint strands its open pages the same way.

## What Insights sends

One `POST` per alert, an alert-category event, when the alert fires, resolves or changes severity:

| Field                 | Value                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `title`               | `<check name>: <entity> (<system>)`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `message`             | `@oncall-<team>` when `oncall_team` is set and the alert is not info, then the check's description and remediation. A resolve says whether the finding cleared or changed severity                                                                                                                                                                                                                                                                                                                                       |
| `aggregation_key`     | A hash of the alert's labels and the endpoint, so two endpoints paging for one alert open separate pages                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `tags`                | Every label as `key:value` (`source:insights`, `system`, `severity`, `entity_type`, `entity_key`, `alertname` and the rest of the [payload labels](/insights/1.3/guides/notifications.md#payload-labels-and-annotations), plus any [custom ones](/insights/1.3/guides/notifications.md#custom-labels-and-annotations)), `check:<code>`, and `service:insights`. A custom `check` or `service` label replaces the one Insights adds. Datadog keeps 100 tags; the built-in ones come first, so only custom ones can be cut |
| `attributes.status`   | `error` for critical, `warn` for warning and info, `ok` when it resolves                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `attributes.priority` | `1` for critical, `3` for warning, `5` for info                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `attributes.links`    | A link to the entity in Insights, when `external-url` is set                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `attributes.custom`   | `fingerprint`, `starts_at`, `ends_at` on a resolve, and the alert's `labels` and `annotations` as objects                                                                                                                                                                                                                                                                                                                                                                                                                |

No `timestamp` is sent, so Datadog stamps each event when it arrives; the finding's own start and end are in `custom`.

Datadog's alert status has no info level, so an info finding is sent as `warn` and told apart by its priority and `severity:info` tag. It never carries the `@oncall` mention, so it shows in Event Management without paging; set `severities: [warning, critical]` to leave info findings out altogether.

## Page lifecycle

On-Call deduplicates on `aggregation_key`, so every event Insights sends for an alert, from its fire to its resolve, lands on one page.

* A **firing** warning or critical event opens a page for the `oncall_team`. The page stays open with nothing more sent.
* An **`ok`** event resolves it. A finding that clears sends one for the alert it had firing.
* A **severity change** is a new alert, because `severity` is a label and so part of the fingerprint. Insights sends an `ok` for the former severity first, with `resolve_reason` set to `severity_changed` in its annotations, then the new severity's event with `previous_severity` set. On-Call shows the old page resolved and a new one open.

The `datadog` type is edge-triggered: an alert is sent when it fires, resolves or changes severity, and never re-sent. A finding that was already firing when the endpoint was added is not sent until it changes state. An `ok` that never arrives (one still waiting for a retry when Insights restarted) leaves its page open until someone resolves it. Changing the alert's `oncall_team` while its page is open, or a label rule while it fires, has the same effect; see [Choosing the team to page](#choosing-the-team-to-page) and [Prefer annotations](/insights/1.3/guides/notifications.md#custom-labels-and-annotations).

Datadog [event monitors](https://docs.datadoghq.com/monitors/types/event/) count events over a time window and do not read `status`, so they recover a few minutes after a finding fires even while it still fires. Page through On-Call rather than an event monitor.

## Testing

Use the **Test** button on the endpoint's page in the Insights UI. It sends a synthetic event titled `TEST: system (<system>)` with whatever `oncall_team` the rules and the endpoint's own block give it, so it pages that team's on-call responder for real, then an `ok` that resolves the page straight away. If that `ok` fails, the test reports the error and is not retried; resolve the test page by hand. A wrong `api-key` is rejected by Datadog and shows in the endpoint's delivery log as a `403`.
