Skip to content

Commit 59b6d80

Browse files
adinauerclaude
andauthored
fix(core): No longer guard direct usage of Sentry.metrics() and Sentry.logger() with enable flags (#6184)
* 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> * changelog * docs: Clarify manual telemetry changelog guidance Separate the automatic-integration behavior from manual filtering instructions. Name the Logs and Metrics before-send APIs and explain how to drop manually emitted telemetry. Co-Authored-By: Claude <noreply@anthropic.com> * docs: Announce telemetry option removal Clarify that the Logs and Metrics enable options will be removed in the next major release. Note that logging integrations will receive individual opt-in flags. Co-Authored-By: Claude <noreply@anthropic.com> * docs(android): Mention Timber in Logs manifest example Clarify that io.sentry.logs.enabled controls automatic logs from both Logcat and Timber integrations. Co-Authored-By: Claude <noreply@anthropic.com> * docs: Mark telemetry ungating as a breaking change Move the manual Logs and Metrics behavior change into the breaking changes section so its impact is clear to users. Co-Authored-By: Claude <noreply@anthropic.com> * docs(core): Clarify Metrics enable option behavior Document that the Metrics enable option currently has no effect because there are no automatic Metrics integrations. Co-Authored-By: Claude <noreply@anthropic.com> * docs(core): Explain synchronous processor shutdown Clarify that restarting an unused Logs or Metrics processor takes the synchronous shutdown path because its executor has no scheduled work. Co-Authored-By: Claude <noreply@anthropic.com> * docs: Clarify automatic Logs integration enablement Document that automatic logging integrations remain gated by the Logs enable option while direct logger calls bypass it. Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent d0843a2 commit 59b6d80

21 files changed

Lines changed: 224 additions & 59 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
### Breaking Changes
6+
7+
- Capture direct `Sentry.logger()` and `Sentry.metrics()` calls regardless of `options.getLogs().isEnabled()` and `options.getMetrics().isEnabled()` ([#6184](https://github.com/getsentry/sentry-java/pull/6184))
8+
- Automatic logging integrations still only capture logs when `options.getLogs().isEnabled()` is `true`. `options.getMetrics().setEnabled(...)` currently has no effect because there are no automatic Metrics integrations. Both enable options will be removed in the upcoming major release, and each logging integration will then have a separate opt-in flag.
9+
- Use `options.getLogs().setBeforeSend(...)` and `options.getMetrics().setBeforeSend(...)` to filter manually emitted telemetry. Return `null` from either callback to drop it.
10+
511
### Fixes
612

713
- Keep the videos of already captured session replay segments when the replay stops, so segments that are still queued are no longer sent without their video ([#6177](https://github.com/getsentry/sentry-java/pull/6177))

‎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 and Timber-->
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
@@ -5548,6 +5548,7 @@ public class io/sentry/logger/LoggerBatchProcessor : io/sentry/logger/ILoggerBat
55485548
public static final field FLUSH_AFTER_MS I
55495549
public static final field MAX_BATCH_SIZE I
55505550
public static final field MAX_QUEUE_SIZE I
5551+
protected field hasAcceptedItem Z
55515552
protected final field options Lio/sentry/SentryOptions;
55525553
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;)V
55535554
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;Lio/sentry/ISentryExecutorService;)V
@@ -5636,6 +5637,7 @@ public class io/sentry/metrics/MetricsBatchProcessor : io/sentry/metrics/IMetric
56365637
public static final field FLUSH_AFTER_MS I
56375638
public static final field MAX_BATCH_SIZE I
56385639
public static final field MAX_QUEUE_SIZE I
5640+
protected field hasAcceptedItem Z
56395641
protected final field options Lio/sentry/SentryOptions;
56405642
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/ISentryClient;)V
56415643
public fun add (Lio/sentry/SentryMetricsEvent;)V

0 commit comments

Comments
 (0)