Skip to content

Commit

Permalink
Add new threshold parameters to monitor config (#3181)
Browse files Browse the repository at this point in the history
* add new threshold parameters to monitor config

* add changelog

* remove ctor overload in MonitorConfig, adapt tests
  • Loading branch information
lbloder authored Feb 5, 2024
1 parent f7714d5 commit 5e04ee8
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Add new threshold parameters to monitor config ([#3181](https://github.com/getsentry/sentry-java/pull/3181))

## 7.3.0

### Features
Expand Down
6 changes: 6 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1004,13 +1004,17 @@ public final class io/sentry/MemoryCollectionData {
public final class io/sentry/MonitorConfig : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public fun <init> (Lio/sentry/MonitorSchedule;)V
public fun getCheckinMargin ()Ljava/lang/Long;
public fun getFailureIssueThreshold ()Ljava/lang/Long;
public fun getMaxRuntime ()Ljava/lang/Long;
public fun getRecoveryThreshold ()Ljava/lang/Long;
public fun getSchedule ()Lio/sentry/MonitorSchedule;
public fun getTimezone ()Ljava/lang/String;
public fun getUnknown ()Ljava/util/Map;
public fun serialize (Lio/sentry/ObjectWriter;Lio/sentry/ILogger;)V
public fun setCheckinMargin (Ljava/lang/Long;)V
public fun setFailureIssueThreshold (Ljava/lang/Long;)V
public fun setMaxRuntime (Ljava/lang/Long;)V
public fun setRecoveryThreshold (Ljava/lang/Long;)V
public fun setSchedule (Lio/sentry/MonitorSchedule;)V
public fun setTimezone (Ljava/lang/String;)V
public fun setUnknown (Ljava/util/Map;)V
Expand All @@ -1024,7 +1028,9 @@ public final class io/sentry/MonitorConfig$Deserializer : io/sentry/JsonDeserial

public final class io/sentry/MonitorConfig$JsonKeys {
public static final field CHECKIN_MARGIN Ljava/lang/String;
public static final field FAILURE_ISSUE_THRESHOLD Ljava/lang/String;
public static final field MAX_RUNTIME Ljava/lang/String;
public static final field RECOVERY_THRESHOLD Ljava/lang/String;
public static final field SCHEDULE Ljava/lang/String;
public static final field TIMEZONE Ljava/lang/String;
public fun <init> ()V
Expand Down
37 changes: 36 additions & 1 deletion sentry/src/main/java/io/sentry/MonitorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public final class MonitorConfig implements JsonUnknown, JsonSerializable {
private @Nullable Long checkinMargin;
private @Nullable Long maxRuntime;
private @Nullable String timezone;

private @Nullable Long failureIssueThreshold;
private @Nullable Long recoveryThreshold;
private @Nullable Map<String, Object> unknown;

public MonitorConfig(final @NotNull MonitorSchedule schedule) {
Expand Down Expand Up @@ -54,13 +55,31 @@ public void setTimezone(@Nullable String timezone) {
this.timezone = timezone;
}

public @Nullable Long getFailureIssueThreshold() {
return failureIssueThreshold;
}

public void setFailureIssueThreshold(@Nullable Long failureIssueThreshold) {
this.failureIssueThreshold = failureIssueThreshold;
}

public @Nullable Long getRecoveryThreshold() {
return recoveryThreshold;
}

public void setRecoveryThreshold(@Nullable Long recoveryThreshold) {
this.recoveryThreshold = recoveryThreshold;
}

// JsonKeys

public static final class JsonKeys {
public static final String SCHEDULE = "schedule";
public static final String CHECKIN_MARGIN = "checkin_margin";
public static final String MAX_RUNTIME = "max_runtime";
public static final String TIMEZONE = "timezone";
public static final String FAILURE_ISSUE_THRESHOLD = "failure_issue_threshold";
public static final String RECOVERY_THRESHOLD = "recovery_threshold";
}

// JsonUnknown
Expand Down Expand Up @@ -92,6 +111,12 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger
if (timezone != null) {
writer.name(JsonKeys.TIMEZONE).value(timezone);
}
if (failureIssueThreshold != null) {
writer.name(JsonKeys.FAILURE_ISSUE_THRESHOLD).value(failureIssueThreshold);
}
if (recoveryThreshold != null) {
writer.name(JsonKeys.RECOVERY_THRESHOLD).value(recoveryThreshold);
}
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
Expand All @@ -111,6 +136,8 @@ public static final class Deserializer implements JsonDeserializer<MonitorConfig
Long checkinMargin = null;
Long maxRuntime = null;
String timezone = null;
Long failureIssureThreshold = null;
Long recoveryThreshold = null;
Map<String, Object> unknown = null;

reader.beginObject();
Expand All @@ -129,6 +156,12 @@ public static final class Deserializer implements JsonDeserializer<MonitorConfig
case JsonKeys.TIMEZONE:
timezone = reader.nextStringOrNull();
break;
case JsonKeys.FAILURE_ISSUE_THRESHOLD:
failureIssureThreshold = reader.nextLongOrNull();
break;
case JsonKeys.RECOVERY_THRESHOLD:
recoveryThreshold = reader.nextLongOrNull();
break;
default:
if (unknown == null) {
unknown = new HashMap<>();
Expand All @@ -150,6 +183,8 @@ public static final class Deserializer implements JsonDeserializer<MonitorConfig
monitorConfig.setCheckinMargin(checkinMargin);
monitorConfig.setMaxRuntime(maxRuntime);
monitorConfig.setTimezone(timezone);
monitorConfig.setFailureIssueThreshold(failureIssureThreshold);
monitorConfig.setRecoveryThreshold(recoveryThreshold);
monitorConfig.setUnknown(unknown);
return monitorConfig;
}
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/test/java/io/sentry/CheckInSerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class CheckInSerializationTest {
checkinMargin = 8L
maxRuntime = 9L
timezone = ZoneId.of("Europe/Vienna").id
failureIssueThreshold = 10
recoveryThreshold = 20
}
}
}
Expand Down Expand Up @@ -90,6 +92,8 @@ class CheckInSerializationTest {
assertEquals(expectedConfig.maxRuntime, actualConfig.maxRuntime)
assertEquals(expectedConfig.checkinMargin, actualConfig.checkinMargin)
assertEquals(expectedConfig.timezone, actualConfig.timezone)
assertEquals(expectedConfig.failureIssueThreshold, actualConfig.failureIssueThreshold)
assertEquals(expectedConfig.recoveryThreshold, actualConfig.recoveryThreshold)
assertEquals(expectedSchedule.type, actualSchedule.type)
assertEquals(expectedSchedule.value, actualSchedule.value)
assertEquals(expectedSchedule.unit, actualSchedule.unit)
Expand All @@ -116,6 +120,8 @@ class CheckInSerializationTest {
assertEquals(expectedConfig.maxRuntime, actualConfig.maxRuntime)
assertEquals(expectedConfig.checkinMargin, actualConfig.checkinMargin)
assertEquals(expectedConfig.timezone, actualConfig.timezone)
assertEquals(expectedConfig.failureIssueThreshold, actualConfig.failureIssueThreshold)
assertEquals(expectedConfig.recoveryThreshold, actualConfig.recoveryThreshold)
assertEquals(expectedSchedule.type, actualSchedule.type)
assertEquals(expectedSchedule.value, actualSchedule.value)
assertEquals(expectedSchedule.unit, actualSchedule.unit)
Expand Down
35 changes: 35 additions & 0 deletions sentry/src/test/java/io/sentry/util/CheckInUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,39 @@ class CheckInUtilsTest {
}
}
}

@Test
fun `sends check-in for wrapped supplier with upsert and thresholds`() {
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)).apply {
failureIssueThreshold = 10
recoveryThreshold = 20
}
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()
}
}
}
}
4 changes: 3 additions & 1 deletion sentry/src/test/resources/json/checkin_crontab.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
},
"checkin_margin": 8,
"max_runtime": 9,
"timezone": "Europe/Vienna"
"timezone": "Europe/Vienna",
"failure_issue_threshold": 10,
"recovery_threshold": 20
},
"contexts":
{
Expand Down
4 changes: 3 additions & 1 deletion sentry/src/test/resources/json/checkin_interval.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
},
"checkin_margin": 8,
"max_runtime": 9,
"timezone": "Europe/Vienna"
"timezone": "Europe/Vienna",
"failure_issue_threshold": 10,
"recovery_threshold": 20
},
"contexts":
{
Expand Down

0 comments on commit 5e04ee8

Please sign in to comment.