diff --git a/CHANGELOG.md b/CHANGELOG.md index 735831f2f98..482f613041b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Profiling: No longer crash on API 35+ devices whose framework can't load `ProfilingManager`. Perfetto profiling is turned off on those devices instead ([#6742](https://github.com/getsentry/sentry-java/pull/6742)) - Android: Out-of-process crash reporting now supports Android 5.0 (API level 21) and later, previously required Android 11 (API level 30) ([#6614](https://github.com/getsentry/sentry-java/pull/6614)) - Android profiling: Uploading a Perfetto profile chunk now keeps the trace in memory once instead of up to three times, lowering peak memory use for traces up to 50 MB ([#6743](https://github.com/getsentry/sentry-java/pull/6743)) +- Stop a transaction's CPU and memory collection 30 seconds after it starts even when the device's wall clock changes in between ([#6739](https://github.com/getsentry/sentry-java/pull/6739)) ### Dependencies diff --git a/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java b/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java index 89373516e2c..785fa28a69c 100644 --- a/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java +++ b/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java @@ -1,5 +1,6 @@ package io.sentry; +import io.sentry.time.Deadline; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.Objects; import java.util.ArrayList; @@ -124,7 +125,7 @@ public void run() { // Add the enriched tempData to all transactions/profiles/objects that collect data. // Then Check if that object timed out. for (CompositeData data : compositeDataMap.values()) { - if (data.addDataAndCheckTimeout(tempData, tempData.getNanoTimestamp())) { + if (data.addDataAndCheckTimeout(tempData)) { // timed out if (data.transaction != null) { timedOutTransactions.add(data.transaction); @@ -212,33 +213,31 @@ public void close() { private class CompositeData { private final @NotNull List dataList; private final @Nullable ITransaction transaction; - private final long startTimestamp; + private final @NotNull Deadline timeout; private CompositeData(final @Nullable ITransaction transaction) { this.dataList = new ArrayList<>(); this.transaction = transaction; - this.startTimestamp = options.getDateProvider().now().nanoTimestamp(); + this.timeout = + Deadline.after( + options.getMonotonicTicker(), + TRANSACTION_COLLECTION_TIMEOUT_MILLIS, + TimeUnit.MILLISECONDS); } /** * Adds the data to the internal list of PerformanceCollectionData. Then it checks if data * collection timed out (for transactions only). * - * @param nowNanos the timestamp of the current collection, passed in so a single clock reading - * is shared by every transaction in this collection round. * @return true if data collection timed out (for transactions only). */ - boolean addDataAndCheckTimeout( - final @NotNull PerformanceCollectionData data, final long nowNanos) { + boolean addDataAndCheckTimeout(final @NotNull PerformanceCollectionData data) { // stop() hands dataList out while this timer thread may still be writing to it, so consumers // synchronize on the list while iterating. We must hold the same monitor here. synchronized (dataList) { dataList.add(data); } - return transaction != null - && nowNanos - > startTimestamp - + TimeUnit.MILLISECONDS.toNanos(TRANSACTION_COLLECTION_TIMEOUT_MILLIS); + return transaction != null && timeout.hasPassed(); } } } diff --git a/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt b/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt index 5726b6ca25c..dabc043a8e5 100644 --- a/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt +++ b/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt @@ -3,9 +3,12 @@ package io.sentry import io.sentry.test.getCtor import io.sentry.test.getProperty import io.sentry.test.injectForField +import io.sentry.time.MonotonicTicker +import io.sentry.time.TestMonotonicTicker import io.sentry.util.thread.ThreadChecker import java.util.Timer import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicReference import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -33,7 +36,12 @@ class DefaultCompositePerformanceCollectorTest { lateinit var transaction2: ITransaction val id1 = "id1" val scopes: IScopes = mock() - val options = SentryOptions() + val ticker = TestMonotonicTicker() + // SentryOptions has no ticker setter. + val options = + object : SentryOptions() { + override fun getMonotonicTicker(): MonotonicTicker = ticker + } var mockTimer: Timer? = null val mockCpuCollector: IPerformanceSnapshotCollector = @@ -183,26 +191,15 @@ class DefaultCompositePerformanceCollectorTest { @Test fun `collector times out after 30 seconds`() { - val mockDateProvider = mock() val mockCollector = mock() - val dates = - listOf( - SentryNanotimeDate(TimeUnit.SECONDS.toMillis(100), TimeUnit.SECONDS.toNanos(100)), - SentryNanotimeDate(TimeUnit.SECONDS.toMillis(131), TimeUnit.SECONDS.toNanos(131)), - ) - whenever(mockDateProvider.now()).thenReturn(dates[0], dates[0], dates[0], dates[1]) - val collector = fixture.getSut { - it.dateProvider = mockDateProvider - it.addPerformanceCollector(mockCollector) - } + val collector = fixture.getSut { it.addPerformanceCollector(mockCollector) } collector.start(fixture.transaction1) verify(fixture.mockTimer, never())!!.cancel() + fixture.ticker.advance(30, TimeUnit.SECONDS) // Let's sleep to make the collector get values Thread.sleep(300) - // When the collector gets the values, it checks the current date, set 31 seconds after the - // begin. This means it should stop itself verify(fixture.mockTimer)!!.cancel() // When the collector times out, the data collection for spans is stopped, too @@ -215,29 +212,51 @@ class DefaultCompositePerformanceCollectorTest { @Test fun `collector collects for 30 seconds`() { - val mockDateProvider = mock() - val dates = - listOf( - SentryNanotimeDate(TimeUnit.SECONDS.toMillis(100), TimeUnit.SECONDS.toNanos(100)), - SentryNanotimeDate(TimeUnit.SECONDS.toMillis(130), TimeUnit.SECONDS.toNanos(130)), - ) - whenever(mockDateProvider.now()).thenReturn(dates[0], dates[0], dates[0], dates[1]) - val collector = fixture.getSut { it.dateProvider = mockDateProvider } + val collector = fixture.getSut() collector.start(fixture.transaction1) - verify(fixture.mockTimer, never())!!.cancel() + fixture.ticker.advance(TimeUnit.SECONDS.toNanos(30) - 1, TimeUnit.NANOSECONDS) // Let's sleep to make the collector get values Thread.sleep(300) - // When the collector gets the values, it checks the current date, set 30 seconds after the - // begin. This means it should continue without being cancelled verify(fixture.mockTimer, never())!!.cancel() - // Data is deleted after the collector times out val data1 = collector.stop(fixture.transaction1) assertNotNull(data1) } + @Test + fun `collector keeps collecting when the wall clock steps forward`() { + val wallClock = AtomicReference(wallClockAt(TimeUnit.HOURS.toSeconds(2))) + val collector = fixture.getSut { it.dateProvider = SentryDateProvider { wallClock.get() } } + collector.start(fixture.transaction1) + + wallClock.set(wallClockAt(TimeUnit.HOURS.toSeconds(3))) + // Let's sleep to make the collector get values + Thread.sleep(300) + + verify(fixture.mockTimer, never())!!.cancel() + assertNotNull(collector.stop(fixture.transaction1)) + } + + @Test + fun `collector times out when the wall clock steps backward`() { + val wallClock = AtomicReference(wallClockAt(TimeUnit.HOURS.toSeconds(2))) + val collector = fixture.getSut { it.dateProvider = SentryDateProvider { wallClock.get() } } + collector.start(fixture.transaction1) + + wallClock.set(wallClockAt(TimeUnit.HOURS.toSeconds(1))) + fixture.ticker.advance(30, TimeUnit.SECONDS) + // Let's sleep to make the collector get values + Thread.sleep(300) + + verify(fixture.mockTimer)!!.cancel() + assertNull(collector.stop(fixture.transaction1)) + } + + private fun wallClockAt(seconds: Long) = + SentryNanotimeDate(TimeUnit.SECONDS.toMillis(seconds), TimeUnit.SECONDS.toNanos(seconds)) + @Test fun `collector has no IPerformanceCollector by default`() { val collector = fixture.getSut(null, null)