Skip to content

Commit 531d5ff

Browse files
committed
Support Baggage propagation to the Instana propagator
1 parent 9d1d3c5 commit 531d5ff

File tree

5 files changed

+190
-28
lines changed

5 files changed

+190
-28
lines changed

src/Propagation/Instana/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
OpenTelemetry Instana Propagator
22

3-
The OpenTelemetry Propagator for Instana provides HTTP header propagation for systems that are using IBM Observability by Instana.
3+
The OpenTelemetry Propagator for Instana provides HTTP header propagation and Baggage propagation for systems that are using IBM Observability by Instana.
44
This propagator translates the Instana trace correlation headers (`X-INSTANA-T/X-INSTANA-S/X-INSTANA-L`) into the OpenTelemetry `SpanContext`, and vice versa.
55
It does not handle `TraceState`.
66

@@ -14,10 +14,10 @@ composer require open-telemetry/opentelemetry-propagation-instana
1414
## Usage
1515

1616
```
17-
$propagator = InstanaMultiPropagator::getInstance();
17+
$propagator = InstanaPropagator::getInstance();
1818
```
1919

20-
Both of the above have extract and inject methods available to extract and inject respectively into the header.
20+
Both of the above have extract and inject methods available to extract and inject respectively into the header.
2121

2222
## Propagator Details
2323

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

33
declare(strict_types=1);
4-
use OpenTelemetry\Contrib\Propagation\Instana\InstanaMultiPropagator;
4+
use OpenTelemetry\Contrib\Propagation\Instana\InstanaPropagator;
55
use OpenTelemetry\SDK\Registry;
66

77
if (!class_exists(Registry::class)) {
88
return;
99
}
10+
1011
Registry::registerTextMapPropagator(
1112
'instana',
12-
InstanaMultiPropagator::getInstance()
13+
InstanaPropagator::getInstance()
1314
);

src/Propagation/Instana/src/InstanaMultiPropagator.php renamed to src/Propagation/Instana/src/InstanaContextPropagator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* "instana" http headers used for trace context propagation across service
2222
* boundaries.
2323
*/
24-
final class InstanaMultiPropagator implements TextMapPropagatorInterface
24+
final class InstanaContextPropagator implements TextMapPropagatorInterface
2525
{
2626
/**
2727
* The X-INSTANA-T header is required and is encoded as 32 lower-hex characters.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)