Passed
Pull Request — master (#19)
by
unknown
12:21
created

SentryConsoleTransactionAdapter::getTraceString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Yii\Console\Event\ApplicationShutdown;
12
13
final class SentryConsoleTransactionAdapter
14
{
15
    public function __construct(private LoggerInterface $logger, private SentryTraceConsoleListener $consoleListener)
16
    {
17
    }
18
19
    public function begin(?string $sentryTraceString = null, ?string $baggage = null): self
20
    {
21
        $hub = SentrySdk::getCurrentHub();
22
        if ($sentryTraceString) {
23
            $context = TransactionContext::fromHeaders($sentryTraceString, $baggage ?? '');
24
        } else {
25
            $context = new TransactionContext();
26
        }
27
        $context->setOp('console subtask');
28
29
        $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

29
        $context->setStartTimestamp(/** @scrutinizer ignore-type */ microtime(true));
Loading history...
30
31
        $transaction = $hub->startTransaction($context);
32
        $transaction->setName('undefined command');
33
        $hub->setSpan($transaction);
34
        $this->consoleListener->setTransaction($transaction);
35
36
        $appContextStart = new SpanContext();
37
        $appContextStart->setOp('handle');
38
        $appContextStart->setStartTimestamp(microtime(true));
39
        $appSpan = $transaction->startChild($appContextStart);
40
        SentrySdk::getCurrentHub()->setSpan($appSpan);
41
42
        return $this;
43
    }
44
45
    public function setName(string $name): self
46
    {
47
        SentrySdk::getCurrentHub()->getTransaction()?->setName($name);
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param array<string, mixed> $data
54
     *
55
     * @return $this
56
     */
57
    public function setData(array $data): self
58
    {
59
        SentrySdk::getCurrentHub()->getTransaction()?->setData($data);
60
61
        return $this;
62
    }
63
64
    public function commit(): ?string
65
    {
66
        $this->logger->info('sentry force commit');
67
        $sentryTraceString = SentrySdk::getCurrentHub()->getSpan()?->toTraceparent();
68
        if (method_exists($this->logger, 'flush') && SentrySdk::getCurrentHub()->getTransaction() !== null) {
69
            $this->logger->flush(true);
0 ignored issues
show
Bug introduced by
The method flush() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as Yiisoft\Log\Logger. ( Ignorable by Annotation )

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

69
            $this->logger->/** @scrutinizer ignore-call */ 
70
                           flush(true);
Loading history...
70
        }
71
72
        $this->consoleListener->listenCommandTerminate(null);
73
        $this->consoleListener->listenShutdown(new ApplicationShutdown(0));
74
        $this->consoleListener->setTransaction(null);
75
76
        return $sentryTraceString;
77
    }
78
79
    public function getBaggage(): string
80
    {
81
        return SentrySdk::getCurrentHub()->getSpan()?->toBaggage() ?? '';
82
    }
83
84
    public function getTraceString(): string
85
    {
86
        return SentrySdk::getCurrentHub()->getSpan()?->toTraceparent() ?? '';
87
    }
88
}
89