Passed
Pull Request — master (#19)
by
unknown
04:48 queued 01:58
created

Integration::sentryTracingMeta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 5
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\Integration;
6
7
use Sentry\Breadcrumb;
8
use Sentry\Event;
9
use Sentry\Integration\IntegrationInterface;
10
use Sentry\SentrySdk;
11
use Sentry\State\Scope;
12
use Sentry\Tracing\Span;
13
use Yiisoft\Router\CurrentRoute;
14
15
use function Sentry\addBreadcrumb;
16
use function Sentry\configureScope;
17
18
final class Integration implements IntegrationInterface
19
{
20
    private static ?string $transaction = null;
21
22
    /**
23
     * Adds a breadcrumb if the integration is enabled for Yii3.
24
     */
25 4
    public static function addBreadcrumb(Breadcrumb $breadcrumb): void
26
    {
27 4
        $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
28
29 4
        if (!$self instanceof self) {
30
            return;
31
        }
32
33 4
        addBreadcrumb($breadcrumb);
34
    }
35
36
    /**
37
     * Configures the scope if the integration is enabled for Yii3.
38
     */
39
    public static function configureScope(callable $callback): void
40
    {
41
        $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
42
43
        if (!$self instanceof self) {
44
            return;
45
        }
46
47
        configureScope($callback);
48
    }
49
50
    /**
51
     * Block until all async events are processed for the HTTP transport.
52
     *
53
     * @internal This is not part of the public API and is here temporarily until
54
     *  the underlying issue can be resolved, this method will be removed.
55
     */
56
    public static function flushEvents(): void
57
    {
58
        $client = SentrySdk::getCurrentHub()->getClient();
59
60
        if ($client !== null) {
61
            $client->flush();
62
        }
63
    }
64
65
    /**
66
     * Extract the readable name for a route.
67
     */
68 1
    public static function extractNameForRoute(?CurrentRoute $route): ?string
69
    {
70 1
        if (null === $route) {
71
            return null;
72
        }
73 1
        $routeName = null;
74
75 1
        if ($route->getName()) {
76
            $routeName = $route->getName();
77
        }
78
79 1
        if (empty($routeName) && $route->getUri()) {
80
            $routeName = $route->getUri()->getPath();
81
        }
82
83 1
        return $routeName;
84
    }
85
86
    /**
87
     * Retrieve the meta tags with tracing information to link this request to front-end requests.
88
     */
89
    public static function sentryTracingMeta(): string
90
    {
91
        $span = self::currentTracingSpan();
92
93
        if ($span === null) {
94
            return '';
95
        }
96
97
        return sprintf('<meta name="sentry-trace" content="%s"/>', $span->toTraceparent());
98
    }
99
100
    /**
101
     * Get the current active tracing span from the scope.
102
     *
103
     * @internal This is used internally as an easy way to retrieve the current active tracing span.
104
     */
105 4
    public static function currentTracingSpan(): ?Span
106
    {
107 4
        return SentrySdk::getCurrentHub()->getSpan();
108
    }
109
110 4
    public static function logLevelToBreadcrumbLevel(string $level): string
111
    {
112 4
        switch (strtolower($level)) {
113 4
            case 'debug':
114 4
                return Breadcrumb::LEVEL_DEBUG;
115 4
            case 'warning':
116
                return Breadcrumb::LEVEL_WARNING;
117 4
            case 'error':
118 1
                return Breadcrumb::LEVEL_ERROR;
119 3
            case 'critical':
120 3
            case 'alert':
121 3
            case 'emergency':
122
                return Breadcrumb::LEVEL_FATAL;
123 3
            case 'info':
124
            case 'notice':
125
            default:
126 3
                return Breadcrumb::LEVEL_INFO;
127
        }
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 9
    public function setupOnce(): void
134
    {
135 1
        Scope::addGlobalEventProcessor(function (Event $event): Event {
136 9
            $self = SentrySdk::getCurrentHub()->getIntegration(self::class);
137
138 9
            if (!$self instanceof self) {
139
                return $event;
140
            }
141
142 9
            if (empty($event->getTransaction())) {
143 8
                $event->setTransaction($self->getTransaction());
144
            }
145
146 9
            return $event;
147
        });
148
    }
149
150 8
    public static function getTransaction(): ?string
151
    {
152 8
        return self::$transaction;
153
    }
154
155
    public static function setTransaction(?string $transaction): void
156
    {
157
        self::$transaction = $transaction;
158
    }
159
}
160