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