Skip to content

Commit

Permalink
reverted app start span timestamp to SentryLongDate (#3410)
Browse files Browse the repository at this point in the history
* reverted TimeSpan.getStartTimestamp to SentryLongDate, to not break hybrid sdks
* fixed SpanFrameMetricsCollector.realNanos (it now checks the date type and reacts accordingly)
  • Loading branch information
stefanosiano committed May 8, 2024
1 parent 8d236f4 commit ef1c11b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<Frame> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,10 +52,16 @@ class SpanFrameMetricsCollectorTest {
val span = mock<ISpan>()
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 {
Expand All @@ -73,10 +78,16 @@ class SpanFrameMetricsCollectorTest {
val span = mock<ITransaction>()
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 {
Expand Down

0 comments on commit ef1c11b

Please sign in to comment.