Skip to content

Commit ae5ae63

Browse files
committed
Merge branch 'danf/monitor-config-utils' into danf/spring-checkin-scheduled-config
2 parents 1e7aa24 + 0b9b852 commit ae5ae63

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎sentry/src/main/java/io/sentry/util/MonitorConfigUtils.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
public final class MonitorConfigUtils {
2626
private static final @NotNull String CRON_DISABLED = "-";
2727
private static final long MINUTE_MILLIS = TimeUnit.MINUTES.toMillis(1);
28+
private static final long MIN_CHECKIN_MARGIN_WITH_SECONDS = 2;
2829
private static final @NotNull Map<String, String> CRON_MACROS;
2930
private static final @NotNull Pattern SIMPLE_DURATION =
3031
Pattern.compile("^([+-]?\\d+)([a-zA-Z]{0,2})$");
@@ -91,6 +92,13 @@ private MonitorConfigUtils() {}
9192
return null;
9293
}
9394
final @NotNull MonitorConfig config = new MonitorConfig(MonitorSchedule.crontab(crontab));
95+
// Runs check in up to 59s after the minute Sentry expects, so a 1 minute margin is too tight
96+
if (hasNonZeroSeconds(cron)) {
97+
final @Nullable Long margin = config.getCheckinMargin();
98+
if (margin == null || margin < MIN_CHECKIN_MARGIN_WITH_SECONDS) {
99+
config.setCheckinMargin(MIN_CHECKIN_MARGIN_WITH_SECONDS);
100+
}
101+
}
94102
if (zone != null && !zone.isEmpty()) {
95103
final @Nullable String timezone = toTimezone(zone);
96104
if (timezone == null) {
@@ -123,6 +131,11 @@ private MonitorConfigUtils() {}
123131
return null;
124132
}
125133

134+
private static boolean hasNonZeroSeconds(final @NotNull String cron) {
135+
final @NotNull String seconds = cron.trim().split("\\s+", -1)[0];
136+
return DIGITS.matcher(seconds).matches() && Integer.parseInt(seconds) > 0;
137+
}
138+
126139
/**
127140
* Builds a monitor config from Spring {@code @Scheduled} values with placeholders resolved. Reads
128141
* the cron and a cron without zone the way Spring does.

‎sentry/src/test/java/io/sentry/util/MonitorConfigUtilsTest.kt‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package io.sentry.util
22

33
import com.google.common.truth.Truth.assertThat
4+
import io.sentry.IScopes
5+
import io.sentry.Sentry
6+
import io.sentry.SentryOptions
47
import java.util.TimeZone
58
import java.util.concurrent.TimeUnit
69
import kotlin.test.Test
10+
import org.mockito.Mockito
11+
import org.mockito.kotlin.mock
12+
import org.mockito.kotlin.whenever
713

814
class MonitorConfigUtilsTest {
915

@@ -18,6 +24,50 @@ class MonitorConfigUtilsTest {
1824
assertThat(config.timezone).isNull()
1925
}
2026

27+
@Test
28+
fun `cron at second 0 keeps the default check-in margin`() {
29+
val config = MonitorConfigUtils.fromSchedule("0 * * * * *", null, null)
30+
31+
assertThat(config!!.checkinMargin).isNull()
32+
}
33+
34+
@Test
35+
fun `cron with non-zero seconds gets a 2 minute check-in margin`() {
36+
for (cron in listOf("1 * * * * *", "59 * * * * *", "30 15 10 * * *")) {
37+
assertThat(MonitorConfigUtils.fromSchedule(cron, null, null)!!.checkinMargin).isEqualTo(2L)
38+
}
39+
}
40+
41+
@Test
42+
fun `cron with non-zero seconds raises a smaller default check-in margin`() {
43+
withDefaultCheckinMargin(1L) {
44+
assertThat(MonitorConfigUtils.fromSchedule("59 * * * * *", null, null)!!.checkinMargin)
45+
.isEqualTo(2L)
46+
}
47+
}
48+
49+
@Test
50+
fun `cron with non-zero seconds keeps a larger default check-in margin`() {
51+
withDefaultCheckinMargin(5L) {
52+
assertThat(MonitorConfigUtils.fromSchedule("59 * * * * *", null, null)!!.checkinMargin)
53+
.isEqualTo(5L)
54+
}
55+
}
56+
57+
private fun withDefaultCheckinMargin(margin: Long, block: () -> Unit) {
58+
Mockito.mockStatic(Sentry::class.java).use { sentry ->
59+
val scopes = mock<IScopes>()
60+
sentry.`when`<Any> { Sentry.getCurrentScopes() }.thenReturn(scopes)
61+
whenever(scopes.options)
62+
.thenReturn(
63+
SentryOptions().apply {
64+
cron = SentryOptions.Cron().apply { defaultCheckinMargin = margin }
65+
}
66+
)
67+
block()
68+
}
69+
}
70+
2171
@Test
2272
fun `cron whitespace is normalized`() {
2373
val config = MonitorConfigUtils.fromSchedule(" 30 0 2 * * * ", null, null)

0 commit comments

Comments
 (0)