forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentryEvent.java
More file actions
393 lines (349 loc) · 12.2 KB
/
SentryEvent.java
File metadata and controls
393 lines (349 loc) · 12.2 KB
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
package io.sentry;
import io.sentry.protocol.*;
import io.sentry.util.CollectionUtils;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
public final class SentryEvent extends SentryBaseEvent implements JsonUnknown, JsonSerializable {
/**
* Timestamp when the event was created.
*
* <p>Indicates when the event was created in the Sentry SDK. The format is either a string as
* defined in [RFC 3339](https://tools.ietf.org/html/rfc3339) or a numeric (integer or float)
* value representing the number of seconds that have elapsed since the [Unix
* epoch](https://en.wikipedia.org/wiki/Unix_time).
*
* <p>Sub-microsecond precision is not preserved with numeric values due to precision limitations
* with floats (at least in our systems). With that caveat in mind, just send whatever is easiest
* to produce.
*
* <p>All timestamps in the event protocol are formatted this way.
*
* <p>```json { "timestamp": "2011-05-02T17:41:36Z" } { "timestamp": 1304358096.0 } ```
*/
private @NotNull Date timestamp;
private @Nullable Message message;
/** Logger that created the event. */
private @Nullable String logger;
/** Threads that were active when the event occurred. */
private @Nullable SentryValues<SentryThread> threads;
/** One or multiple chained (nested) exceptions. */
private @Nullable SentryValues<SentryException> exception;
/**
* Severity level of the event. Defaults to `error`.
*
* <p>Example:
*
* <p>```json {"level": "warning"} ```
*/
private @Nullable SentryLevel level;
/**
* Transaction name of the event.
*
* <p>For example, in a web app, this might be the route name (`"/users/<username>/"` or
* `UserView`), in a task queue it might be the function + module name.
*/
private @Nullable String transaction;
/**
* Manual fingerprint override.
*
* <p>A list of strings used to dictate how this event is supposed to be grouped with other events
* into issues. For more information about overriding grouping see [Customize Grouping with
* Fingerprints](https://docs.sentry.io/data-management/event-grouping/).
*
* <p>```json { "fingerprint": ["myrpc", "POST", "/foo.bar"] }
*/
private @Nullable List<String> fingerprint;
private @Nullable Map<String, Object> unknown;
/**
* Name and versions of all installed modules/packages/dependencies in the current
* environment/application.
*
* <p>```json { "django": "3.0.0", "celery": "4.2.1" } ```
*
* <p>In Python this is a list of installed packages as reported by `pkg_resources` together with
* their reported version string.
*
* <p>This is primarily used for suggesting to enable certain SDK integrations from within the UI
* and for making informed decisions on which frameworks to support in future development efforts.
*/
private @Nullable Map<String, String> modules;
/** Meta data for event processing and debugging. */
private @Nullable DebugMeta debugMeta;
SentryEvent(final @NotNull SentryId eventId, final @NotNull Date timestamp) {
super(eventId);
this.timestamp = timestamp;
}
/**
* SentryEvent ctor with the captured Throwable
*
* @param throwable the Throwable or null
*/
public SentryEvent(final @Nullable Throwable throwable) {
this();
this.throwable = throwable;
}
public SentryEvent() {
this(new SentryId(), DateUtils.getCurrentDateTime());
}
@TestOnly
public SentryEvent(final @NotNull Date timestamp) {
this(new SentryId(), timestamp);
}
@SuppressWarnings({"JdkObsolete", "JavaUtilDate"})
public Date getTimestamp() {
return (Date) timestamp.clone();
}
public @Nullable Message getMessage() {
return message;
}
public void setMessage(final @Nullable Message message) {
this.message = message;
}
public @Nullable String getLogger() {
return logger;
}
public void setLogger(final @Nullable String logger) {
this.logger = logger;
}
public @Nullable List<SentryThread> getThreads() {
if (threads != null) {
return threads.getValues();
} else {
return null;
}
}
public void setThreads(final @Nullable List<SentryThread> threads) {
this.threads = new SentryValues<>(threads);
}
public @Nullable List<SentryException> getExceptions() {
return exception == null ? null : exception.getValues();
}
public void setExceptions(final @Nullable List<SentryException> exception) {
this.exception = new SentryValues<>(exception);
}
public @Nullable SentryLevel getLevel() {
return level;
}
public void setLevel(final @Nullable SentryLevel level) {
this.level = level;
}
public @Nullable String getTransaction() {
return transaction;
}
public void setTransaction(final @Nullable String transaction) {
this.transaction = transaction;
}
public @Nullable List<String> getFingerprints() {
return fingerprint;
}
public void setFingerprints(final @Nullable List<String> fingerprint) {
this.fingerprint = fingerprint != null ? new ArrayList<>(fingerprint) : null;
}
@Nullable
Map<String, String> getModules() {
return modules;
}
public void setModules(final @Nullable Map<String, String> modules) {
this.modules = CollectionUtils.newHashMap(modules);
}
public void setModule(final @NotNull String key, final @NotNull String value) {
if (modules == null) {
modules = new HashMap<>();
}
modules.put(key, value);
}
public void removeModule(final @NotNull String key) {
if (modules != null) {
modules.remove(key);
}
}
public @Nullable String getModule(final @NotNull String key) {
if (modules != null) {
return modules.get(key);
}
return null;
}
public @Nullable DebugMeta getDebugMeta() {
return debugMeta;
}
public void setDebugMeta(final @Nullable DebugMeta debugMeta) {
this.debugMeta = debugMeta;
}
/**
* Returns true if any exception was unhandled by the user.
*
* @return true if its crashed or false otherwise
*/
public boolean isCrashed() {
if (exception != null) {
for (SentryException e : exception.getValues()) {
if (e.getMechanism() != null
&& e.getMechanism().isHandled() != null
&& !e.getMechanism().isHandled()) {
return true;
}
}
}
return false;
}
/**
* Returns true if this event has any sort of exception
*
* @return true if errored or false otherwise
*/
public boolean isErrored() {
return exception != null && !exception.getValues().isEmpty();
}
// JsonSerializable
public static final class JsonKeys {
public static final String TIMESTAMP = "timestamp";
public static final String MESSAGE = "message";
public static final String LOGGER = "logger";
public static final String THREADS = "threads";
public static final String EXCEPTION = "exception";
public static final String LEVEL = "level";
public static final String TRANSACTION = "transaction";
public static final String FINGERPRINT = "fingerprint";
public static final String MODULES = "modules";
public static final String DEBUG_META = "debug_meta";
}
@Override
public void serialize(@NotNull JsonObjectWriter writer, @NotNull ILogger logger)
throws IOException {
writer.beginObject();
writer.name(JsonKeys.TIMESTAMP).value(logger, timestamp);
if (message != null) {
writer.name(JsonKeys.MESSAGE).value(logger, message);
}
if (this.logger != null) {
writer.name(JsonKeys.LOGGER).value(this.logger);
}
if (threads != null && !threads.getValues().isEmpty()) {
writer.name(JsonKeys.THREADS);
writer.beginObject();
writer.name(SentryValues.JsonKeys.VALUES).value(logger, threads.getValues());
writer.endObject();
}
if (exception != null && !exception.getValues().isEmpty()) {
writer.name(JsonKeys.EXCEPTION);
writer.beginObject();
writer.name(SentryValues.JsonKeys.VALUES).value(logger, exception.getValues());
writer.endObject();
}
if (level != null) {
writer.name(JsonKeys.LEVEL).value(logger, level);
}
if (transaction != null) {
writer.name(JsonKeys.TRANSACTION).value(transaction);
}
if (fingerprint != null) {
writer.name(JsonKeys.FINGERPRINT).value(logger, fingerprint);
}
if (modules != null) {
writer.name(JsonKeys.MODULES).value(logger, modules);
}
if (debugMeta != null) {
writer.name(JsonKeys.DEBUG_META).value(logger, debugMeta);
}
new SentryBaseEvent.Serializer().serialize(this, writer, logger);
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
writer.name(key);
writer.value(logger, value);
}
}
writer.endObject();
}
@Nullable
@Override
public Map<String, Object> getUnknown() {
return unknown;
}
@Override
public void setUnknown(@Nullable Map<String, Object> unknown) {
this.unknown = unknown;
}
public static final class Deserializer implements JsonDeserializer<SentryEvent> {
@SuppressWarnings("unchecked")
@Override
public @NotNull SentryEvent deserialize(
@NotNull JsonObjectReader reader, @NotNull ILogger logger) throws Exception {
reader.beginObject();
SentryEvent event = new SentryEvent();
Map<String, Object> unknown = null;
SentryBaseEvent.Deserializer baseEventDeserializer = new SentryBaseEvent.Deserializer();
while (reader.peek() == JsonToken.NAME) {
final String nextName = reader.nextName();
switch (nextName) {
case JsonKeys.TIMESTAMP:
Date deserializedTimestamp = reader.nextDateOrNull(logger);
if (deserializedTimestamp != null) {
event.timestamp = deserializedTimestamp;
}
break;
case JsonKeys.MESSAGE:
event.message = reader.nextOrNull(logger, new Message.Deserializer());
break;
case JsonKeys.LOGGER:
event.logger = reader.nextStringOrNull();
break;
case JsonKeys.THREADS:
reader.beginObject();
reader.nextName(); // SentryValues.JsonKeys.VALUES
event.threads =
new SentryValues<>(reader.nextList(logger, new SentryThread.Deserializer()));
reader.endObject();
break;
case JsonKeys.EXCEPTION:
reader.beginObject();
reader.nextName(); // SentryValues.JsonKeys.VALUES
event.exception =
new SentryValues<>(reader.nextList(logger, new SentryException.Deserializer()));
reader.endObject();
break;
case JsonKeys.LEVEL:
event.level = reader.nextOrNull(logger, new SentryLevel.Deserializer());
break;
case JsonKeys.TRANSACTION:
event.transaction = reader.nextStringOrNull();
break;
case JsonKeys.FINGERPRINT:
List<String> deserializedFingerprint = (List<String>) reader.nextObjectOrNull();
if (deserializedFingerprint != null) {
event.fingerprint = deserializedFingerprint;
}
break;
case JsonKeys.MODULES:
Map<String, String> deserializedModules =
(Map<String, String>) reader.nextObjectOrNull();
event.modules = CollectionUtils.newConcurrentHashMap(deserializedModules);
break;
case JsonKeys.DEBUG_META:
event.debugMeta = reader.nextOrNull(logger, new DebugMeta.Deserializer());
break;
default:
if (!baseEventDeserializer.deserializeValue(event, nextName, reader, logger)) {
if (unknown == null) {
unknown = new ConcurrentHashMap<>();
}
reader.nextUnknown(logger, unknown, nextName);
}
break;
}
}
event.setUnknown(unknown);
reader.endObject();
return event;
}
}
}