-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: Streamline isolation scope handling & reset in isolation scopes #22890
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
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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,93 @@ | ||
| import { getClient, getCurrentScope, withIsolationScope } from './currentScopes'; | ||
| import { DEBUG_BUILD } from './debug-build'; | ||
| import { startNewTrace } from './tracing/trace'; | ||
| import type { CheckIn, FinishedCheckIn, MonitorConfig } from './types/checkin'; | ||
| import { debug } from './utils/debug-logger'; | ||
| import { isThenable } from './utils/is'; | ||
| import { uuid4 } from './utils/misc'; | ||
| import { timestampInSeconds } from './utils/time'; | ||
|
|
||
| /** | ||
| * Wraps a callback with a cron monitor check in. The check in will be sent to Sentry when the callback finishes. | ||
| * | ||
| * @param monitorSlug The distinct slug of the monitor. | ||
| * @param callback Callback to be monitored | ||
| * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want | ||
| * to create a monitor automatically when sending a check in. | ||
| */ | ||
| export function withMonitor<T>( | ||
| monitorSlug: CheckIn['monitorSlug'], | ||
| callback: () => T, | ||
| upsertMonitorConfig?: MonitorConfig, | ||
| ): T { | ||
| function runCallback(): T { | ||
| const checkInId = captureCheckIn({ monitorSlug, status: 'in_progress' }, upsertMonitorConfig); | ||
| const now = timestampInSeconds(); | ||
|
|
||
| function finishCheckIn(status: FinishedCheckIn['status']): void { | ||
| captureCheckIn({ monitorSlug, status, checkInId, duration: timestampInSeconds() - now }); | ||
| } | ||
| // Default behavior without isolateTrace | ||
| let maybePromiseResult: T; | ||
| try { | ||
| maybePromiseResult = callback(); | ||
| } catch (e) { | ||
| finishCheckIn('error'); | ||
| throw e; | ||
| } | ||
|
|
||
| if (isThenable(maybePromiseResult)) { | ||
| return maybePromiseResult.then( | ||
| r => { | ||
| finishCheckIn('ok'); | ||
| return r; | ||
| }, | ||
| e => { | ||
| finishCheckIn('error'); | ||
| throw e; | ||
| }, | ||
| ) as T; | ||
| } | ||
| finishCheckIn('ok'); | ||
|
|
||
| return maybePromiseResult; | ||
| } | ||
|
|
||
| // With isolation scope resets the propagation context, so if we do not pass isolateTace, we want to manually continue the trace | ||
| const oldPropagationContext = getCurrentScope().getPropagationContext(); | ||
|
|
||
| return withIsolationScope(() => { | ||
| if (upsertMonitorConfig?.isolateTrace) { | ||
| return startNewTrace(runCallback); | ||
| } | ||
|
|
||
| // If we are not isolating the trace, in this case we want to keep the same trace as the parent | ||
| const newPropagationContext = getCurrentScope().getPropagationContext(); | ||
| if (!newPropagationContext.parentSpanId) { | ||
| getCurrentScope().setPropagationContext(oldPropagationContext); | ||
| } | ||
|
|
||
| return runCallback(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Create a cron monitor check in and send it to Sentry. | ||
| * | ||
| * @param checkIn An object that describes a check in. | ||
| * @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want | ||
| * to create a monitor automatically when sending a check in. | ||
| */ | ||
| export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorConfig): string { | ||
| const scope = getCurrentScope(); | ||
| const client = getClient(); | ||
| if (!client) { | ||
| DEBUG_BUILD && debug.warn('Cannot capture check-in. No client defined.'); | ||
| } else if (!client.captureCheckIn) { | ||
| DEBUG_BUILD && debug.warn('Cannot capture check-in. Client does not support sending check-ins.'); | ||
| } else { | ||
| return client.captureCheckIn(checkIn, upsertMonitorConfig, scope); | ||
| } | ||
|
|
||
| return uuid4(); | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -367,7 +367,7 @@ function createChildOrRootSpan({ | |
| const isolationScope = getIsolationScope(); | ||
|
|
||
| if (!hasSpansEnabled()) { | ||
| const scopePropagationContext = { ...isolationScope.getPropagationContext(), ...scope.getPropagationContext() }; | ||
| const scopePropagationContext = scope.getPropagationContext(); | ||
|
Member
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. it seems like this removes a footgun where we potentially incorrectly merged a propagation context before here. I like it! |
||
| const traceId = parentSpan ? parentSpan.spanContext().traceId : scopePropagationContext.traceId; | ||
|
|
||
| // The placeholder is a thin marker; it carries no sampling decision or DSC. Both are read from | ||
|
|
||
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
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.