mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(cloudwatch): preserve metric alarm state on definition updates (#6464)
This commit is contained in:
1 parent
fe9dce4061
commit
f49376f538
3 files changed
+68
-7
No files matched your search
@@ -408,6 +408,13 @@ Composite alarms support boolean rules over alarm states. Request `AlarmTypes` c
|
||||
action lists are stored but not executed. Action suppression is not implemented and is
|
||||
rejected with `ValidationError`.
|
||||
|
||||
### Metric alarm updates
|
||||
|
||||
Updating a metric alarm preserves its current state, state reason, state timestamp and tags.
|
||||
Tags supplied to `PutMetricAlarm` apply only when creating an alarm; use `TagResource` and
|
||||
`UntagResource` to change existing tags. `SetAlarmState` rejects missing or unknown state
|
||||
values before changing the alarm.
|
||||
|
||||
### SNS alarm actions
|
||||
|
||||
State transitions publish to same-account standard SNS topics in `AlarmActions`, `OKActions`, or
|
||||
|
||||
+10
-7
@@ -382,13 +382,11 @@ public class CloudWatchMetricsService {
|
||||
if (!previous.getClass().equals(alarm.getClass())) {
|
||||
throw new AwsException("ValidationError", "An alarm of another type already has this name", 400);
|
||||
}
|
||||
if (alarm instanceof CompositeAlarm) {
|
||||
alarm.setStateValue(previous.getStateValue());
|
||||
alarm.setStateReason(previous.getStateReason());
|
||||
alarm.setStateReasonData(previous.getStateReasonData());
|
||||
alarm.setStateUpdatedTimestamp(previous.getStateUpdatedTimestamp());
|
||||
alarm.setTags(new HashMap<>(previous.getTags()));
|
||||
}
|
||||
alarm.setStateValue(previous.getStateValue());
|
||||
alarm.setStateReason(previous.getStateReason());
|
||||
alarm.setStateReasonData(previous.getStateReasonData());
|
||||
alarm.setStateUpdatedTimestamp(previous.getStateUpdatedTimestamp());
|
||||
alarm.setTags(new HashMap<>(previous.getTags()));
|
||||
}
|
||||
if (alarm.getAlarmArn() == null) {
|
||||
alarm.setAlarmArn(regionResolver.buildArn("cloudwatch", region, "alarm:" + name));
|
||||
@@ -556,6 +554,11 @@ public class CloudWatchMetricsService {
|
||||
}
|
||||
|
||||
public void setAlarmState(String alarmName, String stateValue, String stateReason, String stateReasonData, String region) {
|
||||
List<String> states = List.of("OK", "ALARM", "INSUFFICIENT_DATA");
|
||||
if (stateValue == null || !states.contains(stateValue)) {
|
||||
throw SmithyValidationErrors.validationError(stateValue == null ? SmithyValidationErrors.notNull("stateValue")
|
||||
: SmithyValidationErrors.notInEnum(stateValue, "stateValue", states));
|
||||
}
|
||||
List<AlarmSnsNotifier.Notification> notifications = List.of();
|
||||
synchronized (this) {
|
||||
String key = region + "::" + alarmName;
|
||||
|
||||
+51
@@ -9,9 +9,14 @@ import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricAlarm;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricDatum;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -31,6 +36,52 @@ class CloudWatchMetricsServiceTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void metricDefinitionUpdatePreservesStateAndTags() {
|
||||
MetricAlarm original = new MetricAlarm();
|
||||
original.setAlarmName("updated");
|
||||
original.setTags(new HashMap<>(Map.of("team", "original")));
|
||||
service.putMetricAlarm(original, REGION);
|
||||
service.setAlarmState("updated", "ALARM", "manual", "{}", REGION);
|
||||
service.tagResource(original.getAlarmArn(), Map.of("owner", "retained"), REGION);
|
||||
service.untagResource(original.getAlarmArn(), List.of("team"), REGION);
|
||||
MetricAlarm replacement = new MetricAlarm();
|
||||
replacement.setAlarmName("updated");
|
||||
replacement.setThreshold(42.0);
|
||||
replacement.setTags(new HashMap<>(Map.of("ignored", "update")));
|
||||
service.putMetricAlarm(replacement, REGION);
|
||||
MetricAlarm result = service.describeAlarms(List.of("updated"), null, REGION).getFirst();
|
||||
assertEquals("ALARM", result.getStateValue());
|
||||
assertEquals("manual", result.getStateReason());
|
||||
assertEquals("{}", result.getStateReasonData());
|
||||
assertEquals(original.getStateUpdatedTimestamp(), result.getStateUpdatedTimestamp());
|
||||
assertEquals(42.0, result.getThreshold());
|
||||
assertEquals(Map.of("owner", "retained"), result.getTags());
|
||||
assertNotSame(original.getTags(), result.getTags());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = {"", "alarm", "UNKNOWN"})
|
||||
void invalidManualStatePreservesAlarm(String value) {
|
||||
MetricAlarm alarm = new MetricAlarm();
|
||||
alarm.setAlarmName("manual");
|
||||
service.putMetricAlarm(alarm, REGION);
|
||||
service.setAlarmState("manual", "OK", "preserved", "{}", REGION);
|
||||
long timestamp = alarm.getStateUpdatedTimestamp();
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.setAlarmState("manual", value, "rejected", "changed", REGION));
|
||||
assertEquals("ValidationError", error.getErrorCode());
|
||||
assertEquals(value == null
|
||||
? "1 validation error detected: Value null at 'stateValue' failed to satisfy constraint: Member must not be null"
|
||||
: "1 validation error detected: Value '" + value + "' at 'stateValue' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: [OK, ALARM, INSUFFICIENT_DATA]", error.getMessage());
|
||||
assertEquals("OK", alarm.getStateValue());
|
||||
assertEquals("preserved", alarm.getStateReason());
|
||||
assertEquals("{}", alarm.getStateReasonData());
|
||||
assertEquals(timestamp, alarm.getStateUpdatedTimestamp());
|
||||
}
|
||||
|
||||
private MetricDatum datum(String name, double value) {
|
||||
MetricDatum d = new MetricDatum();
|
||||
d.setMetricName(name);
|
||||
|
||||
Reference in new issue
Block a user