# Cloudflare Workers Logs

> Monitor Cloudflare Workers using OpenTelemetry and Last9

Source: https://last9.io/docs/integrations/observability/cloudflare-workers-logs/

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](/docs/onboard/).
2. Keep the following information handy from the [Integrations](https://app.last9.io/integrations?integration=OpenTelemetry) page:
   - `$last9_otlp_endpoint`: Last9's OTLP endpoint copies from Cloudflare Integration section from [Integrations](https://app.last9.io/integrations/) page.
   - `$last9_basic_auth_header`: OTLP Basic authorization header

## Setup

Instrument your [Cloudflare Worker](https://developers.cloudflare.com/workers/) applications with [OpenTelemetry](https://opentelemetry.io/) using the [otel-cf-workers](https://github.com/evanderkoogh/otel-cf-workers) SDK.

### Step 1: Install the SDK

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

```bash
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.

```toml
compatibility_flags = [ "nodejs_compat" ]
```

### Step 3: Configure the tracer

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

```typescript
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](https://developers.cloudflare.com/workers/configuration/secrets/) add the `LAST9_BASIC_AUTH`.

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

```js
LAST9_BASIC_AUTH = $last9_basic_auth_header;
```

In your `wrangler.toml` file set the `SERVICE_NAME` variable

```toml
[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](https://app.last9.io/traces).

## Adding custom OpenTelemetry spans

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

```bash
npm i @opentelemetry/api
```

And manually add spans to your traces.

```typescript
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](https://app.last9.io/traces) to see the Cloudflare traces in action.

---

## Troubleshooting

Please get in touch with us on [Discord](https://discord.com/invite/Q3p2EEucx9) or [Email](mailto:support@last9.io) if you have any questions.
