Skip to content

fix: use System.nanoTime() for cron check-in duration measurement#5611

Merged
adinauer merged 2 commits into
getsentry:mainfrom
tsushanth:fix/cron-checkin-monotonic-duration
Jun 24, 2026
Merged

fix: use System.nanoTime() for cron check-in duration measurement#5611
adinauer merged 2 commits into
getsentry:mainfrom
tsushanth:fix/cron-checkin-monotonic-duration

Conversation

@tsushanth

Copy link
Copy Markdown
Contributor

Fixes #5579

Problem

Cron check-in durations were computed by subtracting two System.currentTimeMillis() values. The wall clock is not monotonic — it is subject to NTP steps, leap-second smearing, and DST transitions. For long-running cron jobs this gives wide exposure to clock jumps, which can produce incorrect or negative duration values in the check-in payload.

Fix

Switch the start/end capture to System.nanoTime(), which is guaranteed monotonic and is the correct choice for measuring elapsed time. DateUtils.nanosToSeconds() (already present in the SDK) is used to convert the nanosecond delta to the seconds value expected by setDuration().

Changed files:

  • sentry/src/main/java/io/sentry/util/CheckInUtils.java
  • sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java
  • sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java
  • sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckInAdvice.java

Existing duration tests (assertNotNull(doneCheckIn.duration)) continue to pass unchanged; the delta of two nanoTime() calls is always non-negative and non-zero for any real workload

@runningcode runningcode left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution. This looks great!

@romtsn romtsn force-pushed the fix/cron-checkin-monotonic-duration branch from 6a8d264 to 0b1ba59 Compare June 24, 2026 12:57

@adinauer adinauer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for the help!

@adinauer adinauer enabled auto-merge (squash) June 24, 2026 14:01
tsushanth and others added 2 commits June 24, 2026 16:45
System.currentTimeMillis() is a wall-clock value and is subject to
NTP adjustments and DST transitions. For long-running cron jobs this
can produce incorrect or even negative durations in the check-in payload.

Switch the start/end capture in CheckInUtils.withCheckIn() and the three
SentryCheckInAdvice implementations (sentry-spring, sentry-spring-jakarta,
sentry-spring-7) to System.nanoTime(), which is guaranteed monotonic.
Use DateUtils.nanosToSeconds() (already present) to convert the delta.

Fixes getsentry#5579
@runningcode runningcode force-pushed the fix/cron-checkin-monotonic-duration branch from 0b1ba59 to 200b1c8 Compare June 24, 2026 14:46
@adinauer adinauer merged commit 693fc15 into getsentry:main Jun 24, 2026
62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cron check-in durations are measured with the wall clock

4 participants