evanderkoogh / otel-cf-workers

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

[feature request/question] support without node dependencies #85

Closed Hebilicious closed 6 months ago

Hebilicious commented 7 months ago

Hello there, Thank you for making this library.

I am not well versed in otel, but I would like to know if there's a way to write this package without any node dependencies. From what I can see it's just sending HTTP requests to an endpoint, and that would be great if we could use this without the compatibility flag.

My use case being some workers and pages projects that use pg, which requires node_compat=true which injects polyfills that conflicts with the compatibility flag.

image

https://opentelemetry.io/docs/languages/js/getting-started/browser/

jahands commented 6 months ago

This package has a hard dependency on the nodejs_compat compatibility flag. Without it, it would not be feasible to create auto instrumentation. There are no current plans to try to remove the nodejs_compat requirement.

jesseditson commented 5 months ago

Out of curiosity, why is this a hard dependency? I can also look at code but I only see mention of the requirement, not the reason why, in the documentation. I'd love to use this library but it's important to me that my workers are as lightweight as possible. No worries if that's not an eventual goal of this lib, but want to make sure it's not roadmap material before exploring other options.

DaniFoldi commented 5 months ago

Hey @jesseditson,

You're probably getting confused between node_compat build option and nodejs_compat compatibility flag. See the screenshot above, these two are mutually exclusive ways to make nodejs code work on Workers.

The former adds polyfills/shims at build time so imports from fs and path won't fail, while the latter enables some extra modules in the runtime, no code added to your worker. The hard dependency is on async local storage, which the library uses for context propagation. The native js solution to that is currently in stage 1, so it'll be a while before we can remove the dependency. The rest (event emitter, buffer) are used in the opentelemetry libraries, so to avoid increasing your worker bundle size, we rely on the runtime's native modules for those as well.

In short: your worker is as lightweight as it can be with nodejs_compat compatibility flag.

See docs here (and feel free to ask on the Cloudflare Discord if you're curious to know more): https://developers.cloudflare.com/workers/runtime-apis/nodejs/#nodejs-compatibility and https://developers.cloudflare.com/workers/wrangler/configuration/#add-polyfills-using-wrangler

jesseditson commented 5 months ago

That is extremely helpful! I wasn't clear on the difference between those flags and this makes sense. Thank you!