FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sentry-php/src/Client.php at source-frame-cache · getsentry/sentry-php · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
getsentry
/
sentry-php
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
475
Star
1.9k
Code
Issues
20
Pull requests
11
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
sentry-php
/
src
/
Client.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
550 lines (463 loc) · 17.1 KB
Breadcrumbs
sentry-php
/
src
/
Client.php
Copy path
File metadata and controls
550 lines (463 loc) · 17.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
declare
(strict_types=
1
);
namespace
Sentry
;
use
Psr
\
Log
\
LoggerInterface
;
use
Psr
\
Log
\
NullLogger
;
use
Sentry
\
Integration
\
IntegrationInterface
;
use
Sentry
\
Integration
\
IntegrationRegistry
;
use
Sentry
\
Serializer
\
RepresentationSerializer
;
use
Sentry
\
Serializer
\
RepresentationSerializerInterface
;
use
Sentry
\
State
\
Scope
;
use
Sentry
\
Transport
\
Result
;
use
Sentry
\
Transport
\
TransportInterface
;
/**
* Default implementation of the {@see ClientInterface} interface.
*/
class
Client
implements
ClientInterface
{
/**
* The version of the protocol to communicate with the Sentry server.
*/
public
const
PROTOCOL_VERSION
=
'
7
'
;
/**
* The identifier of the SDK.
*/
public
const
SDK_IDENTIFIER
=
'
sentry.php
'
;
/**
* The version of the SDK.
*/
public
const
SDK_VERSION
=
'
4.29.0
'
;
/**
* Regex pattern to detect if a string is a regex pattern (starts and ends with / optionally followed by flags).
* Supported flags: i (case-insensitive), m (multiline), s (dotall), u (unicode).
*/
private
const
REGEX_PATTERN_DETECTION
=
'
/^\/.*\/[imsu]*$/
'
;
/**
* @var Options The client options
*/
private
$
options
;
/**
* @var TransportInterface The transport
*/
private
$
transport
;
/**
* @var LoggerInterface The PSR-3 logger
*/
private
$
logger
;
/**
* @var array<string, IntegrationInterface> The stack of integrations
*
* @phpstan-var array<class-string<IntegrationInterface>, IntegrationInterface>
*/
private
$
integrations
;
/**
* @var StacktraceBuilder
*/
private
$
stacktraceBuilder
;
/**
* @var string The Sentry SDK identifier
*/
private
$
sdkIdentifier
;
/**
* @var string The SDK version of the Client
*/
private
$
sdkVersion
;
/**
* Constructor.
*
* @param Options $options The client configuration
* @param TransportInterface $transport The transport
* @param string|null $sdkIdentifier The Sentry SDK identifier
* @param string|null $sdkVersion The Sentry SDK version
* @param RepresentationSerializerInterface|null $representationSerializer The serializer for function arguments
* @param LoggerInterface|null $logger The PSR-3 logger
*/
public
function
__construct
(
Options
$
options
,
TransportInterface
$
transport
,
?
string
$
sdkIdentifier
=
null
,
?
string
$
sdkVersion
=
null
,
?
RepresentationSerializerInterface
$
representationSerializer
=
null
,
?
LoggerInterface
$
logger
=
null
) {
$
this
->
options
=
$
options
;
$
this
->
transport
=
$
transport
;
$
this
->
sdkIdentifier
=
$
sdkIdentifier
??
self
::
SDK_IDENTIFIER
;
$
this
->
sdkVersion
=
$
sdkVersion
??
self
::
SDK_VERSION
;
$
this
->
stacktraceBuilder
=
new
StacktraceBuilder
(
$
options
,
$
representationSerializer
??
new
RepresentationSerializer
(
$
this
->
options
));
$
this
->
logger
=
$
logger
??
new
NullLogger
();
$
this
->
integrations
= IntegrationRegistry::
getInstance
()->
setupIntegrations
(
$
options
,
$
this
->
logger
);
}
/**
* {@inheritdoc}
*/
public
function
getOptions
():
Options
{
return
$
this
->
options
;
}
/**
* {@inheritdoc}
*/
public
function
getCspReportUrl
(): ?
string
{
$
dsn
=
$
this
->
options
->
getDsn
();
if
(
$
dsn
===
null
) {
return
null
;
}
$
endpoint
=
$
dsn
->
getCspReportEndpointUrl
();
$
query
=
array_filter
([
'
sentry_release
'
=>
$
this
->
options
->
getRelease
(),
'
sentry_environment
'
=>
$
this
->
options
->
getEnvironment
(),
]);
if
(!
empty
(
$
query
)) {
$
endpoint
.=
'
&
'
.
http_build_query
(
$
query
,
''
,
'
&
'
);
}
return
$
endpoint
;
}
/**
* {@inheritdoc}
*/
public
function
captureMessage
(
string
$
message
, ?
Severity
$
level
=
null
, ?
Scope
$
scope
=
null
, ?
EventHint
$
hint
=
null
): ?
EventId
{
$
event
= Event::
createEvent
();
$
event
->
setMessage
(
$
message
);
$
event
->
setLevel
(
$
level
);
return
$
this
->
captureEvent
(
$
event
,
$
hint
,
$
scope
);
}
/**
* {@inheritdoc}
*/
public
function
captureException
(
\
Throwable
$
exception
, ?
Scope
$
scope
=
null
, ?
EventHint
$
hint
=
null
): ?
EventId
{
$
className
=
\get_class
(
$
exception
);
if
(
$
this
->
shouldIgnoreException
(
$
className
)) {
$
this
->
logger
->
info
(
'
The exception will be discarded because it matches an entry in "ignore_exceptions".
'
,
[
'
className
'
=>
$
className
]
);
return
null
;
// short circuit to avoid unnecessary processing
}
$
hint
=
$
hint
??
new
EventHint
();
if
(
$
hint
->
exception
===
null
) {
$
hint
->
exception
=
$
exception
;
}
return
$
this
->
captureEvent
(Event::
createEvent
(),
$
hint
,
$
scope
);
}
/**
* {@inheritdoc}
*/
public
function
captureEvent
(
Event
$
event
, ?
EventHint
$
hint
=
null
, ?
Scope
$
scope
=
null
): ?
EventId
{
// Client reports don't need to be augmented in the prepareEvent pipeline.
if
(
$
event
->
getType
() !== EventType::
clientReport
()) {
$
event
=
$
this
->
prepareEvent
(
$
event
,
$
hint
,
$
scope
);
}
if
(
$
event
===
null
) {
return
null
;
}
try
{
/** @var Result $result */
$
result
=
$
this
->
transport
->
send
(
$
event
);
$
event
=
$
result
->
getEvent
();
if
(
$
event
!==
null
) {
return
$
event
->
getId
();
}
}
catch
(
\
Throwable
$
exception
) {
$
this
->
logger
->
error
(
\sprintf
(
'
Failed to send the event to Sentry. Reason: "%s".
'
,
$
exception
->
getMessage
()),
[
'
exception
'
=>
$
exception
,
'
event
'
=>
$
event
]
);
}
return
null
;
}
/**
* {@inheritdoc}
*/
public
function
captureLastError
(?
Scope
$
scope
=
null
, ?
EventHint
$
hint
=
null
): ?
EventId
{
$
error
=
error_get_last
();
if
(
$
error
===
null
|| !
isset
(
$
error
[
'
message
'
][
0
])) {
return
null
;
}
$
exception
=
new
\
ErrorException
(@
$
error
[
'
message
'
],
0
, @
$
error
[
'
type
'
], @
$
error
[
'
file
'
], @
$
error
[
'
line
'
]);
return
$
this
->
captureException
(
$
exception
,
$
scope
,
$
hint
);
}
/**
* {@inheritdoc}
*
* @phpstan-template T of IntegrationInterface
*/
public
function
getIntegration
(
string
$
className
): ?
IntegrationInterface
{
/** @phpstan-var T|null */
return
$
this
->
integrations
[
$
className
] ??
null
;
}
/**
* {@inheritdoc}
*/
public
function
flush
(?
int
$
timeout
=
null
):
Result
{
return
$
this
->
transport
->
close
(
$
timeout
);
}
/**
* {@inheritdoc}
*/
public
function
getStacktraceBuilder
():
StacktraceBuilder
{
return
$
this
->
stacktraceBuilder
;
}
/**
* @internal
*/
public
function
getLogger
():
LoggerInterface
{
return
$
this
->
logger
;
}
/**
* @internal
*/
public
function
getTransport
():
TransportInterface
{
return
$
this
->
transport
;
}
public
function
getSdkIdentifier
():
string
{
return
$
this
->
sdkIdentifier
;
}
public
function
getSdkVersion
():
string
{
return
$
this
->
sdkVersion
;
}
/**
* Assembles an event and prepares it to be sent of to Sentry.
*
* @param Event $event The payload that will be converted to an Event
* @param EventHint|null $hint May contain additional information about the event
* @param Scope|null $scope Optional scope which enriches the Event
*
* @return Event|null The prepared event object or null if it must be discarded
*/
private
function
prepareEvent
(
Event
$
event
, ?
EventHint
$
hint
=
null
, ?
Scope
$
scope
=
null
): ?
Event
{
if
(
$
hint
!==
null
) {
if
(
$
hint
->
exception
!==
null
&&
empty
(
$
event
->
getExceptions
())) {
$
this
->
addThrowableToEvent
(
$
event
,
$
hint
->
exception
,
$
hint
);
}
if
(
$
hint
->
stacktrace
!==
null
&&
$
event
->
getStacktrace
() ===
null
) {
$
event
->
setStacktrace
(
$
hint
->
stacktrace
);
}
}
$
this
->
addMissingStacktraceToEvent
(
$
event
);
$
event
->
setSdkIdentifier
(
$
this
->
sdkIdentifier
);
$
event
->
setSdkVersion
(
$
this
->
sdkVersion
);
$
event
->
setTags
(
array_merge
(
$
this
->
options
->
getTags
(),
$
event
->
getTags
()));
if
(
$
event
->
getServerName
() ===
null
) {
$
event
->
setServerName
(
$
this
->
options
->
getServerName
());
}
if
(
$
event
->
getRelease
() ===
null
) {
$
event
->
setRelease
(
$
this
->
options
->
getRelease
());
}
if
(
$
event
->
getEnvironment
() ===
null
) {
$
event
->
setEnvironment
(
$
this
->
options
->
getEnvironment
() ?? Event::
DEFAULT_ENVIRONMENT
);
}
$
eventDescription
=
\sprintf
(
'
%s%s [%s]
'
,
$
event
->
getLevel
() !==
null
?
$
event
->
getLevel
() .
'
'
:
''
,
(
string
)
$
event
->
getType
(),
(
string
)
$
event
->
getId
()
);
$
isEvent
= EventType::
event
() ===
$
event
->
getType
();
$
sampleRate
=
$
this
->
options
->
getSampleRate
();
// only sample with the `sample_rate` on errors/messages
if
(
$
isEvent
&&
$
sampleRate
<
1
&&
mt_rand
(
1
,
100
) /
100.0
>
$
sampleRate
) {
$
this
->
logger
->
info
(
\sprintf
(
'
The %s will be discarded because it has been sampled.
'
,
$
eventDescription
), [
'
event
'
=>
$
event
]);
return
null
;
}
$
event
=
$
this
->
applyIgnoreOptions
(
$
event
,
$
eventDescription
);
if
(
$
event
===
null
) {
return
null
;
}
if
(
$
scope
!==
null
) {
$
beforeEventProcessors
=
$
event
;
$
event
=
$
scope
->
applyToEvent
(
$
event
,
$
hint
,
$
this
->
options
);
if
(
$
event
===
null
) {
$
this
->
logger
->
info
(
\sprintf
(
'
The %s will be discarded because one of the event processors returned "null".
'
,
$
eventDescription
),
[
'
event
'
=>
$
beforeEventProcessors
]
);
return
null
;
}
}
$
beforeSendCallback
=
$
event
;
$
event
=
$
this
->
applyBeforeSendCallback
(
$
event
,
$
hint
);
if
(
$
event
===
null
) {
$
this
->
logger
->
info
(
\sprintf
(
'
The %s will be discarded because the "%s" callback returned "null".
'
,
$
eventDescription
,
$
this
->
getBeforeSendCallbackName
(
$
beforeSendCallback
)
),
[
'
event
'
=>
$
beforeSendCallback
]
);
}
return
$
event
;
}
/**
* Checks if an exception should be ignored based on configured patterns.
* Supports both class hierarchy matching and regex patterns.
* Patterns starting and ending with '/' are treated as regex patterns.
*/
private
function
shouldIgnoreException
(
string
$
className
):
bool
{
foreach
(
$
this
->
options
->
getIgnoreExceptions
()
as
$
pattern
) {
// Check for regex pattern (starts with / and ends with / optionally followed by flags)
if
(
preg_match
(
self
::
REGEX_PATTERN_DETECTION
,
$
pattern
)) {
try
{
if
(
preg_match
(
$
pattern
,
$
className
)) {
return
true
;
}
}
catch
(
\
Throwable
$
e
) {
// Invalid regex pattern, log and skip
$
this
->
logger
->
warning
(
\sprintf
(
'
Invalid regex pattern in ignore_exceptions: "%s". Error: %s
'
,
$
pattern
,
$
e
->
getMessage
())
);
continue
;
}
}
else
{
// Class hierarchy check
if
(
is_a
(
$
className
,
$
pattern
,
true
)) {
return
true
;
}
}
}
return
false
;
}
/**
* Checks if a transaction should be ignored based on configured patterns.
* Supports both exact string matching and regex patterns.
* Patterns starting and ending with '/' are treated as regex patterns.
*/
private
function
shouldIgnoreTransaction
(
string
$
transactionName
):
bool
{
foreach
(
$
this
->
options
->
getIgnoreTransactions
()
as
$
pattern
) {
// Check for regex pattern (starts with / and ends with / optionally followed by flags)
if
(
preg_match
(
self
::
REGEX_PATTERN_DETECTION
,
$
pattern
)) {
try
{
if
(
preg_match
(
$
pattern
,
$
transactionName
)) {
return
true
;
}
}
catch
(
\
Throwable
$
e
) {
// Invalid regex pattern, log and skip
$
this
->
logger
->
warning
(
\sprintf
(
'
Invalid regex pattern in ignore_transactions: "%s". Error: %s
'
,
$
pattern
,
$
e
->
getMessage
())
);
continue
;
}
}
else
{
// Exact string match
if
(
$
transactionName
===
$
pattern
) {
return
true
;
}
}
}
return
false
;
}
private
function
applyIgnoreOptions
(
Event
$
event
,
string
$
eventDescription
): ?
Event
{
if
(
$
event
->
getType
() === EventType::
event
()) {
$
exceptions
=
$
event
->
getExceptions
();
if
(
empty
(
$
exceptions
)) {
return
$
event
;
}
foreach
(
$
exceptions
as
$
exception
) {
if
(
$
this
->
shouldIgnoreException
(
$
exception
->
getType
())) {
$
this
->
logger
->
info
(
\sprintf
(
'
The %s will be discarded because it matches an entry in "ignore_exceptions".
'
,
$
eventDescription
),
[
'
event
'
=>
$
event
]
);
return
null
;
}
}
}
if
(
$
event
->
getType
() === EventType::
transaction
()) {
$
transactionName
=
$
event
->
getTransaction
();
if
(
$
transactionName
===
null
) {
return
$
event
;
}
if
(
$
this
->
shouldIgnoreTransaction
(
$
transactionName
)) {
$
this
->
logger
->
info
(
\sprintf
(
'
The %s will be discarded because it matches a entry in "ignore_transactions".
'
,
$
eventDescription
),
[
'
event
'
=>
$
event
]
);
return
null
;
}
}
return
$
event
;
}
private
function
applyBeforeSendCallback
(
Event
$
event
, ?
EventHint
$
hint
): ?
Event
{
switch
(
$
event
->
getType
()) {
case
EventType::
event
():
return
(
$
this
->
options
->
getBeforeSendCallback
())(
$
event
,
$
hint
);
case
EventType::
transaction
():
return
(
$
this
->
options
->
getBeforeSendTransactionCallback
())(
$
event
,
$
hint
);
case
EventType::
checkIn
():
return
(
$
this
->
options
->
getBeforeSendCheckInCallback
())(
$
event
,
$
hint
);
default
:
return
$
event
;
}
}
private
function
getBeforeSendCallbackName
(
Event
$
event
):
string
{
switch
(
$
event
->
getType
()) {
case
EventType::
transaction
():
return
'
before_send_transaction
'
;
case
EventType::
checkIn
():
return
'
before_send_check_in
'
;
default
:
return
'
before_send
'
;
}
}
/**
* Optionally adds a missing stacktrace to the Event if the client is configured to do so.
*
* @param Event $event The Event to add the missing stacktrace to
*/
private
function
addMissingStacktraceToEvent
(
Event
$
event
):
void
{
if
(!
$
this
->
options
->
shouldAttachStacktrace
()) {
return
;
}
// We should not add a stacktrace when the event already has one or contains exceptions
if
(
$
event
->
getStacktrace
() !==
null
|| !
empty
(
$
event
->
getExceptions
())) {
return
;
}
$
event
->
setStacktrace
(
$
this
->
stacktraceBuilder
->
buildFromBacktrace
(
debug_backtrace
(
0
),
__FILE__
,
__LINE__
-
3
));
}
/**
* Stores the given exception in the passed event.
*
* @param Event $event The event that will be enriched with the exception
* @param \Throwable $exception The exception that will be processed and added to the event
* @param EventHint $hint Contains additional information about the event
*/
private
function
addThrowableToEvent
(
Event
$
event
,
\
Throwable
$
exception
,
EventHint
$
hint
):
void
{
if
(
$
exception
instanceof
\ErrorException &&
$
event
->
getLevel
() ===
null
) {
$
event
->
setLevel
(Severity::
fromError
(
$
exception
->
getSeverity
()));
}
$
exceptions
= [];
do
{
$
exceptions
[] =
new
ExceptionDataBag
(
$
exception
,
$
this
->
stacktraceBuilder
->
buildFromException
(
$
exception
),
$
hint
->
mechanism
??
new
ExceptionMechanism
(ExceptionMechanism::
TYPE_GENERIC
,
true
, [
'
code
'
=>
$
exception
->
getCode
()])
);
}
while
(
$
exception
=
$
exception
->
getPrevious
());
$
event
->
setExceptions
(
$
exceptions
);
}
}
Back
|
FazBrowse Home
|
New Git URL