Skip to content

Commit 52bf161

Browse files
runningcodeclaude
andauthored
ref(core): Measure the performance-collection budget on a monotonic ticker (JAVA-579) (#6101)
* ref(core): Measure the performance-collection budget on a monotonic ticker (JAVA-579) DefaultCompositePerformanceCollector decided a transaction had been collecting for 30s by subtracting two options.getDateProvider() readings. Those are wall-clock on every platform, including Android: SentryNanotimeDate.nanoTimestamp() returns millisToNanos(unixDateMillis), not its nanoTime component, so the extra precision that type exists for never entered this comparison. A device time change therefore ended collection early or kept it running past the budget. Each CompositeData now holds a Deadline on a MonotonicTicker. The budget is not a serialized value, so this only changes when the collector stops. Two side effects worth review: the 30s boundary is now inclusive, where the old strict `>` let a sample land exactly at 30s; and addDataAndCheckTimeout no longer takes the shared clock reading, since each transaction owns its own deadline. TestMonotonicTicker's field is now volatile, as the timer thread reads a ticker the test thread advances. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * changelog * ref(core): Read the collection ticker from options Drop the package-private constructor overload that took a MonotonicTicker and read options.getMonotonicTicker() instead. The test overrides getMonotonicTicker() on a SentryOptions subclass, as SentryOptionsTest already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 722bd53 commit 52bf161

4 files changed

Lines changed: 36 additions & 37 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575

7676
- Promote the metrics capture API from experimental to stable ([#6425](https://github.com/getsentry/sentry-java/pull/6425))
7777

78+
### Internal
79+
80+
- Measure the 30 second performance-collection budget on a monotonic ticker, so that a device time change no longer ends collection early or extends it past the budget ([#6101](https://github.com/getsentry/sentry-java/pull/6101))
81+
7882
## 8.63.0
7983

8084
### Features

‎sentry-test-support/src/main/kotlin/io/sentry/time/TestMonotonicTicker.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.util.concurrent.TimeUnit
99
* nanosecond ticker is off by a factor of a million and still compiles, whereas `advance(1001,
1010
* MILLISECONDS)` cannot be.
1111
*/
12-
class TestMonotonicTicker(private var nanos: Long = 0) : MonotonicTicker {
12+
class TestMonotonicTicker(@Volatile private var nanos: Long = 0) : MonotonicTicker {
1313
override fun tickNanos(): Long = nanos
1414

1515
fun advance(amount: Long, unit: TimeUnit) {

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry;
22

3+
import io.sentry.time.Deadline;
34
import io.sentry.util.AutoClosableReentrantLock;
45
import io.sentry.util.Objects;
56
import java.util.ArrayList;
@@ -124,7 +125,7 @@ public void run() {
124125
// Add the enriched tempData to all transactions/profiles/objects that collect data.
125126
// Then Check if that object timed out.
126127
for (CompositeData data : compositeDataMap.values()) {
127-
if (data.addDataAndCheckTimeout(tempData, tempData.getNanoTimestamp())) {
128+
if (data.addDataAndCheckTimeout(tempData)) {
128129
// timed out
129130
if (data.transaction != null) {
130131
timedOutTransactions.add(data.transaction);
@@ -212,33 +213,33 @@ public void close() {
212213
private class CompositeData {
213214
private final @NotNull List<PerformanceCollectionData> dataList;
214215
private final @Nullable ITransaction transaction;
215-
private final long startTimestamp;
216+
private final @NotNull Deadline collectUntil;
216217

217218
private CompositeData(final @Nullable ITransaction transaction) {
218219
this.dataList = new ArrayList<>();
219220
this.transaction = transaction;
220-
this.startTimestamp = options.getDateProvider().now().nanoTimestamp();
221+
// On a ticker rather than the date provider: this is how long we have been collecting, and
222+
// a device time change must not end a collection early or keep a finished one going.
223+
this.collectUntil =
224+
Deadline.after(
225+
options.getMonotonicTicker(),
226+
TRANSACTION_COLLECTION_TIMEOUT_MILLIS,
227+
TimeUnit.MILLISECONDS);
221228
}
222229

223230
/**
224231
* Adds the data to the internal list of PerformanceCollectionData. Then it checks if data
225232
* collection timed out (for transactions only).
226233
*
227-
* @param nowNanos the timestamp of the current collection, passed in so a single clock reading
228-
* is shared by every transaction in this collection round.
229234
* @return true if data collection timed out (for transactions only).
230235
*/
231-
boolean addDataAndCheckTimeout(
232-
final @NotNull PerformanceCollectionData data, final long nowNanos) {
236+
boolean addDataAndCheckTimeout(final @NotNull PerformanceCollectionData data) {
233237
// stop() hands dataList out while this timer thread may still be writing to it, so consumers
234238
// synchronize on the list while iterating. We must hold the same monitor here.
235239
synchronized (dataList) {
236240
dataList.add(data);
237241
}
238-
return transaction != null
239-
&& nowNanos
240-
> startTimestamp
241-
+ TimeUnit.MILLISECONDS.toNanos(TRANSACTION_COLLECTION_TIMEOUT_MILLIS);
242+
return transaction != null && collectUntil.hasPassed();
242243
}
243244
}
244245
}

‎sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt‎

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package io.sentry
33
import io.sentry.test.getCtor
44
import io.sentry.test.getProperty
55
import io.sentry.test.injectForField
6+
import io.sentry.time.MonotonicTicker
7+
import io.sentry.time.TestMonotonicTicker
68
import io.sentry.util.thread.ThreadChecker
79
import java.util.Timer
810
import java.util.concurrent.TimeUnit
@@ -33,7 +35,11 @@ class DefaultCompositePerformanceCollectorTest {
3335
lateinit var transaction2: ITransaction
3436
val id1 = "id1"
3537
val scopes: IScopes = mock()
36-
val options = SentryOptions()
38+
val ticker = TestMonotonicTicker()
39+
val options =
40+
object : SentryOptions() {
41+
override fun getMonotonicTicker(): MonotonicTicker = ticker
42+
}
3743
var mockTimer: Timer? = null
3844

3945
val mockCpuCollector: IPerformanceSnapshotCollector =
@@ -183,26 +189,19 @@ class DefaultCompositePerformanceCollectorTest {
183189

184190
@Test
185191
fun `collector times out after 30 seconds`() {
186-
val mockDateProvider = mock<SentryDateProvider>()
187192
val mockCollector = mock<IPerformanceContinuousCollector>()
188-
val dates =
189-
listOf(
190-
SentryNanotimeDate(TimeUnit.SECONDS.toMillis(100), TimeUnit.SECONDS.toNanos(100)),
191-
SentryNanotimeDate(TimeUnit.SECONDS.toMillis(131), TimeUnit.SECONDS.toNanos(131)),
192-
)
193-
whenever(mockDateProvider.now()).thenReturn(dates[0], dates[0], dates[0], dates[1])
194-
val collector = fixture.getSut {
195-
it.dateProvider = mockDateProvider
196-
it.addPerformanceCollector(mockCollector)
197-
}
193+
val collector = fixture.getSut { it.addPerformanceCollector(mockCollector) }
198194
collector.start(fixture.transaction1)
199195
verify(fixture.mockTimer, never())!!.cancel()
200196

197+
// 31 seconds of collecting have gone by
198+
fixture.ticker.advance(31, TimeUnit.SECONDS)
199+
201200
// Let's sleep to make the collector get values
202201
Thread.sleep(300)
203202

204-
// When the collector gets the values, it checks the current date, set 31 seconds after the
205-
// begin. This means it should stop itself
203+
// When the collector gets the values, it checks how long it has been collecting for, which is
204+
// now past the 30 second budget. This means it should stop itself
206205
verify(fixture.mockTimer)!!.cancel()
207206

208207
// When the collector times out, the data collection for spans is stopped, too
@@ -214,23 +213,18 @@ class DefaultCompositePerformanceCollectorTest {
214213
}
215214

216215
@Test
217-
fun `collector collects for 30 seconds`() {
218-
val mockDateProvider = mock<SentryDateProvider>()
219-
val dates =
220-
listOf(
221-
SentryNanotimeDate(TimeUnit.SECONDS.toMillis(100), TimeUnit.SECONDS.toNanos(100)),
222-
SentryNanotimeDate(TimeUnit.SECONDS.toMillis(130), TimeUnit.SECONDS.toNanos(130)),
223-
)
224-
whenever(mockDateProvider.now()).thenReturn(dates[0], dates[0], dates[0], dates[1])
225-
val collector = fixture.getSut { it.dateProvider = mockDateProvider }
216+
fun `collector keeps collecting while inside the 30 second budget`() {
217+
val collector = fixture.getSut()
226218
collector.start(fixture.transaction1)
227219
verify(fixture.mockTimer, never())!!.cancel()
228220

221+
// 29 seconds of collecting have gone by
222+
fixture.ticker.advance(29, TimeUnit.SECONDS)
223+
229224
// Let's sleep to make the collector get values
230225
Thread.sleep(300)
231226

232-
// When the collector gets the values, it checks the current date, set 30 seconds after the
233-
// begin. This means it should continue without being cancelled
227+
// Still inside the 30 second budget, so it should continue without being cancelled
234228
verify(fixture.mockTimer, never())!!.cancel()
235229

236230
// Data is deleted after the collector times out

0 commit comments

Comments
 (0)