-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Extract scope application to util #9804
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
Merged
Changes from all commits
Commits
Show all changes
4 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
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,97 @@ | ||
import type { Breadcrumb, Event, PropagationContext, ScopeData, Span } from '@sentry/types'; | ||
import { arrayify } from '@sentry/utils'; | ||
|
||
/** | ||
* Applies data from the scope to the event and runs all event processors on it. | ||
*/ | ||
export function applyScopeDataToEvent(event: Event, data: ScopeData): void { | ||
const { fingerprint, span, breadcrumbs, sdkProcessingMetadata, propagationContext } = data; | ||
|
||
// Apply general data | ||
applyDataToEvent(event, data); | ||
|
||
// We want to set the trace context for normal events only if there isn't already | ||
// a trace context on the event. There is a product feature in place where we link | ||
// errors with transaction and it relies on that. | ||
if (span) { | ||
applySpanToEvent(event, span); | ||
} | ||
|
||
applyFingerprintToEvent(event, fingerprint); | ||
applyBreadcrumbsToEvent(event, breadcrumbs); | ||
applySdkMetadataToEvent(event, sdkProcessingMetadata, propagationContext); | ||
} | ||
|
||
function applyDataToEvent(event: Event, data: ScopeData): void { | ||
const { extra, tags, user, contexts, level, transactionName } = data; | ||
|
||
if (extra && Object.keys(extra).length) { | ||
event.extra = { ...extra, ...event.extra }; | ||
} | ||
if (tags && Object.keys(tags).length) { | ||
event.tags = { ...tags, ...event.tags }; | ||
} | ||
if (user && Object.keys(user).length) { | ||
event.user = { ...user, ...event.user }; | ||
} | ||
if (contexts && Object.keys(contexts).length) { | ||
event.contexts = { ...contexts, ...event.contexts }; | ||
} | ||
if (level) { | ||
event.level = level; | ||
} | ||
if (transactionName) { | ||
event.transaction = transactionName; | ||
} | ||
} | ||
|
||
function applyBreadcrumbsToEvent(event: Event, breadcrumbs: Breadcrumb[]): void { | ||
const mergedBreadcrumbs = [...(event.breadcrumbs || []), ...breadcrumbs]; | ||
event.breadcrumbs = mergedBreadcrumbs.length ? mergedBreadcrumbs : undefined; | ||
} | ||
|
||
function applySdkMetadataToEvent( | ||
event: Event, | ||
sdkProcessingMetadata: ScopeData['sdkProcessingMetadata'], | ||
propagationContext: PropagationContext, | ||
): void { | ||
event.sdkProcessingMetadata = { | ||
...event.sdkProcessingMetadata, | ||
...sdkProcessingMetadata, | ||
propagationContext: propagationContext, | ||
}; | ||
} | ||
|
||
function applySpanToEvent(event: Event, span: Span): void { | ||
event.contexts = { trace: span.getTraceContext(), ...event.contexts }; | ||
const transaction = span.transaction; | ||
if (transaction) { | ||
event.sdkProcessingMetadata = { | ||
dynamicSamplingContext: transaction.getDynamicSamplingContext(), | ||
...event.sdkProcessingMetadata, | ||
}; | ||
const transactionName = transaction.name; | ||
if (transactionName) { | ||
event.tags = { transaction: transactionName, ...event.tags }; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Applies fingerprint from the scope to the event if there's one, | ||
* uses message if there's one instead or get rid of empty fingerprint | ||
*/ | ||
function applyFingerprintToEvent(event: Event, fingerprint: ScopeData['fingerprint'] | undefined): void { | ||
// Make sure it's an array first and we actually have something in place | ||
event.fingerprint = event.fingerprint ? arrayify(event.fingerprint) : []; | ||
|
||
// If we have something on the scope, then merge it with event | ||
if (fingerprint) { | ||
event.fingerprint = event.fingerprint.concat(fingerprint); | ||
} | ||
|
||
// If we have no data at all, remove empty array default | ||
if (event.fingerprint && !event.fingerprint.length) { | ||
delete event.fingerprint; | ||
} | ||
} |
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.
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.
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.
just to confirm since we deprecated this function: The same logic here is already present in places calling
applyScopeDataToEvent
, right?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.
yes, the behavior remains the same here as before!