-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Capture scope when event loop blocked #18040
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
base: develop
Are you sure you want to change the base?
Changes from all commits
51e639c
4097065
677c3cc
b820d7b
7ba228f
2086558
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { longWork } from './long-work.js'; | ||
|
|
||
| setTimeout(() => { | ||
| process.exit(); | ||
| }, 10000); | ||
|
|
||
| function neverResolve() { | ||
| return new Promise(() => { | ||
| // | ||
| }); | ||
| } | ||
|
|
||
| const fns = [ | ||
| neverResolve, | ||
| neverResolve, | ||
| neverResolve, | ||
| neverResolve, | ||
| neverResolve, | ||
| longWork, // [5] | ||
| neverResolve, | ||
| neverResolve, | ||
| neverResolve, | ||
| neverResolve, | ||
| ]; | ||
|
|
||
| setTimeout(() => { | ||
| for (let id = 0; id < 10; id++) { | ||
| Sentry.withIsolationScope(async () => { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`Starting task ${id}`); | ||
| Sentry.setUser({ id }); | ||
|
|
||
| await fns[id](); | ||
| }); | ||
| } | ||
| }, 1000); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| import { workerData } from 'node:worker_threads'; | ||
| import type { DebugImage, Event, Session, StackFrame, Thread } from '@sentry/core'; | ||
| import type { DebugImage, Event, ScopeData, Session, StackFrame, Thread } from '@sentry/core'; | ||
| import { | ||
| applyScopeDataToEvent, | ||
| createEventEnvelope, | ||
| createSessionEnvelope, | ||
| filenameIsInApp, | ||
| generateSpanId, | ||
| getEnvelopeEndpointWithUrlEncodedAuth, | ||
| makeSession, | ||
| mergeScopeData, | ||
| normalizeUrlToBase, | ||
| Scope, | ||
| stripSentryFramesAndReverse, | ||
| updateSession, | ||
| uuid4, | ||
|
|
@@ -16,6 +20,11 @@ import { captureStackTrace, getThreadsLastSeen } from '@sentry-internal/node-nat | |
| import type { ThreadState, WorkerStartData } from './common'; | ||
| import { POLL_RATIO } from './common'; | ||
|
|
||
| type CurrentScopes = { | ||
| scope: Scope; | ||
| isolationScope: Scope; | ||
| }; | ||
|
|
||
| const { | ||
| threshold, | ||
| appRootPath, | ||
|
|
@@ -178,7 +187,7 @@ function applyDebugMeta(event: Event, debugImages: Record<string, string>): void | |
|
|
||
| function getExceptionAndThreads( | ||
| crashedThreadId: string, | ||
| threads: ReturnType<typeof captureStackTrace<ThreadState>>, | ||
| threads: ReturnType<typeof captureStackTrace<CurrentScopes, ThreadState>>, | ||
| ): Event { | ||
| const crashedThread = threads[crashedThreadId]; | ||
|
|
||
|
|
@@ -217,12 +226,28 @@ function getExceptionAndThreads( | |
| }; | ||
| } | ||
|
|
||
| function applyScopeToEvent(event: Event, scope: ScopeData): void { | ||
| applyScopeDataToEvent(event, scope); | ||
|
|
||
| if (!event.contexts?.trace) { | ||
| const { traceId, parentSpanId, propagationSpanId } = scope.propagationContext; | ||
| event.contexts = { | ||
| trace: { | ||
| trace_id: traceId, | ||
| span_id: propagationSpanId || generateSpanId(), | ||
| parent_span_id: parentSpanId, | ||
| }, | ||
| ...event.contexts, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| async function sendBlockEvent(crashedThreadId: string): Promise<void> { | ||
| if (isRateLimited()) { | ||
| return; | ||
| } | ||
|
|
||
| const threads = captureStackTrace<ThreadState>(); | ||
| const threads = captureStackTrace<CurrentScopes, ThreadState>(); | ||
| const crashedThread = threads[crashedThreadId]; | ||
|
|
||
| if (!crashedThread) { | ||
|
|
@@ -231,7 +256,7 @@ async function sendBlockEvent(crashedThreadId: string): Promise<void> { | |
| } | ||
|
|
||
| try { | ||
| await sendAbnormalSession(crashedThread.state?.session); | ||
| await sendAbnormalSession(crashedThread.pollState?.session); | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
timfish marked this conversation as resolved.
Show resolved
Hide resolved
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| log(`Failed to send abnormal session for thread '${crashedThreadId}':`, error); | ||
| } | ||
|
|
@@ -250,8 +275,17 @@ async function sendBlockEvent(crashedThreadId: string): Promise<void> { | |
| ...getExceptionAndThreads(crashedThreadId, threads), | ||
| }; | ||
|
|
||
| const asyncState = threads[crashedThreadId]?.asyncState; | ||
| if (asyncState) { | ||
| // We need to rehydrate the scopes from the serialized objects so we can call getScopeData() | ||
| const scope = Object.assign(new Scope(), asyncState.scope).getScopeData(); | ||
| const isolationScope = Object.assign(new Scope(), asyncState.isolationScope).getScopeData(); | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mergeScopeData(scope, isolationScope); | ||
| applyScopeToEvent(event, scope); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Scope merge order is backwards, giving wrong priorityThe |
||
| } | ||
|
|
||
| const allDebugImages: Record<string, string> = Object.values(threads).reduce((acc, threadState) => { | ||
| return { ...acc, ...threadState.state?.debugImages }; | ||
| return { ...acc, ...threadState.pollState?.debugImages }; | ||
| }, {}); | ||
|
|
||
| applyDebugMeta(event, allDebugImages); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.