diff --git a/CHANGELOG.md b/CHANGELOG.md index 36705ed654..b46b50517d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Features + +- Allow passing `environment` to `CheckinUtils.withCheckIn` ([3889](https://github.com/getsentry/sentry-java/pull/3889)) + ## 8.0.0-beta.2 ### Breaking Changes diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index a6553cafb8..ba1fee1b03 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -5919,6 +5919,8 @@ 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/lang/String;Lio/sentry/MonitorConfig;Ljava/util/concurrent/Callable;)Ljava/lang/Object; + public static fun withCheckIn (Ljava/lang/String;Ljava/lang/String;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 3f13064cbb..49846ac139 100644 --- a/sentry/src/main/java/io/sentry/util/CheckInUtils.java +++ b/sentry/src/main/java/io/sentry/util/CheckInUtils.java @@ -22,12 +22,14 @@ public final class CheckInUtils { * * @param monitorSlug - the slug of the monitor * @param monitorConfig - configuration of the monitor, can be used for upserting schedule + * @param environment - the name of the environment * @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 @Nullable String environment, final @Nullable MonitorConfig monitorConfig, final @NotNull Callable callable) throws Exception { @@ -43,6 +45,9 @@ public static U withCheckIn( if (monitorConfig != null) { inProgressCheckIn.setMonitorConfig(monitorConfig); } + if (environment != null) { + inProgressCheckIn.setEnvironment(environment); + } @Nullable SentryId checkInId = scopes.captureCheckIn(inProgressCheckIn); try { return callable.call(); @@ -52,12 +57,49 @@ public static U withCheckIn( } finally { final @NotNull CheckInStatus status = didError ? CheckInStatus.ERROR : CheckInStatus.OK; CheckIn checkIn = new CheckIn(checkInId, monitorSlug, status); + if (environment != null) { + checkIn.setEnvironment(environment); + } checkIn.setDuration(DateUtils.millisToSeconds(System.currentTimeMillis() - startTime)); scopes.captureCheckIn(checkIn); } } } + /** + * 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 @Nullable MonitorConfig monitorConfig, + final @NotNull Callable callable) + throws Exception { + return withCheckIn(monitorSlug, null, monitorConfig, callable); + } + + /** + * Helper method to send monitor check-ins for a {@link Callable} + * + * @param monitorSlug - the slug of the monitor + * @param environment - the name of the environment + * @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 @Nullable String environment, + final @NotNull Callable callable) + throws Exception { + return withCheckIn(monitorSlug, environment, null, callable); + } + /** * Helper method to send monitor check-ins for a {@link Callable} * @@ -68,7 +110,7 @@ public static U withCheckIn( */ public static U withCheckIn( final @NotNull String monitorSlug, final @NotNull Callable callable) throws Exception { - return withCheckIn(monitorSlug, null, callable); + return withCheckIn(monitorSlug, null, null, callable); } /** Checks if a check-in for a monitor (CRON) has been ignored. */ diff --git a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt index 943837d697..53c5dd7de6 100644 --- a/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt @@ -92,6 +92,46 @@ class CheckInUtilsTest { } } + @Test + fun `sends check-in for wrapped supplier with environment`() { + Mockito.mockStatic(Sentry::class.java).use { sentry -> + val scopes = mock() + val lifecycleToken = mock() + sentry.`when` { Sentry.getCurrentScopes() }.thenReturn(scopes) + sentry.`when` { Sentry.forkedScopes(any()) }.then { + scopes.forkedScopes("test") + } + whenever(scopes.forkedScopes(any())).thenReturn(scopes) + whenever(scopes.makeCurrent()).thenReturn(lifecycleToken) + whenever(scopes.options).thenReturn(SentryOptions()) + val returnValue = CheckInUtils.withCheckIn("monitor-1", "environment-1") { + return@withCheckIn "test1" + } + + assertEquals("test1", returnValue) + inOrder(scopes, lifecycleToken) { + verify(scopes).forkedScopes(any()) + verify(scopes).makeCurrent() + verify(scopes).configureScope(any()) + verify(scopes).captureCheckIn( + check { + assertEquals("monitor-1", it.monitorSlug) + assertEquals("environment-1", it.environment) + assertEquals(CheckInStatus.IN_PROGRESS.apiName(), it.status) + } + ) + verify(scopes).captureCheckIn( + check { + assertEquals("monitor-1", it.monitorSlug) + assertEquals("environment-1", it.environment) + assertEquals(CheckInStatus.OK.apiName(), it.status) + } + ) + verify(lifecycleToken).close() + } + } + } + @Test fun `sends check-in for wrapped supplier with exception`() { Mockito.mockStatic(Sentry::class.java).use { sentry ->