From 96d0a5c30b8912cdc4e0432f3c480f3a4dd23b71 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 27 Sep 2023 12:07:40 +0200 Subject: [PATCH] Push scope before cron execution and create new propagation context; push scope after --- .../io/sentry/quartz/SentryJobListener.java | 5 +++ .../jakarta/checkin/SentryCheckInAdvice.java | 5 +++ .../spring/jakarta/SentryCheckInAdviceTest.kt | 31 +++++++++++++++++++ .../spring/checkin/SentryCheckInAdvice.java | 5 +++ .../sentry/spring/SentryCheckInAdviceTest.kt | 31 +++++++++++++++++++ 5 files changed, 77 insertions(+) diff --git a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java index 84e499fca2e..28a0e512005 100644 --- a/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java +++ b/sentry-quartz/src/main/java/io/sentry/quartz/SentryJobListener.java @@ -9,6 +9,7 @@ import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.util.Objects; +import io.sentry.util.TracingUtils; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -48,6 +49,8 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) { if (maybeSlug == null) { return; } + hub.pushScope(); + TracingUtils.startNewTrace(hub); final @NotNull String slug = maybeSlug; final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS); final @NotNull SentryId checkInId = hub.captureCheckIn(checkIn); @@ -97,6 +100,8 @@ public void jobWasExecuted(JobExecutionContext context, JobExecutionException jo hub.getOptions() .getLogger() .log(SentryLevel.ERROR, "Unable to capture check-in in jobWasExecuted.", t); + } finally { + hub.popScope(); } } } diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java index c7805bf6071..fca5200575c 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java @@ -8,6 +8,7 @@ import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.util.Objects; +import io.sentry.util.TracingUtils; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; @@ -56,6 +57,9 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } + hub.pushScope(); + TracingUtils.startNewTrace(hub); + @Nullable SentryId checkInId = null; final long startTime = System.currentTimeMillis(); boolean didError = false; @@ -73,6 +77,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); hub.captureCheckIn(checkIn); + hub.popScope(); } } } diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt index 06f1da81a13..e15254398a7 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt @@ -10,8 +10,14 @@ import io.sentry.spring.jakarta.checkin.SentryCheckInAdviceConfiguration import io.sentry.spring.jakarta.checkin.SentryCheckInPointcutConfiguration import org.junit.jupiter.api.assertThrows import org.junit.runner.RunWith +import org.mockito.kotlin.any import org.mockito.kotlin.argumentCaptor +import org.mockito.kotlin.inOrder import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.reset +import org.mockito.kotlin.times +import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Bean @@ -44,6 +50,7 @@ class SentryCheckInAdviceTest { @BeforeTest fun setup() { + reset(hub) whenever(hub.options).thenReturn(SentryOptions()) } @@ -63,6 +70,11 @@ class SentryCheckInAdviceTest { val doneCheckIn = checkInCaptor.lastValue assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub, times(2)).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -82,6 +94,11 @@ class SentryCheckInAdviceTest { val doneCheckIn = checkInCaptor.lastValue assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub, times(2)).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -97,6 +114,11 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_2", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -113,6 +135,11 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_2e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -123,6 +150,10 @@ class SentryCheckInAdviceTest { val result = sampleServiceNoSlug.hello() assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) + + verify(hub, never()).pushScope() + verify(hub, never()).captureCheckIn(any()) + verify(hub, never()).popScope() } @Configuration diff --git a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java index 1f66ebae284..e452cc35003 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java +++ b/sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java @@ -8,6 +8,7 @@ import io.sentry.SentryLevel; import io.sentry.protocol.SentryId; import io.sentry.util.Objects; +import io.sentry.util.TracingUtils; import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; @@ -56,6 +57,9 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl return invocation.proceed(); } + hub.pushScope(); + TracingUtils.startNewTrace(hub); + @Nullable SentryId checkInId = null; final long startTime = System.currentTimeMillis(); boolean didError = false; @@ -73,6 +77,7 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); hub.captureCheckIn(checkIn); + hub.popScope(); } } } diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt index d71ed0d61bb..a31453b0ed1 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentryCheckInAdviceTest.kt @@ -10,8 +10,14 @@ import io.sentry.spring.checkin.SentryCheckInAdviceConfiguration import io.sentry.spring.checkin.SentryCheckInPointcutConfiguration import org.junit.jupiter.api.assertThrows import org.junit.runner.RunWith +import org.mockito.kotlin.any import org.mockito.kotlin.argumentCaptor +import org.mockito.kotlin.inOrder import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.reset +import org.mockito.kotlin.times +import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Bean @@ -44,6 +50,7 @@ class SentryCheckInAdviceTest { @BeforeTest fun setup() { + reset(hub) whenever(hub.options).thenReturn(SentryOptions()) } @@ -63,6 +70,11 @@ class SentryCheckInAdviceTest { val doneCheckIn = checkInCaptor.lastValue assertEquals("monitor_slug_1", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub, times(2)).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -82,6 +94,11 @@ class SentryCheckInAdviceTest { val doneCheckIn = checkInCaptor.lastValue assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub, times(2)).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -97,6 +114,11 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_2", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -113,6 +135,11 @@ class SentryCheckInAdviceTest { assertEquals("monitor_slug_2e", doneCheckIn.monitorSlug) assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status) assertNotNull(doneCheckIn.duration) + + val order = inOrder(hub) + order.verify(hub).pushScope() + order.verify(hub).captureCheckIn(any()) + order.verify(hub).popScope() } @Test @@ -123,6 +150,10 @@ class SentryCheckInAdviceTest { val result = sampleServiceNoSlug.hello() assertEquals(1, result) assertEquals(0, checkInCaptor.allValues.size) + + verify(hub, never()).pushScope() + verify(hub, never()).captureCheckIn(any()) + verify(hub, never()).popScope() } @Configuration