-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathClient.php
185 lines (153 loc) · 4.86 KB
/
Client.php
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
<?php
declare(strict_types=1);
namespace Sentry;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\PromiseInterface;
use Sentry\Integration\Handler;
use Sentry\Integration\IntegrationInterface;
use Sentry\State\Scope;
use Sentry\Transport\ClosableTransportInterface;
use Sentry\Transport\TransportInterface;
/**
* Default implementation of the {@see ClientInterface} interface.
*
* @author Stefano Arlandini <[email protected]>
*/
final class Client implements FlushableClientInterface
{
/**
* 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';
/**
* @var Options The client options
*/
private $options;
/**
* @var TransportInterface The transport
*/
private $transport;
/**
* @var EventFactoryInterface The factory to create {@see Event} from raw data
*/
private $eventFactory;
/**
* @var IntegrationInterface[] The stack of integrations
*/
private $integrations;
/**
* Constructor.
*
* @param Options $options The client configuration
* @param TransportInterface $transport The transport
* @param EventFactoryInterface $eventFactory The factory for events
*/
public function __construct(Options $options, TransportInterface $transport, EventFactoryInterface $eventFactory)
{
$this->options = $options;
$this->transport = $transport;
$this->eventFactory = $eventFactory;
$this->integrations = Handler::setupIntegrations($options->getIntegrations());
}
/**
* {@inheritdoc}
*/
public function getOptions(): Options
{
return $this->options;
}
/**
* {@inheritdoc}
*/
public function captureMessage(string $message, ?Severity $level = null, ?Scope $scope = null): ?string
{
$payload = [
'message' => $message,
'level' => $level,
];
$event = $this->prepareEvent($payload, $scope, $this->options->shouldAttachStacktrace());
if (null === $event) {
return null;
}
return $this->transport->send($event);
}
/**
* {@inheritdoc}
*/
public function captureException(\Throwable $exception, ?Scope $scope = null): ?string
{
if ($this->options->isExcludedException($exception)) {
return null;
}
return $this->captureEvent(['exception' => $exception], $scope);
}
/**
* {@inheritdoc}
*/
public function captureEvent(array $payload, ?Scope $scope = null): ?string
{
$event = $this->prepareEvent($payload, $scope);
if (null === $event) {
return null;
}
return $this->transport->send($event);
}
/**
* {@inheritdoc}
*/
public function captureLastError(?Scope $scope = null): ?string
{
$error = error_get_last();
if (null === $error || !isset($error['message'][0])) {
return null;
}
$exception = new \ErrorException(@$error['message'], 0, @$error['type'], @$error['file'], @$error['line']);
return $this->captureException($exception, $scope);
}
/**
* {@inheritdoc}
*/
public function getIntegration(string $className): ?IntegrationInterface
{
return $this->integrations[$className] ?? null;
}
/**
* {@inheritdoc}
*/
public function flush(?int $timeout = null): PromiseInterface
{
if (!$this->transport instanceof ClosableTransportInterface) {
return new FulfilledPromise(true);
}
return $this->transport->close($timeout);
}
/**
* Assembles an event and prepares it to be sent of to Sentry.
*
* @param array $payload the payload that will be converted to an Event
* @param Scope|null $scope optional scope which enriches the Event
* @param bool $withStacktrace True if the event should have and attached stacktrace
*
* @return Event|null returns ready to send Event, however depending on options it can be discarded
*/
private function prepareEvent(array $payload, ?Scope $scope = null, bool $withStacktrace = false): ?Event
{
$sampleRate = $this->getOptions()->getSampleRate();
if ($sampleRate < 1 && mt_rand(1, 100) / 100.0 > $sampleRate) {
return null;
}
if ($withStacktrace) {
$event = $this->eventFactory->createWithStacktrace($payload);
} else {
$event = $this->eventFactory->create($payload);
}
if (null !== $scope) {
$event = $scope->applyToEvent($event, $payload);
}
return \call_user_func($this->options->getBeforeSendCallback(), $event);
}
}