-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathCloudTracePropagator.php
More file actions
99 lines (79 loc) · 2.9 KB
/
CloudTracePropagator.php
File metadata and controls
99 lines (79 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
namespace OpenTelemetry\Extension\Propagator\CloudTrace;
use OpenTelemetry\API\Trace\Span;
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;
/**
* CloudTracePropagator is a propagator that supports the specification for the X-Cloud-Trace-Context
* header used for trace context propagation across service boundaries.
* (https://cloud.google.com/trace/docs/setup#force-trace)
*/
final class CloudTracePropagator implements TextMapPropagatorInterface
{
private static ?TextMapPropagatorInterface $oneWayInstance = null;
private static ?TextMapPropagatorInterface $instance = null;
public static function getOneWayInstance(): TextMapPropagatorInterface
{
if (self::$oneWayInstance === null) {
self::$oneWayInstance = new CloudTracePropagator(true);
}
return self::$oneWayInstance;
}
public static function getInstance(): TextMapPropagatorInterface
{
if (self::$instance === null) {
self::$instance = new CloudTracePropagator(false);
}
return self::$instance;
}
private const XCLOUD = 'x-cloud-trace-context';
private const FIELDS = [
self::XCLOUD,
];
private function __construct(private readonly bool $oneWay)
{
}
/** {@inheritdoc} */
#[\Override]
public function fields(): array
{
return self::FIELDS;
}
/** {@inheritdoc} */
#[\Override]
public function inject(&$carrier, ?PropagationSetterInterface $setter = null, ?ContextInterface $context = null): void
{
if ($this->oneWay) {
return;
}
$setter ??= ArrayAccessGetterSetter::getInstance();
$context ??= Context::getCurrent();
$spanContext = Span::fromContext($context)->getContext();
if (!$spanContext->isValid()) {
return;
}
$headerValue = CloudTraceFormatter::serialize($spanContext);
$setter->set($carrier, self::XCLOUD, $headerValue);
}
/** {@inheritdoc} */
#[\Override]
public function extract($carrier, ?PropagationGetterInterface $getter = null, ?ContextInterface $context = null): ContextInterface
{
$getter ??= ArrayAccessGetterSetter::getInstance();
$context ??= Context::getCurrent();
$headerValue = $getter->get($carrier, self::XCLOUD);
if ($headerValue === null) {
return $context;
}
$spanContext = CloudTraceFormatter::deserialize($headerValue);
if (!$spanContext->isValid()) {
return $context;
}
return $context->withContextValue(Span::wrap($spanContext));
}
}