← All changes
|
vendor_prefixed/sentry/sentry/src/SentrySdk.php
+49
-9
1.10.16
→
1.10.23
View file →
| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | use WCPOS\Vendor\Sentry\State\Hub; |
| 9 | 9 | use WCPOS\Vendor\Sentry\State\HubInterface; |
| 10 | 10 | use WCPOS\Vendor\Sentry\State\RuntimeContext; |
| 11 | 11 | use WCPOS\Vendor\Sentry\State\RuntimeContextManager; |
| 12 | +use WCPOS\Vendor\Sentry\State\RuntimeContextStorageInterface; | |
| 12 | 13 | /** |
| 13 | 14 | * This class is the main entry point for all the most common SDK features. |
| 14 | 15 | * |
| 15 | 16 | * @author Stefano Arlandini <[email protected]> |
| @@ -24,8 +25,12 @@ | ||
| 24 | 25 | * @var RuntimeContextManager|null |
| 25 | 26 | */ |
| 26 | 27 | private static $runtimeContextManager; |
| 27 | 28 | /** |
| 29 | + * @var RuntimeContextStorageInterface|null | |
| 30 | + */ | |
| 31 | + private static $runtimeContextStorage; | |
| 32 | + /** | |
| 28 | 33 | * Constructor. |
| 29 | 34 | */ |
| 30 | 35 | private function __construct() |
| 31 | 36 | { |
| @@ -35,13 +40,32 @@ | ||
| 35 | 40 | * gets called. |
| 36 | 41 | */ |
| 37 | 42 | public static function init() : HubInterface |
| 38 | 43 | { |
| 44 | + if (self::$runtimeContextManager !== null) { | |
| 45 | + self::$runtimeContextManager->discardActiveContext(); | |
| 46 | + } | |
| 39 | 47 | self::$currentHub = new Hub(); |
| 40 | - self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub); | |
| 48 | + self::$runtimeContextManager = null; | |
| 41 | 49 | return self::getCurrentHub(); |
| 42 | 50 | } |
| 43 | 51 | /** |
| 52 | + * Registers storage for isolating runtime contexts across overlapping logical executions. | |
| 53 | + * | |
| 54 | + * The registration persists across SDK initialization. Changing it discards the active | |
| 55 | + * context for the current logical execution without flushing it. Concurrent runtimes | |
| 56 | + * should register storage before logical executions begin and must not replace it while | |
| 57 | + * other logical executions are active. | |
| 58 | + */ | |
| 59 | + public static function setRuntimeContextStorage(?RuntimeContextStorageInterface $runtimeContextStorage) : void | |
| 60 | + { | |
| 61 | + if (self::$runtimeContextManager !== null) { | |
| 62 | + self::$runtimeContextManager->discardActiveContext(); | |
| 63 | + } | |
| 64 | + self::$runtimeContextStorage = $runtimeContextStorage; | |
| 65 | + self::$runtimeContextManager = null; | |
| 66 | + } | |
| 67 | + /** | |
| 44 | 68 | * Gets the current hub. If it's not initialized then creates a new instance |
| 45 | 69 | * and sets it as current hub. |
| 46 | 70 | */ |
| 47 | 71 | public static function getCurrentHub() : HubInterface |
| @@ -64,12 +88,31 @@ | ||
| 64 | 88 | self::$currentHub = $hub; |
| 65 | 89 | } |
| 66 | 90 | return $hub; |
| 67 | 91 | } |
| 68 | - public static function startContext() : void | |
| 92 | + /** | |
| 93 | + * Starts an isolated context for the current logical execution. | |
| 94 | + * | |
| 95 | + * A provided hub is used as-is, allowing runtimes with their own HubInterface | |
| 96 | + * implementation to manage hub isolation. When no hub is provided, the SDK | |
| 97 | + * creates an isolated hub from the baseline. | |
| 98 | + * | |
| 99 | + * If a context is already active, this method is a no-op and the provided hub | |
| 100 | + * is ignored. Use setCurrentHub() to replace the active context's hub. | |
| 101 | + * | |
| 102 | + * @param HubInterface|null $hub The hub to use for the new context | |
| 103 | + */ | |
| 104 | + public static function startContext(?HubInterface $hub = null) : void | |
| 69 | 105 | { |
| 70 | - self::getRuntimeContextManager()->startContext(); | |
| 106 | + self::getRuntimeContextManager()->startContext($hub); | |
| 71 | 107 | } |
| 108 | + /** | |
| 109 | + * Ends and flushes the active context for the current logical execution. | |
| 110 | + * | |
| 111 | + * When no context is active this is a no-op. | |
| 112 | + * | |
| 113 | + * @param int|null $timeout The maximum number of seconds to wait while flushing the client transport | |
| 114 | + */ | |
| 72 | 115 | public static function endContext(?int $timeout = null) : void |
| 73 | 116 | { |
| 74 | 117 | self::getRuntimeContextManager()->endContext($timeout); |
| 75 | 118 | } |
| @@ -75,9 +118,9 @@ | ||
| 75 | 118 | } |
| 76 | 119 | /** |
| 77 | 120 | * Executes the given callback within an isolated context. |
| 78 | 121 | * |
| 79 | - * If a context is already active for the current execution key, this method | |
| 122 | + * If a context is already active for the current logical execution, this method | |
| 80 | 123 | * reuses it and only executes the callback. |
| 81 | 124 | * |
| 82 | 125 | * @param callable $callback The callback to execute |
| 83 | 126 | * |
| @@ -91,12 +134,9 @@ | ||
| 91 | 134 | */ |
| 92 | 135 | public static function withContext(callable $callback, ?int $timeout = null) |
| 93 | 136 | { |
| 94 | 137 | $runtimeContextManager = self::getRuntimeContextManager(); |
| 95 | - $startedNewContext = !$runtimeContextManager->hasActiveContext(); | |
| 96 | - if ($startedNewContext) { | |
| 97 | - $runtimeContextManager->startContext(); | |
| 98 | - } | |
| 138 | + $startedNewContext = $runtimeContextManager->startContext(); | |
| 99 | 139 | try { |
| 100 | 140 | return $callback(); |
| 101 | 141 | } finally { |
| 102 | 142 | if ($startedNewContext) { |
| @@ -137,9 +177,9 @@ | ||
| 137 | 177 | if (self::$currentHub === null) { |
| 138 | 178 | self::$currentHub = new Hub(); |
| 139 | 179 | } |
| 140 | 180 | if (self::$runtimeContextManager === null) { |
| 141 | - self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub); | |
| 181 | + self::$runtimeContextManager = new RuntimeContextManager(self::$currentHub, self::$runtimeContextStorage); | |
| 142 | 182 | } |
| 143 | 183 | return self::$runtimeContextManager; |
| 144 | 184 | } |
| 145 | 185 | } |