dylibso / observe-sdk

Continuous runtime observablity SDKs to monitor WebAssembly code.
https://dev.dylibso.com/docs/observe/overview
Apache License 2.0
156 stars 7 forks source link

bug: JS SDK doesn't work in Cloudflare workers #150

Closed mhmd-azeez closed 8 months ago

mhmd-azeez commented 8 months ago

When trying to use the Observe SDK in cloudflare workers, there is an error:

[wrangler:err] TypeError: wasm.__wbindgen_add_to_stack_pointer is not a function
    at demangle (/home/nilslice/Projects/dylibso/workers-wasm-observability/node_modules/@dylibso/observe-sdk-honeycomb/dist/esm/index.js:2204:10)
    at null.<anonymous> (/home/nilslice/Projects/dylibso/workers-wasm-observability/node_modules/@dylibso/observe-sdk-honeycomb/dist/esm/index.js:2293:27)
    at [object Object]
    at SpanCollector.setNames (/home/nilslice/Projects/dylibso/workers-wasm-observability/node_modules/@dylibso/observe-sdk-honeycomb/dist/esm/index.js:2292:18)
    at HoneycombAdapter.start (/home/nilslice/Projects/dylibso/workers-wasm-observability/node_modules/@dylibso/observe-sdk-honeycomb/dist/esm/index.js:3898:21)
    at Object.fetch (/home/nilslice/Projects/dylibso/workers-wasm-observability/src/worker.ts:38:38)
    at async jsonError (/home/nilslice/Projects/dylibso/workers-wasm-observability/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
[wrangler:inf] POST / 500 Internal Server Error (44ms)

From my investigation, it seems like Cloudlfare Workers doesn't support loading wasm modules from a Base64 string: https://github.com/cloudflare/workers-sdk/issues/1366#issuecomment-1172255671

Which is what we are doing in the published npm pacakges: (look for var modsurfer_demangle_bg_default = __toBinary("AGFzbQEAAAABngEUYAJ/fwF).

Repro

https://github.com/dylibso/workers-wasm-observability

Workaround

I manually patched the problematic code like so:

import _wasm from "./demangle.wasm";
const _wasm_memory = new WebAssembly.Memory({ initial: 512 });
let importsObject = {
  env: { memory: _wasm_memory },
};

var wasm = new WebAssembly.Instance(_wasm, importsObject).exports;

Please note that we are using this pattern here too, but it seems like the build process encodes the binary during bundling. So we probably need to change the build process to fix this