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

SentryWebTransactionAdapter::commit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 6
rs 10
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
13
class SentryWebTransactionAdapter
14
{
15
    protected ?Logger $logger = null;
16
17
    private SentryTraceMiddleware $middleware;
18
19
    public function __construct(LoggerInterface $logger, SentryTraceMiddleware $middleware)
20
    {
21
        if ($logger instanceof Logger) {
22
            $this->logger = $logger;
23
        }
24
        $this->middleware = $middleware;
25
    }
26
27
    public function begin(?string $sentryTraceString = null): self
28
    {
29
        $hub = SentrySdk::getCurrentHub();
30
        if ($sentryTraceString) {
31
            $context = TransactionContext::fromSentryTrace($sentryTraceString);
32
        } else {
33
            $context = new TransactionContext();
34
        }
35
        $context->setOp('web sub task');
36
37
        $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

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