-
-
Notifications
You must be signed in to change notification settings - Fork 451
/
Scope.php
339 lines (285 loc) · 8.39 KB
/
Scope.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
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
<?php
declare(strict_types=1);
namespace Sentry\State;
use Sentry\Breadcrumb;
use Sentry\Context\Context;
use Sentry\Context\TagsContext;
use Sentry\Context\UserContext;
use Sentry\Event;
use Sentry\Severity;
/**
* The scope holds data that should implicitly be sent with Sentry events. It
* can hold context data, extra parameters, level overrides, fingerprints etc.
*/
final class Scope
{
/**
* @var Breadcrumb[] The list of breadcrumbs recorded in this scope
*/
private $breadcrumbs = [];
/**
* @var UserContext The user data associated to this scope
*/
private $user;
/**
* @var array<string, array<string, mixed>> The list of contexts associated to this scope
*/
private $contexts = [];
/**
* @var TagsContext The list of tags associated to this scope
*/
private $tags;
/**
* @var Context A set of extra data associated to this scope
*/
private $extra;
/**
* @var string[] List of fingerprints used to group events together in
* Sentry
*/
private $fingerprint = [];
/**
* @var Severity|null The severity to associate to the events captured in
* this scope
*/
private $level;
/**
* @var callable[] List of event processors
*/
private $eventProcessors = [];
/**
* @var callable[] List of event processors
*/
private static $globalEventProcessors = [];
/**
* Constructor.
*/
public function __construct()
{
$this->user = new UserContext();
$this->tags = new TagsContext();
$this->extra = new Context();
$this->contexts = [];
}
/**
* Sets a new tag in the tags context.
*
* @param string $key The key that uniquely identifies the tag
* @param string $value The value
*
* @return $this
*/
public function setTag(string $key, string $value): self
{
$this->tags[$key] = $value;
return $this;
}
/**
* Merges the given tags into the current tags context.
*
* @param array<string, string> $tags The tags to merge into the current context
*
* @return $this
*/
public function setTags(array $tags): self
{
$this->tags->merge($tags);
return $this;
}
/**
* Sets context data with the given name.
*
* @param string $name The name that uniquely identifies the context
* @param array<string, mixed> $value The value
*
* @return $this
*/
public function setContext(string $name, array $value): self
{
$this->contexts[$name] = $value;
return $this;
}
/**
* Removes the context from the scope.
*
* @param string $name The name that uniquely identifies the context
*
* @return $this
*/
public function removeContext(string $name): self
{
unset($this->contexts[$name]);
return $this;
}
/**
* Sets a new information in the extra context.
*
* @param string $key The key that uniquely identifies the information
* @param mixed $value The value
*
* @return $this
*/
public function setExtra(string $key, $value): self
{
$this->extra[$key] = $value;
return $this;
}
/**
* Merges the given data into the current extras context.
*
* @param array<string, mixed> $extras Data to merge into the current context
*
* @return $this
*/
public function setExtras(array $extras): self
{
$this->extra->merge($extras);
return $this;
}
/**
* Sets the given data in the user context.
*
* @param array $data The data
* @param bool $merge If true, $data will be merged into user context instead of replacing it
*
* @return $this
*/
public function setUser(array $data, bool $merge = false): self
{
if ($merge) {
$this->user->merge($data);
return $this;
}
@trigger_error('Replacing the data is deprecated since version 2.3 and will stop working from version 3.0. Set the second argument to `true` to merge the data instead.', E_USER_DEPRECATED);
$this->user->replaceData($data);
return $this;
}
/**
* Sets the list of strings used to dictate the deduplication of this event.
*
* @param string[] $fingerprint The fingerprint values
*
* @return $this
*/
public function setFingerprint(array $fingerprint): self
{
$this->fingerprint = $fingerprint;
return $this;
}
/**
* Sets the severity to apply to all events captured in this scope.
*
* @param Severity|null $level The severity
*
* @return $this
*/
public function setLevel(?Severity $level): self
{
$this->level = $level;
return $this;
}
/**
* Add the given breadcrumb to the scope.
*
* @param Breadcrumb $breadcrumb The breadcrumb to add
* @param int $maxBreadcrumbs The maximum number of breadcrumbs to record
*
* @return $this
*/
public function addBreadcrumb(Breadcrumb $breadcrumb, int $maxBreadcrumbs = 100): self
{
$this->breadcrumbs[] = $breadcrumb;
$this->breadcrumbs = \array_slice($this->breadcrumbs, -$maxBreadcrumbs);
return $this;
}
/**
* Clears all the breadcrumbs.
*
* @return $this
*/
public function clearBreadcrumbs(): self
{
$this->breadcrumbs = [];
return $this;
}
/**
* Adds a new event processor that will be called after {@see Scope::applyToEvent}
* finished its work.
*
* @param callable $eventProcessor The event processor
*
* @return $this
*/
public function addEventProcessor(callable $eventProcessor): self
{
$this->eventProcessors[] = $eventProcessor;
return $this;
}
/**
* Adds a new event processor that will be called after {@see Scope::applyToEvent}
* finished its work.
*
* @param callable $eventProcessor The event processor
*/
public static function addGlobalEventProcessor(callable $eventProcessor): void
{
self::$globalEventProcessors[] = $eventProcessor;
}
/**
* Clears the scope and resets any data it contains.
*
* @return $this
*/
public function clear(): self
{
$this->tags->clear();
$this->extra->clear();
$this->user->clear();
$this->level = null;
$this->fingerprint = [];
$this->breadcrumbs = [];
$this->contexts = [];
return $this;
}
/**
* Applies the current context and fingerprint to the event. If the event has
* already some breadcrumbs on it, the ones from this scope won't get merged.
*
* @param Event $event The event object that will be enriched with scope data
* @param array $payload The raw payload of the event that will be propagated to the event processors
*/
public function applyToEvent(Event $event, array $payload): ?Event
{
if (empty($event->getFingerprint())) {
$event->setFingerprint($this->fingerprint);
}
if (empty($event->getBreadcrumbs())) {
$event->setBreadcrumb($this->breadcrumbs);
}
if (null !== $this->level) {
$event->setLevel($this->level);
}
$event->getTagsContext()->merge($this->tags->toArray());
$event->getExtraContext()->merge($this->extra->toArray());
$event->getUserContext()->merge($this->user->toArray());
foreach (array_merge($this->contexts, $event->getContexts()) as $name => $data) {
$event->setContext($name, $data);
}
foreach (array_merge(self::$globalEventProcessors, $this->eventProcessors) as $processor) {
$event = \call_user_func($processor, $event, $payload);
if (null === $event) {
return null;
}
if (!$event instanceof Event) {
throw new \InvalidArgumentException(sprintf('The event processor must return null or an instance of the %s class', Event::class));
}
}
return $event;
}
public function __clone()
{
$this->user = clone $this->user;
$this->tags = clone $this->tags;
$this->extra = clone $this->extra;
}
}