1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Sentry\Tracing; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Sentry\SentrySdk; |
9
|
|
|
use Sentry\Tracing\SpanContext; |
10
|
|
|
use Sentry\Tracing\TransactionContext; |
11
|
|
|
use Yiisoft\Log\Logger; |
12
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationShutdown; |
13
|
|
|
|
14
|
|
|
final class SentryConsoleTransactionAdapter |
15
|
|
|
{ |
16
|
|
|
protected ?Logger $logger = null; |
17
|
|
|
|
18
|
|
|
private SentryTraceConsoleListener $consoleListener; |
19
|
|
|
|
20
|
|
|
public function __construct(LoggerInterface $logger, SentryTraceConsoleListener $consoleListener) |
21
|
|
|
{ |
22
|
|
|
if ($logger instanceof Logger) { |
23
|
|
|
$this->logger = $logger; |
24
|
|
|
} |
25
|
|
|
$this->consoleListener = $consoleListener; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function begin(?string $sentryTraceString = null): self |
29
|
|
|
{ |
30
|
|
|
$hub = SentrySdk::getCurrentHub(); |
31
|
|
|
if ($sentryTraceString) { |
32
|
|
|
$context = TransactionContext::fromSentryTrace($sentryTraceString); |
33
|
|
|
} else { |
34
|
|
|
$context = new TransactionContext(); |
35
|
|
|
} |
36
|
|
|
$context->setOp('console sub task'); |
37
|
|
|
|
38
|
|
|
$context->setStartTimestamp(microtime(true)); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$transaction = $hub->startTransaction($context); |
41
|
|
|
$transaction->setName('undefined command'); |
42
|
|
|
$hub->setSpan($transaction); |
43
|
|
|
$this->consoleListener->setTransaction($transaction); |
44
|
|
|
|
45
|
|
|
$appContextStart = new SpanContext(); |
46
|
|
|
$appContextStart->setOp('handle'); |
47
|
|
|
$appContextStart->setStartTimestamp(microtime(true)); |
48
|
|
|
$appSpan = $transaction->startChild($appContextStart); |
49
|
|
|
SentrySdk::getCurrentHub()->setSpan($appSpan); |
50
|
|
|
$this->consoleListener->setAppSpan($appSpan); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setName(string $name): self |
56
|
|
|
{ |
57
|
|
|
SentrySdk::getCurrentHub()->getTransaction()?->setName($name); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array<string, mixed> $data |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setData(array $data): self |
68
|
|
|
{ |
69
|
|
|
SentrySdk::getCurrentHub()->getTransaction()?->setData($data); |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function commit(): ?string |
75
|
|
|
{ |
76
|
|
|
$this->logger?->info('sentry force commit'); |
77
|
|
|
$sentryTraceString = SentrySdk::getCurrentHub()->getSpan()?->toTraceparent(); |
78
|
|
|
if (SentrySdk::getCurrentHub()->getTransaction() !== null) { |
79
|
|
|
$this->logger?->flush(true); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->consoleListener->listenCommandTerminate(null); |
83
|
|
|
$this->consoleListener->listenShutdown(new ApplicationShutdown(0)); |
84
|
|
|
$this->consoleListener->setTransaction(null); |
85
|
|
|
|
86
|
|
|
return $sentryTraceString; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|