Passed
Pull Request — master (#19)
by
unknown
02:57
created

SentryConsoleTransactionAdapter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 70
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setData() 0 5 1
A __construct() 0 4 2
A setName() 0 5 1
A commit() 0 13 2
A begin() 0 25 2
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
    public function __construct(LoggerInterface $logger, private SentryTraceConsoleListener $consoleListener)
19
    {
20
        if ($logger instanceof Logger) {
21
            $this->logger = $logger;
22
        }
23
    }
24
25
    public function begin(?string $sentryTraceString = null): self
26
    {
27
        $hub = SentrySdk::getCurrentHub();
28
        if ($sentryTraceString) {
29
            $context = TransactionContext::fromSentryTrace($sentryTraceString);
30
        } else {
31
            $context = new TransactionContext();
32
        }
33
        $context->setOp('console sub task');
34
35
        $context->setStartTimestamp(microtime(true));
0 ignored issues
show
Bug introduced by
It seems like microtime(true) can also be of type string; however, parameter $startTimestamp of Sentry\Tracing\SpanContext::setStartTimestamp() does only seem to accept double|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $context->setStartTimestamp(/** @scrutinizer ignore-type */ microtime(true));
Loading history...
36
37
        $transaction = $hub->startTransaction($context);
38
        $transaction->setName('undefined command');
39
        $hub->setSpan($transaction);
40
        $this->consoleListener->setTransaction($transaction);
41
42
        $appContextStart = new SpanContext();
43
        $appContextStart->setOp('handle');
44
        $appContextStart->setStartTimestamp(microtime(true));
45
        $appSpan = $transaction->startChild($appContextStart);
46
        SentrySdk::getCurrentHub()->setSpan($appSpan);
47
        $this->consoleListener->setAppSpan($appSpan);
48
49
        return $this;
50
    }
51
52
    public function setName(string $name): self
53
    {
54
        SentrySdk::getCurrentHub()->getTransaction()?->setName($name);
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param array<string, mixed> $data
61
     *
62
     * @return $this
63
     */
64
    public function setData(array $data): self
65
    {
66
        SentrySdk::getCurrentHub()->getTransaction()?->setData($data);
67
68
        return $this;
69
    }
70
71
    public function commit(): ?string
72
    {
73
        $this->logger?->info('sentry force commit');
74
        $sentryTraceString = SentrySdk::getCurrentHub()->getSpan()?->toTraceparent();
75
        if (SentrySdk::getCurrentHub()->getTransaction() !== null) {
76
            $this->logger?->flush(true);
77
        }
78
79
        $this->consoleListener->listenCommandTerminate(null);
80
        $this->consoleListener->listenShutdown(new ApplicationShutdown(0));
81
        $this->consoleListener->setTransaction(null);
82
83
        return $sentryTraceString;
84
    }
85
}
86