|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Contrib\Propagation\Instana; |
| 6 | + |
| 7 | +use OpenTelemetry\API\Baggage\Propagation\BaggagePropagator; |
| 8 | +use OpenTelemetry\Context\ContextInterface; |
| 9 | +use OpenTelemetry\Context\Propagation\PropagationGetterInterface; |
| 10 | +use OpenTelemetry\Context\Propagation\PropagationSetterInterface; |
| 11 | +use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface; |
| 12 | + |
| 13 | +class InstanaPropagator implements TextMapPropagatorInterface |
| 14 | +{ |
| 15 | + /** @var TextMapPropagatorInterface[] */ |
| 16 | + private array $propagators; |
| 17 | + |
| 18 | + private static ?self $instance = null; |
| 19 | + |
| 20 | + public function __construct(array $propagators) |
| 21 | + { |
| 22 | + $this->propagators = $propagators; |
| 23 | + } |
| 24 | + |
| 25 | + public static function getInstance(): self |
| 26 | + { |
| 27 | + if (null === self::$instance) { |
| 28 | + // Create propagator instances |
| 29 | + $instanaPropagator = InstanaContextPropagator::getInstance(); |
| 30 | + $baggagePropagator = new BaggagePropagator(); |
| 31 | + self::$instance = new self([$instanaPropagator, $baggagePropagator]); |
| 32 | + } |
| 33 | + |
| 34 | + return self::$instance; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @suppress PhanUndeclaredClassAttribute |
| 39 | + */ |
| 40 | + #[\Override] |
| 41 | + public function fields(): array |
| 42 | + { |
| 43 | + $fields = []; |
| 44 | + foreach ($this->propagators as $propagator) { |
| 45 | + $fields = array_merge($fields, $propagator->fields()); |
| 46 | + } |
| 47 | + |
| 48 | + return array_values(array_unique($fields)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @suppress PhanUndeclaredClassAttribute |
| 53 | + */ |
| 54 | + #[\Override] |
| 55 | + public function inject(&$carrier, ?PropagationSetterInterface $setter = null, ?ContextInterface $context = null): void |
| 56 | + { |
| 57 | + foreach ($this->propagators as $propagator) { |
| 58 | + $propagator->inject($carrier, $setter, $context); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @suppress PhanUndeclaredClassAttribute |
| 64 | + */ |
| 65 | + #[\Override] |
| 66 | + public function extract($carrier, ?PropagationGetterInterface $getter = null, ?ContextInterface $context = null): ContextInterface |
| 67 | + { |
| 68 | + |
| 69 | + foreach ($this->propagators as $propagator) { |
| 70 | + $context = $propagator->extract($carrier, $getter, $context); |
| 71 | + } |
| 72 | + |
| 73 | + return $context; |
| 74 | + } |
| 75 | +} |
0 commit comments