From ef1c11ba48022070626118dbf85e4a73fadc48a9 Mon Sep 17 00:00:00 2001 From: Stefano Date: Wed, 8 May 2024 12:39:14 +0200 Subject: [PATCH] reverted app start span timestamp to SentryLongDate (#3410) * reverted TimeSpan.getStartTimestamp to SentryLongDate, to not break hybrid sdks * fixed SpanFrameMetricsCollector.realNanos (it now checks the date type and reacts accordingly) --- .../core/SpanFrameMetricsCollector.java | 15 ++++++++++--- .../android/core/performance/TimeSpan.java | 4 +--- .../core/SpanFrameMetricsCollectorTest.kt | 21 ++++++++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SpanFrameMetricsCollector.java b/sentry-android-core/src/main/java/io/sentry/android/core/SpanFrameMetricsCollector.java index e32351936b..d9ed1e5aa0 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SpanFrameMetricsCollector.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SpanFrameMetricsCollector.java @@ -1,11 +1,13 @@ package io.sentry.android.core; +import io.sentry.DateUtils; import io.sentry.IPerformanceContinuousCollector; import io.sentry.ISpan; import io.sentry.ITransaction; import io.sentry.NoOpSpan; import io.sentry.NoOpTransaction; import io.sentry.SentryDate; +import io.sentry.SentryLongDate; import io.sentry.SentryNanotimeDate; import io.sentry.SentryTracer; import io.sentry.SpanDataConvention; @@ -143,8 +145,6 @@ private void captureFrameMetrics(@NotNull final ISpan span) { if (spanFinishDate == null) { return; } - // Note: The comparison between two values obtained by realNanos() works only if both are the - // same kind of dates (both are SentryNanotimeDate or both SentryLongDate) final long spanEndNanos = realNanos(spanFinishDate); final @NotNull SentryFrameMetrics frameMetrics = new SentryFrameMetrics(); @@ -308,7 +308,16 @@ private static int addPendingFrameDelay( * @return a timestamp in nano precision */ private static long realNanos(final @NotNull SentryDate date) { - return date.diff(UNIX_START_DATE); + // SentryNanotimeDate nanotime is based on System.nanotime(), like UNIX_START_DATE + if (date instanceof SentryNanotimeDate) { + return date.diff(UNIX_START_DATE); + } + + // SentryLongDate nanotime is based on current date converted to nanoseconds, which is a + // different order than frames based System.nanotime(). So we have to convert the nanotime of + // the SentryLongDate to a System.nanotime() compatible one. + return date.diff(new SentryLongDate(DateUtils.millisToNanos(System.currentTimeMillis()))) + + System.nanoTime(); } private static class Frame implements Comparable { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/performance/TimeSpan.java b/sentry-android-core/src/main/java/io/sentry/android/core/performance/TimeSpan.java index ff5cfe8649..dac78920f8 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/performance/TimeSpan.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/performance/TimeSpan.java @@ -4,7 +4,6 @@ import io.sentry.DateUtils; import io.sentry.SentryDate; import io.sentry.SentryLongDate; -import io.sentry.SentryNanotimeDate; import java.util.concurrent.TimeUnit; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -95,8 +94,7 @@ public long getStartTimestampMs() { */ public @Nullable SentryDate getStartTimestamp() { if (hasStarted()) { - return new SentryNanotimeDate( - DateUtils.nanosToDate(DateUtils.millisToNanos(getStartTimestampMs())), startSystemNanos); + return new SentryLongDate(DateUtils.millisToNanos(getStartTimestampMs())); } return null; } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SpanFrameMetricsCollectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SpanFrameMetricsCollectorTest.kt index 5ec5622ac2..d8ff8fde2e 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SpanFrameMetricsCollectorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SpanFrameMetricsCollectorTest.kt @@ -4,7 +4,6 @@ import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.NoOpSpan import io.sentry.NoOpTransaction -import io.sentry.SentryLongDate import io.sentry.SentryNanotimeDate import io.sentry.SpanContext import io.sentry.android.core.internal.util.SentryFrameMetricsCollector @@ -53,10 +52,16 @@ class SpanFrameMetricsCollectorTest { val span = mock() val spanContext = SpanContext("op.fake") whenever(span.spanContext).thenReturn(spanContext) - whenever(span.startDate).thenReturn(SentryLongDate(startTimeStampNanos)) + whenever(span.startDate).thenReturn( + SentryNanotimeDate( + Date(), + startTimeStampNanos + ) + ) whenever(span.finishDate).thenReturn( if (endTimeStampNanos != null) { - SentryLongDate( + SentryNanotimeDate( + Date(), endTimeStampNanos ) } else { @@ -73,10 +78,16 @@ class SpanFrameMetricsCollectorTest { val span = mock() val spanContext = SpanContext("op.fake") whenever(span.spanContext).thenReturn(spanContext) - whenever(span.startDate).thenReturn(SentryLongDate(startTimeStampNanos)) + whenever(span.startDate).thenReturn( + SentryNanotimeDate( + Date(), + startTimeStampNanos + ) + ) whenever(span.finishDate).thenReturn( if (endTimeStampNanos != null) { - SentryLongDate( + SentryNanotimeDate( + Date(), endTimeStampNanos ) } else {