-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |