-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove implicit root scope * Provide access to active scope and context of scope * Add local storage to scope * Trigger fiber error only when crossing fiber boundaries * Split ContextStorage into two interfaces * Add swoole context storage Implementation currently incompatible with fibers. * Fix cs * Make default context storage execution context aware * Add scope bound callable and promise for async user implementations * Resolve/suppress phan/psalm/phpstan issues * Improve ContextStorage test coverage Adds tests for newly added features and fixes covers annotations. * Apply feedback - move Swoole context storage to Contrib/Context/Swoole - use self instead of classname
- Loading branch information
Showing
24 changed files
with
887 additions
and
146 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Context; | ||
|
||
use ArrayAccess; | ||
|
||
interface ContextStorageScopeInterface extends ScopeInterface, ArrayAccess | ||
{ | ||
public function context(): Context; | ||
|
||
/** | ||
* @param string $offset | ||
*/ | ||
public function offsetSet($offset, $value): void; | ||
} |
This file contains 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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Context; | ||
|
||
interface ExecutionContextAwareInterface | ||
{ | ||
/** | ||
* @param int|string $id | ||
*/ | ||
public function fork($id): void; | ||
|
||
/** | ||
* @param int|string $id | ||
*/ | ||
public function switch($id): void; | ||
|
||
/** | ||
* @param int|string $id | ||
*/ | ||
public function destroy($id): void; | ||
} |
This file contains 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,84 @@ | ||
<?php | ||
|
||
/** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Context; | ||
|
||
use function assert; | ||
use function class_exists; | ||
use const E_USER_WARNING; | ||
use Fiber; | ||
use function trigger_error; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @phan-file-suppress PhanUndeclaredClassReference | ||
* @phan-file-suppress PhanUndeclaredClassMethod | ||
*/ | ||
final class FiberBoundContextStorage implements ContextStorageInterface, ExecutionContextAwareInterface | ||
{ | ||
/** @var ContextStorageInterface&ExecutionContextAwareInterface */ | ||
private ContextStorageInterface $storage; | ||
|
||
/** | ||
* @param ContextStorageInterface&ExecutionContextAwareInterface $storage | ||
*/ | ||
public function __construct(ContextStorageInterface $storage) | ||
{ | ||
$this->storage = $storage; | ||
} | ||
|
||
public function fork($id): void | ||
{ | ||
$this->storage->fork($id); | ||
} | ||
|
||
public function switch($id): void | ||
{ | ||
$this->storage->switch($id); | ||
} | ||
|
||
public function destroy($id): void | ||
{ | ||
$this->storage->destroy($id); | ||
} | ||
|
||
public function scope(): ?ContextStorageScopeInterface | ||
{ | ||
$this->checkFiberMismatch(); | ||
|
||
if (!$scope = $this->storage->scope()) { | ||
return null; | ||
} | ||
|
||
return new FiberBoundContextStorageScope($scope); | ||
} | ||
|
||
public function current(): Context | ||
{ | ||
$this->checkFiberMismatch(); | ||
|
||
return $this->storage->current(); | ||
} | ||
|
||
public function attach(Context $context): ContextStorageScopeInterface | ||
{ | ||
$scope = $this->storage->attach($context); | ||
assert(class_exists(Fiber::class, false)); | ||
$scope[Fiber::class] = Fiber::getCurrent(); | ||
|
||
return new FiberBoundContextStorageScope($scope); | ||
} | ||
|
||
private function checkFiberMismatch(): void | ||
{ | ||
$scope = $this->storage->scope(); | ||
assert(class_exists(Fiber::class, false)); | ||
if ($scope && $scope[Fiber::class] !== Fiber::getCurrent()) { | ||
trigger_error('Fiber context switching not supported', E_USER_WARNING); | ||
} | ||
} | ||
} |
This file contains 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,67 @@ | ||
<?php | ||
|
||
/** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Context; | ||
|
||
use function assert; | ||
use function class_exists; | ||
use Fiber; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @phan-file-suppress PhanUndeclaredClassReference | ||
* @phan-file-suppress PhanUndeclaredClassMethod | ||
*/ | ||
final class FiberBoundContextStorageScope implements ScopeInterface, ContextStorageScopeInterface | ||
{ | ||
private ContextStorageScopeInterface $scope; | ||
|
||
public function __construct(ContextStorageScopeInterface $scope) | ||
{ | ||
$this->scope = $scope; | ||
} | ||
|
||
public function offsetExists($offset): bool | ||
{ | ||
return $this->scope->offsetExists($offset); | ||
} | ||
|
||
/** | ||
* @phan-suppress PhanUndeclaredClassAttribute | ||
*/ | ||
#[\ReturnTypeWillChange] | ||
public function offsetGet($offset) | ||
{ | ||
return $this->scope->offsetGet($offset); | ||
} | ||
|
||
public function offsetSet($offset, $value): void | ||
{ | ||
$this->scope->offsetSet($offset, $value); | ||
} | ||
|
||
public function offsetUnset($offset): void | ||
{ | ||
$this->scope->offsetUnset($offset); | ||
} | ||
|
||
public function context(): Context | ||
{ | ||
return $this->scope->context(); | ||
} | ||
|
||
public function detach(): int | ||
{ | ||
$flags = $this->scope->detach(); | ||
assert(class_exists(Fiber::class, false)); | ||
if ($this->scope[Fiber::class] !== Fiber::getCurrent()) { | ||
$flags |= ScopeInterface::INACTIVE; | ||
} | ||
|
||
return $flags; | ||
} | ||
} |
Oops, something went wrong.