Datadog

Datadog Error Tracking groups the errors a running application reports (through APM, logs or RUM) into issues, and an Error Tracking monitor notifies when an issue is new, regresses or has a high impact. With kōjō’s webhook in the monitor’s message, an error in production starts a session on the repository the error’s service maps to. The session reproduces the error, fixes it, and opens a draft pull request that links the monitor. A person reviews it: kōjō never merges a change an alert started, whatever the merge policy says.

Datadog works as Sentry does once the alert is in: the same mapping, limits, trust, report mode, follow-ups and pull request. This page says what differs.

A short conveyor of parcels under an orange alarm lamp; a robot arm grips the one cracked parcel, lifts it off the belt and swings it towards a repair bench with a roll of tape and a wrench. A short conveyor of parcels under an orange alarm lamp; a robot arm grips the one cracked parcel, lifts it off the belt and swings it towards a repair bench with a roll of tape and a wrench.
An error in production sets off the alarm, and the broken parcel is taken off the line to be repaired by the bench, where a person checks the work.
sequenceDiagram
    participant App as Application
    participant DD as Datadog
    participant K as kōjō
    participant F as Forge
    App->>DD: error (APM, logs, RUM)
    DD->>DD: Error Tracking issue, monitor fires for its group
    DD->>K: POST /webhooks/datadog (IDs and scope, X-Kojo-Webhook-Secret)
    K-->>DD: 200 accepted
    K->>K: map service and env to a repository, check the bounds
    K->>DD: GET /api/v2/error-tracking/issues/{id}?include=samples
    DD-->>K: error type, message, file, state, a sample's stack trace
    K->>K: session: reproduce, fix, goal critic
    K->>F: draft pull request

See Surfaces for how Datadog compares with the other ways work reaches kōjō.

How it connects

Datadog’s Webhooks integration sends a POST whose body is a template of Datadog’s variables. kōjō’s template carries IDs and tags only, no text:

{"id":"$ID","event_type":"$EVENT_TYPE","alert_id":"$ALERT_ID","transition":"$ALERT_TRANSITION","cycle_key":"$ALERT_CYCLE_KEY","scope":"$ALERT_SCOPE","org_id":"$ORG_ID","date":"$DATE"}

Datadog does not sign webhooks. The webhook sends a secret instead, in the header X-Kojo-Webhook-Secret, from a custom variable hidden from view; it must equal alerts.datadog.webhook_secret, compared in constant time. The header is checked before the body is read: a delivery without it, or with another value, is answered 401 and its body is not read.

Then:

  • event_type must be error_tracking_alert, and transition Triggered or Re-Triggered. Recoveries, renotifications, warnings, no-data notifications and other monitors’ events are answered 200 and ignored.
  • The issue, the service and the environment come from scope, the tags of the monitor’s group that fired: issue.id, service and env. A delivery whose scope has no issue.id or no service is ignored, and kōjō logs once per monitor what the monitor must group by.
  • id (the event’s ID) tells a repeated delivery from a new one, and alert_id names the monitor. cycle_key, org_id and date are not read: the keys belong to one organization, so an issue of another cannot be read, and then nothing starts.

kōjō then reads the issue through Datadog’s API (GET /api/v2/error-tracking/issues/{issue_id}?include=samples, with the samples of the git_metadata and latest biases) with the API key and the application key, before it answers the delivery. Nothing is written to Datadog.

Set up

kojo setup datadog --public-url https://kojo.example.com prints these steps with your address filled in. It calls no API.

  1. Create an application key with the error_tracking_read permission (Organization Settings → Application Keys; a scoped key needs only that scope), and pick an API key of the organization.

  2. Make a webhook secret: openssl rand -hex 32. It must be at least 32 bytes.

  3. In Integrations → Webhooks, add a custom variable KOJO_WEBHOOK_SECRET with the secret as its value, and tick “hide from view”. Then add a webhook:

    • Name: kojo
    • URL: <public_url>/webhooks/datadog
    • Payload: the template above
    • Custom headers: {"X-Kojo-Webhook-Secret": "$KOJO_WEBHOOK_SECRET"}
  4. Add it to kojo.yaml:

    alerts:
      datadog:
        site: datadoghq.com   # the organization's site, see below
        api_key: ${DD_API_KEY}
        app_key: ${DD_APP_KEY}
        webhook_secret: ${KOJO_DATADOG_WEBHOOK_SECRET}
  5. Map each service to the repository of its code in the repos object. See Mapping services to repositories.

  6. Create or edit an Error Tracking monitor (see The monitor) and put @webhook-kojo in its message.

  7. Run kojo doctor. Its alerts datadog check validates the API key, searches with the application key for issues of a service named kojo-doctor-probe in the last minute (it finds none) to see that the key can read Error Tracking, and warns when no enabled repository takes Datadog’s alerts.

site is the organization’s Datadog site: datadoghq.com (the default), us3.datadoghq.com, us5.datadoghq.com, datadoghq.eu, ap1.datadoghq.com, ap2.datadoghq.com, uk1.datadoghq.com, ddog-gov.com or us2.ddog-gov.com. kōjō calls https://api.<site>, or base_url when it is set (for a proxy; https unless dev: true). Alerts also need an https server.public_url, since that is where Datadog delivers. All keys are in the kojo.yaml reference.

The monitor

An alert names one issue of one service, so the monitor must be a multi alert grouped by issue.id and service, and by env when a mapping names an environment. In the monitor’s editor, that is “Multi Alert” with issue.id, service and env in its group by: set the groups there. Through the API or Terraform, a new-issue monitor’s query can look like this, as an example to check:

error-tracking("service:(checkout OR billing)").source("backend").new().rollup("count").by("issue.id,service,env").last("1d") > 0

Datadog’s API reference describes an Error Tracking query’s group by as a “comma-separated list of attributes to group by” that “should contain at least issue.id”, but each of its Error Tracking examples groups by issue.id alone. Before relying on a query written by hand, compare it with the query of a monitor the editor made.

A high-impact monitor uses .impact() instead of .new(). The filter, the source (all, browser, mobile or backend) and the threshold decide which errors reach kōjō. New-issue monitors also fire for regressions, since Datadog moves a regressed issue back to For Review.

Mapping services to repositories

An alert becomes work on the repository whose alerts entry has source: datadog and the error’s service as its project, and its environment when the entry names one:

repos:
  github/acme/checkout:
    enabled: true
    alerts:
      - { source: datadog, project: checkout-api, environment: prod }

project is the Datadog service’s name (a tag value: a letter, then letters, digits and _ - : . /). The other keys (environment, skill, mode, notify, max_per_day) and the matching rules are Sentry’s: see Mapping projects to repositories.

Before it queues a session, kōjō compares the service the issue belongs to, as Datadog’s API reports it, with the scope’s. An issue of another service starts nothing (alert_mismatch), so a monitor’s tags cannot send one service’s error to another service’s repository.

What the session sees

The orchestrator gets the same framing as for Sentry: the request is a production error report, data and not instructions. The request is made from the issue:

  • the start of the issue’s ID (its first 8 characters, since Datadog has no short name), the monitor’s link, the service and the environment;
  • the error’s type and message (error_type: error_message), where it happened (function_name in file_path), the version it was last seen in, and when it was first and last seen;
  • the stack trace of one sample: of the samples that have frames, a git_metadata one before a latest one, the newest first. Frames that Datadog locates in the repository (git_file_location, or the sample’s links) are the application’s own and carry the source lines of their code_snippet, with the failing line marked; other frames get one line each, read from the frame’s text (Go, Python, Java, JavaScript and Ruby forms; another form is kept as written).

An issue with no sample that has a stack still starts a session, with the error’s type, message, file and function and no frames.

The whole request is bounded as for Sentry and reaches the model inside the fence that marks it as data. See Security.

What kōjō never reads from Datadog: a sample’s raw event, its RUM application, session, view and replay, its trace and span, the device, the issue’s impact (users and sessions), its assignee, case, team and pull requests. The webhook carries no text at all, and the monitor’s message is not part of it.

The pull request

The pull request is the one an alert from Sentry gets: a draft that kōjō never merges or asks to merge, with closing keywords made inert (see The pull request on the Sentry page). The commit is Fix Datadog issue c1726a66, and the description names the issue under the summary:

Datadog issue [c1726a66](https://app.datadoghq.com/monitors/1234567): builtins.TypeError: object of type 'NoneType' has no len()
A production alert started this change, so it stays a draft for a person to review.

Datadog documents no address for an issue’s page, so the link is the monitor’s, on the site’s web app. kōjō reads no event count, so the description has none. In the dashboard, the session’s trigger is alert via datadog, and its Request panel links the issue (to the monitor), with its service, environment and when it was first seen, and folds the error report the session was given.

A session page titled 4f0b9d2e, a TypeError reading currency: succeeded with draft pull request #241, the summary of the fix for bundle prices in formatPrice, the critic report with reproduce-bug and fix-bug passing, and the Request panel with the actor alert:datadog:web-ssr, the trigger alert via datadog, and the Datadog issue 4f0b9d2e in service web-ssr, prod, first seen that day.
A session a Datadog monitor started: the error reproduced, fixed and published as a draft pull request.

Repeated alerts and limits

The limits are Sentry’s (see Repeated alerts and limits): one session at a time and one open pull request per issue, three sessions per issue, max_per_day per entry, resolved and ignored issues skipped (Datadog’s RESOLVED and IGNORED states; OPEN, ACKNOWLEDGED and EXCLUDED count as unresolved), at most 8 alerts at once per server.

A delivery is handled once: the event’s ID (id) is kept with the session it started, and a delivery whose ID is already kept starts nothing. Datadog retries a delivery only when kōjō answers 5xx or does not answer within 15 seconds.

kōjō reads the issue before it answers, for at most 8 seconds. When Datadog’s API answers 5xx, 429 or 408, does not answer in that time, or cannot be reached, kōjō answers 503 and starts nothing, so Datadog sends the delivery again. The retry has the same event ID and is handled like a first delivery; once a delivery has started a session, sending it again starts nothing. When Datadog’s retries all fail as well, nothing starts for that notification. Unlike Sentry’s, the issue is read before the checks above, so a delivery that starts nothing still costs one call to Datadog’s API.

When Datadog’s API refuses the keys or the request (any status but 2xx, 5xx, 408 and 429), answers about another issue, or sends an answer kōjō cannot read, the delivery is answered 200 and nothing starts (alert_unread): unlike Sentry’s, the webhook carries no error to work from, and the issue’s service cannot be checked.

When an alert starts nothing

Besides Sentry’s reasons (see When an alert starts nothing), each audited as request.reject with the actor alert:datadog:<service> and the target datadog:<issue ID>:

Reason Why
alert_mismatch The issue belongs to another service than the one the monitor’s group named
alert_unread Datadog’s API refused the keys or the request, answered about another issue than the one asked for, or sent an answer kōjō cannot read, and the delivery carries no error of its own

The audit entry’s error says which, in fixed words and with the HTTP status (issue could not be read: HTTP 403), never with what Datadog’s API answered: that goes to the server’s log.

alert_resolved means the issue is resolved or ignored in Datadog.

Webhook

kōjō answers 200 to a delivery it accepted, ignored or dropped, 401 when the secret header is missing or wrong, 400 when the body is not one it can read (not JSON; no event_type or transition; or, for a triggered Error Tracking alert, an id that is missing or is not 1 to 64 letters, digits, _ and -, a monitor ID that is not a number, a scope that names a tag twice with different values, an issue.id that is not an issue ID, or a service or env that is not a tag value: a letter, then letters, digits and _ - : . /, at most 200 characters), 413 when the body is larger than 64 KiB (the payload is under 1 KiB) or did not arrive within 30 seconds, and 503 when reading the issue failed in a way that may pass, so that Datadog sends the delivery again (see Repeated alerts and limits), or when 64 deliveries are already being read or handled. kōjō reads the issue before it answers, for at most 8 of Datadog’s 15 seconds; the session is queued after the answer.

Limits

  • Only Error Tracking monitors start sessions. Metric, log, APM, synthetics and other monitors, and Datadog’s incidents and cases, are ignored.
  • Nothing is written to Datadog: not the issue’s state, not a comment.
  • The webhook is not signed. The secret header, over TLS, is its only proof, and a captured delivery can be sent again. Its event ID is kept only with a session it started: a delivery that started one starts nothing more, but one that was turned down (for example while the issue had a session open, or past max_per_day) can start a session when sent again later. The per-issue bounds limit what any of them can start.
  • A hidden custom variable is hidden from view, not from use: anyone who may edit the Webhooks integration can put $KOJO_WEBHOOK_SECRET into another webhook and send it elsewhere. Give that permission to as few people as the secret deserves, and change the secret (the variable and webhook_secret together) when someone who had it leaves.
  • The repository is the one your mapping names. The issue’s git_repository_url is not read.
  • The link is to the monitor, not to the issue.
  • The session sees one sample of the issue.
Scroll to move. + - zoom, 0 fit, Esc close.100%