evanderkoogh / otel-cf-workers

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

`TypeError: Illegal invocation` when intrumenting calls to CF cache #41

Closed betimshahini closed 1 year ago

betimshahini commented 1 year ago

When using Cloudflare's kv-asset-handler, there are instances where the auto-instrumentation results in Illegal invocation errors, causing the request to be cancelled in the CF Workers environment. More specifically, when setting the bypassCache: false flag in the params of the getAssetFromKV() call is when this can be reproduced consistently when there are multiple items in cache.

Repro branch - URL pointing to the file with the interesting bits: https://github.com/proofzero/rollupid/blob/spike/otel-cf-workers-repro/apps/passport/server.ts

Steps to run locally:

  1. Go to /apps/passport
  2. Rename .dev.vars.example to .dev.vars (no need to populate with values)
  3. yarn && yarn start

No issues locally; when workers is deployed to CF and hard-refreshed in the browser a few times (likely to cause some cache hits/misses), you'll see TypeError: Illegal invocation logs in wrangler tail.

tygern commented 1 year ago

I'm seeing the same issue (using Remix on Cloudflare workers) in the same https://github.com/tygern/remix-otel-issue that we used as an example for a previous issue (https://github.com/evanderkoogh/otel-cf-workers/issues/17). You can reproduce by running npm run build && npm run start, then you'll see TypeError: Illegal invocation for all static assets. If you add bypassCache: true to getAssetFromKV's cacheControl config in server.ts (line 38) then everything works as expected.

evanderkoogh commented 1 year ago

Yeah, I need to have a look into what is going on there.. But I am a bit under the pump these days, so it might take a while..

evanderkoogh commented 1 year ago

When I update the otel package to the latest rc.10 version everything seems to work. Did a bunch of work in between to make wrapping things into Proxies a bit more robust when libraries do funky things, so this might have been one of those things. Please re-open if you still see this issue.