diff --git a/CHANGELOG.md b/CHANGELOG.md index 54d8e895bb..3d17169c16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ ### Features - Add current activity name to app context ([#2999](https://github.com/getsentry/sentry-java/pull/2999)) +- Add `MonitorConfig` param to `CheckInUtils.withCheckIn` ([#3038](https://github.com/getsentry/sentry-java/pull/3038)) + - This makes it easier to automatically create or update (upsert) monitors. ## 6.33.1 diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 4272f044b4..d82c53853b 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -4325,6 +4325,7 @@ public abstract class io/sentry/transport/TransportResult { public final class io/sentry/util/CheckInUtils { public fun ()V public static fun isIgnored (Ljava/util/List;Ljava/lang/String;)Z + public static fun withCheckIn (Ljava/lang/String;Lio/sentry/MonitorConfig;Ljava/util/concurrent/Callable;)Ljava/lang/Object; public static fun withCheckIn (Ljava/lang/String;Ljava/util/concurrent/Callable;)Ljava/lang/Object; } diff --git a/sentry/src/main/java/io/sentry/util/CheckInUtils.java b/sentry/src/main/java/io/sentry/util/CheckInUtils.java index 9d4446226e..e15603adaf 100644 --- a/sentry/src/main/java/io/sentry/util/CheckInUtils.java +++ b/sentry/src/main/java/io/sentry/util/CheckInUtils.java @@ -4,6 +4,7 @@ import io.sentry.CheckInStatus; import io.sentry.DateUtils; import io.sentry.IHub; +import io.sentry.MonitorConfig; import io.sentry.Sentry; import io.sentry.protocol.SentryId; import java.util.List; @@ -18,12 +19,17 @@ public final class CheckInUtils { /** * Helper method to send monitor check-ins for a {@link Callable} * + * @param monitorSlug - the slug of the monitor + * @param monitorConfig - configuration of the monitor, can be used for upserting schedule * @param callable - the {@link Callable} to be called * @return the return value of the {@link Callable} * @param - the result type of the {@link Callable} */ public static U withCheckIn( - final @NotNull String monitorSlug, final @NotNull Callable callable) throws Exception { + final @NotNull String monitorSlug, + final @Nullable MonitorConfig monitorConfig, + final @NotNull Callable callable) + throws Exception { final @NotNull IHub hub = Sentry.getCurrentHub(); final long startTime = System.currentTimeMillis(); boolean didError = false; @@ -31,8 +37,11 @@ public static U withCheckIn( hub.pushScope(); TracingUtils.startNewTrace(hub); - @Nullable - SentryId checkInId = hub.captureCheckIn(new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS)); + CheckIn inProgressCheckIn = new CheckIn(monitorSlug, CheckInStatus.IN_PROGRESS); + if (monitorConfig != null) { + inProgressCheckIn.setMonitorConfig(monitorConfig); + } + @Nullable SentryId checkInId = hub.captureCheckIn(inProgressCheckIn); try { return callable.call(); } catch (Throwable t) { @@ -47,6 +56,19 @@ public static U withCheckIn( } } + /** + * Helper method to send monitor check-ins for a {@link Callable} + * + * @param monitorSlug - the slug of the monitor + * @param callable - the {@link Callable} to be called + * @return the return value of the {@link Callable} + * @param - the result type of the {@link Callable} + */ + public static U withCheckIn( + final @NotNull String monitorSlug, final @NotNull Callable callable) throws Exception { + return withCheckIn(monitorSlug, null, callable); + } + /** Checks if a check-in for a monitor (CRON) has been ignored. */ @ApiStatus.Internal public static boolean isIgnored( diff --git a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt index ccff57821a..fdc9b0151e 100644 --- a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt @@ -2,6 +2,9 @@ package io.sentry.util import io.sentry.CheckInStatus import io.sentry.IHub +import io.sentry.MonitorConfig +import io.sentry.MonitorSchedule +import io.sentry.MonitorScheduleUnit import io.sentry.Sentry import org.mockito.Mockito import org.mockito.kotlin.any @@ -13,6 +16,7 @@ import java.lang.RuntimeException import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse +import kotlin.test.assertSame import kotlin.test.assertTrue class CheckInUtilsTest { @@ -111,4 +115,36 @@ class CheckInUtilsTest { } } } + + @Test + fun `sends check-in for wrapped supplier with upsert`() { + Mockito.mockStatic(Sentry::class.java).use { sentry -> + val hub = mock() + sentry.`when` { Sentry.getCurrentHub() }.thenReturn(hub) + val monitorConfig = MonitorConfig(MonitorSchedule.interval(7, MonitorScheduleUnit.DAY)) + val returnValue = CheckInUtils.withCheckIn("monitor-1", monitorConfig) { + "test1" + } + + assertEquals("test1", returnValue) + inOrder(hub) { + verify(hub).pushScope() + verify(hub).configureScope(any()) + verify(hub).captureCheckIn( + check { + assertEquals("monitor-1", it.monitorSlug) + assertSame(monitorConfig, it.monitorConfig) + assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) + } + ) + verify(hub).captureCheckIn( + check { + assertEquals("monitor-1", it.monitorSlug) + assertEquals(CheckInStatus.OK.apiName(), it.status) + } + ) + verify(hub).popScope() + } + } + } }