From 76a313e43003dfbf8134158eec63148fb087da0d Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 24 Jun 2021 13:21:22 +0200 Subject: [PATCH 1/2] Set mechanism type in SentryExceptionResolver. (#1556) Fixes #1525 --- CHANGELOG.md | 1 + .../main/java/io/sentry/spring/SentryExceptionResolver.java | 3 +++ .../kotlin/io/sentry/spring/SentrySpringIntegrationTest.kt | 1 + 3 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64497b8058..c7cbb61577 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Fix: Handling missing Spring Security on classpath on Java 8 (#1552) * Feat: Support transaction waiting for children to finish. (#1535) * Feat: Capture logged marker in log4j2 and logback appenders (#1551) +* Feat: Set mechanism type in SentryExceptionResolver (#1556) ## 5.1.0-beta.1 diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java b/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java index 46ba6579ae..835ab27330 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentryExceptionResolver.java @@ -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(); @@ -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); diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringIntegrationTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringIntegrationTest.kt index fc7119a25a..0366460680 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringIntegrationTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringIntegrationTest.kt @@ -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()) } } From a2331809bb13b88d593ebe04643714f0314f6518 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Thu, 24 Jun 2021 13:54:58 +0200 Subject: [PATCH 2/2] Fix clock drift issue when calling DateUtils#getDateTimeWithMillisPrecision (#1557) Fixes #1541 --- CHANGELOG.md | 1 + sentry/src/main/java/io/sentry/DateUtils.java | 9 +++---- .../src/test/java/io/sentry/DateUtilsTest.kt | 25 +++++++++++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7cbb61577..8c8c61b009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Fix: Handling missing Spring Security on classpath on Java 8 (#1552) * Feat: Support transaction waiting for children to finish. (#1535) * Feat: Capture logged marker in log4j2 and logback appenders (#1551) +* Fix: Clock drift issue when calling DateUtils#getDateTimeWithMillisPrecision (#1557) * Feat: Set mechanism type in SentryExceptionResolver (#1556) ## 5.1.0-beta.1 diff --git a/sentry/src/main/java/io/sentry/DateUtils.java b/sentry/src/main/java/io/sentry/DateUtils.java index 2da8f16daf..ee41b39c6e 100644 --- a/sentry/src/main/java/io/sentry/DateUtils.java +++ b/sentry/src/main/java/io/sentry/DateUtils.java @@ -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; @@ -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); } diff --git a/sentry/src/test/java/io/sentry/DateUtilsTest.kt b/sentry/src/test/java/io/sentry/DateUtilsTest.kt index cb39cb3628..b1f3f7756d 100644 --- a/sentry/src/test/java/io/sentry/DateUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/DateUtilsTest.kt @@ -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