Skip to content

Commit c6d9cab

Browse files
authored
Merge branch 'main' into 08-11-do_not_store_no-op_scopes_onto_opentelemetry_context_when_wrapping
2 parents b807936 + d5ff4e5 commit c6d9cab

File tree

14 files changed

+479
-34
lines changed

14 files changed

+479
-34
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
## Unreleased
3+
## 8.19.0
4+
5+
### Features
6+
7+
- Add a `isEnableSystemEventBreadcrumbsExtras` option to disable reporting system events extras for breadcrumbs ([#4625](https://github.com/getsentry/sentry-java/pull/4625))
48

59
### Improvements
610

@@ -28,6 +32,8 @@
2832
- Ensure frame metrics listeners are registered/unregistered on the main thread ([#4582](https://github.com/getsentry/sentry-java/pull/4582))
2933
- Do not report cached events as lost ([#4575](https://github.com/getsentry/sentry-java/pull/4575))
3034
- Previously events were recorded as lost early despite being retried later through the cache
35+
- Move and flush unfinished previous session on init ([#4624](https://github.com/getsentry/sentry-java/pull/4624))
36+
- This removes the need for unnecessary blocking our background queue for 15 seconds in the case of a background app start
3137
- Switch to compileOnly dependency for compose-ui-material ([#4630](https://github.com/getsentry/sentry-java/pull/4630))
3238
- This fixes `StackOverflowError` when using OSS Licenses plugin
3339

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ org.jetbrains.dokka.experimental.gradle.pluginMode=V2Enabled
1111
android.useAndroidX=true
1212

1313
# Release information
14-
versionName=8.18.0
14+
versionName=8.19.0
1515

1616
# Override the SDK name on native crashes on Android
1717
sentryAndroidSdkName=sentry.native.android

sentry-android-core/api/sentry-android-core.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
336336
public fun isEnableRootCheck ()Z
337337
public fun isEnableScopeSync ()Z
338338
public fun isEnableSystemEventBreadcrumbs ()Z
339+
public fun isEnableSystemEventBreadcrumbsExtras ()Z
339340
public fun isReportHistoricalAnrs ()Z
340341
public fun setAnrEnabled (Z)V
341342
public fun setAnrReportInDebug (Z)V
@@ -360,6 +361,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
360361
public fun setEnableRootCheck (Z)V
361362
public fun setEnableScopeSync (Z)V
362363
public fun setEnableSystemEventBreadcrumbs (Z)V
364+
public fun setEnableSystemEventBreadcrumbsExtras (Z)V
363365
public fun setFrameMetricsCollector (Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;)V
364366
public fun setNativeHandlerStrategy (Lio/sentry/android/core/NdkHandlerStrategy;)V
365367
public fun setNativeSdkName (Ljava/lang/String;)V

sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroidOptions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ public final class SentryAndroidOptions extends SentryOptions {
177177
*/
178178
private boolean enableAutoTraceIdGeneration = true;
179179

180+
/** Enable or disable intent extras reporting for system event breadcrumbs. Default is false. */
181+
private boolean enableSystemEventBreadcrumbsExtras = false;
182+
180183
public interface BeforeCaptureCallback {
181184

182185
/**
@@ -614,6 +617,15 @@ public void setEnableAutoTraceIdGeneration(final boolean enableAutoTraceIdGenera
614617
this.enableAutoTraceIdGeneration = enableAutoTraceIdGeneration;
615618
}
616619

620+
public boolean isEnableSystemEventBreadcrumbsExtras() {
621+
return enableSystemEventBreadcrumbsExtras;
622+
}
623+
624+
public void setEnableSystemEventBreadcrumbsExtras(
625+
final boolean enableSystemEventBreadcrumbsExtras) {
626+
this.enableSystemEventBreadcrumbsExtras = enableSystemEventBreadcrumbsExtras;
627+
}
628+
617629
static class AndroidUserFeedbackIDialogHandler implements SentryFeedbackOptions.IDialogHandler {
618630
@Override
619631
public void showDialog(

sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ String getStringAfterDotFast(final @Nullable String str) {
370370
if (batteryState.charging != null) {
371371
breadcrumb.setData("charging", batteryState.charging);
372372
}
373-
} else {
373+
} else if (options.isEnableSystemEventBreadcrumbsExtras()) {
374374
final Bundle extras = intent.getExtras();
375375
if (extras != null && !extras.isEmpty()) {
376376
final Map<String, String> newExtras = new HashMap<>(extras.size());

sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidOptionsTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ class SentryAndroidOptionsTest {
181181
)
182182
}
183183

184+
@Test
185+
fun `system event breadcrumbs extras disabled by default`() {
186+
val sentryOptions = SentryAndroidOptions()
187+
188+
assertFalse(sentryOptions.isEnableSystemEventBreadcrumbsExtras)
189+
}
190+
191+
@Test
192+
fun `system event breadcrumbs extras can be enabled`() {
193+
val sentryOptions = SentryAndroidOptions()
194+
sentryOptions.isEnableSystemEventBreadcrumbsExtras = true
195+
assertTrue(sentryOptions.isEnableSystemEventBreadcrumbsExtras)
196+
}
197+
184198
private class CustomDebugImagesLoader : IDebugImagesLoader {
185199
override fun loadDebugImages(): List<DebugImage>? = null
186200

sentry-android-core/src/test/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegrationTest.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ class SystemEventsBreadcrumbsIntegrationTest {
5151

5252
fun getSut(
5353
enableSystemEventBreadcrumbs: Boolean = true,
54+
enableSystemEventBreadcrumbsExtras: Boolean = false,
5455
executorService: ISentryExecutorService = ImmediateExecutorService(),
5556
): SystemEventsBreadcrumbsIntegration {
5657
options =
5758
SentryAndroidOptions().apply {
5859
isEnableSystemEventBreadcrumbs = enableSystemEventBreadcrumbs
60+
isEnableSystemEventBreadcrumbsExtras = enableSystemEventBreadcrumbsExtras
5961
this.executorService = executorService
6062
}
6163
return SystemEventsBreadcrumbsIntegration(
@@ -528,4 +530,59 @@ class SystemEventsBreadcrumbsIntegrationTest {
528530

529531
assertNull(sut.receiver)
530532
}
533+
534+
@Test
535+
fun `system event breadcrumbs include extras when enableSystemEventBreadcrumbsExtras is true`() {
536+
val sut = fixture.getSut(enableSystemEventBreadcrumbsExtras = true)
537+
538+
sut.register(fixture.scopes, fixture.options)
539+
val intent =
540+
Intent().apply {
541+
action = Intent.ACTION_TIME_CHANGED
542+
putExtra("test", 10)
543+
putExtra("test2", 20)
544+
}
545+
sut.receiver!!.onReceive(fixture.context, intent)
546+
547+
verify(fixture.scopes)
548+
.addBreadcrumb(
549+
check<Breadcrumb> {
550+
assertEquals("device.event", it.category)
551+
assertEquals("system", it.type)
552+
assertEquals(SentryLevel.INFO, it.level)
553+
assertEquals("TIME_SET", it.data["action"])
554+
assertNotNull(it.data["extras"])
555+
val extras = it.data["extras"] as Map<String, String>
556+
assertEquals("10", extras["test"])
557+
assertEquals("20", extras["test2"])
558+
},
559+
anyOrNull(),
560+
)
561+
}
562+
563+
@Test
564+
fun `system event breadcrumbs do not include extras when enableSystemEventBreadcrumbsExtras is false`() {
565+
val sut = fixture.getSut(enableSystemEventBreadcrumbsExtras = false)
566+
567+
sut.register(fixture.scopes, fixture.options)
568+
val intent =
569+
Intent().apply {
570+
action = Intent.ACTION_TIME_CHANGED
571+
putExtra("test", 10)
572+
putExtra("test2", 20)
573+
}
574+
sut.receiver!!.onReceive(fixture.context, intent)
575+
576+
verify(fixture.scopes)
577+
.addBreadcrumb(
578+
check<Breadcrumb> {
579+
assertEquals("device.event", it.category)
580+
assertEquals("system", it.type)
581+
assertEquals(SentryLevel.INFO, it.level)
582+
assertEquals("TIME_SET", it.data["action"])
583+
assertNull(it.data["extras"])
584+
},
585+
anyOrNull(),
586+
)
587+
}
531588
}

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,13 +4346,15 @@ public class io/sentry/cache/EnvelopeCache : io/sentry/cache/IEnvelopeCache {
43464346
public static final field SUFFIX_ENVELOPE_FILE Ljava/lang/String;
43474347
protected static final field UTF_8 Ljava/nio/charset/Charset;
43484348
protected final field cacheLock Lio/sentry/util/AutoClosableReentrantLock;
4349+
protected final field sessionLock Lio/sentry/util/AutoClosableReentrantLock;
43494350
public fun <init> (Lio/sentry/SentryOptions;Ljava/lang/String;I)V
43504351
public static fun create (Lio/sentry/SentryOptions;)Lio/sentry/cache/IEnvelopeCache;
43514352
public fun discard (Lio/sentry/SentryEnvelope;)V
43524353
public fun flushPreviousSession ()V
43534354
public static fun getCurrentSessionFile (Ljava/lang/String;)Ljava/io/File;
43544355
public static fun getPreviousSessionFile (Ljava/lang/String;)Ljava/io/File;
43554356
public fun iterator ()Ljava/util/Iterator;
4357+
public fun movePreviousSession (Ljava/io/File;Ljava/io/File;)V
43564358
public fun store (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)V
43574359
public fun storeEnvelope (Lio/sentry/SentryEnvelope;Lio/sentry/Hint;)Z
43584360
public fun waitPreviousSessionFlush ()Z
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.sentry;
2+
3+
import static io.sentry.SentryLevel.DEBUG;
4+
import static io.sentry.SentryLevel.INFO;
5+
6+
import io.sentry.cache.EnvelopeCache;
7+
import io.sentry.cache.IEnvelopeCache;
8+
import java.io.File;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
final class MovePreviousSession implements Runnable {
12+
13+
private final @NotNull SentryOptions options;
14+
15+
MovePreviousSession(final @NotNull SentryOptions options) {
16+
this.options = options;
17+
}
18+
19+
@Override
20+
public void run() {
21+
final String cacheDirPath = options.getCacheDirPath();
22+
if (cacheDirPath == null) {
23+
options.getLogger().log(INFO, "Cache dir is not set, not moving the previous session.");
24+
return;
25+
}
26+
27+
if (!options.isEnableAutoSessionTracking()) {
28+
options
29+
.getLogger()
30+
.log(DEBUG, "Session tracking is disabled, bailing from previous session mover.");
31+
return;
32+
}
33+
34+
final IEnvelopeCache cache = options.getEnvelopeDiskCache();
35+
if (cache instanceof EnvelopeCache) {
36+
final File currentSessionFile = EnvelopeCache.getCurrentSessionFile(cacheDirPath);
37+
final File previousSessionFile = EnvelopeCache.getPreviousSessionFile(cacheDirPath);
38+
39+
((EnvelopeCache) cache).movePreviousSession(currentSessionFile, previousSessionFile);
40+
41+
((EnvelopeCache) cache).flushPreviousSession();
42+
}
43+
}
44+
}

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ private static void init(final @NotNull SentryOptions options, final boolean glo
346346
options.setExecutorService(new SentryExecutorService(options));
347347
options.getExecutorService().prewarm();
348348
}
349+
350+
movePreviousSession(options);
349351
// when integrations are registered on Scopes ctor and async integrations are fired,
350352
// it might and actually happened that integrations called captureSomething
351353
// and Scopes was still NoOp.
@@ -497,6 +499,16 @@ private static void handleAppStartProfilingConfig(
497499
return options.getInternalTracesSampler().sample(appStartSamplingContext);
498500
}
499501

502+
@SuppressWarnings("FutureReturnValueIgnored")
503+
private static void movePreviousSession(final @NotNull SentryOptions options) {
504+
// enqueue a task to move previous unfinished session to its own file
505+
try {
506+
options.getExecutorService().submit(new MovePreviousSession(options));
507+
} catch (Throwable e) {
508+
options.getLogger().log(SentryLevel.DEBUG, "Failed to move previous session.", e);
509+
}
510+
}
511+
500512
@SuppressWarnings("FutureReturnValueIgnored")
501513
private static void finalizePreviousSession(
502514
final @NotNull SentryOptions options, final @NotNull IScopes scopes) {

0 commit comments

Comments
 (0)