-
-
Notifications
You must be signed in to change notification settings - Fork 470
feat(scopes): add clients to scopes #2114
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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| use Sentry\State\HubInterface; | ||
| use Sentry\State\RuntimeContext; | ||
| use Sentry\State\RuntimeContextManager; | ||
| use Sentry\State\Scope; | ||
|
|
||
| /** | ||
| * This class is the main entry point for all the most common SDK features. | ||
|
|
@@ -23,6 +24,11 @@ final class SentrySdk | |
| */ | ||
| private static $currentHub; | ||
|
|
||
| /** | ||
| * @var Scope|null The process-global scope | ||
| */ | ||
| private static $globalScope; | ||
|
|
||
| /** | ||
| * @var RuntimeContextManager|null | ||
| */ | ||
|
|
@@ -41,10 +47,12 @@ private function __construct() | |
| */ | ||
| public static function init(?ClientInterface $client = null): HubInterface | ||
| { | ||
| if ($client === null) { | ||
| $client = new NoOpClient(); | ||
| $hubClient = $client ?? new NoOpClient(); | ||
|
|
||
| if ($client !== null) { | ||
| self::getGlobalScope()->setClient($client); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
| self::$currentHub = new Hub($client); | ||
| self::$currentHub = new Hub($hubClient); | ||
| self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub); | ||
|
|
||
| return self::getCurrentHub(); | ||
|
|
@@ -79,6 +87,31 @@ public static function setCurrentHub(HubInterface $hub): HubInterface | |
| return $hub; | ||
| } | ||
|
|
||
| public static function getGlobalScope(): Scope | ||
| { | ||
| if (self::$globalScope === null) { | ||
| self::$globalScope = new Scope(); | ||
| } | ||
|
|
||
| return self::$globalScope; | ||
| } | ||
|
|
||
| public static function getIsolationScope(): Scope | ||
| { | ||
| return self::getCurrentRuntimeContext()->getIsolationScope(); | ||
| } | ||
|
|
||
| public static function getClient(): ClientInterface | ||
| { | ||
| $client = self::getIsolationScope()->getClient(); | ||
|
|
||
| if (!$client instanceof NoOpClient) { | ||
| return $client; | ||
| } | ||
|
|
||
| return self::getGlobalScope()->getClient(); | ||
|
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. bindClient ignores global scopeMedium Severity
Reviewed by Cursor Bugbot for commit c86807c. Configure here.
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public static function startContext(): void | ||
| { | ||
| self::getRuntimeContextManager()->startContext(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,12 @@ | |
|
|
||
| use Sentry\Attachment\Attachment; | ||
| use Sentry\Breadcrumb; | ||
| use Sentry\ClientInterface; | ||
| use Sentry\Event; | ||
| use Sentry\EventHint; | ||
| use Sentry\EventId; | ||
| use Sentry\EventType; | ||
| use Sentry\NoOpClient; | ||
| use Sentry\Options; | ||
| use Sentry\Severity; | ||
| use Sentry\Tracing\DynamicSamplingContext; | ||
|
|
@@ -36,6 +39,16 @@ class Scope | |
| */ | ||
| private $propagationContext; | ||
|
|
||
| /** | ||
| * @var ClientInterface The client bound to this scope | ||
| */ | ||
| private $client; | ||
|
|
||
| /** | ||
| * @var EventId|null The ID of the last captured event | ||
| */ | ||
| private $lastEventId; | ||
|
|
||
| /** | ||
| * @var Breadcrumb[] The list of breadcrumbs recorded in this scope | ||
| */ | ||
|
|
@@ -110,6 +123,7 @@ class Scope | |
| public function __construct(?PropagationContext $propagationContext = null) | ||
| { | ||
| $this->propagationContext = $propagationContext ?? PropagationContext::fromDefaults(); | ||
| $this->client = new NoOpClient(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -143,6 +157,42 @@ public static function mergeScopes(self $globalScope, self $isolationScope): sel | |
| return $mergedScope; | ||
| } | ||
|
|
||
| /** | ||
|
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. mergeScopes drops global clientHigh Severity
Reviewed by Cursor Bugbot for commit c86807c. Configure here.
cursor[bot] marked this conversation as resolved.
|
||
| * Returns the client bound to this scope. | ||
| */ | ||
| public function getClient(): ClientInterface | ||
| { | ||
| return $this->client; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the client bound to this scope. | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function setClient(ClientInterface $client): self | ||
| { | ||
| $this->client = $client; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the ID of the last captured event. | ||
| */ | ||
| public function getLastEventId(): ?EventId | ||
| { | ||
| return $this->lastEventId; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| public function setLastEventId(?EventId $lastEventId): void | ||
| { | ||
| $this->lastEventId = $lastEventId; | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array<string, bool>> $globalFlags | ||
| * @param array<int, array<string, bool>> $isolationFlags | ||
|
|
@@ -161,7 +211,7 @@ private static function mergeFlags(array $globalFlags, array $isolationFlags): a | |
| } | ||
|
|
||
| unset($flagsByKey[$flagKey]); | ||
| $flagsByKey[$flagKey] = (bool) current($flag); | ||
| $flagsByKey[$flagKey] = current($flag); | ||
| } | ||
|
|
||
| $flagsByKey = \array_slice($flagsByKey, -self::MAX_FLAGS, self::MAX_FLAGS, true); | ||
|
|
@@ -483,7 +533,8 @@ public static function getExternalPropagationContext(): ?array | |
| } | ||
|
|
||
| /** | ||
| * Clears the scope and resets any data it contains. | ||
| * Clears event payload data from the scope. The client binding and last | ||
| * event ID are preserved. | ||
| * | ||
| * @return $this | ||
| */ | ||
|
|
||


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.
Re-init leaves stale global client
Medium Severity
Calling
SentrySdk::init()without a client creates a hub withNoOpClientbut does not update the process-global scope’s client when one was set by an earlierinit($client).SentrySdk::getClient()then keeps returning the previous real client while the current hub uses a no-op client.Additional Locations (1)
src/SentrySdk.php#L103-L112Reviewed by Cursor Bugbot for commit c86807c. Configure here.