Skip to content

Commit d83b12a

Browse files
adinauerclaude
andcommitted
fix(core): Capture manual telemetry without enable flags
Capture direct Logs and Metrics API calls regardless of their legacy enable flags while preserving those flags for automatic integrations in v8. Always create the configured batch processors and keep their workers lazy until the first accepted item. Keep automatic Timber forwarding gated and avoid unused Android background flush submissions. Fixes #6170 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 91fbff1 commit d83b12a

20 files changed

Lines changed: 210 additions & 59 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/AndroidLoggerBatchProcessor.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public void onForeground() {
2424

2525
@Override
2626
public void onBackground() {
27+
if (!hasAcceptedItem) {
28+
return;
29+
}
2730
try {
2831
options
2932
.getExecutorService()

‎sentry-android-core/src/main/java/io/sentry/android/core/AndroidMetricsBatchProcessor.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public void onForeground() {
2424

2525
@Override
2626
public void onBackground() {
27+
if (!hasAcceptedItem) {
28+
return;
29+
}
2730
try {
2831
options
2932
.getExecutorService()

‎sentry-android-core/src/test/java/io/sentry/android/core/AndroidLoggerBatchProcessorTest.kt‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.sentry.android.core
22

33
import androidx.test.ext.junit.runners.AndroidJUnit4
44
import io.sentry.ISentryClient
5+
import io.sentry.ISentryExecutorService
56
import io.sentry.SentryLogEvent
67
import io.sentry.SentryLogLevel
78
import io.sentry.SentryOptions
@@ -16,6 +17,7 @@ import org.junit.runner.RunWith
1617
import org.mockito.kotlin.any
1718
import org.mockito.kotlin.mock
1819
import org.mockito.kotlin.verify
20+
import org.mockito.kotlin.verifyNoInteractions
1921
import org.mockito.kotlin.whenever
2022

2123
@RunWith(AndroidJUnit4::class)
@@ -66,13 +68,24 @@ class AndroidLoggerBatchProcessorTest {
6668
verify(fixture.client).captureBatchedLogEvents(any())
6769
}
6870

71+
@Test
72+
fun `onBackground does not submit shared executor work without accepted items`() {
73+
val sharedExecutor = mock<ISentryExecutorService>()
74+
val sut = fixture.getSut { options -> options.executorService = sharedExecutor }
75+
76+
sut.onBackground()
77+
78+
verifyNoInteractions(sharedExecutor)
79+
}
80+
6981
@Test
7082
fun `onBackground handles executor exception gracefully`() {
7183
val sut = fixture.getSut { options ->
72-
val rejectingExecutor = mock<io.sentry.ISentryExecutorService>()
84+
val rejectingExecutor = mock<ISentryExecutorService>()
7385
whenever(rejectingExecutor.submit(any())).thenThrow(RuntimeException("Rejected"))
7486
options.executorService = rejectingExecutor
7587
}
88+
sut.add(SentryLogEvent(SentryId(), 1.0, "test", SentryLogLevel.INFO))
7689

7790
// Should not throw
7891
sut.onBackground()

‎sentry-android-core/src/test/java/io/sentry/android/core/AndroidMetricsBatchProcessorTest.kt‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.sentry.android.core
22

33
import androidx.test.ext.junit.runners.AndroidJUnit4
44
import io.sentry.ISentryClient
5+
import io.sentry.ISentryExecutorService
56
import io.sentry.SentryMetricsEvent
67
import io.sentry.SentryOptions
78
import io.sentry.protocol.SentryId
@@ -15,6 +16,7 @@ import org.junit.runner.RunWith
1516
import org.mockito.kotlin.any
1617
import org.mockito.kotlin.mock
1718
import org.mockito.kotlin.verify
19+
import org.mockito.kotlin.verifyNoInteractions
1820
import org.mockito.kotlin.whenever
1921

2022
@RunWith(AndroidJUnit4::class)
@@ -65,13 +67,24 @@ class AndroidMetricsBatchProcessorTest {
6567
verify(fixture.client).captureBatchedMetricsEvents(any())
6668
}
6769

70+
@Test
71+
fun `onBackground does not submit shared executor work without accepted items`() {
72+
val sharedExecutor = mock<ISentryExecutorService>()
73+
val sut = fixture.getSut { options -> options.executorService = sharedExecutor }
74+
75+
sut.onBackground()
76+
77+
verifyNoInteractions(sharedExecutor)
78+
}
79+
6880
@Test
6981
fun `onBackground handles executor exception gracefully`() {
7082
val sut = fixture.getSut { options ->
71-
val rejectingExecutor = mock<io.sentry.ISentryExecutorService>()
83+
val rejectingExecutor = mock<ISentryExecutorService>()
7284
whenever(rejectingExecutor.submit(any())).thenThrow(RuntimeException("Rejected"))
7385
options.executorService = rejectingExecutor
7486
}
87+
sut.add(SentryMetricsEvent(SentryId(), 1.0, "test", "counter", 3.0))
7588

7689
// Should not throw
7790
sut.onBackground()

‎sentry-android-timber/src/main/java/io/sentry/android/timber/SentryTimberTree.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ public class SentryTimberTree(
246246
throwable: Throwable?,
247247
vararg args: Any?,
248248
) {
249+
if (!scopes.options.logs.isEnabled) {
250+
return
251+
}
252+
249253
// checks the log level
250254
if (isLoggable(sentryLogLevel, minLogLevel)) {
251255
val attributes = tag?.let {

‎sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberIntegrationTest.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class SentryTimberIntegrationTest {
2424
val scopes = mock<IScopes>()
2525
val options = SentryOptions().apply { sdkVersion = SdkVersion("test", "1.2.3") }
2626

27+
init {
28+
whenever(scopes.options).thenReturn(options)
29+
}
30+
2731
fun getSut(
2832
minEventLevel: SentryLevel = SentryLevel.ERROR,
2933
minBreadcrumbLevel: SentryLevel = SentryLevel.INFO,

‎sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.sentry.Breadcrumb
55
import io.sentry.Scopes
66
import io.sentry.SentryLevel
77
import io.sentry.SentryLogLevel
8+
import io.sentry.SentryOptions
89
import io.sentry.logger.ILoggerApi
910
import io.sentry.logger.SentryLogParameters
1011
import kotlin.test.BeforeTest
@@ -31,9 +32,12 @@ class SentryTimberTreeTest {
3132
minEventLevel: SentryLevel = SentryLevel.ERROR,
3233
minBreadcrumbLevel: SentryLevel = SentryLevel.INFO,
3334
minLogsLevel: SentryLogLevel = SentryLogLevel.INFO,
35+
logsEnabled: Boolean = true,
3436
): SentryTimberTree {
3537
logs = mock<ILoggerApi>()
3638
scopes = mock<Scopes>()
39+
val options = SentryOptions().apply { logs.isEnabled = logsEnabled }
40+
whenever(scopes.options).thenReturn(options)
3741
whenever(scopes.logger()).thenReturn(logs)
3842
return SentryTimberTree(scopes, minEventLevel, minBreadcrumbLevel, minLogsLevel)
3943
}
@@ -332,6 +336,17 @@ class SentryTimberTreeTest {
332336
verifyNoInteractions(fixture.logs)
333337
}
334338

339+
@Test
340+
fun `Tree does not add a log when automatic Logs are disabled`() {
341+
val sut = fixture.getSut(logsEnabled = false)
342+
343+
sut.e("message")
344+
345+
verifyNoInteractions(fixture.logs)
346+
verify(fixture.scopes).captureEvent(any())
347+
verify(fixture.scopes).addBreadcrumb(any<Breadcrumb>())
348+
}
349+
335350
@Test
336351
fun `Tree adds an info log`() {
337352
val sut = fixture.getSut()

‎sentry-samples/sentry-samples-android/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
android:name="io.sentry.debug"
148148
android:value="${sentryDebug}" />
149149

150-
<!-- how to enable Sentry Logs (Sentry.logger() calls are dropped unless this is on)-->
150+
<!-- how to enable automatic Sentry Logs from Logcat-->
151151
<meta-data
152152
android:name="io.sentry.logs.enabled"
153153
android:value="true" />

‎sentry/api/sentry.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5547,6 +5547,7 @@ public class io/sentry/logger/LoggerBatchProcessor : io/sentry/logger/ILoggerBat
55475547
public static final field FLUSH_AFTER_MS I
55485548
public static final field MAX_BATCH_SIZE I
55495549
public static final field MAX_QUEUE_SIZE I
5550+
protected field hasAcceptedItem Z
55505551
protected final field options Lio/sentry/SentryOptions;
55515552
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;)V
55525553
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;Lio/sentry/ISentryExecutorService;)V
@@ -5635,6 +5636,7 @@ public class io/sentry/metrics/MetricsBatchProcessor : io/sentry/metrics/IMetric
56355636
public static final field FLUSH_AFTER_MS I
56365637
public static final field MAX_BATCH_SIZE I
56375638
public static final field MAX_QUEUE_SIZE I
5639+
protected field hasAcceptedItem Z
56385640
protected final field options Lio/sentry/SentryOptions;
56395641
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;)V
56405642
public fun add (Lio/sentry/SentryMetricsEvent;)V

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import io.sentry.hints.DiskFlushNotification;
1010
import io.sentry.hints.TransactionEnd;
1111
import io.sentry.logger.ILoggerBatchProcessor;
12-
import io.sentry.logger.NoOpLoggerBatchProcessor;
1312
import io.sentry.metrics.IMetricsBatchProcessor;
14-
import io.sentry.metrics.NoOpMetricsBatchProcessor;
1513
import io.sentry.protocol.Contexts;
1614
import io.sentry.protocol.DebugMeta;
1715
import io.sentry.protocol.FeatureFlags;
@@ -60,18 +58,9 @@ public SentryClient(final @NotNull SentryOptions options) {
6058

6159
final RequestDetailsResolver requestDetailsResolver = new RequestDetailsResolver(options);
6260
transport = transportFactory.create(options, requestDetailsResolver.resolve());
63-
if (options.getLogs().isEnabled()) {
64-
loggerBatchProcessor =
65-
options.getLogs().getLoggerBatchProcessorFactory().create(options, this);
66-
} else {
67-
loggerBatchProcessor = NoOpLoggerBatchProcessor.getInstance();
68-
}
69-
if (options.getMetrics().isEnabled()) {
70-
metricsBatchProcessor =
71-
options.getMetrics().getMetricsBatchProcessorFactory().create(options, this);
72-
} else {
73-
metricsBatchProcessor = NoOpMetricsBatchProcessor.getInstance();
74-
}
61+
loggerBatchProcessor = options.getLogs().getLoggerBatchProcessorFactory().create(options, this);
62+
metricsBatchProcessor =
63+
options.getMetrics().getMetricsBatchProcessorFactory().create(options, this);
7564
}
7665

7766
private boolean shouldApplyScopeData(

0 commit comments

Comments
 (0)