-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
SendCachedEnvelopeIntegration.java
168 lines (151 loc) · 6.7 KB
/
SendCachedEnvelopeIntegration.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
package io.sentry.android.core;
import io.sentry.DataCategory;
import io.sentry.IConnectionStatusProvider;
import io.sentry.IHub;
import io.sentry.Integration;
import io.sentry.SendCachedEnvelopeFireAndForgetIntegration;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.transport.RateLimiter;
import io.sentry.util.LazyEvaluator;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
final class SendCachedEnvelopeIntegration
implements Integration, IConnectionStatusProvider.IConnectionStatusObserver, Closeable {
private final @NotNull SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForgetFactory
factory;
private final @NotNull LazyEvaluator<Boolean> startupCrashMarkerEvaluator;
private final AtomicBoolean startupCrashHandled = new AtomicBoolean(false);
private @Nullable IConnectionStatusProvider connectionStatusProvider;
private @Nullable IHub hub;
private @Nullable SentryAndroidOptions options;
private @Nullable SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForget sender;
private final AtomicBoolean isInitialized = new AtomicBoolean(false);
private final AtomicBoolean isClosed = new AtomicBoolean(false);
public SendCachedEnvelopeIntegration(
final @NotNull SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForgetFactory factory,
final @NotNull LazyEvaluator<Boolean> startupCrashMarkerEvaluator) {
this.factory = Objects.requireNonNull(factory, "SendFireAndForgetFactory is required");
this.startupCrashMarkerEvaluator = startupCrashMarkerEvaluator;
}
@Override
public void register(@NotNull IHub hub, @NotNull SentryOptions options) {
this.hub = Objects.requireNonNull(hub, "Hub is required");
this.options =
Objects.requireNonNull(
(options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null,
"SentryAndroidOptions is required");
final String cachedDir = options.getCacheDirPath();
if (!factory.hasValidPath(cachedDir, options.getLogger())) {
options.getLogger().log(SentryLevel.ERROR, "No cache dir path is defined in options.");
return;
}
sendCachedEnvelopes(hub, this.options);
}
@Override
public void close() throws IOException {
isClosed.set(true);
if (connectionStatusProvider != null) {
connectionStatusProvider.removeConnectionStatusObserver(this);
}
}
@Override
public void onConnectionStatusChanged(
final @NotNull IConnectionStatusProvider.ConnectionStatus status) {
if (hub != null && options != null) {
sendCachedEnvelopes(hub, options);
}
}
@SuppressWarnings({"NullAway"})
private synchronized void sendCachedEnvelopes(
final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) {
try {
final Future<?> future =
options
.getExecutorService()
.submit(
() -> {
try {
if (isClosed.get()) {
options
.getLogger()
.log(
SentryLevel.INFO,
"SendCachedEnvelopeIntegration, not trying to send after closing.");
return;
}
if (!isInitialized.getAndSet(true)) {
connectionStatusProvider = options.getConnectionStatusProvider();
connectionStatusProvider.addConnectionStatusObserver(this);
sender = factory.create(hub, options);
}
if (connectionStatusProvider != null
&& connectionStatusProvider.getConnectionStatus()
== IConnectionStatusProvider.ConnectionStatus.DISCONNECTED) {
options
.getLogger()
.log(SentryLevel.INFO, "SendCachedEnvelopeIntegration, no connection.");
return;
}
// in case there's rate limiting active, skip processing
final @Nullable RateLimiter rateLimiter = hub.getRateLimiter();
if (rateLimiter != null
&& rateLimiter.isActiveForCategory(DataCategory.All)) {
options
.getLogger()
.log(
SentryLevel.INFO,
"SendCachedEnvelopeIntegration, rate limiting active.");
return;
}
if (sender == null) {
options
.getLogger()
.log(
SentryLevel.ERROR,
"SendCachedEnvelopeIntegration factory is null.");
return;
}
sender.send();
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed trying to send cached events.", e);
}
});
// startupCrashMarkerEvaluator remains true on subsequent runs, let's ensure we only block on
// the very first execution (=app start)
if (startupCrashMarkerEvaluator.getValue()
&& startupCrashHandled.compareAndSet(false, true)) {
options.getLogger().log(SentryLevel.DEBUG, "Startup Crash marker exists, blocking flush.");
try {
future.get(options.getStartupCrashFlushTimeoutMillis(), TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
options
.getLogger()
.log(SentryLevel.DEBUG, "Synchronous send timed out, continuing in the background.");
}
}
options.getLogger().log(SentryLevel.DEBUG, "SendCachedEnvelopeIntegration installed.");
} catch (RejectedExecutionException e) {
options
.getLogger()
.log(
SentryLevel.ERROR,
"Failed to call the executor. Cached events will not be sent. Did you call Sentry.close()?",
e);
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to call the executor. Cached events will not be sent", e);
}
}
}