Skip to content

Commit

Permalink
add cf tracing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankcorn committed Oct 26, 2023
1 parent 1e21222 commit 28e1b41
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 113 additions & 0 deletions sending-data/opentelemetry/cloudflare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
order: 0
label: Cloudflare
---

# OpenTelemetry for Cloudflare Workers

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

---


## Instrumentation

### Step 1: Install the SDK


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

```bash # :icon-terminal: terminal
npm i @microlabs/otel-cf-workers
```

### Step 2: Configure the tracer

Create a file `instrumentation.ts` in the root of your project and add the following code to configure and initialize OpenTelemetry.


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

export interface Env {
BASELIME_KEY: string
SERVICE_NAME: string
}

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://otel.baselime.io/v1,
headers: { 'x-honeycomb-team': env.BASELIME_KEY },
},
service: { name: env.SERVICE_NAME },
}
}

export default instrument(handler, config)
```

### Step 3: Set the Baselime environment variables

In your `wrangler.toml` file set the `BASELIME_KEY` and `SERVICE_NAME` variables

!!!
Get your pulic BASELIME_KEY from the [Baselime console](https://console.baselime.io).
!!!

```toml # :icon-code: wrangler.toml
[vars]

BASELIME_KEY = "your api key"
SERVICE_NAME = "pokedex"
```

Once these steps are completed, distributed traces from your Next.js applications should be available in Baselime to query via the console or the Baselime CLI.

![Example Cloudflare Trace](../../assets/images/illustrations/sending-data/opentelemetry/cf-tracing.png)

---

## Adding custom OpenTelemetry spans

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

```bash # :icon-terminal: terminal
npm i @opentelemetry/api
```

And manually add spans to your traces.

```typescript # :icon-code: page.js
import { trace } from "@opentelemetry/api";

const tracer = trace.getTracer('your-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(`business-logic`, async (span) => {
// your business logic
const input = { search }
span.setAttributes(input);
const result = await yourBusinessLogic(input)
span.setAttributes(result)
return result
});
}
}
```
---

## Special Thanks

This is powered by [otel-cf-workers](https://github.com/evanderkoogh/otel-cf-workers) developed by [Erwin van der Koogh](https://github.com/evanderkoogh). It's a fantastic vendor agnostic OpenTelemetry SDK for Cloudflare Workers and you should check it out.

0 comments on commit 28e1b41

Please sign in to comment.