evanderkoogh / otel-cf-workers

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

Instrumentation breaks new RPC style service calls #116

Open johtso opened 2 months ago

johtso commented 2 months ago

https://blog.cloudflare.com/javascript-native-rpc

If you instrument your worker, and it tries to call a method on a bound service using the new RPC style, you get a TypeError: Illegal invocation error.

[wrangler:err] TypeError: Illegal invocation
    at [object Object]
    at Object.get (file:///xxxxxx/node_modules/.pnpm/@microlabs+otel-cf-workers@1.0.0-rc.32/node_modules/@microlabs/otel-cf-workers/src/instrumentation/kv.ts:114:23)
    at proxyHandler.get (file:///xxxxxx/node_modules/.pnpm/@microlabs+otel-cf-workers@1.0.0-rc.32/node_modules/@microlabs/otel-cf-workers/src/wrap.ts:23:20)
    at xxxxxxx
DaniFoldi commented 2 months ago

Hey,

Thanks for filing this. I think #104 should fix this (skip any RPC bindings for now until we have proper instrumentation for them). cc @jahands what do you think about a prerelease?

jahands commented 2 months ago

Should be fixed in @microlabs/otel-cf-workers@1.0.0-rc.33 @johtso can you please test and confirm?

johtso commented 2 months ago

Should be fixed in @microlabs/otel-cf-workers@1.0.0-rc.33 @johtso can you please test and confirm?

Seems to be working!

I'm guessing there's no way to get tracing between a worker and a called RPC service atm, even with manual steps right?

Do you think there might be a way to at least do manual traces from inside a named service?

johtso commented 2 months ago

Hmm, also my custom spans don't seem to be working anymore..

    await ping()
    const span = tracer.startSpan('parsePdf');
    const pdfData = await env.PDFPARSER_SERVICE.parsePdf(request);
    await ping()
    span.end()
DaniFoldi commented 2 months ago

Hey, could you elaborate on "don't seem to be working" a bit, please - please make sure you have no sampling on, and for debugging could you console.log spans (and return spans) in your postProcessor, making sure to redact any sensitive information before posting.

johtso commented 2 months ago

@DaniFoldi ah, it was this issue and this fix.. https://github.com/evanderkoogh/otel-cf-workers/issues/115#issuecomment-2041365703

DaniFoldi commented 2 months ago

Glad to hear you managed to fix it. I have it on my todo list to set up peerDependencies properly

khiller-cf commented 1 month ago

Should be fixed in @microlabs/otel-cf-workers@1.0.0-rc.33 @johtso can you please test and confirm?

The fix released here appears to have broken regular service bindings (no RPC). The isJRPC call matches any service bound worker, not just the JS-native RPC calls. I'm trying to find a fix for this, but identifying a way to discriminate between them accurately has proved elusive.

DaniFoldi commented 1 month ago

Hey @khiller-cf,

I believe you're right, since there are no _non_JSRPC service bindings, we currently miss instrumenting all of them. The initial fix was just to avoid matching them as anything, as they would've had all imaginable properties.

Are you able to test the following change in instrumentation/env.ts:

            if (isJSRPC(item)) {
                // TODO instrument JSRPC and maybe remove serviceBinding?
                return instrumentServiceBinding(item, String(prop))
            } else if (isKVNamespace(item)) {

The current (temporary) solution passes any JSRPC found back without instrumentation, this change would at least instrument fetch again. We'll have to figure out a way to pass context around without interfering with user code for the true RPC stuff.

khiller-cf commented 1 month ago

That resolves the issue for me. Appreciate you looking at this!

DaniFoldi commented 1 month ago

Glad to hear, I have a PR #135 open that upstreams this change. Now we just need @jahands to approve and release 😁

jahands commented 1 month ago

Released in 1.0.0-rc.38 - thanks!