Skip to content

Commit 1dbf886

Browse files
committed
fix(cloudflare): Make sure enableRpcTracePropagation is ranked higher
1 parent 0b6628f commit 1dbf886

3 files changed

Lines changed: 66 additions & 7 deletions

File tree

packages/cloudflare/src/durableobject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export function instrumentDurableObjectWithSentry<
133133

134134
// When using the deprecated `instrumentPrototypeMethods` option, always create spans.
135135
// When using the new `enableRpcTracePropagation`, only create spans when RPC metadata is present.
136-
const alwaysTrace = options.instrumentPrototypeMethods !== undefined;
136+
const alwaysTrace = options.enableRpcTracePropagation === undefined;
137137

138138
// Return a Proxy that binds all methods to the original object and creates spans
139139
// for RPC calls that have Sentry trace context propagated.

packages/cloudflare/src/wrapMethodWithSentry.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DurableObjectStorage } from '@cloudflare/workers-types';
2-
import type { SerializedTraceData, Span } from '@sentry/core';
2+
import type { SerializedTraceData } from '@sentry/core';
33
import {
44
captureException,
55
continueTrace,
@@ -18,11 +18,7 @@ import { flushAndDispose } from './flush';
1818
import { ensureInstrumented } from './instrument';
1919
import { init } from './sdk';
2020
import { extractRpcMeta } from './utils/rpcMeta';
21-
import {
22-
buildSpanLinks,
23-
getStoredSpanContext,
24-
storeSpanContext,
25-
} from './utils/traceLinks';
21+
import { buildSpanLinks, getStoredSpanContext, storeSpanContext } from './utils/traceLinks';
2622

2723
/** Extended DurableObjectState with originalStorage exposed by instrumentContext */
2824
interface InstrumentedDurableObjectState extends DurableObjectState {

packages/cloudflare/test/durableobject.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,69 @@ describe('instrumentDurableObjectWithSentry', () => {
8181
expect(initCore).nthCalledWith(2, expect.any(Function), expect.objectContaining({ orgId: 2 }));
8282
});
8383

84+
it('does not create RPC spans without metadata when both RPC options are set', () => {
85+
const startSpanSpy = vi.spyOn(SentryCore, 'startSpan');
86+
vi.spyOn(SentryCore, 'getClient').mockReturnValue(undefined);
87+
88+
const testClass = class {
89+
rpcMethod() {
90+
return 'result';
91+
}
92+
};
93+
const instrumented = instrumentDurableObjectWithSentry(
94+
vi.fn().mockReturnValue({
95+
enableRpcTracePropagation: true,
96+
instrumentPrototypeMethods: true,
97+
}),
98+
testClass as any,
99+
);
100+
const obj = Reflect.construct(instrumented, []);
101+
102+
expect(obj.rpcMethod()).toBe('result');
103+
expect(startSpanSpy).not.toHaveBeenCalled();
104+
});
105+
106+
it('does not create RPC spans without metadata when enableRpcTracePropagation is true', () => {
107+
const startSpanSpy = vi.spyOn(SentryCore, 'startSpan');
108+
vi.spyOn(SentryCore, 'getClient').mockReturnValue(undefined);
109+
110+
const testClass = class {
111+
rpcMethod() {
112+
return 'result';
113+
}
114+
};
115+
const instrumented = instrumentDurableObjectWithSentry(
116+
vi.fn().mockReturnValue({
117+
enableRpcTracePropagation: true,
118+
instrumentPrototypeMethods: false,
119+
}),
120+
testClass as any,
121+
);
122+
const obj = Reflect.construct(instrumented, []);
123+
124+
expect(obj.rpcMethod()).toBe('result');
125+
expect(startSpanSpy).not.toHaveBeenCalled();
126+
});
127+
128+
it('creates RPC spans without metadata when using deprecated instrumentPrototypeMethods', () => {
129+
const startSpanSpy = vi.spyOn(SentryCore, 'startSpan').mockImplementation((_, callback) => callback({} as any));
130+
vi.spyOn(SentryCore, 'getClient').mockReturnValue(undefined);
131+
132+
const testClass = class {
133+
rpcMethod() {
134+
return 'result';
135+
}
136+
};
137+
const instrumented = instrumentDurableObjectWithSentry(
138+
vi.fn().mockReturnValue({ instrumentPrototypeMethods: true }),
139+
testClass as any,
140+
);
141+
const obj = Reflect.construct(instrumented, []);
142+
143+
expect(obj.rpcMethod()).toBe('result');
144+
expect(startSpanSpy).toHaveBeenCalled();
145+
});
146+
84147
it('Binds prototype methods to original object when enableRpcTracePropagation is true', () => {
85148
const testClass = class {
86149
method() {

0 commit comments

Comments
 (0)