-
Notifications
You must be signed in to change notification settings - Fork 5.6k
fix(sentry): stop billing spans that declared a parent as segments #45581
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
MajorLift
wants to merge
3
commits into
main
Choose a base branch
from
fix/trace-unresolvable-parent-becomes-segment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−3
Open
Changes from all commits
Commits
Show all changes
3 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -483,7 +483,9 @@ function isValidSentrySpan(value: unknown): value is Sentry.Span { | |
| * @returns Resolved Sentry Span or null. | ||
| */ | ||
| function resolveParentSpan(parentContext: unknown): Sentry.Span | null { | ||
| if (!parentContext) { | ||
| // Nullish, not falsy: `TraceContext` is `unknown`, so `0` / `''` / `false` | ||
| // reach here, and none of them state "no parent was declared". | ||
| if (parentContext === null || parentContext === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
|
|
@@ -508,6 +510,22 @@ function resolveParentSpan(parentContext: unknown): Sentry.Span | null { | |
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Whether a parent context names a trace this process started and expects to | ||
| * resolve from `tracesByKey`. Such a context is not a cross-process | ||
| * continuation, even when it also carries serialized ids for propagation. | ||
| * | ||
| * @param value - Candidate parent context. | ||
| * @returns True when the context names an in-process parent. | ||
| */ | ||
| function hasInProcessIdentity(value: unknown): value is { _name: string } { | ||
| return ( | ||
| isObject(value) && | ||
| hasProperty(value, '_name') && | ||
| typeof value._name === 'string' | ||
| ); | ||
| } | ||
|
|
||
| function hasDistributedTraceIds( | ||
| value: unknown, | ||
| ): value is { _traceId: string; _spanId: string } { | ||
|
|
@@ -529,13 +547,26 @@ function startSpan<T>( | |
| const { data: attributes, name, parentContext, startTime, op } = request; | ||
| let parentSpan = resolveParentSpan(parentContext); | ||
|
|
||
| // `undefined` and `null` are different statements and must not be collapsed: | ||
|
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. This seems to contradict the changes made in |
||
| // | ||
| // undefined -> no parent was declared. Inheriting the ambient span and | ||
| // promoting to a transaction is the intended behaviour. | ||
| // null -> a parent WAS intended but no span exists for it. | ||
| // `traceCallback` is typed `(span: Sentry.Span | null)`, so a | ||
| // caller receives `null` whenever the SDK created no span and | ||
| // passes it straight back. Promoting that to a transaction | ||
| // bills a root for what the caller asked to be a child. | ||
| // | ||
| // See https://github.com/MetaMask/MetaMask-planning/issues/7569 | ||
| const parentDeclared = parentContext !== undefined; | ||
|
|
||
| // Inherit from active span (e.g. browserTracingIntegration's pageload/navigation) | ||
| // when no explicit parent is provided. Must capture before withIsolationScope | ||
| // severs the active span context chain. | ||
| // forceTransaction preserves transaction-level visibility for monitoring while | ||
| // linking to the auto-instrumentation hierarchy. | ||
| let forceTransaction: boolean | undefined; | ||
| if (!parentSpan && !parentContext) { | ||
| if (!parentSpan && !parentDeclared) { | ||
| const activeSpan = sentryGetActiveSpan(); | ||
| if (activeSpan) { | ||
| parentSpan = activeSpan; | ||
|
|
@@ -554,7 +585,14 @@ function startSpan<T>( | |
|
|
||
| // Cross-process propagation via continueTrace when we have serialized | ||
| // trace/span IDs but couldn't resolve a local parent span from the map. | ||
| if (!parentSpan && hasDistributedTraceIds(parentContext)) { | ||
| // Only a context WITHOUT in-process identity is a genuine cross-process | ||
| // continuation. One that names a local trace was meant to resolve from the | ||
| // map; routing a lookup miss here mints a segment, and a segment is billed. | ||
| if ( | ||
| !parentSpan && | ||
| hasDistributedTraceIds(parentContext) && | ||
| !hasInProcessIdentity(parentContext) | ||
| ) { | ||
| const sentryTrace = `${parentContext._traceId}-${parentContext._spanId}-1`; | ||
| return sentryContinueTrace(sentryTrace, () => | ||
| sentryWithIsolationScope((scope: Sentry.Scope) => { | ||
|
|
@@ -564,6 +602,20 @@ function startSpan<T>( | |
| ); | ||
| } | ||
|
|
||
| // A parent was declared (`null`, or an in-process context that missed the | ||
| // map) but did not resolve. Nest under whatever is active rather than | ||
| // starting a new segment — and deliberately WITHOUT `forceTransaction`, since | ||
| // a failed lookup is not a statement that this operation is a root. | ||
| if (!parentSpan && parentDeclared) { | ||
| const activeSpan = sentryGetActiveSpan(); | ||
| if (activeSpan) { | ||
| return sentryWithIsolationScope((scope: Sentry.Scope) => { | ||
| initScope(scope, request); | ||
| return callback({ ...spanOptions, parentSpan: activeSpan }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return sentryWithIsolationScope((scope: Sentry.Scope) => { | ||
| initScope(scope, request); | ||
| return callback(spanOptions); | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This explanation seems a bit excessive. Is it really helpful to list all nullish values here? Also, why use a different term here than the variable
TraceContextvsparentContext.