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
47 changes: 3 additions & 44 deletions packages/cloudflare/src/orchestrion-deferred-channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,17 @@
*
* On workerd, `channel.publish()`/`runStores()` throw at module/global scope, so a library
* instantiated at module scope (`const app = new Hono()`) cannot emit its wrapping event
* there. {@link ./orchestrion-diagnostics-channel} defers those events here;
* there. `./orchestrion-diagnostics-channel` defers those events on the global marker;
* {@link flushDeferredChannelEvents} emits them once a request is being handled (where
* publish works) and the SDK's `init()` has registered subscribers.
*
* This module imports NO `node:*` builtin, so it is safe to pull into the request wrapper on
* runtimes without `nodejs_compat` — there the queue is always empty (no channels were ever
* injected) and the flush is a no-op.
*/
import { debug, GLOBAL_OBJ } from '@sentry/core';
import { debug } from '@sentry/core';
import { DEBUG_BUILD } from './debug-build';

type Ctx = Record<string, unknown>;

// The minimal slice of a `node:diagnostics_channel` TracingChannel we emit on. Declared locally
// (rather than imported from `node:diagnostics_channel`) so this module's graph carries no `node:`
// specifier — `wrapRequestHandler` must load on runtimes without `nodejs_compat`, and the
// module-graph test forbids any `node:` import reachable from it.
interface PublishableTracingChannel {
start: { publish(ctx: Ctx): void };
end: { publish(ctx: Ctx): void };
error: { publish(ctx: Ctx): void };
}

interface DeferredChannelEvent {
channel: PublishableTracingChannel;
ctx: Ctx;
hadError: boolean;
}

const DEFERRED = Symbol('sentryOrchestrionDeferred');

// The queue is on the global marker, not in this module: a bundle can hold both builds of this module. A transformed
// CommonJS package (`express`) `require`s the facade, so it defers through the CommonJS build, while the request
// wrapper that flushes is usually the ES module build.
function getDeferredEvents(): DeferredChannelEvent[] {
const marker = (GLOBAL_OBJ.__SENTRY_ORCHESTRION__ ??= {});
return (marker.deferredChannelEvents ??= []) as DeferredChannelEvent[];
}

/** workerd's global-scope guard is the only error we degrade on; anything else is a real fault. */
export function isGlobalScopeError(error: unknown): boolean {
return error instanceof Error && /within global scope/.test(error.message);
}

/** Queue a channel event that could not be published now, at most once per context object. */
export function deferChannelEvent(channel: PublishableTracingChannel, ctx: Ctx): void {
if ((ctx as Record<symbol, unknown>)[DEFERRED]) {
return;
}
(ctx as Record<symbol, unknown>)[DEFERRED] = true;
getDeferredEvents().push({ channel, ctx, hadError: 'error' in ctx });
}
import { getDeferredEvents } from './orchestrion-deferred-queue';

/**
* Emit every deferred channel event via the real channel. Called at the top of the request
Expand Down
32 changes: 32 additions & 0 deletions packages/cloudflare/src/orchestrion-deferred-queue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* The queue of orchestrion channel events that could not be published at module scope, shared by
* `./orchestrion-diagnostics-channel`, which defers them, and `./orchestrion-deferred-channels`,
* which flushes them.
*
* This module has no runtime imports: the facade must not load `@sentry/core` (its CommonJS build
* would add a second copy of `@sentry/core` to the bundle), and the request wrapper must not load a
* `node:` builtin (it runs on runtimes without `nodejs_compat`).
*/
import type { InternalGlobal } from '@sentry/core';

export type Ctx = Record<string, unknown>;

export interface PublishableTracingChannel {
start: { publish(ctx: Ctx): void };
end: { publish(ctx: Ctx): void };
error: { publish(ctx: Ctx): void };
}

export interface DeferredChannelEvent {
channel: PublishableTracingChannel;
ctx: Ctx;
hadError: boolean;
}

// The queue is on the global marker, not in this module: a bundle can hold both builds of the SDK. Transformed
// CommonJS packages (`express`, `mysql`) `require` the facade, so they defer through its CommonJS build, while the
// request wrapper that flushes is usually the ES module build.
export function getDeferredEvents(): DeferredChannelEvent[] {
const marker = ((globalThis as InternalGlobal).__SENTRY_ORCHESTRION__ ??= {});
return (marker.deferredChannelEvents ??= []) as DeferredChannelEvent[];
}
28 changes: 23 additions & 5 deletions packages/cloudflare/src/orchestrion-diagnostics-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,35 @@
* buys nothing — and disabling it is what lets the wrapper run (and reach us) at module scope,
* before the per-request cached `init()` has subscribed.
* 2. When a delegated `publish`/`runStores` throws the global-scope error, the event is queued via
* {@link deferChannelEvent} instead. {@link flushDeferredChannelEvents}, called at the top of
* the request handler after `init()`, emits it via the real channel — now inside a request,
* where publish works.
* {@link deferChannelEvent} instead. `flushDeferredChannelEvents` (see
* `./orchestrion-deferred-channels`), called at the top of the request handler after `init()`,
* emits it via the real channel — now inside a request, where publish works.
*
* In a request handler nothing throws, so every call is a transparent pass-through to the real
* channel: lifecycle channels (spans, context binding) behave exactly as on Node.
*
* Transformed CommonJS packages (`express`, `mysql`) `require` this module, so a bundle takes its
* CommonJS build. That is why this module must not import `@sentry/core` at runtime: it would add the
* CommonJS build of `@sentry/core` next to the ES module build that the SDK already uses.
*/
import * as realDc from 'node:diagnostics_channel';
import { deferChannelEvent, isGlobalScopeError } from './orchestrion-deferred-channels';
import { type Ctx, getDeferredEvents } from './orchestrion-deferred-queue';

const DEFERRED = Symbol('sentryOrchestrionDeferred');

/** workerd's global-scope guard is the only error we degrade on; anything else is a real fault. */
function isGlobalScopeError(error: unknown): boolean {
return error instanceof Error && /within global scope/.test(error.message);
}

type Ctx = Record<string, unknown>;
/** Queue a channel event that could not be published now, at most once per context object. */
function deferChannelEvent(channel: realDc.TracingChannel, ctx: Ctx): void {
if ((ctx as Record<symbol, unknown>)[DEFERRED]) {
return;
}
(ctx as Record<symbol, unknown>)[DEFERRED] = true;
getDeferredEvents().push({ channel, ctx, hadError: 'error' in ctx });
}

// One façade per channel name (node's `tracingChannel` is itself a per-name singleton), built
// once when the transformed module declares `const CH = tracingChannel(name)`.
Expand Down
Loading