Skip to content

Commit

Permalink
Add MonitorConfig param to CheckInUtils.withCheckIn (#3038)
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer authored Nov 16, 2023
1 parent 4e29063 commit 8c11638
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -4325,6 +4325,7 @@ public abstract class io/sentry/transport/TransportResult {
public final class io/sentry/util/CheckInUtils {
public fun <init> ()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;
}

Expand Down
28 changes: 25 additions & 3 deletions sentry/src/main/java/io/sentry/util/CheckInUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,21 +19,29 @@ 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 <U> - the result type of the {@link Callable}
*/
public static <U> U withCheckIn(
final @NotNull String monitorSlug, final @NotNull Callable<U> callable) throws Exception {
final @NotNull String monitorSlug,
final @Nullable MonitorConfig monitorConfig,
final @NotNull Callable<U> callable)
throws Exception {
final @NotNull IHub hub = Sentry.getCurrentHub();
final long startTime = System.currentTimeMillis();
boolean didError = false;

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) {
Expand All @@ -47,6 +56,19 @@ public static <U> 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 <U> - the result type of the {@link Callable}
*/
public static <U> U withCheckIn(
final @NotNull String monitorSlug, final @NotNull Callable<U> 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(
Expand Down
36 changes: 36 additions & 0 deletions sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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<IHub>()
sentry.`when`<Any> { 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()
}
}
}
}

0 comments on commit 8c11638

Please sign in to comment.