Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ea62aa1
ref(core): Add MonitorConfigUtils to convert schedules to monitor con…
wedamija Oct 2, 2026
28761ae
fix(core): Only send monitor configs Sentry reads the same way
wedamija Oct 2, 2026
bd47026
ref(core): Add MonitorConfigUtils.fromSpringScheduled
wedamija Oct 2, 2026
fca4216
fix(core): Return no config for stepped single value ranges
wedamija Oct 2, 2026
780c605
fix(core): Read Spring crons per parser and skip fixed delays
wedamija Oct 2, 2026
5d745c7
feat(spring): Send monitor config from @Scheduled with @SentryCheckIn
wedamija Oct 5, 2026
44ad1c7
feat(quartz): Send monitor config from the trigger with check-ins
wedamija Oct 5, 2026
bcd5682
feat(spring): Send @Scheduled monitor config by default
wedamija Oct 5, 2026
a82b34c
feat(quartz): Send trigger monitor config by default
wedamija Oct 5, 2026
045b6d8
Merge remote-tracking branch 'origin/danf/spring-checkin-scheduled-co…
wedamija Oct 5, 2026
6f5ad4e
ref(spring): Shorten upsertMonitorConfig Javadoc
wedamija Oct 5, 2026
d758789
fix(core): Keep Sunday-only day of week ranges
wedamija Oct 6, 2026
d903a79
fix(spring): Only catch runtime exceptions when deriving monitor config
wedamija Oct 6, 2026
43e6900
fix(quartz): Catch only expected errors when deriving monitor config
wedamija Oct 6, 2026
5db58a4
ref(spring): Shorten upsertMonitorConfig Javadoc
wedamija Oct 15, 2026
408c89a
Merge remote-tracking branch 'origin/main' into danf/monitor-config-u…
wedamija Oct 29, 2026
4c08ff0
Merge branch 'danf/monitor-config-utils' into danf/spring-checkin-sch…
wedamija Oct 29, 2026
1e7aa24
ref(spring): Use pattern-matching instanceof for the @Scheduled time …
wedamija Oct 29, 2026
0b9b852
fix(core): Use a 2 minute check-in margin for crons with fixed seconds
wedamija Nov 5, 2026
ae5ae63
Merge branch 'danf/monitor-config-utils' into danf/spring-checkin-sch…
wedamija Nov 5, 2026
129cb73
fix(spring): Default upsertMonitorConfig to false and read @Scheduled…
wedamija Nov 5, 2026
f68cff4
Merge branch 'danf/spring-checkin-scheduled-config' into danf/quartz-…
wedamija Nov 5, 2026
0493cce
fix(quartz): Send the trigger's monitor config only when opted in
wedamija Nov 5, 2026
8ae0efd
Merge remote-tracking branch 'origin/main' into danf/quartz-cron-config
wedamija Nov 5, 2026
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Features

- Quartz `SentryJobListener` can send a monitor config derived from the trigger with check-ins, so Sentry creates or updates the monitor from code. Set the `sentry-upsert-monitor-config` job or trigger data key to `"true"` to send the schedule ([#6216](https://github.com/getsentry/sentry-java/pull/6216))
- 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))

## 8.63.0
Expand Down
1 change: 1 addition & 0 deletions sentry-quartz/api/sentry-quartz.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public final class io/sentry/quartz/SentryJobListener : org/quartz/JobListener {
public static final field SENTRY_CHECK_IN_ID_KEY Ljava/lang/String;
public static final field SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY Ljava/lang/String;
public static final field SENTRY_SLUG_KEY Ljava/lang/String;
public static final field SENTRY_UPSERT_MONITOR_CONFIG_KEY Ljava/lang/String;
public fun <init> ()V
public fun <init> (Lio/sentry/IScopes;)V
public fun getName ()Ljava/lang/String;
Expand Down
2 changes: 2 additions & 0 deletions sentry-quartz/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies {
testImplementation(libs.kotlin.test.junit)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.mockito.core)
testImplementation(libs.google.truth)
testImplementation(libs.quartz)
}

tasks.withType<JavaCompile>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,33 @@
import io.sentry.CheckInStatus;
import io.sentry.IScopes;
import io.sentry.ISentryLifecycleToken;
import io.sentry.MonitorConfig;
import io.sentry.ScopesAdapter;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.SentryLevel;
import io.sentry.protocol.SentryId;
import io.sentry.util.LifecycleHelper;
import io.sentry.util.MonitorConfigUtils;
import io.sentry.util.Objects;
import io.sentry.util.TracingUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Pattern;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;

@ApiStatus.Experimental
public final class SentryJobListener implements JobListener {
Expand All @@ -32,6 +45,19 @@ public final class SentryJobListener implements JobListener {
public static final String SENTRY_SLUG_KEY = "sentry-slug";
public static final String SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY = "sentry-scope-lifecycle";

/**
* Job or trigger data key. Set it to {@code "true"} to send the trigger's schedule with
* check-ins, so Sentry creates or updates the monitor.
*/
public static final String SENTRY_UPSERT_MONITOR_CONFIG_KEY = "sentry-upsert-monitor-config";

private static final @NotNull List<String> DAY_NAMES =
Arrays.asList("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT");

// Quartz ignores a step after a name, so SUN/2 is every Sunday
private static final @NotNull Pattern NAME_STEP =
Pattern.compile("(^|,)[A-Za-z]{3}(-[A-Za-z]{3})?/");

private final @NotNull IScopes scopes;

public SentryJobListener() {
Expand All @@ -55,15 +81,18 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) {
if (maybeSlug == null) {
return;
}
final @Nullable MonitorConfig monitorConfig =
shouldUpsertMonitorConfig(context) ? monitorConfigFromTrigger(context) : null;
final @NotNull ISentryLifecycleToken lifecycleToken =
scopes.forkedScopes("SentryJobListener").makeCurrent();
context.put(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY, lifecycleToken);
TracingUtils.startNewTrace(scopes);
final @NotNull String slug = maybeSlug;
final @NotNull CheckIn checkIn = new CheckIn(slug, CheckInStatus.IN_PROGRESS);
checkIn.setMonitorConfig(monitorConfig);
final @NotNull SentryId checkInId = scopes.captureCheckIn(checkIn);
context.put(SENTRY_CHECK_IN_ID_KEY, checkInId);
context.put(SENTRY_SLUG_KEY, slug);
context.put(SENTRY_SCOPE_LIFECYCLE_TOKEN_KEY, lifecycleToken);
} catch (Throwable t) {
scopes
.getOptions()
Expand All @@ -84,6 +113,154 @@ public void jobToBeExecuted(final @NotNull JobExecutionContext context) {
return null;
}

private boolean shouldUpsertMonitorConfig(final @NotNull JobExecutionContext context) {
final @Nullable JobDataMap jobDataMap = context.getMergedJobDataMap();
if (jobDataMap == null) {
return false;
}
final @Nullable Object o = jobDataMap.get(SENTRY_UPSERT_MONITOR_CONFIG_KEY);
return o != null && "true".equalsIgnoreCase(o.toString().trim());
}

private @Nullable MonitorConfig monitorConfigFromTrigger(
final @NotNull JobExecutionContext context) {
try {
final @Nullable Trigger trigger = context.getTrigger();
// Sentry can't express calendar exclusions
if (trigger == null || trigger.getCalendarName() != null) {
return null;
}
// each trigger would overwrite the others' schedule
if (hasSeveralTriggers(context)) {
return null;
}
if (trigger instanceof CronTrigger) {
final @NotNull CronTrigger cronTrigger = (CronTrigger) trigger;
final @Nullable String cron = toSixFieldCron(cronTrigger.getCronExpression());
if (cron == null) {
return null;
}
final @Nullable TimeZone timeZone = cronTrigger.getTimeZone();
return MonitorConfigUtils.fromSchedule(
cron, timeZone == null ? null : timeZone.getID(), null);
}
if (trigger instanceof SimpleTrigger) {
final @NotNull SimpleTrigger simpleTrigger = (SimpleTrigger) trigger;
if (simpleTrigger.getRepeatCount() != SimpleTrigger.REPEAT_INDEFINITELY) {
return null;
}
return MonitorConfigUtils.fromSchedule(null, null, simpleTrigger.getRepeatInterval());
}
return null;
} catch (RuntimeException | SchedulerException | NoSuchMethodError | AbstractMethodError e) {
// Trigger implementations may come from other Quartz versions or third parties
scopes
.getOptions()
.getLogger()
.log(
SentryLevel.WARNING, "Could not derive a monitor config from the Quartz trigger.", e);
return null;
Comment thread
wedamija marked this conversation as resolved.
Resolved
}
}

private static boolean hasSeveralTriggers(final @NotNull JobExecutionContext context)
throws SchedulerException {
final @Nullable Scheduler scheduler = context.getScheduler();
final @Nullable JobDetail jobDetail = context.getJobDetail();
if (scheduler == null || jobDetail == null) {
return false;
}
return scheduler.getTriggersOfJob(jobDetail.getKey()).size() > 1;
}

/**
* Quartz numbers days of week 1-7 from Sunday; crontab uses 0-6. Null if a year is set or a name
* has a step.
*/
static @Nullable String toSixFieldCron(final @Nullable String quartzCron) {
if (quartzCron == null) {
return null;
}
final @NotNull String[] fields = quartzCron.trim().split("\\s+", -1);
if (fields.length == 7 && !"*".equals(fields[6])) {
return null;
}
if (fields.length != 6 && fields.length != 7) {
return null;
}
if (NAME_STEP.matcher(fields[4]).find() || NAME_STEP.matcher(fields[5]).find()) {
return null;
}
final @Nullable String dayOfWeek = toZeroBasedDayOfWeek(fields[5]);
if (dayOfWeek == null) {
return null;
}
final @NotNull StringBuilder cron = new StringBuilder();
for (int i = 0; i < 5; i++) {
cron.append(fields[i]).append(' ');
}
return cron.append(dayOfWeek).toString();
}

private static @Nullable String toZeroBasedDayOfWeek(final @NotNull String field) {
final @NotNull StringBuilder result = new StringBuilder();
for (final @NotNull String item : field.split(",", -1)) {
final @Nullable String converted = toZeroBasedDayOfWeekItem(item.toUpperCase(Locale.ROOT));
if (converted == null) {
return null;
}
if (result.length() > 0) {
result.append(',');
}
result.append(converted);
}
return result.toString();
}

private static @Nullable String toZeroBasedDayOfWeekItem(final @NotNull String item) {
if (item.startsWith("*") || item.startsWith("?")) {
// '*' starts on Sunday in both
return item;
}
final int hash = item.indexOf('#');
if (hash >= 0) {
final @Nullable Integer day = toZeroBasedDay(item.substring(0, hash));
return day == null ? null : day + item.substring(hash);
}
if (item.length() > 1 && item.endsWith("L")) {
final @Nullable Integer day = toZeroBasedDay(item.substring(0, item.length() - 1));
return day == null ? null : day + "L";
}
final int slash = item.indexOf('/');
final @NotNull String days = slash >= 0 ? item.substring(0, slash) : item;
final @NotNull String step = slash >= 0 ? item.substring(slash) : "";
final @NotNull String[] range = days.split("-", -1);
if (range.length > 2) {
return null;
}
final @Nullable Integer start = toZeroBasedDay(range[0]);
if (start == null) {
return null;
}
if (range.length == 2) {
final @Nullable Integer end = toZeroBasedDay(range[1]);
return end == null ? null : start + "-" + end + step;
}
if (slash < 0 || start == 6) {
return String.valueOf(start);
}
// a step from a single day ends on Saturday in Quartz, but on Sunday (7) in crontab
return start + "-6" + step;
}

private static @Nullable Integer toZeroBasedDay(final @NotNull String day) {
if (day.matches("[1-7]")) {
return Integer.parseInt(day) - 1;
}
final int index = DAY_NAMES.indexOf(day);
return index >= 0 ? index : null;
}

@Override
public void jobExecutionVetoed(JobExecutionContext context) {
// do nothing
Expand Down
Loading
Loading