Skip to content

feat: add instrumentation document for cloudflare workers #1593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions constants/docsSideNav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ const docsSideNav = [
route: '/docs/instrumentation/opentelemetry-wordpress',
label: 'WordPress',
},
{
type: 'doc',
route: '/docs/instrumentation/opentelemetry-cloudflare',
label: 'Cloudflare Workers',
},
{
type: 'category',
isExpanded: false,
Expand Down
158 changes: 158 additions & 0 deletions data/docs/instrumentation/opentelemetry-cloudflare.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
date: 2025-06-23
id: opentelemetry-cloudflare
title: Cloudflare Workers OpenTelemetry Instrumentation
description: Instrument Cloudflare Workers with OpenTelemetry and send traces to SigNoz
tags: [opentelemetry, cloudflare, instrumentation]
hide_table_of_contents: true
---

This document explains how to instrument Cloudflare Workers with OpenTelemetry and send traces to SigNoz.

## Prerequisites

You will need the following in place before moving on to the next step:
- A Cloudflare account
- [Wrangler](https://developers.cloudflare.com/workers/wrangler/install-and-update/), the CLI tool for Cloudflare.

## Send traces to SigNoz

<Tabs entityName="plans">
<TabItem value="signoz-cloud" label="SigNoz Cloud" default>

**Step 1: Install the SDK**

Install `@microlabs/otel-cf-workers` in your [project](https://developers.cloudflare.com/workers/get-started/guide/#1-create-a-new-worker-project).

```bash
npm i @microlabs/otel-cf-workers
```

<Admonition type="info">
[@microlabs/otel-cf-workers](https://www.npmjs.com/package/@microlabs/otel-cf-workers) is a third party OpenTelemetry compatible library
for instrumenting and exporting traces from Cloudflare Workers.
</Admonition>

**Step 2: Add Node.js Compatibility Flag**

OpenTelemetry requires the Node.js compatibility flag to be enabled at the top level of your `wrangler.toml`.

```yaml:wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
```

**Step 3: Configure tracer in Cloudflare Workers project**

Navigate to the wrangler project directory, and add the following code to your `src/index.ts` file.

```javascript:index.ts
import { instrument, ResolveConfigFn } from '@microlabs/otel-cf-workers'

const handler = {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
// your cloudflare worker code
},
}

const config: ResolveConfigFn = (env: Env, _trigger) => {
return {
exporter: {
url: 'https://ingest.<region>.signoz.cloud:443/v1/traces',
headers: { 'signoz-access-token': '<your-ingestion-key>' },
},
service: { name: '<service-name>' },
}
}

export default instrument(handler, config)
```

- Set the `<region>` to match your SigNoz Cloud [region](https://signoz.io/docs/ingestion/signoz-cloud/overview/#endpoint)
- Replace `<your-ingestion-key>` with your SigNoz [ingestion key](https://signoz.io/docs/ingestion/signoz-cloud/keys/)
- Replace `<service-name>` with the name of the service associated with this trace

**Step 4: Deploy the project**

Deploy the project to Cloudflare Worker.

```bash
npm run deploy
```

</TabItem>
<TabItem value='self-host' label='Self-Host'>

**Step 1: Install the SDK**

Install `@microlabs/otel-cf-workers` in your [project](https://developers.cloudflare.com/workers/get-started/guide/#1-create-a-new-worker-project).

```bash
npm i @microlabs/otel-cf-workers
```

<Admonition type="info">
[@microlabs/otel-cf-workers](https://www.npmjs.com/package/@microlabs/otel-cf-workers) is a third party OpenTelemetry compatible library
for instrumenting and exporting traces from Cloudflare Workers.
</Admonition>

**Step 2: Add Node.js Compatibility Flag**

OpenTelemetry requires the Node.js compatibility flag to be enabled at the top level of your `wrangler.toml`.

```yaml:wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
```

**Step 3: Configure tracer in Cloudflare Workers project**

Navigate to the wrangler project directory, and add the following code to your `src/index.ts` file.

```javascript:index.ts
import { instrument, ResolveConfigFn } from '@microlabs/otel-cf-workers'

const handler = {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
// your cloudflare worker code
},
}

const config: ResolveConfigFn = (env: Env, _trigger) => {
return {
exporter: {
url: 'http://localhost:4318/v1/traces'
},
service: { name: '<service-name>' },
}
}

export default instrument(handler, config)
```

- Replace `<service-name>` with the name of the service associated with this trace

**Step 4: Deploy the project**

Deploy the project to Cloudflare Worker.

```bash
npm run deploy
```

</TabItem>
</Tabs>

## Visualize the traces in SigNoz

- Traces can be viewed under the `Traces` tab in the SigNoz UI.

<Figure src="/img/docs/instrumentation/cloudflare/TracesList.webp" alt="Cloudflare Traces in Traces View" caption="Cloudflare Traces in Traces View" />

- You can click on a particular `TraceID` in the `Traces` view to get the detailed view of the Cloudflare Worker as shown in the image below.

<Figure src="/img/docs/instrumentation/cloudflare/TraceID.webp" alt="Sample Cloudflare Trace" caption="Sample Cloudflare Trace" />

## Send logs to SigNoz

In order to push logs from Cloudflare Worker, you need the Enterprise plan. For sending Cloudflare Worker logs to SigNoz, you can
use the cloudflare opentelemetry receiver. Refer [this page](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/cloudflarereceiver)
for detailed instructions.
Binary file not shown.
Binary file not shown.