Skip to content

Commit 129cb73

Browse files
committed
fix(spring): Default upsertMonitorConfig to false and read @scheduled like Spring
Existing @SentryCheckIn users shouldn't have their monitors changed on upgrade, so sending the schedule is opt-in. Find @scheduled with Spring's own lookup so an overriding method doesn't inherit a parent's schedule.
1 parent ae5ae63 commit 129cb73

10 files changed

Lines changed: 157 additions & 64 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Features
66

7-
- Send a monitor config derived from Spring `@Scheduled` with `@SentryCheckIn` check-ins by default, so Sentry creates or updates the monitor from code. Set `@SentryCheckIn(upsertMonitorConfig = false)` to turn this off ([#6215](https://github.com/getsentry/sentry-java/pull/6215))
7+
- Send a monitor config derived from Spring `@Scheduled` with `@SentryCheckIn` check-ins, so Sentry creates or updates the monitor from code. Set `@SentryCheckIn(upsertMonitorConfig = true)` to send the schedule ([#6215](https://github.com/getsentry/sentry-java/pull/6215))
88
- Added `Environment` to the `Scope`, allowing you to overwrite the `Environment` set on the options ([#5772](https://github.com/getsentry/sentry-java/pull/5772))
99
- The scope `Environment` is also applied to the trace envelope header (DSC), logs, metrics and profiles, so all telemetry of a trace reports the same environment
1010
- Add an `attachTracingHeaders` flag to `SentryApolloInterceptor` to stop attaching `sentry-trace` and `baggage` headers to Apollo v2 requests ([#6414](https://github.com/getsentry/sentry-java/pull/6414))

‎sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckIn.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@
3131

3232
/**
3333
* Whether to send the schedule and zone from the method's {@code @Scheduled} with check-ins, so
34-
* Sentry creates or updates the monitor. Set to false to manage the monitor's schedule in Sentry
35-
* instead.
34+
* Sentry creates or updates the monitor.
3635
*
3736
* <p>Heartbeat check-ins never send a monitor config.
3837
*
39-
* @return true to send a monitor config, true by default
38+
* @return true to send a monitor config, false by default
4039
*/
41-
boolean upsertMonitorConfig() default true;
40+
boolean upsertMonitorConfig() default false;
4241

4342
/**
4443
* Monitor slug. If not set, no check-in will be sent.

‎sentry-spring-7/src/main/java/io/sentry/spring7/checkin/SentryCheckInAdvice.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.core.annotation.AnnotatedElementUtils;
3232
import org.springframework.core.annotation.AnnotationUtils;
3333
import org.springframework.scheduling.annotation.Scheduled;
34+
import org.springframework.scheduling.annotation.Schedules;
3435
import org.springframework.util.ClassUtils;
3536
import org.springframework.util.ObjectUtils;
3637
import org.springframework.util.StringValueResolver;
@@ -151,7 +152,8 @@ public SentryCheckInAdvice(final @NotNull IScopes scopes) {
151152
private @Nullable MonitorConfig monitorConfigFromScheduled(final @NotNull Method method) {
152153
try {
153154
final @NotNull Set<Scheduled> schedules =
154-
AnnotatedElementUtils.findMergedRepeatableAnnotations(method, Scheduled.class);
155+
AnnotatedElementUtils.getMergedRepeatableAnnotations(
156+
method, Scheduled.class, Schedules.class);
155157
if (schedules.size() != 1) {
156158
return null;
157159
}

‎sentry-spring-7/src/test/kotlin/io/sentry/spring7/SentryCheckInAdviceTest.kt‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class SentryCheckInAdviceTest {
6767

6868
@Autowired lateinit var sampleServiceScheduled: SampleServiceScheduled
6969

70+
@Autowired lateinit var sampleServiceInheritedSchedule: SampleServiceInheritedSchedule
71+
7072
@Autowired lateinit var scopes: IScopes
7173

7274
val lifecycleToken = mock<ISentryLifecycleToken>()
@@ -370,6 +372,16 @@ class SentryCheckInAdviceTest {
370372
assertNull(inProgressMonitorConfig { sampleServiceScheduled.upsertDisabled() })
371373
}
372374

375+
@Test
376+
fun `upsertMonitorConfig defaults to false`() {
377+
assertNull(inProgressMonitorConfig { sampleServiceScheduled.upsertDefault() })
378+
}
379+
380+
@Test
381+
fun `@Scheduled on an overridden parent method sends no monitor config`() {
382+
assertNull(inProgressMonitorConfig { sampleServiceInheritedSchedule.job() })
383+
}
384+
373385
@Test
374386
fun `heartbeat with @Scheduled sends a single check-in without monitor config`() {
375387
val checkInCaptor = argumentCaptor<CheckIn>()
@@ -405,6 +417,8 @@ class SentryCheckInAdviceTest {
405417

406418
@Bean open fun sampleServiceScheduled() = SampleServiceScheduled()
407419

420+
@Bean open fun sampleServiceInheritedSchedule() = SampleServiceInheritedSchedule()
421+
408422
@Bean
409423
open fun scopes(): IScopes {
410424
val scopes = mock<IScopes>()
@@ -457,57 +471,63 @@ class SentryCheckInAdviceTest {
457471

458472
open class SampleServiceScheduled {
459473

460-
@SentryCheckIn("cron_zone")
474+
@SentryCheckIn("cron_zone", upsertMonitorConfig = true)
461475
@Scheduled(cron = "0 15 10 * * MON-FRI", zone = "Europe/Vienna")
462476
open fun cronWithZone() {}
463477

464-
@SentryCheckIn("cron") @Scheduled(cron = "0 0 2 * * *") open fun cron() {}
478+
@SentryCheckIn("cron", upsertMonitorConfig = true)
479+
@Scheduled(cron = "0 0 2 * * *")
480+
open fun cron() {}
465481

466-
@SentryCheckIn("cron_properties")
482+
@SentryCheckIn("cron_properties", upsertMonitorConfig = true)
467483
@Scheduled(cron = "\${my.cron.schedule}", zone = "\${my.cron.zone}")
468484
open fun cronFromProperties() {}
469485

470-
@SentryCheckIn("fixed_rate_hours")
486+
@SentryCheckIn("fixed_rate_hours", upsertMonitorConfig = true)
471487
@Scheduled(fixedRate = 2, timeUnit = TimeUnit.HOURS)
472488
open fun fixedRateHours() {}
473489

474-
@SentryCheckIn("fixed_rate_iso")
490+
@SentryCheckIn("fixed_rate_iso", upsertMonitorConfig = true)
475491
@Scheduled(fixedRateString = "PT10M")
476492
open fun fixedRateIso() {}
477493

478-
@SentryCheckIn("fixed_rate_simple")
494+
@SentryCheckIn("fixed_rate_simple", upsertMonitorConfig = true)
479495
@Scheduled(fixedRateString = "5m")
480496
open fun fixedRateSimpleDuration() {}
481497

482-
@SentryCheckIn("fixed_delay") @Scheduled(fixedDelay = 300_000) open fun fixedDelay() {}
498+
@SentryCheckIn("fixed_delay", upsertMonitorConfig = true)
499+
@Scheduled(fixedDelay = 300_000)
500+
open fun fixedDelay() {}
483501

484-
@SentryCheckIn("fixed_delay_iso")
502+
@SentryCheckIn("fixed_delay_iso", upsertMonitorConfig = true)
485503
@Scheduled(fixedDelayString = "PT10M")
486504
open fun fixedDelayIso() {}
487505

488-
@SentryCheckIn("disabled_cron") @Scheduled(cron = "-") open fun disabledCron() {}
506+
@SentryCheckIn("disabled_cron", upsertMonitorConfig = true)
507+
@Scheduled(cron = "-")
508+
open fun disabledCron() {}
489509

490-
@SentryCheckIn("cron_empty_zone")
510+
@SentryCheckIn("cron_empty_zone", upsertMonitorConfig = true)
491511
@Scheduled(cron = "0 0 3 * * *", zone = "\${my.cron.empty.zone}")
492512
open fun cronWithEmptyZone() {}
493513

494-
@SentryCheckIn("cron_zone_defaults")
514+
@SentryCheckIn("cron_zone_defaults", upsertMonitorConfig = true)
495515
@Scheduled(cron = "0 0 4 * * *", zone = "Europe/Vienna")
496516
open fun cronWithZoneAndDefaults() {}
497517

498-
@SentryCheckIn("schedules_container")
518+
@SentryCheckIn("schedules_container", upsertMonitorConfig = true)
499519
@Schedules(Scheduled(cron = "0 0 1 * * *"), Scheduled(cron = "0 0 13 * * *"))
500520
open fun schedulesContainer() {}
501521

502-
@SentryCheckIn("unresolvable_cron")
522+
@SentryCheckIn("unresolvable_cron", upsertMonitorConfig = true)
503523
@Scheduled(cron = "\${my.cron.missing}")
504524
open fun unresolvableCron() {}
505525

506-
@SentryCheckIn("derivation_throws")
526+
@SentryCheckIn("derivation_throws", upsertMonitorConfig = true)
507527
@Scheduled(cron = "\${my.cron.exception.property}")
508528
open fun derivationThrows() = 1
509529

510-
@SentryCheckIn("multiple")
530+
@SentryCheckIn("multiple", upsertMonitorConfig = true)
511531
@Scheduled(cron = "0 0 1 * * *")
512532
@Scheduled(cron = "0 0 13 * * *")
513533
open fun multipleSchedules() {}
@@ -516,11 +536,21 @@ class SentryCheckInAdviceTest {
516536
@Scheduled(cron = "0 0 1 * * *")
517537
open fun upsertDisabled() {}
518538

519-
@SentryCheckIn("heartbeat", heartbeat = true)
539+
@SentryCheckIn("upsert_default") @Scheduled(cron = "0 0 1 * * *") open fun upsertDefault() {}
540+
541+
@SentryCheckIn("heartbeat", heartbeat = true, upsertMonitorConfig = true)
520542
@Scheduled(cron = "0 0 1 * * *")
521543
open fun heartbeat() {}
522544
}
523545

546+
open class ScheduledParent {
547+
@Scheduled(cron = "0 0 1 * * *") open fun job() {}
548+
}
549+
550+
open class SampleServiceInheritedSchedule : ScheduledParent() {
551+
@SentryCheckIn("inherited", upsertMonitorConfig = true) override fun job() {}
552+
}
553+
524554
class MyPropertyPlaceholderConfigurer : PropertySourcesPlaceholderConfigurer() {
525555

526556
override fun doProcessProperties(

‎sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckIn.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@
3131

3232
/**
3333
* Whether to send the schedule and zone from the method's {@code @Scheduled} with check-ins, so
34-
* Sentry creates or updates the monitor. Set to false to manage the monitor's schedule in Sentry
35-
* instead.
34+
* Sentry creates or updates the monitor.
3635
*
3736
* <p>Heartbeat check-ins never send a monitor config.
3837
*
39-
* @return true to send a monitor config, true by default
38+
* @return true to send a monitor config, false by default
4039
*/
41-
boolean upsertMonitorConfig() default true;
40+
boolean upsertMonitorConfig() default false;
4241

4342
/**
4443
* Monitor slug. If not set, no check-in will be sent.

‎sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/checkin/SentryCheckInAdvice.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.core.annotation.AnnotatedElementUtils;
3232
import org.springframework.core.annotation.AnnotationUtils;
3333
import org.springframework.scheduling.annotation.Scheduled;
34+
import org.springframework.scheduling.annotation.Schedules;
3435
import org.springframework.util.ClassUtils;
3536
import org.springframework.util.ObjectUtils;
3637
import org.springframework.util.StringValueResolver;
@@ -149,7 +150,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl
149150
private @Nullable MonitorConfig monitorConfigFromScheduled(final @NotNull Method method) {
150151
try {
151152
final @NotNull Set<Scheduled> schedules =
152-
AnnotatedElementUtils.findMergedRepeatableAnnotations(method, Scheduled.class);
153+
AnnotatedElementUtils.getMergedRepeatableAnnotations(
154+
method, Scheduled.class, Schedules.class);
153155
if (schedules.size() != 1) {
154156
return null;
155157
}

‎sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentryCheckInAdviceTest.kt‎

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class SentryCheckInAdviceTest {
6767

6868
@Autowired lateinit var sampleServiceScheduled: SampleServiceScheduled
6969

70+
@Autowired lateinit var sampleServiceInheritedSchedule: SampleServiceInheritedSchedule
71+
7072
@Autowired lateinit var scopes: IScopes
7173

7274
val lifecycleToken = mock<ISentryLifecycleToken>()
@@ -370,6 +372,16 @@ class SentryCheckInAdviceTest {
370372
assertNull(inProgressMonitorConfig { sampleServiceScheduled.upsertDisabled() })
371373
}
372374

375+
@Test
376+
fun `upsertMonitorConfig defaults to false`() {
377+
assertNull(inProgressMonitorConfig { sampleServiceScheduled.upsertDefault() })
378+
}
379+
380+
@Test
381+
fun `@Scheduled on an overridden parent method sends no monitor config`() {
382+
assertNull(inProgressMonitorConfig { sampleServiceInheritedSchedule.job() })
383+
}
384+
373385
@Test
374386
fun `heartbeat with @Scheduled sends a single check-in without monitor config`() {
375387
val checkInCaptor = argumentCaptor<CheckIn>()
@@ -405,6 +417,8 @@ class SentryCheckInAdviceTest {
405417

406418
@Bean open fun sampleServiceScheduled() = SampleServiceScheduled()
407419

420+
@Bean open fun sampleServiceInheritedSchedule() = SampleServiceInheritedSchedule()
421+
408422
@Bean
409423
open fun scopes(): IScopes {
410424
val scopes = mock<IScopes>()
@@ -457,57 +471,63 @@ class SentryCheckInAdviceTest {
457471

458472
open class SampleServiceScheduled {
459473

460-
@SentryCheckIn("cron_zone")
474+
@SentryCheckIn("cron_zone", upsertMonitorConfig = true)
461475
@Scheduled(cron = "0 15 10 * * MON-FRI", zone = "Europe/Vienna")
462476
open fun cronWithZone() {}
463477

464-
@SentryCheckIn("cron") @Scheduled(cron = "0 0 2 * * *") open fun cron() {}
478+
@SentryCheckIn("cron", upsertMonitorConfig = true)
479+
@Scheduled(cron = "0 0 2 * * *")
480+
open fun cron() {}
465481

466-
@SentryCheckIn("cron_properties")
482+
@SentryCheckIn("cron_properties", upsertMonitorConfig = true)
467483
@Scheduled(cron = "\${my.cron.schedule}", zone = "\${my.cron.zone}")
468484
open fun cronFromProperties() {}
469485

470-
@SentryCheckIn("fixed_rate_hours")
486+
@SentryCheckIn("fixed_rate_hours", upsertMonitorConfig = true)
471487
@Scheduled(fixedRate = 2, timeUnit = TimeUnit.HOURS)
472488
open fun fixedRateHours() {}
473489

474-
@SentryCheckIn("fixed_rate_iso")
490+
@SentryCheckIn("fixed_rate_iso", upsertMonitorConfig = true)
475491
@Scheduled(fixedRateString = "PT10M")
476492
open fun fixedRateIso() {}
477493

478-
@SentryCheckIn("fixed_rate_simple")
494+
@SentryCheckIn("fixed_rate_simple", upsertMonitorConfig = true)
479495
@Scheduled(fixedRateString = "5m")
480496
open fun fixedRateSimpleDuration() {}
481497

482-
@SentryCheckIn("fixed_delay") @Scheduled(fixedDelay = 300_000) open fun fixedDelay() {}
498+
@SentryCheckIn("fixed_delay", upsertMonitorConfig = true)
499+
@Scheduled(fixedDelay = 300_000)
500+
open fun fixedDelay() {}
483501

484-
@SentryCheckIn("fixed_delay_iso")
502+
@SentryCheckIn("fixed_delay_iso", upsertMonitorConfig = true)
485503
@Scheduled(fixedDelayString = "PT10M")
486504
open fun fixedDelayIso() {}
487505

488-
@SentryCheckIn("disabled_cron") @Scheduled(cron = "-") open fun disabledCron() {}
506+
@SentryCheckIn("disabled_cron", upsertMonitorConfig = true)
507+
@Scheduled(cron = "-")
508+
open fun disabledCron() {}
489509

490-
@SentryCheckIn("cron_empty_zone")
510+
@SentryCheckIn("cron_empty_zone", upsertMonitorConfig = true)
491511
@Scheduled(cron = "0 0 3 * * *", zone = "\${my.cron.empty.zone}")
492512
open fun cronWithEmptyZone() {}
493513

494-
@SentryCheckIn("cron_zone_defaults")
514+
@SentryCheckIn("cron_zone_defaults", upsertMonitorConfig = true)
495515
@Scheduled(cron = "0 0 4 * * *", zone = "Europe/Vienna")
496516
open fun cronWithZoneAndDefaults() {}
497517

498-
@SentryCheckIn("schedules_container")
518+
@SentryCheckIn("schedules_container", upsertMonitorConfig = true)
499519
@Schedules(Scheduled(cron = "0 0 1 * * *"), Scheduled(cron = "0 0 13 * * *"))
500520
open fun schedulesContainer() {}
501521

502-
@SentryCheckIn("unresolvable_cron")
522+
@SentryCheckIn("unresolvable_cron", upsertMonitorConfig = true)
503523
@Scheduled(cron = "\${my.cron.missing}")
504524
open fun unresolvableCron() {}
505525

506-
@SentryCheckIn("derivation_throws")
526+
@SentryCheckIn("derivation_throws", upsertMonitorConfig = true)
507527
@Scheduled(cron = "\${my.cron.exception.property}")
508528
open fun derivationThrows() = 1
509529

510-
@SentryCheckIn("multiple")
530+
@SentryCheckIn("multiple", upsertMonitorConfig = true)
511531
@Scheduled(cron = "0 0 1 * * *")
512532
@Scheduled(cron = "0 0 13 * * *")
513533
open fun multipleSchedules() {}
@@ -516,11 +536,21 @@ class SentryCheckInAdviceTest {
516536
@Scheduled(cron = "0 0 1 * * *")
517537
open fun upsertDisabled() {}
518538

519-
@SentryCheckIn("heartbeat", heartbeat = true)
539+
@SentryCheckIn("upsert_default") @Scheduled(cron = "0 0 1 * * *") open fun upsertDefault() {}
540+
541+
@SentryCheckIn("heartbeat", heartbeat = true, upsertMonitorConfig = true)
520542
@Scheduled(cron = "0 0 1 * * *")
521543
open fun heartbeat() {}
522544
}
523545

546+
open class ScheduledParent {
547+
@Scheduled(cron = "0 0 1 * * *") open fun job() {}
548+
}
549+
550+
open class SampleServiceInheritedSchedule : ScheduledParent() {
551+
@SentryCheckIn("inherited", upsertMonitorConfig = true) override fun job() {}
552+
}
553+
524554
class MyPropertyPlaceholderConfigurer : PropertySourcesPlaceholderConfigurer() {
525555

526556
override fun doProcessProperties(

‎sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckIn.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@
3131

3232
/**
3333
* Whether to send the schedule and zone from the method's {@code @Scheduled} with check-ins, so
34-
* Sentry creates or updates the monitor. Set to false to manage the monitor's schedule in Sentry
35-
* instead.
34+
* Sentry creates or updates the monitor.
3635
*
3736
* <p>Heartbeat check-ins never send a monitor config.
3837
*
39-
* @return true to send a monitor config, true by default
38+
* @return true to send a monitor config, false by default
4039
*/
41-
boolean upsertMonitorConfig() default true;
40+
boolean upsertMonitorConfig() default false;
4241

4342
/**
4443
* Monitor slug. If not set, no check-in will be sent.

‎sentry-spring/src/main/java/io/sentry/spring/checkin/SentryCheckInAdvice.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.core.annotation.AnnotatedElementUtils;
3232
import org.springframework.core.annotation.AnnotationUtils;
3333
import org.springframework.scheduling.annotation.Scheduled;
34+
import org.springframework.scheduling.annotation.Schedules;
3435
import org.springframework.util.ClassUtils;
3536
import org.springframework.util.ObjectUtils;
3637
import org.springframework.util.StringValueResolver;
@@ -152,7 +153,8 @@ public Object invoke(final @NotNull MethodInvocation invocation) throws Throwabl
152153
private @Nullable MonitorConfig monitorConfigFromScheduled(final @NotNull Method method) {
153154
try {
154155
final @NotNull Set<Scheduled> schedules =
155-
AnnotatedElementUtils.findMergedRepeatableAnnotations(method, Scheduled.class);
156+
AnnotatedElementUtils.getMergedRepeatableAnnotations(
157+
method, Scheduled.class, Schedules.class);
156158
if (schedules.size() != 1) {
157159
return null;
158160
}

0 commit comments

Comments
 (0)