-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(deno): instrument Deno.serve with async context support #19230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
isaacs
merged 1 commit into
develop
from
isaacschlueter/js-305-support-asynclocalstorage-in-deno
Feb 13, 2026
+13,787
−7,185
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Need to use node: prefix for deno compatibility | ||
| import { AsyncLocalStorage } from 'node:async_hooks'; | ||
| import type { Scope } from '@sentry/core'; | ||
| import { getDefaultCurrentScope, getDefaultIsolationScope, setAsyncContextStrategy } from '@sentry/core'; | ||
|
|
||
| /** | ||
| * Sets the async context strategy to use AsyncLocalStorage. | ||
| * | ||
| * @internal Only exported to be used in higher-level Sentry packages | ||
| * @hidden Only exported to be used in higher-level Sentry packages | ||
| */ | ||
| export function setAsyncLocalStorageAsyncContextStrategy(): void { | ||
| const asyncStorage = new AsyncLocalStorage<{ | ||
| scope: Scope; | ||
| isolationScope: Scope; | ||
| }>(); | ||
|
|
||
| function getScopes(): { scope: Scope; isolationScope: Scope } { | ||
| const scopes = asyncStorage.getStore(); | ||
|
|
||
| if (scopes) { | ||
| return scopes; | ||
| } | ||
|
|
||
| // fallback behavior: | ||
| // if, for whatever reason, we can't find scopes on the context here, we have to fix this somehow | ||
| return { | ||
| scope: getDefaultCurrentScope(), | ||
| isolationScope: getDefaultIsolationScope(), | ||
| }; | ||
| } | ||
|
|
||
| function withScope<T>(callback: (scope: Scope) => T): T { | ||
| const scope = getScopes().scope.clone(); | ||
| const isolationScope = getScopes().isolationScope; | ||
| return asyncStorage.run({ scope, isolationScope }, () => { | ||
| return callback(scope); | ||
| }); | ||
| } | ||
|
|
||
| function withSetScope<T>(scope: Scope, callback: (scope: Scope) => T): T { | ||
| const isolationScope = getScopes().isolationScope.clone(); | ||
| return asyncStorage.run({ scope, isolationScope }, () => { | ||
| return callback(scope); | ||
| }); | ||
| } | ||
|
|
||
| function withIsolationScope<T>(callback: (isolationScope: Scope) => T): T { | ||
| const scope = getScopes().scope; | ||
| const isolationScope = getScopes().isolationScope.clone(); | ||
| return asyncStorage.run({ scope, isolationScope }, () => { | ||
| return callback(isolationScope); | ||
| }); | ||
| } | ||
|
|
||
| function withSetIsolationScope<T>(isolationScope: Scope, callback: (isolationScope: Scope) => T): T { | ||
| const scope = getScopes().scope; | ||
| return asyncStorage.run({ scope, isolationScope }, () => { | ||
| return callback(isolationScope); | ||
| }); | ||
| } | ||
|
|
||
| // In contrast to the browser, we can rely on async context isolation here | ||
| function suppressTracing<T>(callback: () => T): T { | ||
| return withScope(scope => { | ||
| scope.setSDKProcessingMetadata({ __SENTRY_SUPPRESS_TRACING__: true }); | ||
| return callback(); | ||
| }); | ||
| } | ||
|
|
||
| setAsyncContextStrategy({ | ||
| suppressTracing, | ||
| withScope, | ||
| withSetScope, | ||
| withIsolationScope, | ||
| withSetIsolationScope, | ||
| getCurrentScope: () => getScopes().scope, | ||
| getIsolationScope: () => getScopes().isolationScope, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { IntegrationFn } from '@sentry/core'; | ||
| import { defineIntegration } from '@sentry/core'; | ||
| import { setAsyncLocalStorageAsyncContextStrategy } from '../async'; | ||
| import type { RequestHandlerWrapperOptions } from '../wrap-deno-request-handler'; | ||
| import { wrapDenoRequestHandler } from '../wrap-deno-request-handler'; | ||
|
|
||
| const INTEGRATION_NAME = 'DenoServe'; | ||
|
|
||
| export type ServeParams = | ||
| // [(Request) => Response] | ||
| | [Deno.ServeHandler<Deno.NetAddr>] | ||
| // [{ options }, (Request) => Response] | ||
| | [Deno.ServeUnixOptions, Deno.ServeHandler<Deno.UnixAddr>] | ||
| | [Deno.ServeVsockOptions, Deno.ServeHandler<Deno.VsockAddr>] | ||
| | [Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem), Deno.ServeHandler<Deno.NetAddr>] | ||
| // [{ handler: (Request) => Response }] | ||
| | [Deno.ServeUnixOptions & Deno.ServeInit<Deno.UnixAddr>] | ||
| | [Deno.ServeVsockOptions & Deno.ServeInit<Deno.VsockAddr>] | ||
| | [(Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem)) & Deno.ServeInit<Deno.NetAddr>]; | ||
|
|
||
| const isSimpleHandler = (p: ServeParams): p is [Deno.ServeHandler<Deno.Addr>] => typeof p[0] === 'function'; | ||
|
|
||
| const isServeOptWithFunction = (p: ServeParams): p is [Deno.ServeOptions<Deno.Addr>, Deno.ServeHandler<Deno.Addr>] => | ||
| p.length >= 2 && typeof p[1] === 'function' && !!p[0] && typeof p[0] === 'object'; | ||
|
|
||
| const isServeInitOptions = (p: ServeParams): p is [Deno.ServeOptions<Deno.Addr> & Deno.ServeInit<Deno.Addr>] => | ||
| typeof p[0] === 'object' && | ||
| !!p[0] && | ||
| !isServeOptWithFunction(p) && | ||
| 'handler' in p[0] && | ||
| typeof p[0].handler === 'function'; | ||
|
|
||
| const applyHandlerWrap = <A extends Deno.Addr>( | ||
| handler: (request: Request, info: Deno.ServeHandlerInfo<A>) => Response | Promise<Response>, | ||
| serveOptions?: Deno.ServeOptions, | ||
| ): Deno.ServeHandler => | ||
| ((request, info) => | ||
| wrapDenoRequestHandler<A>( | ||
| { | ||
| request, | ||
| info, | ||
| serveOptions, | ||
| } as RequestHandlerWrapperOptions<A>, | ||
| () => handler(request, info as Deno.ServeHandlerInfo<A>), | ||
| )) as Deno.ServeHandler; | ||
|
|
||
| const _denoServeIntegration = (() => { | ||
| return { | ||
| name: INTEGRATION_NAME, | ||
| setupOnce() { | ||
| setAsyncLocalStorageAsyncContextStrategy(); | ||
| Deno.serve = new Proxy(Deno.serve, { | ||
| apply(target, thisArg, args: ServeParams) { | ||
| if (isSimpleHandler(args)) { | ||
| args[0] = applyHandlerWrap(args[0]); | ||
| } else if (isServeOptWithFunction(args)) { | ||
| args[1] = applyHandlerWrap(args[1], args[0]); | ||
| } else if (isServeInitOptions(args)) { | ||
| args[0].handler = applyHandlerWrap(args[0].handler, args[0]); | ||
| } | ||
| // if none of those matched, it'll crash, most likely. | ||
| return target.apply(thisArg, args); | ||
| }, | ||
| }); | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| export const denoServeIntegration = defineIntegration(_denoServeIntegration); | ||
isaacs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import type { Span } from '@sentry/core'; | ||
|
|
||
| export type StreamingGuess = { | ||
| isStreaming: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Classifies a Response as streaming or non-streaming. | ||
| * | ||
| * Heuristics: | ||
| * - No body → not streaming | ||
| * - Known streaming Content-Types → streaming (SSE, NDJSON, JSON streaming) | ||
| * - text/plain without Content-Length → streaming (some AI APIs) | ||
| * - Otherwise → not streaming (conservative default, including HTML/SSR) | ||
| * | ||
| * We avoid probing the stream to prevent blocking on transform streams (like injectTraceMetaTags) | ||
| * or SSR streams that may not have data ready immediately. | ||
| */ | ||
| export function classifyResponseStreaming(res: Response): StreamingGuess { | ||
| if (!res.body) { | ||
| return { isStreaming: false }; | ||
| } | ||
|
|
||
| const contentType = res.headers.get('content-type') ?? ''; | ||
| const contentLength = res.headers.get('content-length'); | ||
|
|
||
| // Streaming: Known streaming content types | ||
| // - text/event-stream: Server-Sent Events (Vercel AI SDK, real-time APIs) | ||
| // - application/x-ndjson, application/ndjson: Newline-delimited JSON | ||
| // - application/stream+json: JSON streaming | ||
| // - text/plain (without Content-Length): Some AI APIs use this for streaming text | ||
| if ( | ||
| /^text\/event-stream\b/i.test(contentType) || | ||
| /^application\/(x-)?ndjson\b/i.test(contentType) || | ||
| /^application\/stream\+json\b/i.test(contentType) || | ||
| (/^text\/plain\b/i.test(contentType) && !contentLength) | ||
| ) { | ||
| return { isStreaming: true }; | ||
| } | ||
|
|
||
| // Default: treat as non-streaming | ||
| return { isStreaming: false }; | ||
| } | ||
|
|
||
| /** | ||
| * Tee a stream, and end the provided span when the stream ends. | ||
| * Returns the other side of the tee, which can be used to send the | ||
| * response to a client. | ||
| */ | ||
| export async function streamResponse(span: Span, res: Response): Promise<Response> { | ||
| const classification = classifyResponseStreaming(res); | ||
|
|
||
| // not streaming, just end the span and return the response | ||
| if (!classification.isStreaming || !res.body) { | ||
| span.end(); | ||
| return res; | ||
| } | ||
|
|
||
| // Streaming response detected - monitor consumption to keep span alive | ||
| try { | ||
| return new Response( | ||
| monitorStream(res.body, () => span.end()), | ||
| { | ||
| status: res.status, | ||
| statusText: res.statusText, | ||
| headers: res.headers, | ||
| }, | ||
| ); | ||
| } catch (e) { | ||
| // tee() failed - handle without streaming | ||
| span.end(); | ||
| return res; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * zero-copy monitoring of stream progress. | ||
| */ | ||
| function monitorStream( | ||
| stream: ReadableStream<Uint8Array<ArrayBufferLike>>, | ||
| onDone: () => void, | ||
| ): ReadableStream<Uint8Array<ArrayBufferLike>> { | ||
| const reader = stream.getReader(); | ||
| reader.closed.finally(() => onDone()); | ||
| return new ReadableStream({ | ||
| async start(controller) { | ||
| let result: ReadableStreamReadResult<Uint8Array<ArrayBufferLike>>; | ||
| do { | ||
| result = await reader.read(); | ||
| if (result.value) { | ||
| try { | ||
| controller.enqueue(result.value); | ||
| } catch (er) { | ||
| controller.error(er); | ||
| reader.releaseLock(); | ||
| return; | ||
| } | ||
| } | ||
| } while (!result.done); | ||
| controller.close(); | ||
isaacs marked this conversation as resolved.
Show resolved
Hide resolved
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| reader.releaseLock(); | ||
| }, | ||
| }); | ||
isaacs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.