Skip to content

Commit e5fee01

Browse files
authored
Merge 3472209 into a3c77bc
2 parents a3c77bc + 3472209 commit e5fee01

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4320,6 +4320,7 @@ public abstract class io/sentry/transport/TransportResult {
43204320
public final class io/sentry/util/CheckInUtils {
43214321
public fun <init> ()V
43224322
public static fun isIgnored (Ljava/util/List;Ljava/lang/String;)Z
4323+
public static fun withCheckIn (Ljava/lang/String;Lio/sentry/MonitorConfig;Ljava/util/concurrent/Callable;)Ljava/lang/Object;
43234324
public static fun withCheckIn (Ljava/lang/String;Ljava/util/concurrent/Callable;)Ljava/lang/Object;
43244325
}
43254326

sentry/src/main/java/io/sentry/util/CheckInUtils.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.sentry.CheckInStatus;
55
import io.sentry.DateUtils;
66
import io.sentry.IHub;
7+
import io.sentry.MonitorConfig;
78
import io.sentry.Sentry;
89
import io.sentry.protocol.SentryId;
910
import java.util.List;
@@ -18,21 +19,29 @@ public final class CheckInUtils {
1819
/**
1920
* Helper method to send monitor check-ins for a {@link Callable}
2021
*
22+
* @param monitorSlug - the slug of the monitor
23+
* @param monitorConfig - configuration of the monitor, can be used for upserting schedule
2124
* @param callable - the {@link Callable} to be called
2225
* @return the return value of the {@link Callable}
2326
* @param <U> - the result type of the {@link Callable}
2427
*/
2528
public static <U> U withCheckIn(
26-
final @NotNull String monitorSlug, final @NotNull Callable<U> callable) throws Exception {
29+
final @NotNull String monitorSlug,
30+
final @Nullable MonitorConfig monitorConfig,
31+
final @NotNull Callable<U> callable)
32+
throws Exception {
2733
final @NotNull IHub hub = Sentry.getCurrentHub();
2834
final long startTime = System.currentTimeMillis();
2935
boolean didError = false;
3036

3137
hub.pushScope();
3238
TracingUtils.startNewTrace(hub);
3339

34-
@Nullable
35-
SentryId checkInId = hub.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS));
40+
CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS);
41+
if (monitorConfig != null) {
42+
inProgressCheckIn.setMonitorConfig(monitorConfig);
43+
}
44+
@Nullable SentryId checkInId = hub.captureCheckIn(inProgressCheckIn);
3645
try {
3746
return callable.call();
3847
} catch (Throwable t) {
@@ -47,6 +56,19 @@ public static <U> U withCheckIn(
4756
}
4857
}
4958

59+
/**
60+
* Helper method to send monitor check-ins for a {@link Callable}
61+
*
62+
* @param monitorSlug - the slug of the monitor
63+
* @param callable - the {@link Callable} to be called
64+
* @return the return value of the {@link Callable}
65+
* @param <U> - the result type of the {@link Callable}
66+
*/
67+
public static <U> U withCheckIn(
68+
final @NotNull String monitorSlug, final @NotNull Callable<U> callable) throws Exception {
69+
return withCheckIn(monitorSlug, null, callable);
70+
}
71+
5072
/** Checks if a check-in for a monitor (CRON) has been ignored. */
5173
@ApiStatus.Internal
5274
public static boolean isIgnored(

sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package io.sentry.util
22

33
import io.sentry.CheckInStatus
44
import io.sentry.IHub
5+
import io.sentry.MonitorConfig
6+
import io.sentry.MonitorSchedule
7+
import io.sentry.MonitorScheduleUnit
58
import io.sentry.Sentry
69
import org.mockito.Mockito
710
import org.mockito.kotlin.any
@@ -13,6 +16,7 @@ import java.lang.RuntimeException
1316
import kotlin.test.Test
1417
import kotlin.test.assertEquals
1518
import kotlin.test.assertFalse
19+
import kotlin.test.assertSame
1620
import kotlin.test.assertTrue
1721

1822
class CheckInUtilsTest {
@@ -111,4 +115,36 @@ class CheckInUtilsTest {
111115
}
112116
}
113117
}
118+
119+
@Test
120+
fun `sends check-in for wrapped supplier with upser`() {
121+
Mockito.mockStatic(Sentry::class.java).use { sentry ->
122+
val hub = mock<IHub>()
123+
sentry.`when`<Any> { Sentry.getCurrentHub() }.thenReturn(hub)
124+
val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY))
125+
val returnValue = CheckInUtils.withCheckIn("monitor-1", monitorConfig) {
126+
return@withCheckIn "test1"
127+
}
128+
129+
assertEquals("test1", returnValue)
130+
inOrder(hub) {
131+
verify(hub).pushScope()
132+
verify(hub).configureScope(any())
133+
verify(hub).captureCheckIn(
134+
check {
135+
assertEquals("monitor-1", it.monitorSlug)
136+
assertSame(monitorConfig, it.monitorConfig)
137+
assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status)
138+
}
139+
)
140+
verify(hub).captureCheckIn(
141+
check {
142+
assertEquals("monitor-1", it.monitorSlug)
143+
assertEquals(CheckInStatus.OK.apiName(), it.status)
144+
}
145+
)
146+
verify(hub).popScope()
147+
}
148+
}
149+
}
114150
}

0 commit comments

Comments
 (0)