mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudwatch): return DatapointsToAlarm from the JSON DescribeAlarms response (#3660)
* fix(cloudwatch): return DatapointsToAlarm from the JSON DescribeAlarms response The Query/XML handler has always written DatapointsToAlarm, TreatMissingData and Unit into its alarm element. The JSON/CBOR builder in CloudWatchMetricsJsonHandler.handleDescribeAlarms never emitted any of the three, so a client speaking smithy-rpc-v2-cbor or application/x-amz-json-1.0 to CloudWatch saw them missing from every DescribeAlarms response. Recent botocore and AWS CLI versions default to CBOR for this service, so that is the common path. PutMetricAlarm already parsed and stored all three on both protocols, so only the response builder needed the fields. * fix(cloudwatch): report DatapointsToAlarm only when the caller set one Review feedback on #3660. Emitting the field unconditionally broke Terraform idempotency, and native arm64 / compat-terraform caught it. PutMetricAlarm defaulted DatapointsToAlarm to EvaluationPeriods at storage time for the evaluator's M-out-of-N maths, and the primitive int on MetricAlarm left nothing to distinguish a caller's value from that default. Every alarm then reported a number nobody asked for, and compat-terraform's alarm, which sets no datapoints_to_alarm, drifted on every re-plan. MetricAlarm now holds a nullable Integer carrying only what the caller sent. AlarmEvaluator supplies EvaluationPeriods for the maths instead, so evaluation is unchanged. Both response builders emit the member only when it is set, which closes the same latent gap on the Query path. CloudWatchCfnProvisioner also minted a value from EvaluationPeriods, so a template that names no M now leaves the alarm without one. Its two tests move with it, and a third covers a template that does name one.
This commit is contained in:
1 parent
374b80eb65
commit
a180c140a7
8 files changed
+136
-15
No files matched your search
+16
-4
@@ -15,6 +15,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -107,18 +108,29 @@ class CloudWatchCfnProvisionerTest {
|
||||
|
||||
assertEquals(60, alarm.getPeriod());
|
||||
assertEquals(1, alarm.getEvaluationPeriods());
|
||||
// DatapointsToAlarm defaults to EvaluationPeriods rather than to a fixed number.
|
||||
assertEquals(1, alarm.getDatapointsToAlarm());
|
||||
// A template that names no M leaves the alarm without one, so DescribeAlarms omits the
|
||||
// member the way AWS does. AlarmEvaluator still evaluates M out of N off EvaluationPeriods.
|
||||
assertNull(alarm.getDatapointsToAlarm());
|
||||
}
|
||||
|
||||
@Test
|
||||
void datapointsToAlarmDefaultsToEvaluationPeriods() {
|
||||
void datapointsToAlarmIsAbsentUntilTheTemplateNamesOne() {
|
||||
MetricAlarm alarm = provisionAndCapture("""
|
||||
{"AlarmName": "a", "EvaluationPeriods": "5"}
|
||||
""");
|
||||
|
||||
assertEquals(5, alarm.getEvaluationPeriods());
|
||||
assertEquals(5, alarm.getDatapointsToAlarm());
|
||||
assertNull(alarm.getDatapointsToAlarm());
|
||||
}
|
||||
|
||||
@Test
|
||||
void datapointsToAlarmIsKeptWhenTheTemplateNamesOne() {
|
||||
MetricAlarm alarm = provisionAndCapture("""
|
||||
{"AlarmName": "a", "EvaluationPeriods": "5", "DatapointsToAlarm": "2"}
|
||||
""");
|
||||
|
||||
assertEquals(5, alarm.getEvaluationPeriods());
|
||||
assertEquals(2, alarm.getDatapointsToAlarm());
|
||||
}
|
||||
|
||||
/** ActionsEnabled is true on AWS unless explicitly disabled, so an absent property means true. */
|
||||
|
||||
+51
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.cloudwatch.metrics;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@@ -159,6 +160,56 @@ class CloudWatchMetricsJsonHandlerTest {
|
||||
assertEquals("2", dimensionCount.get("Value").asText());
|
||||
}
|
||||
|
||||
private JsonNode describeFirstAlarm(ObjectNode putAlarmReq) {
|
||||
assertEquals(200, handler.handle("PutMetricAlarm", putAlarmReq, REGION).getStatus());
|
||||
ObjectNode describeReq = MAPPER.createObjectNode();
|
||||
describeReq.putArray("AlarmNames").add(putAlarmReq.get("AlarmName").asText());
|
||||
Response describeResp = handler.handle("DescribeAlarms", describeReq, REGION);
|
||||
assertEquals(200, describeResp.getStatus());
|
||||
return ((ObjectNode) describeResp.getEntity()).get("MetricAlarms").get(0);
|
||||
}
|
||||
|
||||
private static ObjectNode alarmRequest(String name) {
|
||||
ObjectNode req = MAPPER.createObjectNode();
|
||||
req.put("AlarmName", name);
|
||||
req.put("MetricName", "M");
|
||||
req.put("Namespace", "NS");
|
||||
req.put("Period", 300);
|
||||
req.put("EvaluationPeriods", 3);
|
||||
req.put("Threshold", 1.0);
|
||||
req.put("ComparisonOperator", "GreaterThanThreshold");
|
||||
return req;
|
||||
}
|
||||
|
||||
// The Query/XML handler has always returned DatapointsToAlarm, TreatMissingData and Unit.
|
||||
// The JSON/CBOR builder dropped all three, so a botocore client (which speaks CBOR to
|
||||
// CloudWatch by default) saw them missing from every DescribeAlarms response.
|
||||
@Test
|
||||
void describeAlarms_returnsTheFieldsTheQueryProtocolAlreadyReturns() {
|
||||
ObjectNode req = alarmRequest("FullAlarm");
|
||||
req.put("DatapointsToAlarm", 2);
|
||||
req.put("TreatMissingData", "breaching");
|
||||
req.put("Unit", "Count");
|
||||
|
||||
JsonNode alarm = describeFirstAlarm(req);
|
||||
|
||||
assertEquals(2, alarm.get("DatapointsToAlarm").asInt());
|
||||
assertEquals("breaching", alarm.get("TreatMissingData").asText());
|
||||
assertEquals("Count", alarm.get("Unit").asText());
|
||||
}
|
||||
|
||||
// An alarm the caller never gave an M for reports no DatapointsToAlarm at all. AWS omits the
|
||||
// member in that case, and echoing EvaluationPeriods instead reads as a change to any client
|
||||
// that re-plans against its own configuration, which is what floci-io/floci#3660 hit.
|
||||
@Test
|
||||
void describeAlarms_omittedDatapointsToAlarm_reportsNoDatapointsToAlarm() {
|
||||
JsonNode alarm = describeFirstAlarm(alarmRequest("DefaultedAlarm"));
|
||||
|
||||
assertEquals(3, alarm.get("EvaluationPeriods").asInt());
|
||||
assertFalse(alarm.has("DatapointsToAlarm"),
|
||||
"an alarm with no M out of N must not report one");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMetricStatistics_decimalEpochStartEndTime_filtersOutOfRangeDatapoints() {
|
||||
putMetric("NS", "M", "type", "current", 100.0, EPOCH_NOW);
|
||||
|
||||
+37
@@ -8,6 +8,8 @@ import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -59,4 +61,39 @@ class CloudWatchMetricsQueryHandlerTest {
|
||||
verify(metricsService).putMetricAlarm(captor.capture(), anyString());
|
||||
assertFalse(captor.getValue().isActionsEnabled());
|
||||
}
|
||||
|
||||
// Terraform's provider speaks the Query protocol to CloudWatch, so this is the path that
|
||||
// decides whether a re-plan reports drift. An alarm created without DatapointsToAlarm must
|
||||
// come back without it. floci-io/floci#3660 is what echoing EvaluationPeriods here costs.
|
||||
@Test
|
||||
void putMetricAlarmLeavesDatapointsToAlarmUnsetWhenOmitted() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("AlarmName", "TestAlarm");
|
||||
params.putSingle("MetricName", "M");
|
||||
params.putSingle("Namespace", "NS");
|
||||
params.putSingle("EvaluationPeriods", "3");
|
||||
|
||||
handler.handle("PutMetricAlarm", params, REGION);
|
||||
|
||||
ArgumentCaptor<MetricAlarm> captor = ArgumentCaptor.forClass(MetricAlarm.class);
|
||||
verify(metricsService).putMetricAlarm(captor.capture(), anyString());
|
||||
assertEquals(3, captor.getValue().getEvaluationPeriods());
|
||||
assertNull(captor.getValue().getDatapointsToAlarm());
|
||||
}
|
||||
|
||||
@Test
|
||||
void putMetricAlarmKeepsAnExplicitDatapointsToAlarm() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("AlarmName", "TestAlarm");
|
||||
params.putSingle("MetricName", "M");
|
||||
params.putSingle("Namespace", "NS");
|
||||
params.putSingle("EvaluationPeriods", "3");
|
||||
params.putSingle("DatapointsToAlarm", "2");
|
||||
|
||||
handler.handle("PutMetricAlarm", params, REGION);
|
||||
|
||||
ArgumentCaptor<MetricAlarm> captor = ArgumentCaptor.forClass(MetricAlarm.class);
|
||||
verify(metricsService).putMetricAlarm(captor.capture(), anyString());
|
||||
assertEquals(2, captor.getValue().getDatapointsToAlarm());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user