1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Sentry\Tracing; |
6
|
|
|
|
7
|
|
|
use Sentry\Tracing\SpanContext; |
8
|
|
|
use Yiisoft\Log\Message; |
9
|
|
|
use Yiisoft\Log\Target; |
10
|
|
|
use Yiisoft\Yii\Sentry\Integration\Integration; |
11
|
|
|
|
12
|
|
|
final class SentryTraceLogTarget extends Target |
13
|
|
|
{ |
14
|
4 |
|
protected function export(): void |
15
|
|
|
{ |
16
|
4 |
|
array_map( |
17
|
4 |
|
static function (Message $message): Message { |
18
|
|
|
/** @var array<string, mixed> $context */ |
19
|
4 |
|
$context = $message->context(); |
20
|
4 |
|
$category = (string)($context['category'] ?? 'log'); |
21
|
4 |
|
$time = (float)($message->context()['time'] ?? microtime(true)); |
22
|
4 |
|
unset($context['category'], $context['time']); |
23
|
4 |
|
if (array_key_exists('trace', $context) |
24
|
4 |
|
&& empty($context['trace']) |
25
|
|
|
) { |
26
|
4 |
|
unset($context['trace']); |
27
|
|
|
} |
28
|
4 |
|
if (!empty($context['memory']) && is_numeric($context['memory'])) { |
29
|
4 |
|
$context['memory'] = round( |
30
|
4 |
|
((float)$context['memory'] / (1024 * 1024)), |
31
|
|
|
2 |
32
|
|
|
) . 'MB'; |
33
|
|
|
} |
34
|
4 |
|
$parentSpan = Integration::currentTracingSpan(); |
35
|
|
|
|
36
|
|
|
// If there is no tracing span active there is no need to handle the event |
37
|
4 |
|
if ($parentSpan === null) { |
38
|
3 |
|
return $message; |
39
|
|
|
} |
40
|
1 |
|
$spanContext = new SpanContext(); |
41
|
1 |
|
if (isset($context['tags']) && is_array($context['tags'])) { |
42
|
|
|
/** |
43
|
|
|
* @psalm-suppress MixedArgumentTypeCoercion |
44
|
|
|
*/ |
45
|
|
|
$spanContext->setTags($context['tags']); |
46
|
|
|
unset($context['tags']); |
47
|
|
|
} |
48
|
1 |
|
$spanContext->setOp($category); |
49
|
1 |
|
$spanContext->setDescription($message->message()); |
50
|
1 |
|
$spanContext->setStartTimestamp($time); |
51
|
1 |
|
$elapsed = empty($context['elapsed']) ? null : (float)$context['elapsed']; |
52
|
1 |
|
if ($elapsed) { |
53
|
1 |
|
$spanContext->setEndTimestamp($time + $elapsed); |
54
|
|
|
} |
55
|
1 |
|
$spanContext->setData($context); |
56
|
|
|
|
57
|
1 |
|
$parentSpan->startChild($spanContext); |
58
|
|
|
|
59
|
1 |
|
return $message; |
60
|
|
|
}, |
61
|
4 |
|
$this->getMessages() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|