Skip to content
Draft
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
4 changes: 2 additions & 2 deletions packages/cloudflare/src/durableobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { RPC } from '@sentry/conventions/op';
import { getDefaultIsolationScope, getIsolationScope, isObjectLike, startNewTrace } from '@sentry/core';
import type { DurableObject } from 'cloudflare:workers';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils/no-diagnostic-channels';
import { setAsyncLocalStorageAsyncContextStrategyForWorker } from './utils/asyncContextStrategy';
import type { CloudflareOptions } from './client';
import { getInstrumented, markAsInstrumented } from './instrument';
import { instrumentDurableObjectHandlers } from './instrumentations/instrumentDurableObjectHandlers';
Expand Down Expand Up @@ -53,7 +53,7 @@ export function constructInstrumentedDurableObject<E, T extends DurableObject<E>
context: InstrumentedDurableObjectContext;
frameworkManagedMethods: ReadonlySet<string>;
} {
setAsyncLocalStorageAsyncContextStrategy();
setAsyncLocalStorageAsyncContextStrategyForWorker();
const options = getFinalOptions(optionsCallback(env), env);
// See InstrumentedDurableObjectContext — `ctx` is widened to `any` so the concrete
// `DurableObjectState` type never enters the checker's relation graph in this module.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RpcStub, WorkerEntrypoint } from 'cloudflare:workers';
import { RPC } from '@sentry/conventions/op';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils/no-diagnostic-channels';
import { setAsyncLocalStorageAsyncContextStrategyForWorker } from '../utils/asyncContextStrategy';
import type { CloudflareOptions } from '../client';
import { markAsInstrumented } from '../instrument';
import { getFinalOptions } from '../options';
Expand Down Expand Up @@ -151,7 +151,7 @@ export function instrumentWorkerEntrypoint<
// Set up AsyncLocalStorage strategy ONCE at instrumentation time, not per-request
// This is critical - calling this per-request would create a new AsyncLocalStorage
// each time, breaking scope isolation for concurrent requests
setAsyncLocalStorageAsyncContextStrategy();
setAsyncLocalStorageAsyncContextStrategyForWorker();

const InstrumentedClass = new Proxy(WorkerEntrypointClass, {
construct(target, [ctx, env]) {
Expand Down
12 changes: 12 additions & 0 deletions packages/cloudflare/src/utils/asyncContextStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getAsyncContextStrategy, getMainCarrier } from '@sentry/core';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils/no-diagnostic-channels';

/**
* Installs the AsyncLocalStorage strategy unless an OpenTelemetry strategy is installed. Both use the same storage,
* so a switch during a request breaks it (#24603).
*/
export function setAsyncLocalStorageAsyncContextStrategyForWorker(): void {
if (!getAsyncContextStrategy(getMainCarrier()).withActiveSpan) {
setAsyncLocalStorageAsyncContextStrategy();
}
}
4 changes: 2 additions & 2 deletions packages/cloudflare/src/withSentry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils/no-diagnostic-channels';
import { setAsyncLocalStorageAsyncContextStrategyForWorker } from './utils/asyncContextStrategy';
import { instrumentExportedHandlerEmail } from './instrumentations/worker/instrumentEmail';
import { instrumentExportedHandlerFetch } from './instrumentations/worker/instrumentFetch';
import { instrumentExportedHandlerQueue } from './instrumentations/worker/instrumentQueue';
Expand Down Expand Up @@ -37,7 +37,7 @@ export function withSentry<
return instrumentWorkerEntrypoint(optionsCallback as any, handler);
}

setAsyncLocalStorageAsyncContextStrategy();
setAsyncLocalStorageAsyncContextStrategyForWorker();

try {
// oxlint-disable-next-line typescript/no-explicit-any
Expand Down
4 changes: 2 additions & 2 deletions packages/cloudflare/src/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
WorkflowStepRollbackOptions,
WorkflowTimeoutDuration,
} from 'cloudflare:workers';
import { setAsyncLocalStorageAsyncContextStrategy } from '@sentry/server-utils/no-diagnostic-channels';
import { setAsyncLocalStorageAsyncContextStrategyForWorker } from './utils/asyncContextStrategy';
import type { CloudflareOptions } from './client';
import { flushAndDispose, getOriginalWaitUntil } from './flush';
import { markAsInstrumented } from './instrument';
Expand Down Expand Up @@ -254,7 +254,7 @@ export function instrumentWorkflowWithSentry<
get(obj, prop, receiver) {
if (prop === 'run') {
return async function (event: WorkflowEvent<P>, step: WorkflowStep): Promise<unknown> {
setAsyncLocalStorageAsyncContextStrategy();
setAsyncLocalStorageAsyncContextStrategyForWorker();

return withInvocationIsolationScope(async isolationScope => {
const waitUntil = getOriginalWaitUntil(context).bind(context);
Expand Down
28 changes: 28 additions & 0 deletions packages/cloudflare/test/utils/asyncContextStrategy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getAsyncContextStrategy, getMainCarrier } from '@sentry/core';
import { setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
import { afterEach, describe, expect, it } from 'vitest';
import { setAsyncLocalStorageAsyncContextStrategyForWorker } from '../../src/utils/asyncContextStrategy';
import { resetSdk } from '../testUtils';

describe('setAsyncLocalStorageAsyncContextStrategyForWorker', () => {
afterEach(() => {
resetSdk();
});

it('installs the AsyncLocalStorage strategy', () => {
setAsyncLocalStorageAsyncContextStrategyForWorker();

const strategy = getAsyncContextStrategy(getMainCarrier());
expect(strategy.getTracingChannelBinding).toBeDefined();
expect(strategy.withActiveSpan).toBeUndefined();
});

it('keeps an installed OpenTelemetry strategy', () => {
setOpenTelemetryContextAsyncContextStrategy();
const openTelemetryStrategy = getAsyncContextStrategy(getMainCarrier());

setAsyncLocalStorageAsyncContextStrategyForWorker();

expect(getAsyncContextStrategy(getMainCarrier())).toBe(openTelemetryStrategy);
});
});
Loading