1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SamuelBednarcik\ElasticAPMAgent; |
4
|
|
|
|
5
|
|
|
use SamuelBednarcik\ElasticAPMAgent\Exception\InvalidTraceContextHeaderException; |
6
|
|
|
|
7
|
|
|
class TraceParent |
8
|
|
|
{ |
9
|
|
|
const HEADER_NAME = 'elastic-apm-traceparent'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $traceId; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $spanId; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $traceFlags; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $traceId |
28
|
|
|
* @param string $spanId |
29
|
|
|
* @param string $traceFlags |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $traceId, string $spanId, string $traceFlags) |
32
|
|
|
{ |
33
|
|
|
$this->traceId = $traceId; |
34
|
|
|
$this->spanId = $spanId; |
35
|
|
|
$this->traceFlags = $traceFlags; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getTraceId(): string |
42
|
|
|
{ |
43
|
|
|
return $this->traceId; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $traceId |
48
|
|
|
*/ |
49
|
|
|
public function setTraceId(string $traceId): void |
50
|
|
|
{ |
51
|
|
|
$this->traceId = $traceId; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getSpanId(): string |
58
|
|
|
{ |
59
|
|
|
return $this->spanId; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $spanId |
64
|
|
|
*/ |
65
|
|
|
public function setSpanId(string $spanId): void |
66
|
|
|
{ |
67
|
|
|
$this->spanId = $spanId; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getTraceFlags(): string |
74
|
|
|
{ |
75
|
|
|
return $this->traceFlags; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $traceFlags |
80
|
|
|
*/ |
81
|
|
|
public function setTraceFlags(string $traceFlags): void |
82
|
|
|
{ |
83
|
|
|
$this->traceFlags = $traceFlags; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $header |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public static function isValidHeader(string $header): bool |
91
|
|
|
{ |
92
|
|
|
return preg_match('/^00-[\da-f]{32}-[\da-f]{16}-[\da-f]{2}$/', $header) === 1; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $header |
97
|
|
|
* @return TraceParent |
98
|
|
|
* @throws InvalidTraceContextHeaderException |
99
|
|
|
*/ |
100
|
|
|
public static function createFromHeader(string $header): TraceParent |
101
|
|
|
{ |
102
|
|
|
if (!self::isValidHeader($header)) { |
103
|
|
|
throw new InvalidTraceContextHeaderException(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$parsed = explode('-', $header); |
107
|
|
|
return new TraceParent($parsed[1], $parsed[2], $parsed[3]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function __toString(): string |
114
|
|
|
{ |
115
|
|
|
return sprintf( |
116
|
|
|
'%s-%s-%s-%s', |
117
|
|
|
'00', |
118
|
|
|
$this->getTraceId(), |
119
|
|
|
$this->getSpanId(), |
120
|
|
|
$this->getTraceFlags() |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|