evanderkoogh / otel-cf-workers

An OpenTelemetry compatible library for instrumenting and exporting traces for Cloudflare Workers
BSD 3-Clause "New" or "Revised" License
210 stars 40 forks source link

Docs on Using BatchTraceSpanProcessor? #47

Closed plantfansam closed 10 months ago

plantfansam commented 10 months ago

I am a little concerned that #43 and this follow-on commit may have broken things for users, but I'm also hopeful that some docs might clear things up. Wanted to surface this in case any other users encounter the same issue.

Here's the state of affairs: #43 and the follow-on commit added the ability to specify 1+ span processors for use by the SDK. When I try to pass the BatchTraceSpanProcessor in my config resolution function like so:

export const ResolveTraceConfigFn: ResolveConfigFn = (env: Env, _trigger) => {
    return {
        exporter: new ConsoleSpanExporter,
        service: {...},
        spanProcessors: [new MyCoolSpanProcessor(), new BatchTraceSpanProcessor()],
    }
}

I see the following when run my code in wrangler:

service core:user:example: Uncaught Error: Dynamic require of "node:buffer" is not supported
  at index.js:12:9
  at index.js:3965:25 in ../otel-cf-workers-example/node_modules/@microlabs/otel-cf-workers/dist/cjs/buffer.js
  at index.js:18:50 in __require2
  at index.js:14350:18 in ../otel-cf-workers-example/node_modules/@microlabs/otel-cf-workers/dist/cjs/index.js
  at index.js:18:50 in __require2
  at index.js:14369:29 in ../otel-cf-workers-example/dist/ResolveConfig.js
  at index.js:18:50 in __require2
  at index.js:14428:18 in ../otel-cf-workers-example/dist/index.js
  at index.js:18:50 in __require2

I believe the error stems from this open workerd issue, which suggests that you must import NodeJS runtime APIs, rather than require them. If that's the case, then applications using this library would need to use the esm dist directory rather than the cjs one, since the cjs export includes the line const node_buffer_1 = require("node:buffer"); (whereas the esm dist directory uses import { Buffer } from 'node:buffer';).

This does not happen in 1.0.0-rc.7, where we used to instantiate the BatchTraceSpanProcessor within this library itself.

Side note: is it possible to add a comment about why we're importing Buffer from node:buffer here and adding it to the globalThis?

I'll update this issue with my findings!

plantfansam commented 10 months ago

I believe this issue is occurring because I have a package that was consuming otel-cf-workers as an ESM module but that package exported that package as a common JS module.

plantfansam commented 10 months ago

Closing for now