Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as Sentry from '@sentry/cloudflare';
import { DurableObject } from 'cloudflare:workers';

interface Env {
SENTRY_DSN: string;
MY_DURABLE_OBJECT: DurableObjectNamespace<MyDurableObjectBase>;
}

class MyDurableObjectBase extends DurableObject<Env> {
#name: string | undefined;

setName(name: string): string {
this.#name = name;
return this.#name;
}

bootstrap(name: string): string {
// Regression for #23040 — native Durable Object RPC (facets, the Agents SDK bootstrap
// calling PartyServer's `setName()`) resolves the method on the prototype and invokes it
// with the stored Durable Object instance as the receiver. When the instrumented
// constructor returned a Proxy of the instance, native private field access failed:
// "TypeError: Cannot read private member #name from an object whose class did not declare
// it" — a Proxy never carries the target's private-field brand.
//
// Construct a fresh instrumented instance exactly as the runtime does (the raw
// DurableObjectState sits below the instrumented context's prototype), then dispatch the
// way native RPC does: prototype method, stored instance as receiver.
const rawCtx = Object.getPrototypeOf(this.ctx) as DurableObjectState;
const instance = new MyDurableObject(rawCtx, this.env);
const prototype = Object.getPrototypeOf(instance) as MyDurableObjectBase;
return prototype.setName.call(instance, name);
}
}

export const MyDurableObject = Sentry.instrumentDurableObjectWithSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
traceLifecycle: 'static',
tracesSampleRate: 1.0,
enableRpcTracePropagation: true,
}),
MyDurableObjectBase,
);

export default Sentry.withSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
traceLifecycle: 'static',
tracesSampleRate: 1.0,
enableRpcTracePropagation: true,
}),
{
async fetch(request, env) {
const url = new URL(request.url);

if (url.pathname === '/prototype-dispatch') {
const id = env.MY_DURABLE_OBJECT.idFromName('test');
const stub = env.MY_DURABLE_OBJECT.get(id);
const name = await stub.bootstrap('agent-1');
return new Response(name);
}

if (url.pathname === '/rpc/set-name') {
const id = env.MY_DURABLE_OBJECT.idFromName('test');
const stub = env.MY_DURABLE_OBJECT.get(id);
const name = await stub.setName('agent-2');
return new Response(name);
}

return new Response('Not found', { status: 404 });
},
} satisfies ExportedHandler<Env>,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { expect, it } from 'vitest';
import type { Event } from '@sentry/core';
import { createRunner } from '../../../runner';

// Regression for #23040 — a Durable Object using native private fields must stay functional when
// instrumented with `enableRpcTracePropagation: true`. Native RPC dispatch (Durable Object facets,
// the Agents SDK bootstrap) invokes prototype methods with the stored instance as the receiver,
// so the instrumented instance must not be a Proxy: a Proxy does not carry the private-field
// brand and `this.#field` throws "Cannot read private member".
it('keeps native private fields working when a prototype method is invoked with the instance as receiver', async ({
signal,
}) => {
const runner = createRunner(__dirname)
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as Event;

expect(transactionEvent).toEqual(
expect.objectContaining({
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'rpc',
origin: 'auto.faas.cloudflare.durable_object',
}),
}),
transaction: 'bootstrap',
}),
);
})
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as Event;

expect(transactionEvent).toEqual(
expect.objectContaining({
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'http.server',
origin: 'auto.http.cloudflare',
}),
}),
transaction: 'GET /prototype-dispatch',
}),
);
})
.unordered()
.start(signal);

const response = await runner.makeRequest<string>('get', '/prototype-dispatch');
expect(response).toBe('agent-1');

await runner.completed();
});

it('propagates trace and preserves the result for a regular RPC method call', async ({ signal }) => {
const runner = createRunner(__dirname)
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as Event;

expect(transactionEvent).toEqual(
expect.objectContaining({
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'rpc',
data: expect.objectContaining({
'sentry.origin': 'auto.faas.cloudflare.durable_object',
}),
origin: 'auto.faas.cloudflare.durable_object',
}),
}),
transaction: 'setName',
}),
);
})
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as Event;

expect(transactionEvent).toEqual(
expect.objectContaining({
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'http.server',
origin: 'auto.http.cloudflare',
}),
}),
transaction: 'GET /rpc/set-name',
}),
);
})
.unordered()
.start(signal);

const response = await runner.makeRequest<string>('get', '/rpc/set-name');
expect(response).toBe('agent-2');

await runner.completed();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "cloudflare-do-rpc-private-fields",
"main": "index.ts",
"compatibility_date": "2025-06-17",
"compatibility_flags": ["nodejs_compat"],
"migrations": [
{
"new_sqlite_classes": ["MyDurableObject"],
"tag": "v1",
},
],
"durable_objects": {
"bindings": [
{
"class_name": "MyDurableObject",
"name": "MY_DURABLE_OBJECT",
},
],
},
}
Loading
Loading