# Cloudflare Workers OpenTelemetry

## Send logs and traces in 5 minutes

Export OpenTelemetry logs and traces from your [Cloudflare Workers](https://workers.cloudflare.com) to Better Stack. Cloudflare's built-in Workers observability sends them for you, so no instrumentation code is required.

### Create a source

[Create an OpenTelemetry source](https://telemetry.betterstack.com/team/0/sources/new?platform=open_telemetry ";_blank") and keep the tab open. You'll need its **Ingesting host** and **Source token**.

### Add two Cloudflare destinations

In the Cloudflare dashboard, go to **Workers & Pages → Observability → Telemetry** and click **Add destination** once for traces and once for logs:

[code-tabs]
```bash
[label Traces]
Destination name: betterstack-traces
Destination type: Traces
OTLP endpoint: https://$INGESTING_HOST/v1/traces
Custom header: "Authorization" with value "Bearer $SOURCE_TOKEN"
```

```bash
[label Logs]
Destination name: betterstack-logs
Destination type: Logs
OTLP endpoint: https://$INGESTING_HOST/v1/logs
Custom header: "Authorization" with value "Bearer $SOURCE_TOKEN"
```
[/code-tabs]

### Enable observability in your Worker

Add the `observability` block to your `wrangler.jsonc`. The `destinations` names must match the destinations you just created:

```json
[label wrangler.jsonc]
{
  "observability": {
    "traces": { "enabled": true, "destinations": ["betterstack-traces"], "head_sampling_rate": 1.0, "persist": false },
    "logs":   { "enabled": true, "destinations": ["betterstack-logs"],   "head_sampling_rate": 1.0, "persist": false }
  }
}
```

[info]
#### Want to sample or keep a copy in Cloudflare?
`head_sampling_rate: 1.0` exports every trace. Lower it to sample in production. `persist: false` sends data to Better Stack only. Set it to `true` to also keep logs and traces in Cloudflare.
[/info]

### Deploy your Worker

```sh
[label Deploy your Worker]
npx wrangler deploy
```

### View your logs and traces

Open [Better Stack → Live tail](https://telemetry.betterstack.com/team/0/tail ";_blank"). Your first traces and logs arrive within a minute or two. Send requests to your Worker to see a span per request, each with its `console.log` lines linked to the trace.

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
