mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
fix(cloudwatch): evaluate composite alarms with CloudWatch's reasons and refusals (#6987)
* fix(cloudwatch): evaluate composite alarms with CloudWatch's reasons and refusals
Composite alarms recorded "AlarmRule evaluated to <state>" with no
StateReasonData, were evaluated only when created, accepted a rule
naming an alarm that does not exist, and let DeleteAlarms remove an
alarm a composite alarm still names. Published CloudWatch captures
show each one differently.
- A change of state records the StateReason CloudWatch writes: "<arn>
was created and its alarm rule evaluates to OK", "<arn> was updated
and its new alarm rule evaluates to ALARM", or "<child arn>
transitioned to ALARM at Monday 14 September, 2026 06:29:58 UTC".
- StateReasonData lists triggeringAlarms: the children whose state
agrees with the new one, most recently changed first. After a child's
change only the children that changed since the composite alarm last
did are listed; the alarm history orders those changes.
- PutCompositeAlarm evaluates an alarm it updates, and its parents
follow the change.
- A rule naming a missing alarm is refused with "Could not save the
composite alarm as alarms [<arn>] in the alarm rule do not exist".
- DeleteAlarms deletes nothing when it names an alarm a composite alarm
references ("Cannot delete <name> as there are composite alarm(s)
depending on it.", or "a,b ... depending on them."), or more than one
composite alarm ("Cannot delete [2] Composite Alarms as only one
composite alarm delete is allowed in single request").
The new service, Query, JSON and SDK tests fail on main.
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
* test(cloudformation): cover a composite alarm over its stack's metric alarm
A stack whose composite alarm names the stack's own metric alarm is
created child first and deleted parent first, so neither the new
refusal of unknown children nor the refusal to delete a referenced
alarm fails it. A stack whose rule names a missing alarm rolls back
with CloudWatch's message; that test fails on main.
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
* fix(cloudwatch): evaluate composite alarms only when a child changes state
Review follow-ups to the composite alarm evaluation change.
- A child's PutMetricAlarm update, or a SetAlarmState to the state it
already has, no longer re-evaluates its parents. CloudWatch documents
that a composite alarm set with SetAlarmState "returns to its actual
state only once any of its children alarms change state"; such a
parent used to flip back with a reason naming an old transition.
- Application Auto Scaling deletes a policy's alarms one at a time, so
when a composite alarm names one of them only that one stays.
- A changed child that no longer resolves leaves its parent as it is
instead of failing the request.
- The docs say that an update closing a cycle is not evaluated, where
CloudWatch evaluates it.
- Tests: the wire and SDK tests ignore missing data so the alarm
evaluator cannot reset the states they set, one pins the reason when
two children change together, and the self-reference and cross-region
assertions, which pinned texts CloudWatch does not use, are gone.
The new manual-state and Application Auto Scaling tests fail without
this change.
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
* fix(cloudwatch): evaluate composite alarms above a cycle
A composite alarm on a cycle stayed pending, which blocked every
composite alarm above it: one whose other child changed state was never
evaluated. Alarms on the cycle are still not evaluated, but they no
longer hold up the ones above them.
Also from the second review: the docs say only a metric alarm child's
update is not a change of state, and that alarms above a cycle are
evaluated; a test covers a rule stored before missing children were
refused, whose child is created later; a test docstring links the
SetAlarmState reference.
The test for an alarm above a cycle fails without this change.
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
* chore(cloudwatch): log a composite evaluation that stops with alarms pending
The third review found one way the evaluation can still stop with
alarms pending: an alarm named like another alarm's ARN makes the cycle
check follow the wrong rule. The debug line removed in the previous
commit is back, so that case is visible again; the name lookup itself
predates this branch. The docs say an alarm above a cycle is evaluated
when one of its children changes state.
No behaviour change.
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
* fix(cloudwatch): count composite StateReasonData in code points
triggeringAlarms stopped adding children once the serialized JSON
passed 4000 UTF-16 units, while the StateReasonData length check
counts Unicode code points, as Smithy's length constraint counts
Unicode scalar values. An emoji is two units but one code point, so
children whose entries still fit were dropped. The cap now counts code
points, shares its constant with SetAlarmState's length check, and the
docs say so.
The new test names fifteen children with emoji and a newest one with a
short name, so that twelve entries come to exactly 4000 code points. It
lists 7 of them before this change and all 12 after; an exclusive cap
would list 11.
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
* test(cloudwatch): keep the composite alarm SDK test on Java 17 APIs
CloudWatchCompositeAlarmTest called List.getFirst(), which Java 21
added. compatibility-tests/sdk-test-java compiles for Java 17, and CI
builds it in maven:3.9-eclipse-temurin-17, so its test-compile failed
and took the sdk-test-java and ec2-containers jobs with it. The three
calls use get(0) now.
A test-compile of sdk-test-java with --release 17 fails on those three
lines before this change and passes after it. No other file of the
branch is outside the root module, which builds for Java 25.
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
---------
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
1 parent
b4c4718c36
commit
6acadc3404
9 files changed
+1011
-72
No files matched your search
+141
@@ -0,0 +1,141 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
|
||||
import software.amazon.awssdk.services.cloudwatch.model.AlarmType;
|
||||
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
|
||||
import software.amazon.awssdk.services.cloudwatch.model.ComparisonOperator;
|
||||
import software.amazon.awssdk.services.cloudwatch.model.CompositeAlarm;
|
||||
import software.amazon.awssdk.services.cloudwatch.model.StateValue;
|
||||
import software.amazon.awssdk.services.cloudwatch.model.Statistic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("CloudWatch - Composite alarm evaluation")
|
||||
class CloudWatchCompositeAlarmTest {
|
||||
|
||||
private CloudWatchClient cloudWatch;
|
||||
private final List<String> composites = new ArrayList<>();
|
||||
private final List<String> metricAlarms = new ArrayList<>();
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
cloudWatch = TestFixtures.cloudWatchClient();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
try {
|
||||
// Parents first: CloudWatch refuses to delete an alarm a composite alarm still references.
|
||||
for (int index = composites.size() - 1; index >= 0; index--) {
|
||||
String name = composites.get(index);
|
||||
cloudWatch.deleteAlarms(request -> request.alarmNames(name));
|
||||
}
|
||||
if (!metricAlarms.isEmpty()) {
|
||||
cloudWatch.deleteAlarms(request -> request.alarmNames(metricAlarms));
|
||||
}
|
||||
} finally {
|
||||
cloudWatch.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Reasons and triggering alarms follow creation, child transitions and updates")
|
||||
void stateReasonsFollowCreationTransitionsAndUpdates() {
|
||||
String a = metricAlarm("sdk-composite-a");
|
||||
String b = metricAlarm("sdk-composite-b");
|
||||
String both = composite("sdk-composite-both", "ALARM(\"" + a + "\") AND ALARM(\"" + b + "\")");
|
||||
|
||||
CompositeAlarm created = describe(both);
|
||||
assertThat(created.stateValue()).isEqualTo(StateValue.OK);
|
||||
assertThat(created.stateReason()).isEqualTo(created.alarmArn() + " was created and its alarm rule evaluates to OK");
|
||||
assertThat(created.stateReasonData()).startsWith("{\"triggeringAlarms\":[{\"arn\":\"")
|
||||
.contains(arn(a), arn(b));
|
||||
|
||||
cloudWatch.setAlarmState(request -> request.alarmName(a).stateValue(StateValue.ALARM).stateReason("test"));
|
||||
assertThat(describe(both).stateReason()).isEqualTo(created.stateReason());
|
||||
cloudWatch.setAlarmState(request -> request.alarmName(b).stateValue(StateValue.ALARM).stateReason("test"));
|
||||
CompositeAlarm transitioned = describe(both);
|
||||
assertThat(transitioned.stateValue()).isEqualTo(StateValue.ALARM);
|
||||
assertThat(transitioned.stateReason()).matches("\\Q" + arn(b) + " transitioned to ALARM at \\E"
|
||||
+ "[A-Z][a-z]+day \\d{2} [A-Z][a-z]+, \\d{4} \\d{2}:\\d{2}:\\d{2} UTC");
|
||||
assertThat(transitioned.stateReasonData()).startsWith("{\"triggeringAlarms\":[{\"arn\":\"" + arn(b) + "\"")
|
||||
.contains(arn(a));
|
||||
|
||||
cloudWatch.putCompositeAlarm(request -> request.alarmName(both).alarmRule("ALARM(" + a + ") AND OK(" + b + ")")
|
||||
.actionsEnabled(false));
|
||||
CompositeAlarm updated = describe(both);
|
||||
assertThat(updated.stateValue()).isEqualTo(StateValue.OK);
|
||||
assertThat(updated.stateReason()).isEqualTo(updated.alarmArn() + " was updated and its new alarm rule evaluates to OK");
|
||||
assertThat(updated.stateReasonData()).matches("\\Q{\"triggeringAlarms\":[{\"arn\":\"" + arn(b)
|
||||
+ "\",\"state\":{\"value\":\"ALARM\",\"timestamp\":\"\\E"
|
||||
+ "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\+0000\\Q\"}}]}\\E");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Unknown children and deleting referenced alarms fail with CloudWatch's messages")
|
||||
void refusalsMatchCloudWatch() {
|
||||
String child = metricAlarm("sdk-composite-child");
|
||||
String parent = composite("sdk-composite-parent", "OK(" + child + ")");
|
||||
String other = composite("sdk-composite-other", "TRUE");
|
||||
|
||||
assertValidationError(() -> cloudWatch.deleteAlarms(request -> request.alarmNames(child)),
|
||||
"Cannot delete " + child + " as there are composite alarm(s) depending on it.");
|
||||
assertValidationError(() -> cloudWatch.deleteAlarms(request -> request.alarmNames(child, parent)),
|
||||
"Cannot delete " + child + " as there are composite alarm(s) depending on it.");
|
||||
assertValidationError(() -> cloudWatch.deleteAlarms(request -> request.alarmNames(parent, other)),
|
||||
"Cannot delete [2] Composite Alarms as only one composite alarm delete is allowed in single request");
|
||||
String missing = TestFixtures.uniqueName("sdk-composite-missing");
|
||||
assertValidationError(() -> cloudWatch.putCompositeAlarm(request -> request
|
||||
.alarmName(TestFixtures.uniqueName("sdk-composite-orphan")).alarmRule("ALARM(" + missing + ")")),
|
||||
"Could not save the composite alarm as alarms [" + arn(missing) + "] in the alarm rule do not exist");
|
||||
assertThat(describe(parent).alarmRule()).isEqualTo("OK(" + child + ")");
|
||||
assertThat(cloudWatch.describeAlarms(request -> request.alarmNames(child)).metricAlarms()).hasSize(1);
|
||||
}
|
||||
|
||||
private void assertValidationError(Runnable call, String message) {
|
||||
assertThatThrownBy(call::run).isInstanceOfSatisfying(CloudWatchException.class, error -> {
|
||||
assertThat(error.statusCode()).isEqualTo(400);
|
||||
assertThat(error.awsErrorDetails().errorCode()).isEqualTo("ValidationError");
|
||||
assertThat(error.awsErrorDetails().errorMessage()).isEqualTo(message);
|
||||
});
|
||||
}
|
||||
|
||||
/** TreatMissingData=ignore keeps the emulator's alarm evaluator from resetting the states these tests set. */
|
||||
private String metricAlarm(String prefix) {
|
||||
String name = TestFixtures.uniqueName(prefix);
|
||||
cloudWatch.putMetricAlarm(request -> request.alarmName(name).namespace("Compat/Composite")
|
||||
.metricName("Errors").statistic(Statistic.SUM).period(3600).evaluationPeriods(1).threshold(1.0)
|
||||
.comparisonOperator(ComparisonOperator.GREATER_THAN_THRESHOLD).treatMissingData("ignore")
|
||||
.actionsEnabled(false));
|
||||
metricAlarms.add(name);
|
||||
return name;
|
||||
}
|
||||
|
||||
private String composite(String prefix, String rule) {
|
||||
String name = TestFixtures.uniqueName(prefix);
|
||||
cloudWatch.putCompositeAlarm(request -> request.alarmName(name).alarmRule(rule).actionsEnabled(false));
|
||||
composites.add(name);
|
||||
return name;
|
||||
}
|
||||
|
||||
private CompositeAlarm describe(String name) {
|
||||
List<CompositeAlarm> found = cloudWatch.describeAlarms(request -> request.alarmNames(name)
|
||||
.alarmTypes(AlarmType.COMPOSITE_ALARM)).compositeAlarms();
|
||||
assertThat(found).hasSize(1);
|
||||
return found.get(0);
|
||||
}
|
||||
|
||||
private String arn(String name) {
|
||||
String any = cloudWatch.describeAlarms(request -> request.alarmNames(metricAlarms.get(0)))
|
||||
.metricAlarms().get(0).alarmArn();
|
||||
return any.substring(0, any.lastIndexOf(':') + 1) + name;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user