Skip to main content

Cloudflare Workers

Monitor Cloudflare Workers using OpenTelemetry and Last9

Introduction

This document lists step-by-step instructions for setting up monitoring for Cloudflare Workers with Last9.

Prerequisites

  1. Create a Last9 account by following Getting Started.
  2. Keep the following information handy from the Integrations page:
    • $last9_otlp_endpoint: Last9's OTLP endpoint copies from Cloudflare Integration section from Integrations page.
    • $last9_basic_auth_header: OTLP Basic authorization header

Setup

Instrument your Cloudflare Worker applications with OpenTelemetry using the the otel-cf-workers SDK.

Step 1: Install the SDK

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

npm i @microlabs/otel-cf-workers

Step 2: Add Node.js Compatibility Flags

OpenTelemetry requires the Node.js Compatibility flag is enabled at the top level of your wrangler.toml file.

compatibility_flags = [ "nodejs_compat" ]

Step 3: Configure the tracer

In your Cloudflare worker file, add the following configuration code to configure OpenTelemetry.

import { instrument, ResolveConfigFn } from '@microlabs/otel-cf-workers'

export interface Env {
LAST9_BASIC_AUTH: string // Last9 Basic Auth Header
SERVICE_NAME: string // Your service name
}

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: `$last9_otlp_endpoint/v1/traces`,
headers: { 'Authorization': env.LAST9_BASIC_AUTH },
},
service: { name: env.SERVICE_NAME },
}
}

export default instrument(handler, config)

Step 4: Set the Last9 environment variables

In your Cloudflare Workers Secret Configuration add the LAST9_BASIC_AUTH.

To enable tracing for local dev add your LAST9_BASIC_AUTH to your .dev.vars file

LAST9_BASIC_AUTH=$last9_basic_auth_header

In your wrangler.toml file set the SERVICE_NAME variable

[vars]
SERVICE_NAME = "my-service-name"

Once these steps are completed, distributed traces from your Cloudflare Workers application should be available in Last9 Trace Explorer.

Adding custom OpenTelemetry spans

To add custom spans to your OpenTelemetry traces, install the @opentelemetry/api package.

npm i @opentelemetry/api

And manually add spans to your traces.

import { trace } from "@opentelemetry/api";

const tracer = trace.getTracer('custom-traces');

const handler = {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const span = trace.getActiveSpan();

span.setAttribute('search', search)

const result = await tracer.startActiveSpan(`transaction-started`, async (span) => {
// your business logic
const input = { search }
span.setAttributes(input);
const result = await transactionLogic(input)
span.setAttributes(result)
return result
});
}
}

Verification

Visit Trace Explorer to see the Cloudflare traces in action.

Troubleshooting

Please get in touch with us on Discord or Email if you have any questions.