Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Preserve Date passed in ctor
  • Loading branch information
romtsn committed Oct 3, 2024
commit e8103492008e0815f1ce234b0b94ac14e405bfc9
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public open class DefaultReplayBreadcrumbConverter : ReplayBreadcrumbConverter {
}
return if (!breadcrumbCategory.isNullOrEmpty()) {
RRWebBreadcrumbEvent().apply {
timestamp = breadcrumb.timestampMs
breadcrumbTimestamp = breadcrumb.timestampMs / 1000.0
timestamp = breadcrumb.timestamp.time
breadcrumbTimestamp = breadcrumb.timestamp.time / 1000.0
breadcrumbType = "default"
category = breadcrumbCategory
message = breadcrumbMessage
Expand All @@ -134,7 +134,7 @@ public open class DefaultReplayBreadcrumbConverter : ReplayBreadcrumbConverter {
val httpStartTimestamp = breadcrumb.data[SpanDataConvention.HTTP_START_TIMESTAMP]
val httpEndTimestamp = breadcrumb.data[SpanDataConvention.HTTP_END_TIMESTAMP]
return RRWebSpanEvent().apply {
timestamp = breadcrumb.timestampMs
timestamp = breadcrumb.timestamp.time
op = "resource.http"
description = breadcrumb.data["url"] as String
// can be double if it was serialized to disk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ internal interface CaptureStrategy {

val urls = LinkedList<String>()
breadcrumbs.forEach { breadcrumb ->
if (breadcrumb.timestampMs >= segmentTimestamp.time &&
breadcrumb.timestampMs < endTimestamp.time
if (breadcrumb.timestamp.time >= segmentTimestamp.time &&
breadcrumb.timestamp.time < endTimestamp.time
) {
val rrwebEvent = options
.replayController
Expand Down
1 change: 0 additions & 1 deletion sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public final class io/sentry/Breadcrumb : io/sentry/JsonSerializable, io/sentry/
public fun getMessage ()Ljava/lang/String;
public fun getOrigin ()Ljava/lang/String;
public fun getTimestamp ()Ljava/util/Date;
public fun getTimestampMs ()J
public fun getType ()Ljava/lang/String;
public fun getUnknown ()Ljava/util/Map;
public static fun graphqlDataFetcher (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lio/sentry/Breadcrumb;
Expand Down
35 changes: 20 additions & 15 deletions sentry/src/main/java/io/sentry/Breadcrumb.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
/** Series of application events */
public final class Breadcrumb implements JsonUnknown, JsonSerializable {

/** A timestamp representing when the breadcrumb occurred. */
private final long timestamp;
/** A timestamp representing when the breadcrumb occurred in milliseconds. */
private @Nullable final Long timestampMs;

/** A timestamp representing when the breadcrumb occurred as java.util.Date. */
private @Nullable Date timestamp;

/** If a message is provided, its rendered as text and the whitespace is preserved. */
private @Nullable String message;
Expand Down Expand Up @@ -53,15 +56,18 @@ public final class Breadcrumb implements JsonUnknown, JsonSerializable {
*/
@SuppressWarnings("JavaUtilDate")
public Breadcrumb(final @NotNull Date timestamp) {
this.timestamp = timestamp.getTime();
this.timestamp = timestamp;
this.timestampMs = null;
}

public Breadcrumb(final long timestamp) {
this.timestamp = timestamp;
this.timestampMs = timestamp;
this.timestamp = null;
}

Breadcrumb(final @NotNull Breadcrumb breadcrumb) {
this.timestamp = breadcrumb.timestamp;
this.timestampMs = breadcrumb.timestampMs;
this.message = breadcrumb.message;
this.type = breadcrumb.type;
this.category = breadcrumb.category;
Expand Down Expand Up @@ -522,22 +528,21 @@ public Breadcrumb(@Nullable String message) {
this.message = message;
}

/**
* Returns the Breadcrumb's timestamp
*
* @return the timestamp
*/
public long getTimestampMs() {
return timestamp;
}

/**
* Returns the Breadcrumb's timestamp as java.util.Date
*
* @return the timestamp
*/
@SuppressWarnings("JavaUtilDate")
public @NotNull Date getTimestamp() {
return DateUtils.getDateTime(timestamp);
if (timestamp != null) {
return (Date) timestamp.clone();
} else if (timestampMs != null) {
// we memoize it here into timestamp to avoid instantiating Calendar again and again
timestamp = DateUtils.getDateTime(timestampMs);
return timestamp;
}
throw new IllegalStateException("No timestamp set for breadcrumb");
}

/**
Expand Down Expand Up @@ -677,7 +682,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Breadcrumb that = (Breadcrumb) o;
return timestamp == that.timestamp
return getTimestamp().getTime() == that.getTimestamp().getTime()
&& Objects.equals(message, that.message)
&& Objects.equals(type, that.type)
&& Objects.equals(category, that.category)
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ private static final class SortBreadcrumbsByDate implements Comparator<Breadcrum
@SuppressWarnings({"JdkObsolete", "JavaUtilDate"})
@Override
public int compare(final @NotNull Breadcrumb b1, final @NotNull Breadcrumb b2) {
return Long.compare(b1.getTimestampMs(), b2.getTimestampMs());
return b1.getTimestamp().compareTo(b2.getTimestamp());
}
}
}
10 changes: 8 additions & 2 deletions sentry/src/test/java/io/sentry/BreadcrumbTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.sentry

import java.util.Date
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNotSame
import kotlin.test.assertNull
import kotlin.test.assertTrue

class BreadcrumbTest {

Expand Down Expand Up @@ -98,7 +98,13 @@ class BreadcrumbTest {
@Test
fun `breadcrumb has timestamp when created`() {
val breadcrumb = Breadcrumb()
assertTrue(breadcrumb.timestampMs > 0)
assertNotNull(breadcrumb.timestamp)
}

@Test
fun `breadcrumb can be created with Date timestamp`() {
val breadcrumb = Breadcrumb(Date(123L))
assertEquals(123L, breadcrumb.timestamp.time)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BreadcrumbSerializationTest {
val actual = Breadcrumb.fromMap(map, SentryOptions())
val expected = fixture.getSut()

assertEquals(expected.timestampMs, actual?.timestampMs)
assertEquals(expected.timestamp, actual?.timestamp)
assertEquals(expected.message, actual?.message)
assertEquals(expected.type, actual?.type)
assertEquals(expected.data, actual?.data)
Expand Down
Loading