Cloudflare Workers OpenTelemetry Instrumentation

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, the CLI tool for Cloudflare.

Send traces to SigNoz

Step 1: Install the SDK

Install @microlabs/otel-cf-workers in your project.

npm i @microlabs/otel-cf-workers
Info

@microlabs/otel-cf-workers is a third party OpenTelemetry compatible library for instrumenting and exporting traces from Cloudflare Workers.

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.

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.

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
  • Replace <your-ingestion-key> with your SigNoz ingestion key
  • Replace <service-name> with the name of the service associated with this trace

Step 4: Deploy the project

Deploy the project to Cloudflare Worker.

npm run deploy

Visualize the traces in SigNoz

  • Traces can be viewed under the Traces tab in the SigNoz UI.
Cloudflare Traces in Traces View
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.
Sample Cloudflare Trace
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 for detailed instructions.

Was this page helpful?