Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. BaggagePropagator.php

class BaggagePropagator

Hierarchy

  • class \OpenTelemetry\API\Baggage\Propagation\BaggagePropagator implements \OpenTelemetry\Context\Propagation\TextMapPropagatorInterface

Expanded class hierarchy of BaggagePropagator

See also

https://www.w3.org/TR/baggage

File

vendor/open-telemetry/api/Baggage/Propagation/BaggagePropagator.php, line 22

Namespace

OpenTelemetry\API\Baggage\Propagation
View source
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((string) $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);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BaggagePropagator::$instance private static property
BaggagePropagator::BAGGAGE public constant
BaggagePropagator::extract public function Extracts specific values from the provided carrier into the provided {via an { Overrides TextMapPropagatorInterface::extract
BaggagePropagator::extractValue private function
BaggagePropagator::fields public function Returns list of fields that will be used by this propagator. Overrides TextMapPropagatorInterface::fields
BaggagePropagator::getInstance public static function
BaggagePropagator::inject public function Injects specific values from the provided {via an { Overrides TextMapPropagatorInterface::inject

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal