|
| 1 | +--- |
| 2 | +date: 2025-06-23 |
| 3 | +id: opentelemetry-cloudflare |
| 4 | +title: Cloudflare Workers OpenTelemetry Instrumentation |
| 5 | +description: Instrument Cloudflare Workers with OpenTelemetry and send traces to SigNoz |
| 6 | +tags: [opentelemetry, cloudflare, instrumentation] |
| 7 | +hide_table_of_contents: true |
| 8 | +--- |
| 9 | + |
| 10 | +This document explains how to instrument Cloudflare Workers with OpenTelemetry and send traces to SigNoz. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +You will need the following in place before moving on to the next step: |
| 15 | +- A Cloudflare account |
| 16 | +- [Wrangler](https://developers.cloudflare.com/workers/wrangler/install-and-update/), the CLI tool for Cloudflare. |
| 17 | + |
| 18 | +## Send traces to SigNoz |
| 19 | + |
| 20 | +<Tabs entityName="plans"> |
| 21 | +<TabItem value="signoz-cloud" label="SigNoz Cloud" default> |
| 22 | + |
| 23 | +**Step 1: Install the SDK** |
| 24 | + |
| 25 | +Install `@microlabs/otel-cf-workers` in your [project](https://developers.cloudflare.com/workers/get-started/guide/#1-create-a-new-worker-project). |
| 26 | + |
| 27 | +```bash |
| 28 | +npm i @microlabs/otel-cf-workers |
| 29 | +``` |
| 30 | + |
| 31 | +<Admonition type="info"> |
| 32 | +[@microlabs/otel-cf-workers](https://www.npmjs.com/package/@microlabs/otel-cf-workers) is a third party OpenTelemetry compatible library |
| 33 | +for instrumenting and exporting traces from Cloudflare Workers. |
| 34 | +</Admonition> |
| 35 | + |
| 36 | +**Step 2: Add Node.js Compatibility Flag** |
| 37 | + |
| 38 | +OpenTelemetry requires the Node.js compatibility flag to be enabled at the top level of your `wrangler.toml`. |
| 39 | + |
| 40 | +```yaml:wrangler.toml |
| 41 | +compatibility_flags = [ "nodejs_compat" ] |
| 42 | +``` |
| 43 | + |
| 44 | +**Step 3: Configure tracer in Cloudflare Workers project** |
| 45 | + |
| 46 | +Navigate to the wrangler project directory, and add the following code to your `src/index.ts` file. |
| 47 | + |
| 48 | +```javascript:index.ts |
| 49 | +import { instrument, ResolveConfigFn } from '@microlabs/otel-cf-workers' |
| 50 | + |
| 51 | +const handler = { |
| 52 | + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { |
| 53 | + // your cloudflare worker code |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | +const config: ResolveConfigFn = (env: Env, _trigger) => { |
| 58 | + return { |
| 59 | + exporter: { |
| 60 | + url: 'https://ingest.<region>.signoz.cloud:443/v1/traces', |
| 61 | + headers: { 'signoz-access-token': '<your-ingestion-key>' }, |
| 62 | + }, |
| 63 | + service: { name: '<service-name>' }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export default instrument(handler, config) |
| 68 | +``` |
| 69 | + |
| 70 | +- Set the `<region>` to match your SigNoz Cloud [region](https://signoz.io/docs/ingestion/signoz-cloud/overview/#endpoint) |
| 71 | +- Replace `<your-ingestion-key>` with your SigNoz [ingestion key](https://signoz.io/docs/ingestion/signoz-cloud/keys/) |
| 72 | +- Replace `<service-name>` with the name of the service associated with this trace |
| 73 | + |
| 74 | +**Step 4: Deploy the project** |
| 75 | + |
| 76 | +Deploy the project to Cloudflare Worker. |
| 77 | + |
| 78 | +```bash |
| 79 | +npm run deploy |
| 80 | +``` |
| 81 | + |
| 82 | +</TabItem> |
| 83 | +<TabItem value='self-host' label='Self-Host'> |
| 84 | + |
| 85 | +**Step 1: Install the SDK** |
| 86 | + |
| 87 | +Install `@microlabs/otel-cf-workers` in your [project](https://developers.cloudflare.com/workers/get-started/guide/#1-create-a-new-worker-project). |
| 88 | + |
| 89 | +```bash |
| 90 | +npm i @microlabs/otel-cf-workers |
| 91 | +``` |
| 92 | + |
| 93 | +<Admonition type="info"> |
| 94 | +[@microlabs/otel-cf-workers](https://www.npmjs.com/package/@microlabs/otel-cf-workers) is a third party OpenTelemetry compatible library |
| 95 | +for instrumenting and exporting traces from Cloudflare Workers. |
| 96 | +</Admonition> |
| 97 | + |
| 98 | +**Step 2: Add Node.js Compatibility Flag** |
| 99 | + |
| 100 | +OpenTelemetry requires the Node.js compatibility flag to be enabled at the top level of your `wrangler.toml`. |
| 101 | + |
| 102 | +```yaml:wrangler.toml |
| 103 | +compatibility_flags = [ "nodejs_compat" ] |
| 104 | +``` |
| 105 | + |
| 106 | +**Step 3: Configure tracer in Cloudflare Workers project** |
| 107 | + |
| 108 | +Navigate to the wrangler project directory, and add the following code to your `src/index.ts` file. |
| 109 | + |
| 110 | +```javascript:index.ts |
| 111 | +import { instrument, ResolveConfigFn } from '@microlabs/otel-cf-workers' |
| 112 | + |
| 113 | +const handler = { |
| 114 | + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { |
| 115 | + // your cloudflare worker code |
| 116 | + }, |
| 117 | +} |
| 118 | + |
| 119 | +const config: ResolveConfigFn = (env: Env, _trigger) => { |
| 120 | + return { |
| 121 | + exporter: { |
| 122 | + url: 'http://localhost:4318/v1/traces' |
| 123 | + }, |
| 124 | + service: { name: '<service-name>' }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +export default instrument(handler, config) |
| 129 | +``` |
| 130 | + |
| 131 | +- Replace `<service-name>` with the name of the service associated with this trace |
| 132 | + |
| 133 | +**Step 4: Deploy the project** |
| 134 | + |
| 135 | +Deploy the project to Cloudflare Worker. |
| 136 | + |
| 137 | +```bash |
| 138 | +npm run deploy |
| 139 | +``` |
| 140 | + |
| 141 | +</TabItem> |
| 142 | +</Tabs> |
| 143 | + |
| 144 | +## Visualize the traces in SigNoz |
| 145 | + |
| 146 | +- Traces can be viewed under the `Traces` tab in the SigNoz UI. |
| 147 | + |
| 148 | +<Figure src="/img/docs/instrumentation/cloudflare/TracesList.webp" alt="Cloudflare Traces in Traces View" caption="Cloudflare Traces in Traces View" /> |
| 149 | + |
| 150 | +- 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. |
| 151 | + |
| 152 | +<Figure src="/img/docs/instrumentation/cloudflare/TraceID.webp" alt="Sample Cloudflare Trace" caption="Sample Cloudflare Trace" /> |
| 153 | + |
| 154 | +## Send logs to SigNoz |
| 155 | + |
| 156 | +In order to push logs from Cloudflare Worker, you need the Enterprise plan. For sending Cloudflare Worker logs to SigNoz, you can |
| 157 | +use the cloudflare opentelemetry receiver. Refer [this page](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/cloudflarereceiver) |
| 158 | +for detailed instructions. |
0 commit comments