-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpanConverter.php
208 lines (170 loc) · 8.06 KB
/
SpanConverter.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace Instana;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace\EventInterface;
use OpenTelemetry\SDK\Trace\SpanConverterInterface;
use OpenTelemetry\SDK\Trace\SpanDataInterface;
use Instana\SpanKind as InstanaSpanKind;
use Exception;
use OpenTelemetry\SemConv\ResourceAttributes;
use function max;
class SpanConverter implements SpanConverterInterface
{
const OTEL_KEY_STATUS_CODE = 'status_code';
const OTEL_KEY_STATUS_DESCRIPTION = 'error';
const OTEL_KEY_INSTRUMENTATION_SCOPE_NAME = 'scope.name';
const OTEL_KEY_INSTRUMENTATION_SCOPE_VERSION = 'scope.version';
const OTEL_KEY_DROPPED_ATTRIBUTES_COUNT = 'dropped_attributes_count';
const OTEL_KEY_DROPPED_EVENTS_COUNT = 'dropped_events_count';
const OTEL_KEY_DROPPED_LINKS_COUNT = 'dropped_links_count';
private readonly string $defaultServiceName;
public function __construct(
private ?string $agentUuid = null,
private ?string $agentPid = null
) {
$this->defaultServiceName = ResourceInfoFactory::defaultResource()->getAttributes()->get(ResourceAttributes::SERVICE_NAME);
}
public function convert(iterable $spans): array
{
$aggregate = [];
foreach ($spans as $span) {
$aggregate[] = $this->convertSpan($span);
}
return $aggregate;
}
private function convertSpan(SpanDataInterface $span): array
{
$startTimestamp = self::nanosToMillis($span->getStartEpochNanos());
$endTimestamp = self::nanosToMillis($span->getEndEpochNanos());
if (is_null($this->agentUuid) || is_null($this->agentPid)) {
throw new Exception('Failed to get agentUuid or agentPid');
}
$instanaSpan = [
'n' => 'php',
't' => $span->getTraceId(),
's' => $span->getSpanId(),
'ts' => $startTimestamp,
'd' => max(0, $endTimestamp - $startTimestamp),
'f' => array('e' => $this->agentPid, 'h' => $this->agentUuid),
'data' => []
];
if ($span->getParentContext()->isValid()) {
$instanaSpan['p'] = $span->getParentSpanId();
$instanaSpan['n'] = 'sdk';
}
$convertedKind = SpanConverter::toSpanKind($span);
if (!is_null($convertedKind)) {
$instanaSpan['k'] = $convertedKind;
}
$serviceName = $span->getResource()->getAttributes()->get(ResourceAttributes::SERVICE_NAME) ?? $this->defaultServiceName;
$instanaSpan['data']['service'] = $_SERVER['INSTANA_SERVICE_NAME'] ?? $serviceName;
$instanaSpan['data']['sdk']['name'] = $span->getName() ?: 'sdk';
$instanaSpan['data']['sdk']['custom']['tags'] = [];
foreach ($span->getResource()->getAttributes() as $key => $attrb) {
if (str_contains($key, 'service.')) {
continue;
}
$instanaSpan['data']['sdk']['custom']['tags'][$key] = $attrb;
}
foreach ($span->getAttributes() as $key => $attrb) {
self::setOrAppend('attributes', $instanaSpan['data']['sdk']['custom']['tags'], array($key => $attrb));
}
foreach ($span->getEvents() as $event) {
self::setOrAppend('events', $instanaSpan['data']['sdk']['custom']['tags'], array($event->getName() => self::convertEvent($event)));
}
foreach ($span->getInstrumentationScope()->getAttributes() as $key => $value) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array($key => $value));
}
if (!empty($span->getInstrumentationScope()->getName())) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array(self::OTEL_KEY_INSTRUMENTATION_SCOPE_NAME => $span->getInstrumentationScope()->getName()));
}
if (!is_null($span->getInstrumentationScope()->getVersion())) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array(self::OTEL_KEY_INSTRUMENTATION_SCOPE_VERSION => $span->getInstrumentationScope()->getVersion()));
}
if ($span->getStatus()->getCode() !== StatusCode::STATUS_UNSET) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array(self::OTEL_KEY_STATUS_CODE => $span->getStatus()->getCode()));
}
if ($span->getStatus()->getCode() === StatusCode::STATUS_ERROR) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array(self::OTEL_KEY_STATUS_DESCRIPTION => $span->getStatus()->getDescription()));
}
$droppedAttributes = $span->getAttributes()->getDroppedAttributesCount()
+ $span->getInstrumentationScope()->getAttributes()->getDroppedAttributesCount()
+ $span->getResource()->getAttributes()->getDroppedAttributesCount();
if ($droppedAttributes > 0) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array(self::OTEL_KEY_DROPPED_ATTRIBUTES_COUNT => $droppedAttributes));
}
if ($span->getTotalDroppedEvents() > 0) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array(self::OTEL_KEY_DROPPED_EVENTS_COUNT => $span->getTotalDroppedEvents()));
}
if ($span->getTotalDroppedLinks() > 0) {
self::setOrAppend('otel', $instanaSpan['data']['sdk']['custom']['tags'], array(self::OTEL_KEY_DROPPED_LINKS_COUNT => $span->getTotalDroppedLinks()));
}
if (array_key_exists('attributes', $instanaSpan['data']['sdk']['custom']['tags'])) {
$keys = array_filter($instanaSpan['data']['sdk']['custom']['tags']['attributes'], function ($k) {
return str_contains($k, 'http.request.header');
}, ARRAY_FILTER_USE_KEY);
$keys += array_filter($instanaSpan['data']['sdk']['custom']['tags']['attributes'], function ($k) {
return str_contains($k, 'http.response.header');
}, ARRAY_FILTER_USE_KEY);
foreach ($keys as $k => $v) {
unset($instanaSpan['data']['sdk']['custom']['tags']['attributes'][$k]);
}
}
self::unsetEmpty($instanaSpan['data']);
return $instanaSpan;
}
private static function toSpanKind(SpanDataInterface $span): ?int
{
return match ($span->getKind()) {
SpanKind::KIND_SERVER => InstanaSpanKind::ENTRY,
SpanKind::KIND_CLIENT => InstanaSpanKind::EXIT,
SpanKind::KIND_PRODUCER => InstanaSpanKind::EXIT,
SpanKind::KIND_CONSUMER => InstanaSpanKind::ENTRY,
SpanKind::KIND_INTERNAL => InstanaSpanKind::INTERMEDIATE,
default => null,
};
}
private static function nanosToMillis(int $nanoseconds): int
{
return intdiv($nanoseconds, ClockInterface::NANOS_PER_MILLISECOND);
}
private static function setOrAppend(string $key, array &$arr, mixed $value): void
{
if (array_key_exists($key, $arr)) {
if (!is_array($arr[$key])) {
$arr[$key] = array($arr[$key]);
}
$arr[$key] += is_array($value) ? $value : array($value);
} else {
$arr[$key] = $value;
}
}
private static function unsetEmpty(array &$arr): void
{
foreach ($arr as $key => $value) {
if (is_array($value)) {
self::unsetEmpty($arr[$key]);
if (empty($arr[$key])) {
unset($arr[$key]);
}
} elseif (is_null($value)) {
unset($arr[$key]);
}
}
}
private static function convertEvent(EventInterface $event): string
{
if (count($event->getAttributes()) === 0) {
return "{}";
}
$res = json_encode(array(
'value' => $event->getAttributes()->toArray(),
'timestamp' => self::nanosToMillis($event->getEpochNanos())
));
return ($res === false) ? "{}" : $res;
}
}