---
title: "Telemetry"
---

> Documentation Index
> Fetch the complete documentation index at: https://workerd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Telemetry

workerd exports a trace and the logs of each invocation as OpenTelemetry
data. A trace has a span for the invocation, for each subrequest and
binding call, and for each `ctx.tracing.enterSpan()` call. The logs are the
`console` calls and uncaught exceptions, each with its trace and span IDs.

workerd has two outputs. Use one of them:

- **Files.** `WORKERD_OTEL=1` writes the data to gzipped NDJSON files, in
  the state directory or in the cluster's bucket. DuckDB can query the
  files there.
- **A collector.** `OTEL_EXPORTER_OTLP_ENDPOINT` sends the data as OTLP/HTTP
  to an OpenTelemetry collector.

If you set both, workerd does not start. `WORKERD_OTEL=1` is experimental
and needs `--experimental`. The file format is `v0-unstable` and can change
in a new version.

## Write telemetry to files

1. Make the `counter` project of the [overview](https://workerd.dev/docs/#run-one-instance).
2. Start workerd with `WORKERD_OTEL=1`:

   ```sh
   WORKERD_OTEL=1 npx workerd serve --experimental .
   ```

3. In another terminal, send a request:

   ```sh
   curl 'http://localhost:8787/?name=alice'
   ```

About one second after the request, workerd writes a file of traces. It
writes a file of logs only when a Worker logs:

```
.wrangler/state/workerd/telemetry/traces/2026/12/03/00/1796257502419-dcfc8650.ndjson.gz
.wrangler/state/workerd/telemetry/logs/2026/12/03/00/1796257502421-5ed45452.ndjson.gz
```

The directories are the UTC year, month, day, and hour. The file name is
the time in milliseconds and a random part. Each file has one row for each
span or log record.

On one instance, the files are in `telemetry/` in the state directory,
`.wrangler/state/workerd` or the directory that `--data-dir` names. A
`config.capnp` has no state directory, so it can use `WORKERD_OTEL=1` only
in a cluster.

In a cluster, each instance writes its files to the bucket, under
`PREFIX/v1/services/telemetry/traces/` and `PREFIX/v1/services/telemetry/logs/`.

## Query the files

Use [DuckDB](https://duckdb.org/docs/stable/data/json/overview). This query
counts the spans of each name and gives their average duration:

```sh
duckdb -c "
  SELECT name, count(*) AS spans, round(avg(duration_us)) AS avg_us
  FROM read_json('.wrangler/state/workerd/telemetry/traces/**/*.ndjson.gz')
  GROUP BY name ORDER BY spans DESC"
```

This query shows each log line with the span that wrote it:

```sql
SELECT t.name, t.url, l.body
FROM read_json('.wrangler/state/workerd/telemetry/logs/**/*.ndjson.gz') AS l
JOIN read_json('.wrangler/state/workerd/telemetry/traces/**/*.ndjson.gz') AS t
  USING (trace_id, span_id);
```

To query the files in a bucket, give DuckDB the bucket credentials, and use
an `s3://` or `gs://` path. See
[DuckDB S3 API support](https://duckdb.org/docs/stable/core_extensions/httpfs/s3api).

### Columns

Trace and log rows both have `service`, `node`, `trace_id`, `span_id`,
`durable_object_id`, and `attributes`. A trace row also has
`parent_span_id`, `name`, `kind`, `start_unix_us`, `duration_us`, `ok`,
`error`, `url`, and `http_status`. A log row also has `time_unix_us`,
`severity_number`, `severity_text`, and `body`.

| column | value |
| --- | --- |
| `service` | The name of the Worker |
| `node` | The ID of the instance in a cluster. On one instance, `null`. |
| `name` | The span name, such as `GET`, `fetch`, or `durable_object_storage_get` |
| `kind` | The OpenTelemetry span kind as a number: `2` is a server span, `3` is a client span |
| `*_unix_us`, `duration_us` | Times in microseconds |
| `ok`, `error` | The result of the span, and its error message when it failed |
| `durable_object_id` | The ID of the Durable Object, when the span or log is in one |
| `url`, `http_status` | The URL and the status of an HTTP request |
| `severity_text`, `body` | The level of a log, such as `INFO`, and the arguments of the `console` call, separated by spaces: a string as it is, other values as JSON. For an uncaught exception, `body` is the message, and `attributes` holds `exception.type`, `exception.message`, and `exception.stacktrace` |
| `attributes` | A JSON object with all the other attributes, the resource attributes included |

## Send telemetry to a collector

Set the URL of the collector and start workerd. This output does not need
`--experimental`:

```sh
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
npx workerd serve .
```

workerd adds `/v1/traces` and `/v1/logs` to the path of the URL. For
example, with `http://collector:4318/otlp`, the traces go to
`/otlp/v1/traces`.

| variable | purpose |
| --- | --- |
| `OTEL_EXPORTER_OTLP_ENDPOINT` | The URL of the collector, `http://` or `https://`. For `https://`, workerd trusts the browser CAs. |
| `OTEL_EXPORTER_OTLP_HEADERS` | Headers for each request, as comma-separated `name=value` pairs. Percent-encode the values. |
| `OTEL_EXPORTER_OTLP_PROTOCOL` | `http/protobuf`, the default, or `http/json` |

## Sample traces

`OTEL_TRACES_SAMPLER_ARG` sets the share of traces that workerd exports to
either output, from `0` to `1`. The default is `1`, all traces. workerd
chooses by trace ID, so it exports a trace whole or not at all. An
invocation that workerd does not trace still exports its logs.

```sh
WORKERD_OTEL=1 OTEL_TRACES_SAMPLER_ARG=0.01 npx workerd serve --experimental .
```

## Limits

- Telemetry uses event-loop CPU time for each span. In one test with a
  CPU-bound load, throughput fell by about 40% with `WORKERD_OTEL=1`, 22%
  with a collector, and 15% with `WORKERD_OTEL=1` at a ratio of `0.01`.
- workerd does not send the queued telemetry when it stops. With
  `WORKERD_OTEL=1`, the data of about the last second before a stop is
  lost.
- workerd queues up to 4096 requests and sends up to 16 at a time. When
  the queue is full or a request fails, workerd drops the data and does
  not retry.
- workerd does not delete telemetry files. Give the bucket a lifecycle
  rule that deletes old files. On one instance, delete old directories in
  `telemetry/`.
- A `config.capnp` that sets `tracing` keeps it, and workerd ignores the
  variables with a warning. `TracingOptions` in
  [workerd.capnp](https://github.com/cloudflare/workerd/blob/main/src/workerd/server/workerd.capnp)
  gives the options.

Source: https://workerd.dev/docs/telemetry/index.mdx
