Skip to content

Commit 41dd4a2

Browse files
runningcodeclaude
andcommitted
feat(time): Let AnchoredClock recover the tick of an instant it projected
Restores the anchor plumbing that JAVA-572's Timestamp/AnchoredClock PR (#6045) shed before merge, because it had no consumer at the time. The span integration below is that consumer. A Timestamp now records the clock that projected it, if any. That lets tickOf invert the projection exactly — projection adds a tick difference to a fixed epoch, so subtraction recovers the tick — and lets it reject an instant this clock did not produce, whose epoch bears no arithmetic relation to these ticks. at() becomes public for the reverse direction, placing something already measured on the ticker, such as a frame or a profiler sample, on the same timeline. Equality stays by instant: the anchor records how an instant was obtained, not what it denotes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 744ff64 commit 41dd4a2

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

‎sentry/api/sentry.api‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7600,9 +7600,11 @@ public final class io/sentry/rrweb/RRWebVideoEvent$JsonKeys {
76007600
}
76017601

76027602
public final class io/sentry/time/AnchoredClock {
7603+
public fun at (J)Lio/sentry/time/Timestamp;
76037604
public static fun create (Lio/sentry/time/EpochClock;Lio/sentry/time/MonotonicTicker;)Lio/sentry/time/AnchoredClock;
76047605
public fun now ()Lio/sentry/time/Timestamp;
76057606
public fun startTime ()Lio/sentry/time/Timestamp;
7607+
public fun tickOf (Lio/sentry/time/Timestamp;)J
76067608
}
76077609

76087610
public final class io/sentry/time/Deadline {
@@ -7638,6 +7640,7 @@ public final class io/sentry/time/SystemEpochClock : io/sentry/time/EpochClock {
76387640
}
76397641

76407642
public final class io/sentry/time/Timestamp {
7643+
public fun anchor ()Lio/sentry/time/AnchoredClock;
76417644
public fun epochNanos ()J
76427645
public fun equals (Ljava/lang/Object;)Z
76437646
public fun hashCode ()I

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,35 @@ private AnchoredClock(
4343
* from a tick. Reads no clock and never changes.
4444
*/
4545
public @NotNull Timestamp startTime() {
46-
return Timestamp.ofEpochNanos(epochNanos);
46+
return Timestamp.anchoredAt(epochNanos, this);
4747
}
4848

4949
/** Now: {@link #startTime()} plus the time the ticker has measured since the anchor. */
5050
public @NotNull Timestamp now() {
5151
return at(ticker.tickNanos());
5252
}
5353

54-
private @NotNull Timestamp at(final long tickNanos) {
55-
return Timestamp.ofEpochNanos(epochNanos + (tickNanos - anchorTick));
54+
/**
55+
* The instant a tick corresponds to, for placing something already measured on this ticker — a
56+
* frame, a profiler sample — on the same timeline as the instants projected here.
57+
*/
58+
public @NotNull Timestamp at(final long tickNanos) {
59+
return Timestamp.anchoredAt(epochNanos + (tickNanos - anchorTick), this);
60+
}
61+
62+
/**
63+
* The tick an instant was projected from. Exact, and reads no clock: projection adds a tick
64+
* difference to a fixed epoch, so subtraction inverts it.
65+
*
66+
* @throws IllegalArgumentException if this clock did not project the instant. Its epoch bears no
67+
* arithmetic relation to these ticks, so converting it would silently produce a tick derived
68+
* from a wall-clock difference.
69+
*/
70+
public long tickOf(final @NotNull Timestamp timestamp) {
71+
if (timestamp.anchor() != this) {
72+
throw new IllegalArgumentException(
73+
"Timestamp was not projected by this AnchoredClock: " + timestamp);
74+
}
75+
return anchorTick + (timestamp.epochNanos() - epochNanos);
5676
}
5777
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,46 @@
1515
* come from a {@link Stopwatch}, or from two instants an {@link AnchoredClock} projected from the
1616
* same tick.
1717
*
18+
* <p>{@link #anchor()} records which of those this is. An instant read straight from the wall
19+
* clock, or stated by something outside this process, has no anchor and can only be serialized. One
20+
* an {@link AnchoredClock} produced references that clock, which lets {@link AnchoredClock#tickOf}
21+
* recover the tick it came from and reject instants it did not produce.
22+
*
1823
* <p>Nanoseconds since the epoch overflow a long in the year 2262.
1924
*/
2025
@ApiStatus.Internal
2126
public final class Timestamp {
2227

2328
private final long epochNanos;
29+
private final @Nullable AnchoredClock anchor;
2430

25-
private Timestamp(final long epochNanos) {
31+
private Timestamp(final long epochNanos, final @Nullable AnchoredClock anchor) {
2632
this.epochNanos = epochNanos;
33+
this.anchor = anchor;
2734
}
2835

36+
/** An instant read straight from a wall clock, or stated by something outside this process. */
2937
public static @NotNull Timestamp ofEpochNanos(final long epochNanos) {
30-
return new Timestamp(epochNanos);
38+
return new Timestamp(epochNanos, null);
39+
}
40+
41+
static @NotNull Timestamp anchoredAt(final long epochNanos, final @NotNull AnchoredClock anchor) {
42+
return new Timestamp(epochNanos, anchor);
3143
}
3244

3345
public long epochNanos() {
3446
return epochNanos;
3547
}
3648

49+
/** The clock that projected this instant, or null if it was read or stated directly. */
50+
public @Nullable AnchoredClock anchor() {
51+
return anchor;
52+
}
53+
54+
/**
55+
* Equality is by instant. The anchor records how the instant was obtained, not what it denotes,
56+
* so two readings of the same moment are equal whether or not they were projected.
57+
*/
3758
@Override
3859
public boolean equals(final @Nullable Object other) {
3960
if (this == other) {

0 commit comments

Comments
 (0)