Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejwalkowiak committed Jun 24, 2021
2 parents 0fc2338 + e7dbbce commit 7a7a529
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Feat: Support transaction waiting for children to finish. (#1535)
* Feat: Capture logged marker in log4j2 and logback appenders (#1551)
* Fix: Add data field to SentrySpan (#1555)
* Fix: Clock drift issue when calling DateUtils#getDateTimeWithMillisPrecision (#1557)
* Feat: Set mechanism type in SentryExceptionResolver (#1556)

## 5.1.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
@Open
public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered {
static final String MECHANISM_TYPE = "HandlerExceptionResolver";

private final @NotNull IHub hub;
private final @NotNull TransactionNameProvider transactionNameProvider =
new TransactionNameProvider();
Expand All @@ -42,6 +44,7 @@ public SentryExceptionResolver(final @NotNull IHub hub, final int order) {

final Mechanism mechanism = new Mechanism();
mechanism.setHandled(false);
mechanism.setType(MECHANISM_TYPE);
final Throwable throwable =
new ExceptionMechanismException(mechanism, ex, Thread.currentThread());
final SentryEvent event = new SentryEvent(throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class SentrySpringIntegrationTest {
assertThat(ex.value).isEqualTo("something went wrong")
assertThat(ex.mechanism).isNotNull()
assertThat(ex.mechanism!!.isHandled).isFalse()
assertThat(ex.mechanism!!.type).isEqualTo(SentryExceptionResolver.MECHANISM_TYPE)
}, anyOrNull())
}
}
Expand Down
9 changes: 4 additions & 5 deletions sentry/src/main/java/io/sentry/DateUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sentry;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -86,11 +88,8 @@ private DateUtils() {}
public static @NotNull Date getDateTimeWithMillisPrecision(final @NotNull String timestamp)
throws IllegalArgumentException {
try {
final String[] times = timestamp.split("\\.", -1);
final long seconds = Long.parseLong(times[0]);
final long millis = times.length > 1 ? Long.parseLong(times[1]) : 0;

return getDateTime((seconds * 1000) + millis);
return getDateTime(
new BigDecimal(timestamp).setScale(3, RoundingMode.DOWN).movePointRight(3).longValue());
} catch (NumberFormatException e) {
throw new IllegalArgumentException("timestamp is not millis format " + timestamp);
}
Expand Down
25 changes: 17 additions & 8 deletions sentry/src/test/java/io/sentry/DateUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,23 @@ class DateUtilsTest {

@Test
fun `Millis timestamp with millis precision, it should be UTC`() {
// Jun 7, 2020 12:38:12 PM UTC
val dateIsoFormat = "1591533492.631"
val actual = DateUtils.getDateTimeWithMillisPrecision(dateIsoFormat)

val utcActual = convertDate(actual)
val timestamp = utcActual.format(isoFormat)

assertEquals("2020-06-07T12:38:12.631Z", timestamp)
val input = listOf(
Pair("1591533492.631", "2020-06-07T12:38:12.631Z"),
Pair("1591533492.63", "2020-06-07T12:38:12.630Z"),
Pair("1591533492.6", "2020-06-07T12:38:12.600Z"),
Pair("1591533492", "2020-06-07T12:38:12.000Z"),
Pair("1591533492.631631", "2020-06-07T12:38:12.631Z"),
Pair("1591533492.999999", "2020-06-07T12:38:12.999Z")
)

input.forEach {
val actual = DateUtils.getDateTimeWithMillisPrecision(it.first)

val utcActual = convertDate(actual)
val timestamp = utcActual.format(isoFormat)

assertEquals(it.second, timestamp)
}
}

@Test
Expand Down

0 comments on commit 7a7a529

Please sign in to comment.