Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SentryCheckIn annotation and advice config for Spring #2946

Merged
merged 28 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3b2debe
checkin
adinauer Sep 11, 2023
1de0ba7
Add trace context, release and env; factory methods; fix serializatio…
adinauer Sep 12, 2023
3dd4fce
add tests
adinauer Sep 13, 2023
41cd16d
remove serial version uid
adinauer Sep 13, 2023
97b8e63
Merge branch 'main' into feat/crons
adinauer Sep 13, 2023
083e021
add back serial version uid
adinauer Sep 13, 2023
4828fa3
quartz support for spring jakarta
adinauer Sep 15, 2023
ef54e76
changelog
adinauer Sep 15, 2023
cc23d3c
code review changes; add sample code to spring scheduled job
adinauer Sep 15, 2023
6038dc5
Merge branch 'feat/crons' into feat/crons-quartz
adinauer Sep 15, 2023
44a3c75
add checkin example to scheduled job
adinauer Sep 15, 2023
b558570
Merge branch 'feat/crons' into feat/crons-quartz
adinauer Sep 15, 2023
0c29f12
Move quartz job listener into a separate module
adinauer Sep 18, 2023
24215e5
add quartz auto config to spring boot 2
adinauer Sep 19, 2023
68931af
Merge branch 'main' into feat/crons-quartz
adinauer Sep 19, 2023
a709a40
fix comment
adinauer Sep 19, 2023
372a2f0
Add enableAutomaticCheckIns and ignoreCheckIns options
adinauer Sep 20, 2023
66cb754
move __ slug logic
adinauer Sep 20, 2023
f4fb1dd
fix sample by adding quartz lib
adinauer Sep 20, 2023
2e7fb74
Merge branch 'feat/crons-quartz' into feat/crons-options
adinauer Sep 20, 2023
86e5ded
changelog
adinauer Sep 20, 2023
409827e
mark crons features experimental
adinauer Sep 20, 2023
49adb9e
SentryCheckIn annotation and advice config for jakarta
adinauer Sep 21, 2023
e522b75
SentryCheckIn annotation and advice config for non jakarta
adinauer Sep 21, 2023
8b721cb
Merge branch 'feat/crons-lib-support' into feat/crons-spring-scheduled
adinauer Sep 25, 2023
b5e1646
changelog
adinauer Sep 25, 2023
e37edb4
code review changes
adinauer Sep 27, 2023
8179966
Apply suggestions from code review
adinauer Sep 27, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/** Sends a {@link io.sentry.CheckIn} for the annotated method. */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Target({ElementType.METHOD})
@ApiStatus.Experimental
public @interface SentryCheckIn {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package io.sentry.spring.jakarta

import io.sentry.CheckIn
import io.sentry.CheckInStatus
import io.sentry.IHub
import io.sentry.SentryOptions
import io.sentry.protocol.SentryId
import io.sentry.spring.jakarta.checkin.SentryCheckIn
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.argumentCaptor
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.EnableAspectJAutoProxy
import org.springframework.context.annotation.Import
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
import org.springframework.test.context.junit4.SpringRunner
import kotlin.RuntimeException
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@RunWith(SpringRunner::class)
@SpringJUnitConfig(SentryCheckInAdviceTest.Config::class)
class SentryCheckInAdviceTest {

@Autowired
lateinit var sampleService: SampleService

@Autowired
lateinit var sampleServiceNoSlug: SampleServiceNoSlug

@Autowired
lateinit var sampleServiceHeartbeat: SampleServiceHeartbeat

@Autowired
lateinit var hub: IHub

@BeforeTest
fun setup() {
whenever(hub.options).thenReturn(SentryOptions())
}

@Test
fun `when class is annotated with @SentryCheckIn, every method call creates two check-ins`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
val result = sampleService.hello()
assertEquals(1, result)
assertEquals(2, checkInCaptor.allValues.size)

val inProgressCheckIn = checkInCaptor.firstValue
assertEquals("monitor_slug_1", inProgressCheckIn.monitorSlug)
assertEquals(CheckInStatus.IN_PROGRESS.apiName(), inProgressCheckIn.status)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_1", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
}

@Test
fun `when class is annotated with @SentryCheckIn, every method call creates two check-ins error`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
assertThrows<RuntimeException> {
sampleService.oops()
}
assertEquals(2, checkInCaptor.allValues.size)

val inProgressCheckIn = checkInCaptor.firstValue
assertEquals("monitor_slug_1e", inProgressCheckIn.monitorSlug)
assertEquals(CheckInStatus.IN_PROGRESS.apiName(), inProgressCheckIn.status)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
}

@Test
fun `when class is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
val result = sampleServiceHeartbeat.hello()
assertEquals(1, result)
assertEquals(1, checkInCaptor.allValues.size)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_2", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
assertNotNull(doneCheckIn.duration)
}

@Test
fun `when class is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end with error`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
assertThrows<RuntimeException> {
sampleServiceHeartbeat.oops()
}
assertEquals(1, checkInCaptor.allValues.size)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_2e", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
assertNotNull(doneCheckIn.duration)
}

@Test
fun `when class is annotated with @SentryCheckIn but slug is missing, does not create check-in`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
val result = sampleServiceNoSlug.hello()
assertEquals(1, result)
assertEquals(0, checkInCaptor.allValues.size)
}

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Import(SentryCheckInAdviceConfiguration::class, SentryCheckInPointcutConfiguration::class)
open class Config {

@Bean
open fun sampleService() = SampleService()

@Bean
open fun sampleServiceNoSlug() = SampleServiceNoSlug()

@Bean
open fun sampleServiceHeartbeat() = SampleServiceHeartbeat()

@Bean
open fun hub() = mock<IHub>()
}

open class SampleService {

@SentryCheckIn("monitor_slug_1")
open fun hello() = 1

@SentryCheckIn("monitor_slug_1e")
open fun oops() {
throw RuntimeException("thrown on purpose")
}
}

open class SampleServiceNoSlug {

@SentryCheckIn
open fun hello() = 1
}

open class SampleServiceHeartbeat {

@SentryCheckIn(monitorSlug = "monitor_slug_2", heartbeat = true)
open fun hello() = 1

@SentryCheckIn(monitorSlug = "monitor_slug_2e", heartbeat = true)
open fun oops() {
throw RuntimeException("thrown on purpose")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/** Sends a {@link io.sentry.CheckIn} for the annotated method. */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Target({ElementType.METHOD})
@ApiStatus.Experimental
public @interface SentryCheckIn {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package io.sentry.spring

import io.sentry.CheckIn
import io.sentry.CheckInStatus
import io.sentry.IHub
import io.sentry.SentryOptions
import io.sentry.protocol.SentryId
import io.sentry.spring.checkin.SentryCheckIn
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.argumentCaptor
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.EnableAspectJAutoProxy
import org.springframework.context.annotation.Import
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
import org.springframework.test.context.junit4.SpringRunner
import kotlin.RuntimeException
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

@RunWith(SpringRunner::class)
@SpringJUnitConfig(SentryCheckInAdviceTest.Config::class)
class SentryCheckInAdviceTest {

@Autowired
lateinit var sampleService: SampleService

@Autowired
lateinit var sampleServiceNoSlug: SampleServiceNoSlug

@Autowired
lateinit var sampleServiceHeartbeat: SampleServiceHeartbeat

@Autowired
lateinit var hub: IHub

@BeforeTest
fun setup() {
whenever(hub.options).thenReturn(SentryOptions())
}

@Test
fun `when class is annotated with @SentryCheckIn, every method call creates two check-ins`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
val result = sampleService.hello()
assertEquals(1, result)
assertEquals(2, checkInCaptor.allValues.size)

val inProgressCheckIn = checkInCaptor.firstValue
assertEquals("monitor_slug_1", inProgressCheckIn.monitorSlug)
assertEquals(CheckInStatus.IN_PROGRESS.apiName(), inProgressCheckIn.status)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_1", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
}

@Test
fun `when class is annotated with @SentryCheckIn, every method call creates two check-ins error`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
assertThrows<RuntimeException> {
sampleService.oops()
}
assertEquals(2, checkInCaptor.allValues.size)

val inProgressCheckIn = checkInCaptor.firstValue
assertEquals("monitor_slug_1e", inProgressCheckIn.monitorSlug)
assertEquals(CheckInStatus.IN_PROGRESS.apiName(), inProgressCheckIn.status)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_1e", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
}

@Test
fun `when class is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
val result = sampleServiceHeartbeat.hello()
assertEquals(1, result)
assertEquals(1, checkInCaptor.allValues.size)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_2", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.OK.apiName(), doneCheckIn.status)
assertNotNull(doneCheckIn.duration)
}

@Test
fun `when class is annotated with @SentryCheckIn and heartbeat only, every method call creates only one check-in at the end with error`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
assertThrows<RuntimeException> {
sampleServiceHeartbeat.oops()
}
assertEquals(1, checkInCaptor.allValues.size)

val doneCheckIn = checkInCaptor.lastValue
assertEquals("monitor_slug_2e", doneCheckIn.monitorSlug)
assertEquals(CheckInStatus.ERROR.apiName(), doneCheckIn.status)
assertNotNull(doneCheckIn.duration)
}

@Test
fun `when class is annotated with @SentryCheckIn but slug is missing, does not create check-in`() {
adinauer marked this conversation as resolved.
Show resolved Hide resolved
val checkInId = SentryId()
val checkInCaptor = argumentCaptor<CheckIn>()
whenever(hub.captureCheckIn(checkInCaptor.capture())).thenReturn(checkInId)
val result = sampleServiceNoSlug.hello()
assertEquals(1, result)
assertEquals(0, checkInCaptor.allValues.size)
}

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Import(SentryCheckInAdviceConfiguration::class, SentryCheckInPointcutConfiguration::class)
open class Config {

@Bean
open fun sampleService() = SampleService()

@Bean
open fun sampleServiceNoSlug() = SampleServiceNoSlug()

@Bean
open fun sampleServiceHeartbeat() = SampleServiceHeartbeat()

@Bean
open fun hub() = mock<IHub>()
}

open class SampleService {

@SentryCheckIn("monitor_slug_1")
open fun hello() = 1

@SentryCheckIn("monitor_slug_1e")
open fun oops() {
throw RuntimeException("thrown on purpose")
}
}

open class SampleServiceNoSlug {

@SentryCheckIn
open fun hello() = 1
}

open class SampleServiceHeartbeat {

@SentryCheckIn(monitorSlug = "monitor_slug_2", heartbeat = true)
open fun hello() = 1

@SentryCheckIn(monitorSlug = "monitor_slug_2e", heartbeat = true)
open fun oops() {
throw RuntimeException("thrown on purpose")
}
}
}