Passed
Pull Request — master (#19)
by
unknown
04:36 queued 02:12
created

SentryConsoleTransactionAdapter::begin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 25
ccs 0
cts 19
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
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));
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

38
        $context->setStartTimestamp(/** @scrutinizer ignore-type */ microtime(true));
Loading history...
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