-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
MainEventProcessor.java
290 lines (251 loc) · 9.01 KB
/
MainEventProcessor.java
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
package io.sentry;
import io.sentry.hints.Cached;
import io.sentry.protocol.DebugImage;
import io.sentry.protocol.DebugMeta;
import io.sentry.protocol.SentryException;
import io.sentry.protocol.SentryTransaction;
import io.sentry.protocol.User;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.VisibleForTesting;
@ApiStatus.Internal
public final class MainEventProcessor implements EventProcessor, Closeable {
/**
* Default value for {@link SentryEvent#getEnvironment()} set when both event and {@link
* SentryOptions} do not have the environment field set.
*/
private static final String DEFAULT_ENVIRONMENT = "production";
private final @NotNull SentryOptions options;
private final @NotNull SentryThreadFactory sentryThreadFactory;
private final @NotNull SentryExceptionFactory sentryExceptionFactory;
private volatile @Nullable HostnameCache hostnameCache = null;
public MainEventProcessor(final @NotNull SentryOptions options) {
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");
final SentryStackTraceFactory sentryStackTraceFactory =
new SentryStackTraceFactory(
this.options.getInAppExcludes(), this.options.getInAppIncludes());
sentryExceptionFactory = new SentryExceptionFactory(sentryStackTraceFactory);
sentryThreadFactory = new SentryThreadFactory(sentryStackTraceFactory, this.options);
}
MainEventProcessor(
final @NotNull SentryOptions options,
final @NotNull SentryThreadFactory sentryThreadFactory,
final @NotNull SentryExceptionFactory sentryExceptionFactory) {
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");
this.sentryThreadFactory =
Objects.requireNonNull(sentryThreadFactory, "The SentryThreadFactory is required.");
this.sentryExceptionFactory =
Objects.requireNonNull(sentryExceptionFactory, "The SentryExceptionFactory is required.");
}
@Override
public @NotNull SentryEvent process(final @NotNull SentryEvent event, final @NotNull Hint hint) {
setCommons(event);
setExceptions(event);
setDebugMeta(event);
setModules(event);
if (shouldApplyScopeData(event, hint)) {
processNonCachedEvent(event);
setThreads(event, hint);
}
return event;
}
private void setDebugMeta(final @NotNull SentryEvent event) {
if (options.getProguardUuid() != null) {
DebugMeta debugMeta = event.getDebugMeta();
if (debugMeta == null) {
debugMeta = new DebugMeta();
}
if (debugMeta.getImages() == null) {
debugMeta.setImages(new ArrayList<>());
}
List<DebugImage> images = debugMeta.getImages();
if (images != null) {
final DebugImage debugImage = new DebugImage();
debugImage.setType(DebugImage.PROGUARD);
debugImage.setUuid(options.getProguardUuid());
images.add(debugImage);
event.setDebugMeta(debugMeta);
}
}
}
private void setModules(final @NotNull SentryEvent event) {
final Map<String, String> modules = event.getModules();
if (modules == null) {
event.setModules(options.getModulesLoader().getOrLoadModules());
}
}
private boolean shouldApplyScopeData(
final @NotNull SentryBaseEvent event, final @NotNull Hint hint) {
if (HintUtils.shouldApplyScopeData(hint)) {
return true;
} else {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Event was cached so not applying data relevant to the current app execution/version: %s",
event.getEventId());
return false;
}
}
private void processNonCachedEvent(final @NotNull SentryBaseEvent event) {
setRelease(event);
setEnvironment(event);
setServerName(event);
setDist(event);
setSdk(event);
setTags(event);
mergeUser(event);
}
@Override
public @NotNull SentryTransaction process(
final @NotNull SentryTransaction transaction, final @NotNull Hint hint) {
setCommons(transaction);
if (shouldApplyScopeData(transaction, hint)) {
processNonCachedEvent(transaction);
}
return transaction;
}
private void setCommons(final @NotNull SentryBaseEvent event) {
setPlatform(event);
}
private void setPlatform(final @NotNull SentryBaseEvent event) {
if (event.getPlatform() == null) {
// this actually means JVM related.
event.setPlatform(SentryBaseEvent.DEFAULT_PLATFORM);
}
}
private void setRelease(final @NotNull SentryBaseEvent event) {
if (event.getRelease() == null) {
event.setRelease(options.getRelease());
}
}
private void setEnvironment(final @NotNull SentryBaseEvent event) {
if (event.getEnvironment() == null) {
event.setEnvironment(
options.getEnvironment() != null ? options.getEnvironment() : DEFAULT_ENVIRONMENT);
}
}
private void setServerName(final @NotNull SentryBaseEvent event) {
if (event.getServerName() == null) {
event.setServerName(options.getServerName());
}
if (options.isAttachServerName() && event.getServerName() == null) {
ensureHostnameCache();
if (hostnameCache != null) {
event.setServerName(hostnameCache.getHostname());
}
}
}
private void ensureHostnameCache() {
if (hostnameCache == null) {
synchronized (this) {
if (hostnameCache == null) {
hostnameCache = HostnameCache.getInstance();
}
}
}
}
private void setDist(final @NotNull SentryBaseEvent event) {
if (event.getDist() == null) {
event.setDist(options.getDist());
}
}
private void setSdk(final @NotNull SentryBaseEvent event) {
if (event.getSdk() == null) {
event.setSdk(options.getSdkVersion());
}
}
private void setTags(final @NotNull SentryBaseEvent event) {
if (event.getTags() == null) {
event.setTags(new HashMap<>(options.getTags()));
} else {
for (Map.Entry<String, String> item : options.getTags().entrySet()) {
if (!event.getTags().containsKey(item.getKey())) {
event.setTag(item.getKey(), item.getValue());
}
}
}
}
private void mergeUser(final @NotNull SentryBaseEvent event) {
if (options.isSendDefaultPii()) {
if (event.getUser() == null) {
final User user = new User();
user.setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
event.setUser(user);
} else if (event.getUser().getIpAddress() == null) {
event.getUser().setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
}
}
}
private void setExceptions(final @NotNull SentryEvent event) {
final Throwable throwable = event.getThrowableMechanism();
if (throwable != null) {
event.setExceptions(sentryExceptionFactory.getSentryExceptions(throwable));
}
}
private void setThreads(final @NotNull SentryEvent event, final @NotNull Hint hint) {
if (event.getThreads() == null) {
// collecting threadIds that came from the exception mechanism, so we can mark threads as
// crashed properly
List<Long> mechanismThreadIds = null;
final List<SentryException> eventExceptions = event.getExceptions();
if (eventExceptions != null && !eventExceptions.isEmpty()) {
for (final SentryException item : eventExceptions) {
if (item.getMechanism() != null && item.getThreadId() != null) {
if (mechanismThreadIds == null) {
mechanismThreadIds = new ArrayList<>();
}
mechanismThreadIds.add(item.getThreadId());
}
}
}
if (options.isAttachThreads()) {
event.setThreads(sentryThreadFactory.getCurrentThreads(mechanismThreadIds));
} else if (options.isAttachStacktrace()
&& (eventExceptions == null || eventExceptions.isEmpty())
&& !isCachedHint(hint)) {
// when attachStacktrace is enabled, we attach only the current thread and its stack traces,
// if there are no exceptions, exceptions have its own stack traces.
event.setThreads(sentryThreadFactory.getCurrentThread());
}
}
}
/**
* If the event has a Cached Hint, it means that it came from the EnvelopeFileObserver. We don't
* want to append the current thread to the event.
*
* @param hint the Hints
* @return true if Cached or false otherwise
*/
private boolean isCachedHint(final @NotNull Hint hint) {
return HintUtils.hasType(hint, Cached.class);
}
@Override
public void close() throws IOException {
if (hostnameCache != null) {
hostnameCache.close();
}
}
boolean isClosed() {
if (hostnameCache != null) {
return hostnameCache.isClosed();
} else {
return true;
}
}
@VisibleForTesting
@Nullable
HostnameCache getHostnameCache() {
return hostnameCache;
}
}