Skip to content
Merged
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,54 @@
import * as Sentry from '@sentry/cloudflare';
import { DurableObject } from 'cloudflare:workers';

interface Env {
SENTRY_DSN: string;
TEST_DURABLE_OBJECT: DurableObjectNamespace;
}

class SyncAlarmDurableObjectBase extends DurableObject<Env> {
public constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
}

async setAlarm(): Promise<void> {
await this.ctx.storage.setAlarm(Date.now() + 100);
}

// Synchronous alarm handler - this is the case we're testing
alarm(): void {
// Intentionally synchronous - no async/await
const _ = 'sync alarm executed';
}
}

export const TestDurableObject = Sentry.instrumentDurableObjectWithSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
enableRpcTracePropagation: true,
}),
SyncAlarmDurableObjectBase,
);

export default Sentry.withSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}),
{
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);

if (url.pathname === '/set-alarm') {
const id = url.searchParams.get('id') || 'default';
const doId = env.TEST_DURABLE_OBJECT.idFromName(id);
const stub = env.TEST_DURABLE_OBJECT.get(doId) as unknown as SyncAlarmDurableObjectBase;
await stub.setAlarm();
return new Response('Alarm scheduled');
}

return new Response('OK');
},
} satisfies ExportedHandler<Env>,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { expect, it } from 'vitest';
import type { TransactionEvent } from '@sentry/core';
import { createRunner } from '../../../runner';

it('sync alarm links to the trace that scheduled it via sentry.previous_trace', async ({ signal }) => {
let setAlarmTransaction: TransactionEvent | undefined;
let alarmTransaction: TransactionEvent | undefined;
const testId = Date.now().toString();

const runner = createRunner(__dirname)
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent;
expect(transactionEvent).toEqual(
expect.objectContaining({
transaction: expect.stringContaining('/set-alarm'),
}),
);
})
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent;
expect(transactionEvent).toEqual(
expect.objectContaining({
transaction: 'setAlarm',
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'rpc',
origin: 'auto.faas.cloudflare.durable_object',
}),
}),
}),
);
setAlarmTransaction = transactionEvent;
})
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent;
expect(transactionEvent).toEqual(
expect.objectContaining({
type: 'transaction',
transaction: 'alarm',
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'function',
origin: 'auto.faas.cloudflare.durable_object',
}),
}),
}),
);
alarmTransaction = transactionEvent;
})
.unordered()
.start(signal);

await runner.makeRequest('get', `/set-alarm?id=${testId}`);
await runner.completed();

// This is the key assertion: even though the alarm handler is synchronous,
// sentry.previous_trace should still be set because we await the linkPromise
// before teardown in the sync path
const traceData = alarmTransaction!.contexts?.trace?.data as Record<string, unknown> | undefined;
const previousTrace = traceData?.['sentry.previous_trace'] as string | undefined;

expect(previousTrace).toBeDefined();
expect(previousTrace).toMatch(/^[a-f0-9]{32}-[a-f0-9]{16}-[01]$/);

const [linkedTraceId] = previousTrace!.split('-');
expect(linkedTraceId).toBe(setAlarmTransaction!.contexts?.trace?.trace_id);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "durableobject-alarm-links-sync",
"main": "index.ts",
"compatibility_date": "2025-06-17",
"migrations": [
{
"new_sqlite_classes": ["TestDurableObject"],
"tag": "v1",
},
],
"durable_objects": {
"bindings": [
{
"class_name": "TestDurableObject",
"name": "TEST_DURABLE_OBJECT",
},
],
},
"compatibility_flags": ["nodejs_als"],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as Sentry from '@sentry/cloudflare';
import { DurableObject } from 'cloudflare:workers';

interface Env {
SENTRY_DSN: string;
TEST_DURABLE_OBJECT: DurableObjectNamespace;
}

class AlarmDurableObjectBase extends DurableObject<Env> {
public constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
}

async setAlarm(): Promise<void> {
await this.ctx.storage.setAlarm(Date.now() + 100);
}

async alarm(): Promise<void> {
await new Promise<void>(resolve => setTimeout(resolve, 10));
}
}

export const TestDurableObject = Sentry.instrumentDurableObjectWithSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
enableRpcTracePropagation: true,
}),
AlarmDurableObjectBase,
);

export default Sentry.withSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}),
{
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);

if (url.pathname === '/set-alarm') {
const id = url.searchParams.get('id') || 'default';
const doId = env.TEST_DURABLE_OBJECT.idFromName(id);
const stub = env.TEST_DURABLE_OBJECT.get(doId) as unknown as AlarmDurableObjectBase;
await stub.setAlarm();
return new Response('Alarm scheduled');
}

return new Response('OK');
},
} satisfies ExportedHandler<Env>,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { expect, it } from 'vitest';
import type { TransactionEvent } from '@sentry/core';
import { createRunner } from '../../../runner';

it('alarm links to the trace that scheduled it via sentry.previous_trace', async ({ signal }) => {
let setAlarmTransaction: TransactionEvent | undefined;
let alarmTransaction: TransactionEvent | undefined;
const testId = Date.now().toString();

const runner = createRunner(__dirname)
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent;
expect(transactionEvent).toEqual(
expect.objectContaining({
transaction: expect.stringContaining('/set-alarm'),
}),
);
})
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent;
expect(transactionEvent).toEqual(
expect.objectContaining({
transaction: 'setAlarm',
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'rpc',
origin: 'auto.faas.cloudflare.durable_object',
}),
}),
}),
);
setAlarmTransaction = transactionEvent;
})
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1] as TransactionEvent;
expect(transactionEvent).toEqual(
expect.objectContaining({
type: 'transaction',
transaction: 'alarm',
contexts: expect.objectContaining({
trace: expect.objectContaining({
op: 'function',
origin: 'auto.faas.cloudflare.durable_object',
}),
}),
}),
);
alarmTransaction = transactionEvent;
})
.unordered()
.start(signal);

await runner.makeRequest('get', `/set-alarm?id=${testId}`);
await runner.completed();

const traceData = alarmTransaction!.contexts?.trace?.data as Record<string, unknown> | undefined;
const previousTrace = traceData?.['sentry.previous_trace'] as string | undefined;

expect(previousTrace).toBeDefined();
expect(previousTrace).toMatch(/^[a-f0-9]{32}-[a-f0-9]{16}-[01]$/);

const [linkedTraceId] = previousTrace!.split('-');
expect(linkedTraceId).toBe(setAlarmTransaction!.contexts?.trace?.trace_id);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "durableobject-alarm-links",
"main": "index.ts",
"compatibility_date": "2025-06-17",
"migrations": [
{
"new_sqlite_classes": ["TestDurableObject"],
"tag": "v1",
},
],
"durable_objects": {
"bindings": [
{
"class_name": "TestDurableObject",
"name": "TEST_DURABLE_OBJECT",
},
],
},
"compatibility_flags": ["nodejs_als"],
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function instrumentDurableObjectStorage(
// We use the original (uninstrumented) storage (target) to avoid creating a span
// for this internal operation. The storage is deferred via waitUntil to not block.
if (methodName === 'setAlarm') {
await storeSpanContext(target, 'alarm');
storeSpanContext(target, 'alarm');
}
};

Expand Down
16 changes: 10 additions & 6 deletions packages/cloudflare/src/utils/traceLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getTraceLinkKey(methodName: string): string {
* Uses the original uninstrumented storage to avoid creating spans for internal operations.
* Errors are silently ignored to prevent internal storage failures from propagating to user code.
*/
export async function storeSpanContext(originalStorage: DurableObjectStorage, methodName: string): Promise<void> {
export function storeSpanContext(originalStorage: DurableObjectStorage, methodName: string): void {
try {
const activeSpan = getActiveSpan();
if (activeSpan) {
Expand All @@ -36,7 +36,8 @@ export async function storeSpanContext(originalStorage: DurableObjectStorage, me
spanId: spanContext.spanId,
sampled: spanIsSampled(activeSpan),
};
await originalStorage.put(getTraceLinkKey(methodName), storedContext);

originalStorage.kv.put(getTraceLinkKey(methodName), storedContext);
}
} catch (error) {
// Silently ignore storage errors to prevent internal failures from affecting user code
Comment on lines +39 to 43
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The code assumes originalStorage.kv exists, but it is undefined for legacy KV-backed Durable Objects. This causes trace linking to silently fail for those users.
Severity: MEDIUM

Suggested Fix

Add a guard to check for the existence of originalStorage.kv before attempting to use its methods. This will prevent silent errors and the loss of tracing functionality for users on legacy KV-backed Durable Objects. If backward compatibility is required, consider adding a fallback to the previous asynchronous storage mechanism.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/cloudflare/src/utils/traceLinks.ts#L39-L43

Potential issue: The functions `storeSpanContext` and `getStoredSpanContext` were
updated to use the synchronous KV API via `originalStorage.kv`. However, for legacy
KV-backed Durable Objects, the `originalStorage.kv` property is `undefined`. This leads
to a `TypeError` when `put` or `get` is called. Although the error is caught by a
`try-catch` block, preventing a crash, it results in a silent failure. Consequently,
span context is not stored or retrieved for these legacy durable objects, which breaks
distributed tracing continuity for that specific, supported configuration.

Did we get this right? 👍 / 👎 to inform future reviews.

Expand All @@ -46,14 +47,17 @@ export async function storeSpanContext(originalStorage: DurableObjectStorage, me

/**
* Retrieves a stored span context from Durable Object storage.
* Errors are silently ignored to prevent internal storage failures from propagating to user code.
*/
export async function getStoredSpanContext(
export function getStoredSpanContext(
originalStorage: DurableObjectStorage,
methodName: string,
): Promise<StoredSpanContext | undefined> {
): StoredSpanContext | undefined {
try {
return await originalStorage.get<StoredSpanContext>(getTraceLinkKey(methodName));
} catch {
return originalStorage.kv.get<StoredSpanContext>(getTraceLinkKey(methodName));
} catch (error) {
// Silently ignore storage errors to prevent internal failures from affecting user code
DEBUG_BUILD && debug.log(`[CloudflareClient] Error retrieving span context for method ${methodName}`, error);
return undefined;
}
}
Expand Down
Loading
Loading