Skip to content

Commit 2fa0faf

Browse files
authored
Merge a5e99b0 into 85d7417
2 parents 85d7417 + a5e99b0 commit 2fa0faf

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
- Replace `UUIDGenerator` implementation with Apache licensed code ([#4662](https://github.com/getsentry/sentry-java/pull/4662))
1010
- Replace `Random` implementation with MIT licensed code ([#4664](https://github.com/getsentry/sentry-java/pull/4664))
1111

12+
### Fixes
13+
14+
- Flush logs on crash ([#4684](https://github.com/getsentry/sentry-java/pull/4684))
15+
1216
## 8.20.0
1317

1418
### Fixes

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
public final class SentryClient implements ISentryClient {
3737
static final String SENTRY_PROTOCOL_VERSION = "7";
3838

39+
private static final int LOG_FLUSH_ON_CRASH_TIMEOUT_MILLIS = 500;
40+
3941
private boolean enabled;
4042

4143
private final @NotNull SentryOptions options;
@@ -211,16 +213,6 @@ private boolean shouldApplyScopeData(final @NotNull CheckIn event, final @NotNul
211213
sentryId = event.getEventId();
212214
}
213215

214-
final boolean isBackfillable = HintUtils.hasType(hint, Backfillable.class);
215-
final boolean isCached =
216-
HintUtils.hasType(hint, Cached.class) && !HintUtils.hasType(hint, ApplyScopeData.class);
217-
// if event is backfillable or cached we don't wanna trigger capture replay, because it's
218-
// an event from the past. If it's cached, but with ApplyScopeData, it comes from the outbox
219-
// folder and we still want to capture replay (e.g. a native captureException error)
220-
if (event != null && !isBackfillable && !isCached && (event.isErrored() || event.isCrashed())) {
221-
options.getReplayController().captureReplay(event.isCrashed());
222-
}
223-
224216
try {
225217
final @Nullable TraceContext traceContext = getTraceContext(scope, hint, event);
226218
final boolean shouldSendAttachments = event != null;
@@ -245,6 +237,20 @@ private boolean shouldApplyScopeData(final @NotNull CheckIn event, final @NotNul
245237
finalizeTransaction(scope, hint);
246238
}
247239

240+
final boolean isBackfillable = HintUtils.hasType(hint, Backfillable.class);
241+
final boolean isCached =
242+
HintUtils.hasType(hint, Cached.class) && !HintUtils.hasType(hint, ApplyScopeData.class);
243+
// if event is backfillable or cached we don't wanna trigger capture replay, because it's
244+
// an event from the past. If it's cached, but with ApplyScopeData, it comes from the outbox
245+
// folder and we still want to capture replay (e.g. a native captureException error)
246+
if (event != null && !isBackfillable && !isCached && (event.isErrored() || event.isCrashed())) {
247+
options.getReplayController().captureReplay(event.isCrashed());
248+
// We need to flush the logs to ensure they are sent on crash
249+
if (event.isCrashed()) {
250+
loggerBatchProcessor.flush(LOG_FLUSH_ON_CRASH_TIMEOUT_MILLIS);
251+
}
252+
}
253+
248254
return sentryId;
249255
}
250256

sentry/src/test/java/io/sentry/SentryClientTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,25 @@ class SentryClientTest {
30233023
assertTrue(terminated == true)
30243024
}
30253025

3026+
@Test
3027+
fun `flush logs for crash events`() {
3028+
val sut = fixture.getSut()
3029+
val batchProcessor = mock<ILoggerBatchProcessor>()
3030+
sut.injectForField("loggerBatchProcessor", batchProcessor)
3031+
sut.captureLog(
3032+
SentryLogEvent(SentryId(), SentryNanotimeDate(), "message", SentryLogLevel.WARN),
3033+
fixture.scopes.scope,
3034+
)
3035+
3036+
sut.captureEvent(
3037+
SentryEvent().apply {
3038+
exceptions =
3039+
listOf(SentryException().apply { mechanism = Mechanism().apply { isHandled = false } })
3040+
}
3041+
)
3042+
verify(batchProcessor).flush(eq(500))
3043+
}
3044+
30263045
@Test
30273046
fun `cleans up replay folder for Backfillable replay events`() {
30283047
val dir = File(tmpDir.newFolder().absolutePath)

0 commit comments

Comments
 (0)