diff options
author | Andrew Dolgov <[email protected]> | 2023-10-20 17:12:29 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2023-10-20 21:13:39 +0300 |
commit | cdd7ad020e165fe680703b6d3319b908b682fb7a (patch) | |
tree | b51eb09b7b4587e8fbc5624ac8d88d28cfcd0b04 /vendor/open-telemetry/api/Baggage | |
parent | 45a9ff0c88cbd33892ff16ab837e9059937d656e (diff) |
jaeger-client -> opentelemetry
Diffstat (limited to 'vendor/open-telemetry/api/Baggage')
9 files changed, 464 insertions, 0 deletions
diff --git a/vendor/open-telemetry/api/Baggage/Baggage.php b/vendor/open-telemetry/api/Baggage/Baggage.php new file mode 100644 index 000000000..06c701605 --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/Baggage.php @@ -0,0 +1,100 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage; + +use OpenTelemetry\Context\Context; +use OpenTelemetry\Context\ContextInterface; +use OpenTelemetry\Context\ContextKeys; +use OpenTelemetry\Context\ScopeInterface; + +final class Baggage implements BaggageInterface +{ + private static ?self $emptyBaggage = null; + + /** @inheritDoc */ + public static function fromContext(ContextInterface $context): BaggageInterface + { + return $context->get(ContextKeys::baggage()) ?? self::getEmpty(); + } + + /** @inheritDoc */ + public static function getBuilder(): BaggageBuilderInterface + { + return new BaggageBuilder(); + } + + /** @inheritDoc */ + public static function getCurrent(): BaggageInterface + { + return self::fromContext(Context::getCurrent()); + } + + /** @inheritDoc */ + public static function getEmpty(): BaggageInterface + { + if (null === self::$emptyBaggage) { + self::$emptyBaggage = new self(); + } + + return self::$emptyBaggage; + } + + /** @var array<string, Entry> */ + private array $entries; + + /** @param array<string, Entry> $entries */ + public function __construct(array $entries = []) + { + $this->entries = $entries; + } + + /** @inheritDoc */ + public function activate(): ScopeInterface + { + return Context::getCurrent()->withContextValue($this)->activate(); + } + + /** @inheritDoc */ + public function getEntry(string $key): ?Entry + { + return $this->entries[$key] ?? null; + } + + /** @inheritDoc */ + public function getValue(string $key) + { + if (($entry = $this->getEntry($key)) !== null) { + return $entry->getValue(); + } + + return null; + } + + /** @inheritDoc */ + public function getAll(): iterable + { + foreach ($this->entries as $key => $entry) { + yield $key => $entry; + } + } + + /** @inheritDoc */ + public function isEmpty(): bool + { + return $this->entries === []; + } + + /** @inheritDoc */ + public function toBuilder(): BaggageBuilderInterface + { + return new BaggageBuilder($this->entries); + } + + /** @inheritDoc */ + public function storeInContext(ContextInterface $context): ContextInterface + { + return $context->with(ContextKeys::baggage(), $this); + } +} diff --git a/vendor/open-telemetry/api/Baggage/BaggageBuilder.php b/vendor/open-telemetry/api/Baggage/BaggageBuilder.php new file mode 100644 index 000000000..d4500eac5 --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/BaggageBuilder.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage; + +final class BaggageBuilder implements BaggageBuilderInterface +{ + /** @var array<string, Entry> */ + private array $entries; + + /** @param array<string, Entry> $entries */ + public function __construct(array $entries = []) + { + $this->entries = $entries; + } + + /** @inheritDoc */ + public function remove(string $key): BaggageBuilderInterface + { + unset($this->entries[$key]); + + return $this; + } + + /** @inheritDoc */ + public function set(string $key, $value, MetadataInterface $metadata = null): BaggageBuilderInterface + { + $metadata ??= Metadata::getEmpty(); + + $this->entries[$key] = new Entry($value, $metadata); + + return $this; + } + + public function build(): BaggageInterface + { + return new Baggage($this->entries); + } +} diff --git a/vendor/open-telemetry/api/Baggage/BaggageBuilderInterface.php b/vendor/open-telemetry/api/Baggage/BaggageBuilderInterface.php new file mode 100644 index 000000000..301cfbc3c --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/BaggageBuilderInterface.php @@ -0,0 +1,23 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage; + +use OpenTelemetry\API\Baggage as API; + +interface BaggageBuilderInterface +{ + /** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/baggage/api.md#set-value + * @param mixed $value + */ + public function set(string $key, $value, API\MetadataInterface $metadata = null): API\BaggageBuilderInterface; + + /** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/baggage/api.md#remove-value + */ + public function remove(string $key): API\BaggageBuilderInterface; + + public function build(): API\BaggageInterface; +} diff --git a/vendor/open-telemetry/api/Baggage/BaggageInterface.php b/vendor/open-telemetry/api/Baggage/BaggageInterface.php new file mode 100644 index 000000000..83f45755d --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/BaggageInterface.php @@ -0,0 +1,62 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage; + +use OpenTelemetry\API\Baggage as API; +use OpenTelemetry\Context\ContextInterface; +use OpenTelemetry\Context\ImplicitContextKeyedInterface; + +/** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/baggage/api.md#operations + */ +interface BaggageInterface extends ImplicitContextKeyedInterface +{ + /** + * Returns the {@see API\BaggageInterface} from the provided *$context*, + * falling back on {@see API\BaggageInterface::getEmpty()} if there is no baggage in the provided context. + */ + public static function fromContext(ContextInterface $context): API\BaggageInterface; + + /** + * Returns a new empty {@see API\BaggageBuilderInterface}. + */ + public static function getBuilder(): API\BaggageBuilderInterface; + + /** + * Returns the current {@see Baggage} from the current {@see ContextInterface}, + * falling back on {@see API\BaggageInterface::getEmpty()} if there is no baggage in the current context. + */ + public static function getCurrent(): API\BaggageInterface; + + /** + * Returns a new {@see API\BaggageInterface} with no entries. + */ + public static function getEmpty(): API\BaggageInterface; + + /** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/baggage/api.md#get-value + */ + public function getEntry(string $key): ?API\Entry; + + /** + * Returns the value from the {@see API\Entry} with the provided *key*. + * @see getEntry + * + * @return mixed + */ + public function getValue(string $key); + + /** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/baggage/api.md#get-all-values + */ + public function getAll(): iterable; + + public function isEmpty(): bool; + + /** + * Returns a new {@see API\BaggageBuilderInterface} pre-initialized with the contents of `$this`. + */ + public function toBuilder(): API\BaggageBuilderInterface; +} diff --git a/vendor/open-telemetry/api/Baggage/Entry.php b/vendor/open-telemetry/api/Baggage/Entry.php new file mode 100644 index 000000000..eb3d0de5b --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/Entry.php @@ -0,0 +1,38 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage; + +final class Entry +{ + /** @var mixed */ + private $value; + + private MetadataInterface $metadata; + + /** + * @param mixed $value + * @param MetadataInterface $metadata + */ + public function __construct( + $value, + MetadataInterface $metadata + ) { + $this->value = $value; + $this->metadata = $metadata; + } + + /** + * @return mixed + */ + public function getValue() + { + return $this->value; + } + + public function getMetadata(): MetadataInterface + { + return $this->metadata; + } +} diff --git a/vendor/open-telemetry/api/Baggage/Metadata.php b/vendor/open-telemetry/api/Baggage/Metadata.php new file mode 100644 index 000000000..043c96a8a --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/Metadata.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage; + +final class Metadata implements MetadataInterface +{ + private static ?self $instance = null; + + public static function getEmpty(): Metadata + { + return self::$instance ??= new self(''); + } + + private string $metadata; + + public function __construct(string $metadata) + { + $this->metadata = $metadata; + } + + public function getValue(): string + { + return $this->metadata; + } +} diff --git a/vendor/open-telemetry/api/Baggage/MetadataInterface.php b/vendor/open-telemetry/api/Baggage/MetadataInterface.php new file mode 100644 index 000000000..cd0a6d1ec --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/MetadataInterface.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage; + +/** + * @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/baggage/api.md#set-value + */ +interface MetadataInterface +{ + public function getValue(): string; +} diff --git a/vendor/open-telemetry/api/Baggage/Propagation/BaggagePropagator.php b/vendor/open-telemetry/api/Baggage/Propagation/BaggagePropagator.php new file mode 100644 index 000000000..fae62dcab --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/Propagation/BaggagePropagator.php @@ -0,0 +1,92 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage\Propagation; + +use OpenTelemetry\API\Baggage\Baggage; +use OpenTelemetry\API\Baggage\BaggageBuilderInterface; +use OpenTelemetry\API\Baggage\Entry; /** @phan-suppress-current-line PhanUnreferencedUseNormal */ +use OpenTelemetry\Context\Context; +use OpenTelemetry\Context\ContextInterface; +use OpenTelemetry\Context\Propagation\ArrayAccessGetterSetter; +use OpenTelemetry\Context\Propagation\PropagationGetterInterface; +use OpenTelemetry\Context\Propagation\PropagationSetterInterface; +use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; +use function rtrim; +use function urlencode; + +/** + * @see https://www.w3.org/TR/baggage + */ +final class BaggagePropagator implements TextMapPropagatorInterface +{ + public const BAGGAGE = 'baggage'; + + private static ?self $instance = null; + + public static function getInstance(): self + { + if (null === self::$instance) { + self::$instance = new self(); + } + + return self::$instance; + } + + public function fields(): array + { + return [self::BAGGAGE]; + } + + public function inject(&$carrier, PropagationSetterInterface $setter = null, ContextInterface $context = null): void + { + $setter ??= ArrayAccessGetterSetter::getInstance(); + $context ??= Context::getCurrent(); + + $baggage = Baggage::fromContext($context); + + if ($baggage->isEmpty()) { + return; + } + + $headerString = ''; + + /** @var Entry $entry */ + foreach ($baggage->getAll() as $key => $entry) { + $value = urlencode($entry->getValue()); + $headerString.= "{$key}={$value}"; + + if (($metadata = $entry->getMetadata()->getValue()) !== '' && ($metadata = $entry->getMetadata()->getValue()) !== '0') { + $headerString .= ";{$metadata}"; + } + + $headerString .= ','; + } + + if ($headerString !== '' && $headerString !== '0') { + $headerString = rtrim($headerString, ','); + $setter->set($carrier, self::BAGGAGE, $headerString); + } + } + + public function extract($carrier, PropagationGetterInterface $getter = null, ContextInterface $context = null): ContextInterface + { + $getter ??= ArrayAccessGetterSetter::getInstance(); + $context ??= Context::getCurrent(); + + if (!$baggageHeader = $getter->get($carrier, self::BAGGAGE)) { + return $context; + } + + $baggageBuilder = Baggage::getBuilder(); + $this->extractValue($baggageHeader, $baggageBuilder); + + return $context->withContextValue($baggageBuilder->build()); + } + + private function extractValue(string $baggageHeader, BaggageBuilderInterface $baggageBuilder): void + { + (new Parser($baggageHeader))->parseInto($baggageBuilder); + } +} diff --git a/vendor/open-telemetry/api/Baggage/Propagation/Parser.php b/vendor/open-telemetry/api/Baggage/Propagation/Parser.php new file mode 100644 index 000000000..3518b858d --- /dev/null +++ b/vendor/open-telemetry/api/Baggage/Propagation/Parser.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\API\Baggage\Propagation; + +use function explode; +use OpenTelemetry\API\Baggage\BaggageBuilderInterface; +use OpenTelemetry\API\Baggage\Metadata; +use function str_replace; +use function trim; +use function urldecode; + +final class Parser +{ + private const EXCLUDED_KEY_CHARS = [' ', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}']; + private const EXCLUDED_VALUE_CHARS = [' ', '"', ',', ';', '\\']; + private const EQUALS = '='; + + /** @readonly */ + private string $baggageHeader; + + public function __construct(string $baggageHeader) + { + $this->baggageHeader = $baggageHeader; + } + + public function parseInto(BaggageBuilderInterface $baggageBuilder): void + { + foreach (explode(',', $this->baggageHeader) as $baggageString) { + if (empty(trim($baggageString))) { + continue; + } + + $explodedString = explode(';', $baggageString, 2); + + $keyValue = trim($explodedString[0]); + + if (empty($keyValue) || mb_strpos($keyValue, self::EQUALS) === false) { + continue; + } + + $metadataString = $explodedString[1] ?? null; + + if ($metadataString && !empty(trim(($metadataString)))) { + $metadata = new Metadata(trim($metadataString)); + } else { + $metadata = null; + } + + [$key, $value] = explode(self::EQUALS, $keyValue, 2); + + $key = urldecode($key); + $value = urldecode($value); + + $key = str_replace(self::EXCLUDED_KEY_CHARS, '', trim($key), $invalidKeyCharacters); + if (empty($key) || $invalidKeyCharacters > 0) { + continue; + } + + $value = str_replace(self::EXCLUDED_VALUE_CHARS, '', trim($value), $invalidValueCharacters); + if (empty($value) || $invalidValueCharacters > 0) { + continue; + } + + $baggageBuilder->set($key, $value, $metadata); + } + } +} |