Skip to content

Commit 52d276c

Browse files
cursoragentclaude
andcommitted
fix(cloudflare): Remove redundant SpanLink type and optimize storage instrumentation
- Export SpanLink and SpanLinkJSON types from @sentry/core - Replace local SpanLink type in cloudflare package with core export - Optimize instrumentDurableObjectStorage to only apply teardown logic to setAlarm - Remove unnecessary .then() wrapper and waitUntil calls for non-setAlarm methods Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 43b8f06 commit 52d276c

4 files changed

Lines changed: 30 additions & 34 deletions

File tree

packages/cloudflare/src/instrumentations/instrumentDurableObjectStorage.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,35 @@ export function instrumentDurableObjectStorage(
5757
},
5858
},
5959
() => {
60-
const teardown = async (): Promise<void> => {
61-
// When setAlarm is called, store the current span context so that when the alarm
62-
// fires later, it can link back to the trace that called setAlarm.
63-
// We use the original (uninstrumented) storage (target) to avoid creating a span
64-
// for this internal operation. The storage is deferred via waitUntil to not block.
65-
if (methodName === 'setAlarm') {
66-
await storeSpanContext(target, 'alarm');
67-
}
68-
};
69-
7060
const result = (original as (...args: unknown[]) => unknown).apply(target, args);
7161

72-
if (!isThenable(result)) {
73-
waitUntil?.(teardown());
62+
// Only setAlarm needs teardown to store span context for trace linking
63+
if (methodName === 'setAlarm') {
64+
const teardown = async (): Promise<void> => {
65+
// Store the current span context so that when the alarm fires later,
66+
// it can link back to the trace that called setAlarm.
67+
// We use the original (uninstrumented) storage (target) to avoid creating a span
68+
// for this internal operation. The storage is deferred via waitUntil to not block.
69+
await storeSpanContext(target, 'alarm');
70+
};
71+
72+
if (!isThenable(result)) {
73+
waitUntil?.(teardown());
74+
return result;
75+
}
7476

75-
return result;
77+
return result.then(
78+
res => {
79+
waitUntil?.(teardown());
80+
return res;
81+
},
82+
e => {
83+
throw e;
84+
},
85+
);
7686
}
7787

78-
return result.then(
79-
res => {
80-
waitUntil?.(teardown());
81-
return res;
82-
},
83-
e => {
84-
throw e;
85-
},
86-
);
88+
return result;
8789
},
8890
);
8991
};

packages/cloudflare/src/utils/traceLinks.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DurableObjectStorage } from '@cloudflare/workers-types';
22
import { TraceFlags } from '@opentelemetry/api';
3-
import { getActiveSpan } from '@sentry/core';
3+
import { getActiveSpan, type SpanLink } from '@sentry/core';
44

55
/** Storage key prefix for the span context that links consecutive method invocations */
66
const SENTRY_TRACE_LINK_KEY_PREFIX = '__SENTRY_TRACE_LINK__';
@@ -12,16 +12,6 @@ export interface StoredSpanContext {
1212
sampled: boolean;
1313
}
1414

15-
/** Span link structure for connecting traces */
16-
export interface SpanLink {
17-
context: {
18-
traceId: string;
19-
spanId: string;
20-
traceFlags: number;
21-
};
22-
attributes?: Record<string, string>;
23-
}
24-
2515
/**
2616
* Gets the storage key for a specific method's trace link.
2717
*/

packages/cloudflare/src/wrapMethodWithSentry.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ export function wrapMethodWithSentry<T extends OriginalMethod>(
9090
// but the scope still holds a reference to it (e.g., alarm handlers in Durable Objects)
9191
// For startNewTrace, always create a fresh client
9292
if (startNewTrace || !scopeClient?.getTransport()) {
93-
const client = init({ ...wrapperOptions.options, ctx: context as unknown as ExecutionContext | undefined });
93+
const client = init({
94+
...wrapperOptions.options,
95+
ctx: context as unknown as ExecutionContext | undefined,
96+
});
9497
scope.setClient(client);
9598
scopeClient = client;
9699
}

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ export type {
361361
XhrBreadcrumbHint,
362362
} from './types-hoist/breadcrumb';
363363
export type { ClientReport, Outcome, EventDropReason } from './types-hoist/clientreport';
364+
export type { SpanLink, SpanLinkJSON } from './types-hoist/link';
364365
export type {
365366
Context,
366367
Contexts,

0 commit comments

Comments
 (0)