Skip to content

Commit 87e190c

Browse files
runningcodeclaude
andcommitted
ref(time): Read the epoch time from EpochClock (JAVA-836)
The ApplicationExitInfo cutoff and okhttp's replay breadcrumb timestamps only need the current epoch time, which options.getEpochClock() gives. Moving them off CurrentDateProvider changes no values, and leaves fewer callers for v9 to touch when it removes ICurrentDateProvider. The three exit-info integrations no longer take a date provider: the dispatcher reads the clock from the options it already has. Timestamp.epochMillis() converts for the call sites that need millis. Fixes JAVA-836 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b0796b3 commit 87e190c

11 files changed

Lines changed: 24 additions & 65 deletions

File tree

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import io.sentry.protocol.Message;
2929
import io.sentry.protocol.SentryId;
3030
import io.sentry.protocol.SentryThread;
31-
import io.sentry.transport.CurrentDateProvider;
32-
import io.sentry.transport.ICurrentDateProvider;
3331
import io.sentry.util.HintUtils;
3432
import io.sentry.util.Objects;
3533
import java.io.BufferedReader;
@@ -47,19 +45,10 @@
4745
public final class AnrV2Integration implements Integration, Closeable {
4846

4947
private final @NotNull Context context;
50-
private final @NotNull ICurrentDateProvider dateProvider;
5148
private @Nullable SentryAndroidOptions options;
5249

5350
public AnrV2Integration(final @NotNull Context context) {
54-
// using CurrentDateProvider instead of AndroidCurrentDateProvider as AppExitInfo uses
55-
// System.currentTimeMillis
56-
this(context, CurrentDateProvider.getInstance());
57-
}
58-
59-
AnrV2Integration(
60-
final @NotNull Context context, final @NotNull ICurrentDateProvider dateProvider) {
6151
this.context = ContextUtils.getApplicationContext(context);
62-
this.dateProvider = dateProvider;
6352
}
6453

6554
@SuppressLint("NewApi") // we do the check in the AnrIntegrationFactory
@@ -88,7 +77,7 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) {
8877
.getExecutorService()
8978
.submit(
9079
new ApplicationExitInfoHistoryDispatcher(
91-
context, scopes, this.options, dateProvider, new AnrV2Policy(this.options)));
80+
context, scopes, this.options, new AnrV2Policy(this.options)));
9281
} catch (Throwable e) {
9382
options.getLogger().log(SentryLevel.DEBUG, "Failed to start ANR processor.", e);
9483
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.sentry.cache.IEnvelopeCache;
1414
import io.sentry.hints.BlockingFlushHint;
1515
import io.sentry.protocol.SentryId;
16-
import io.sentry.transport.ICurrentDateProvider;
1716
import io.sentry.util.HintUtils;
1817
import java.util.ArrayList;
1918
import java.util.Collections;
@@ -57,13 +56,13 @@ final class ApplicationExitInfoHistoryDispatcher implements Runnable {
5756
final @NotNull Context context,
5857
final @NotNull IScopes scopes,
5958
final @NotNull SentryAndroidOptions options,
60-
final @NotNull ICurrentDateProvider dateProvider,
6159
final @NotNull ApplicationExitInfoPolicy policy) {
6260
this.context = ContextUtils.getApplicationContext(context);
6361
this.scopes = scopes;
6462
this.options = options;
6563
this.policy = policy;
66-
this.threshold = dateProvider.getCurrentTimeMillis() - NINETY_DAYS_THRESHOLD;
64+
// ApplicationExitInfo timestamps are epoch millis, so the cutoff has to be on the wall clock.
65+
this.threshold = options.getEpochClock().now().epochMillis() - NINETY_DAYS_THRESHOLD;
6766
}
6867

6968
@RequiresApi(api = Build.VERSION_CODES.R)

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import io.sentry.protocol.Message;
2828
import io.sentry.protocol.SentryException;
2929
import io.sentry.protocol.SentryId;
30-
import io.sentry.transport.CurrentDateProvider;
31-
import io.sentry.transport.ICurrentDateProvider;
3230
import io.sentry.util.ExceptionUtils;
3331
import io.sentry.util.HintUtils;
3432
import io.sentry.util.Objects;
@@ -41,7 +39,6 @@
4139
import org.jetbrains.annotations.ApiStatus;
4240
import org.jetbrains.annotations.NotNull;
4341
import org.jetbrains.annotations.Nullable;
44-
import org.jetbrains.annotations.TestOnly;
4542

4643
/**
4744
* Reports Android process deaths that the OS records as <a
@@ -85,24 +82,12 @@ public final class MemoryLimiterIntegration implements Integration, Closeable {
8582
static final @NotNull String PROCESS_IMPORTANCE_FINGERPRINT_PREFIX = "process_importance:";
8683

8784
private final @NotNull Context context;
88-
private final @NotNull ICurrentDateProvider dateProvider;
8985
private final @NotNull BuildInfoProvider buildInfoProvider;
9086
private @Nullable SentryAndroidOptions androidOptions;
9187

9288
public MemoryLimiterIntegration(
9389
final @NotNull Context context, final @NotNull BuildInfoProvider buildInfoProvider) {
94-
// Use CurrentDateProvider instead of AndroidCurrentDateProvider as ApplicationExitInfo uses
95-
// epochal System.currentTimeMillis and not time since boot.
96-
this(context, CurrentDateProvider.getInstance(), buildInfoProvider);
97-
}
98-
99-
@TestOnly
100-
MemoryLimiterIntegration(
101-
final @NotNull Context context,
102-
final @NotNull ICurrentDateProvider dateProvider,
103-
final @NotNull BuildInfoProvider buildInfoProvider) {
10490
this.context = ContextUtils.getApplicationContext(context);
105-
this.dateProvider = dateProvider;
10691
this.buildInfoProvider = buildInfoProvider;
10792
}
10893

@@ -146,11 +131,7 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) {
146131
.getExecutorService()
147132
.submit(
148133
new ApplicationExitInfoHistoryDispatcher(
149-
context,
150-
scopes,
151-
androidOptions,
152-
dateProvider,
153-
new MemoryLimiterPolicy(androidOptions)));
134+
context, scopes, androidOptions, new MemoryLimiterPolicy(androidOptions)));
154135
} catch (Throwable e) {
155136
ExceptionUtils.rethrowIfFatal(e);
156137
options.getLogger().log(DEBUG, "Failed to start MemoryLimiter processor.", e);

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import io.sentry.protocol.SentryException;
3434
import io.sentry.protocol.SentryId;
3535
import io.sentry.protocol.SentryThread;
36-
import io.sentry.transport.CurrentDateProvider;
37-
import io.sentry.transport.ICurrentDateProvider;
3836
import io.sentry.util.HintUtils;
3937
import io.sentry.util.Objects;
4038
import java.io.ByteArrayInputStream;
@@ -51,19 +49,10 @@
5149
@ApiStatus.Internal
5250
public final class TombstoneIntegration implements Integration, Closeable {
5351
private final @NotNull Context context;
54-
private final @NotNull ICurrentDateProvider dateProvider;
5552
private @Nullable SentryAndroidOptions options;
5653

5754
public TombstoneIntegration(final @NotNull Context context) {
58-
// using CurrentDateProvider instead of AndroidCurrentDateProvider as AppExitInfo uses
59-
// System.currentTimeMillis
60-
this(context, CurrentDateProvider.getInstance());
61-
}
62-
63-
TombstoneIntegration(
64-
final @NotNull Context context, final @NotNull ICurrentDateProvider dateProvider) {
6555
this.context = ContextUtils.getApplicationContext(context);
66-
this.dateProvider = dateProvider;
6756
}
6857

6958
@Override
@@ -97,7 +86,6 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) {
9786
context,
9887
scopes,
9988
this.options,
100-
dateProvider,
10189
new TombstonePolicy(this.options, this.context)));
10290
} catch (Throwable e) {
10391
options.getLogger().log(SentryLevel.DEBUG, "Failed to start tombstone processor.", e);

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,7 @@ class ApplicationExitInfoCrossIntegrationTest {
169169
}
170170
val buildInfoProvider = mock<BuildInfoProvider>()
171171
whenever(buildInfoProvider.sdkInfoVersion).thenReturn(37)
172-
MemoryLimiterIntegration(
173-
fixture.context,
174-
io.sentry.transport.CurrentDateProvider.getInstance(),
175-
buildInfoProvider,
176-
)
172+
MemoryLimiterIntegration(fixture.context, buildInfoProvider)
177173
.register(memoryLimiterScopes, memoryLimiterOptions)
178174

179175
verify(memoryLimiterScopes)
@@ -224,11 +220,7 @@ class ApplicationExitInfoCrossIntegrationTest {
224220
}
225221
val buildInfoProvider = mock<BuildInfoProvider>()
226222
whenever(buildInfoProvider.sdkInfoVersion).thenReturn(37)
227-
MemoryLimiterIntegration(
228-
fixture.context,
229-
io.sentry.transport.CurrentDateProvider.getInstance(),
230-
buildInfoProvider,
231-
)
223+
MemoryLimiterIntegration(fixture.context, buildInfoProvider)
232224
.register(memoryLimiterScopes, memoryLimiterOptions)
233225

234226
verify(memoryLimiterScopes)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import io.sentry.android.core.cache.AndroidEnvelopeCache
1515
import io.sentry.cache.EnvelopeCache
1616
import io.sentry.protocol.SentryId
1717
import io.sentry.test.ImmediateExecutorService
18-
import io.sentry.transport.CurrentDateProvider
1918
import io.sentry.util.HintUtils
2019
import java.io.File
2120
import kotlin.test.BeforeTest
@@ -105,7 +104,7 @@ class MemoryLimiterIntegrationTest {
105104
whenever(scopes.captureEvent(any(), anyOrNull<Hint>())).thenReturn(SentryId())
106105
val buildInfoProvider = mock<BuildInfoProvider>()
107106
whenever(buildInfoProvider.sdkInfoVersion).thenReturn(sdkVersion)
108-
return MemoryLimiterIntegration(context, CurrentDateProvider.getInstance(), buildInfoProvider)
107+
return MemoryLimiterIntegration(context, buildInfoProvider)
109108
}
110109

111110
fun addAppExitInfo(

‎sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEvent.kt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import io.sentry.SentryDate
88
import io.sentry.SpanDataConvention
99
import io.sentry.SpanOptions
1010
import io.sentry.TypeCheckHint
11-
import io.sentry.transport.CurrentDateProvider
1211
import io.sentry.util.Platform
1312
import io.sentry.util.UrlUtils
1413
import io.sentry.util.network.NetworkRequestData
@@ -51,7 +50,7 @@ internal class SentryOkHttpEvent(private val scopes: IScopes, private val reques
5150
// needs this as unix timestamp for rrweb
5251
setData(
5352
SpanDataConvention.HTTP_START_TIMESTAMP,
54-
CurrentDateProvider.getInstance().currentTimeMillis,
53+
scopes.options.epochClock.now().epochMillis(),
5554
)
5655
}
5756

@@ -177,7 +176,7 @@ internal class SentryOkHttpEvent(private val scopes: IScopes, private val reques
177176
// needs this as unix timestamp for rrweb
178177
breadcrumb.setData(
179178
SpanDataConvention.HTTP_END_TIMESTAMP,
180-
CurrentDateProvider.getInstance().currentTimeMillis,
179+
scopes.options.epochClock.now().epochMillis(),
181180
)
182181
// We send the breadcrumb even without spans.
183182
scopes.addBreadcrumb(breadcrumb, hint)

‎sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import io.sentry.TypeCheckHint.OKHTTP_REQUEST
2020
import io.sentry.TypeCheckHint.OKHTTP_RESPONSE
2121
import io.sentry.TypeCheckHint.SENTRY_REPLAY_NETWORK_DETAILS
2222
import io.sentry.clientreport.DiscardReason
23-
import io.sentry.transport.CurrentDateProvider
2423
import io.sentry.util.ExceptionUtils
2524
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
2625
import io.sentry.util.Platform
@@ -109,7 +108,7 @@ public open class SentryOkHttpInterceptor(
109108
)
110109
}
111110

112-
val startTimestamp = CurrentDateProvider.getInstance().currentTimeMillis
111+
val startTimestamp = scopes.options.epochClock.now().epochMillis()
113112

114113
urlDetails.applyToSpan(span)
115114

@@ -269,7 +268,7 @@ public open class SentryOkHttpInterceptor(
269268
breadcrumb.setData(SpanDataConvention.HTTP_START_TIMESTAMP, startTimestamp)
270269
breadcrumb.setData(
271270
SpanDataConvention.HTTP_END_TIMESTAMP,
272-
CurrentDateProvider.getInstance().currentTimeMillis,
271+
scopes.options.epochClock.now().epochMillis(),
273272
)
274273

275274
scopes.addBreadcrumb(breadcrumb, hint)

‎sentry/api/sentry.api‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7773,6 +7773,7 @@ public final class io/sentry/time/SystemEpochClock : io/sentry/time/EpochClock {
77737773
}
77747774

77757775
public final class io/sentry/time/Timestamp {
7776+
public fun epochMillis ()J
77767777
public fun epochNanos ()J
77777778
public fun equals (Ljava/lang/Object;)Z
77787779
public fun hashCode ()I

‎sentry/src/main/java/io/sentry/time/Timestamp.java‎

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

3+
import java.util.concurrent.TimeUnit;
34
import org.jetbrains.annotations.ApiStatus;
45
import org.jetbrains.annotations.NotNull;
56
import org.jetbrains.annotations.Nullable;
@@ -34,6 +35,11 @@ public long epochNanos() {
3435
return epochNanos;
3536
}
3637

38+
/** Truncated to whole milliseconds, the precision of {@link java.util.Date} and most payloads. */
39+
public long epochMillis() {
40+
return TimeUnit.NANOSECONDS.toMillis(epochNanos);
41+
}
42+
3743
@Override
3844
public boolean equals(final @Nullable Object other) {
3945
if (this == other) {

0 commit comments

Comments
 (0)