-
-
Notifications
You must be signed in to change notification settings - Fork 470
feat(scopes): add new scopes #2113
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -112,6 +112,69 @@ | |
| $this->propagationContext = $propagationContext ?? PropagationContext::fromDefaults(); | ||
| } | ||
|
|
||
| /** | ||
| * Merges the process-global scope underneath the current isolation scope. | ||
| * | ||
| * The returned scope is transient and should be used for one event capture. | ||
| * | ||
| * @internal | ||
| */ | ||
| public static function mergeScopes(self $globalScope, self $isolationScope): self | ||
| { | ||
| $mergedScope = clone $isolationScope; | ||
|
|
||
| $mergedScope->tags = array_merge($globalScope->tags, $isolationScope->tags); | ||
| $mergedScope->extra = array_merge($globalScope->extra, $isolationScope->extra); | ||
| $mergedScope->contexts = array_merge($globalScope->contexts, $isolationScope->contexts); | ||
|
|
||
| if ($globalScope->user !== null && $isolationScope->user !== null) { | ||
| $mergedScope->user = (clone $globalScope->user)->merge($isolationScope->user); | ||
| } elseif ($globalScope->user !== null) { | ||
| $mergedScope->user = clone $globalScope->user; | ||
| } | ||
|
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. Global user revives cleared isolationMedium Severity When the isolation scope has no user (including after Reviewed by Cursor Bugbot for commit 15faea1. Configure here. |
||
|
|
||
| $mergedScope->level = $isolationScope->level ?? $globalScope->level; | ||
| $mergedScope->fingerprint = array_merge($globalScope->fingerprint, $isolationScope->fingerprint); | ||
| $mergedScope->breadcrumbs = \array_slice(array_merge($globalScope->breadcrumbs, $isolationScope->breadcrumbs), -100); | ||
| $mergedScope->flags = self::mergeFlags($globalScope->flags, $isolationScope->flags); | ||
| $mergedScope->attachments = array_merge($globalScope->attachments, $isolationScope->attachments); | ||
| $mergedScope->eventProcessors = array_merge($globalScope->eventProcessors, $isolationScope->eventProcessors); | ||
|
|
||
| return $mergedScope; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array<string, bool>> $globalFlags | ||
| * @param array<int, array<string, bool>> $isolationFlags | ||
| * | ||
| * @return array<int, array<string, bool>> | ||
| */ | ||
| private static function mergeFlags(array $globalFlags, array $isolationFlags): array | ||
| { | ||
| $flagsByKey = []; | ||
|
|
||
| foreach (array_merge($globalFlags, $isolationFlags) as $flag) { | ||
| $flagKey = key($flag); | ||
|
|
||
| if ($flagKey === null) { | ||
| continue; | ||
| } | ||
|
|
||
| unset($flagsByKey[$flagKey]); | ||
| $flagsByKey[$flagKey] = (bool) current($flag); | ||
|
Check notice on line 164 in src/State/Scope.php
|
||
| } | ||
|
|
||
| $flagsByKey = \array_slice($flagsByKey, -self::MAX_FLAGS, self::MAX_FLAGS, true); | ||
|
|
||
| $flags = []; | ||
|
|
||
| foreach ($flagsByKey as $flagKey => $flagResult) { | ||
| $flags[] = [$flagKey => $flagResult]; | ||
| } | ||
|
|
||
| return $flags; | ||
| } | ||
|
|
||
| /** | ||
| * Sets a new tag in the tags context. | ||
| * | ||
|
|
||


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.
Bug: The
mergeScopesfunction performs a shallow clone of the scope, causing thespanobject to be shared between the original and merged scopes, which can lead to side effects.Severity: LOW
Suggested Fix
Update the
Scope::__clone()method to perform a deep clone of the$spanproperty. This will ensure that merged scopes have their own independentspaninstance, preventing unintended side effects if the span is mutated after the merge operation. Additionally, add a test case to verify the behavior when the isolation scope has a span.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.