mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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
+5
-2
@@ -45,8 +45,11 @@ public class CloudWatchCfnProvisioner implements CfnResourceProvisioner {
|
||||
alarm.setComparisonOperator(ctx.resolveOptional(props, "ComparisonOperator"));
|
||||
alarm.setPeriod(parseIntProp(props, "Period", ctx, DEFAULT_PERIOD_SECONDS));
|
||||
alarm.setEvaluationPeriods(parseIntProp(props, "EvaluationPeriods", ctx, DEFAULT_EVALUATION_PERIODS));
|
||||
alarm.setDatapointsToAlarm(
|
||||
parseIntProp(props, "DatapointsToAlarm", ctx, alarm.getEvaluationPeriods()));
|
||||
// Left null when the template omits it, for the same reason the request handlers do: an
|
||||
// alarm that never carried an M must not report one back through DescribeAlarms.
|
||||
String datapointsToAlarm = ctx.resolveOptional(props, "DatapointsToAlarm");
|
||||
alarm.setDatapointsToAlarm(datapointsToAlarm == null || datapointsToAlarm.isBlank()
|
||||
? null : parseIntProp(props, "DatapointsToAlarm", ctx, alarm.getEvaluationPeriods()));
|
||||
String threshold = ctx.resolveOptional(props, "Threshold");
|
||||
if (threshold != null && !threshold.isBlank()) {
|
||||
try {
|
||||
|
||||
+3
-2
@@ -215,7 +215,7 @@ public class AlarmEvaluator {
|
||||
* two are indistinguishable across their most recent three buckets alone.</p>
|
||||
*/
|
||||
private static boolean settledBreach(Double[] buckets, MetricAlarm alarm, int evaluationPeriods) {
|
||||
int datapointsToAlarm = alarm.getDatapointsToAlarm() > 0
|
||||
int datapointsToAlarm = alarm.getDatapointsToAlarm() != null
|
||||
? alarm.getDatapointsToAlarm() : evaluationPeriods;
|
||||
int oldestBreachingAge = 0;
|
||||
for (int age = 1; age <= buckets.length; age++) {
|
||||
@@ -252,7 +252,8 @@ public class AlarmEvaluator {
|
||||
}
|
||||
|
||||
private static String evaluateBreachCount(int breaching, MetricAlarm alarm, int evaluationPeriods) {
|
||||
int datapointsToAlarm = alarm.getDatapointsToAlarm() > 0 ? alarm.getDatapointsToAlarm() : evaluationPeriods;
|
||||
int datapointsToAlarm = alarm.getDatapointsToAlarm() != null
|
||||
? alarm.getDatapointsToAlarm() : evaluationPeriods;
|
||||
return breaching >= datapointsToAlarm ? "ALARM" : "OK";
|
||||
}
|
||||
|
||||
|
||||
+7
-2
@@ -157,7 +157,8 @@ public class CloudWatchMetricsJsonHandler {
|
||||
alarm.setPeriod(request.path("Period").asInt(60));
|
||||
alarm.setUnit(request.path("Unit").asText(null));
|
||||
alarm.setEvaluationPeriods(request.path("EvaluationPeriods").asInt(1));
|
||||
alarm.setDatapointsToAlarm(request.path("DatapointsToAlarm").asInt(alarm.getEvaluationPeriods()));
|
||||
alarm.setDatapointsToAlarm(request.hasNonNull("DatapointsToAlarm")
|
||||
? request.path("DatapointsToAlarm").asInt() : null);
|
||||
alarm.setThreshold(request.path("Threshold").asDouble(0));
|
||||
alarm.setComparisonOperator(request.path("ComparisonOperator").asText(null));
|
||||
alarm.setTreatMissingData(request.path("TreatMissingData").asText(null));
|
||||
@@ -221,15 +222,19 @@ public class CloudWatchMetricsJsonHandler {
|
||||
dimNode.put("Value", d.value());
|
||||
});
|
||||
node.put("Period", a.getPeriod());
|
||||
if (a.getUnit() != null) node.put("Unit", a.getUnit());
|
||||
node.put("EvaluationPeriods", a.getEvaluationPeriods());
|
||||
if (a.getDatapointsToAlarm() != null) {
|
||||
node.put("DatapointsToAlarm", a.getDatapointsToAlarm());
|
||||
}
|
||||
node.put("Threshold", a.getThreshold());
|
||||
if (a.getComparisonOperator() != null) node.put("ComparisonOperator", a.getComparisonOperator());
|
||||
if (a.getTreatMissingData() != null) node.put("TreatMissingData", a.getTreatMissingData());
|
||||
node.put("ActionsEnabled", a.isActionsEnabled());
|
||||
if (a.getStateValue() != null) node.put("StateValue", a.getStateValue());
|
||||
if (a.getStateReason() != null) node.put("StateReason", a.getStateReason());
|
||||
if (a.getStateReasonData() != null) node.put("StateReasonData", a.getStateReasonData());
|
||||
node.put("StateUpdatedTimestamp", a.getStateUpdatedTimestamp());
|
||||
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
+7
-2
@@ -663,7 +663,9 @@ public class CloudWatchMetricsQueryHandler {
|
||||
a.setPeriod(parseIntParam(params, "Period", 60));
|
||||
a.setUnit(params.getFirst("Unit"));
|
||||
a.setEvaluationPeriods(parseIntParam(params, "EvaluationPeriods", 1));
|
||||
a.setDatapointsToAlarm(parseIntParam(params, "DatapointsToAlarm", a.getEvaluationPeriods()));
|
||||
String datapointsToAlarm = params.getFirst("DatapointsToAlarm");
|
||||
a.setDatapointsToAlarm(datapointsToAlarm == null || datapointsToAlarm.isBlank()
|
||||
? null : Integer.parseInt(datapointsToAlarm));
|
||||
a.setThreshold(parseDouble(params.getFirst("Threshold"), 0));
|
||||
a.setComparisonOperator(params.getFirst("ComparisonOperator"));
|
||||
a.setTreatMissingData(params.getFirst("TreatMissingData"));
|
||||
@@ -740,10 +742,13 @@ public class CloudWatchMetricsQueryHandler {
|
||||
.elem("Period", String.valueOf(a.getPeriod()))
|
||||
.elem("Unit", a.getUnit())
|
||||
.elem("EvaluationPeriods", String.valueOf(a.getEvaluationPeriods()))
|
||||
.elem("DatapointsToAlarm", String.valueOf(a.getDatapointsToAlarm()))
|
||||
.elem("Threshold", String.valueOf(a.getThreshold()))
|
||||
.elem("ComparisonOperator", a.getComparisonOperator())
|
||||
.elem("TreatMissingData", a.getTreatMissingData());
|
||||
// Only an alarm the caller gave an M for carries the member, matching what AWS returns.
|
||||
if (a.getDatapointsToAlarm() != null) {
|
||||
xml.elem("DatapointsToAlarm", String.valueOf(a.getDatapointsToAlarm()));
|
||||
}
|
||||
|
||||
xml.end("member");
|
||||
}
|
||||
|
||||
+10
-3
@@ -31,7 +31,14 @@ public class MetricAlarm {
|
||||
private int period;
|
||||
private String unit;
|
||||
private int evaluationPeriods;
|
||||
private int datapointsToAlarm;
|
||||
/**
|
||||
* The M of an "M out of N" alarm, exactly as the caller set it, or null when the caller set
|
||||
* nothing. It stays nullable so a response can tell the two apart: AWS omits the member from
|
||||
* DescribeAlarms for an alarm that never carried one, and echoing a number nobody asked for
|
||||
* reads as drift to any client that re-plans. {@link AlarmEvaluator} supplies EvaluationPeriods
|
||||
* for the evaluation maths instead of storing it here.
|
||||
*/
|
||||
private Integer datapointsToAlarm;
|
||||
private double threshold;
|
||||
private String comparisonOperator;
|
||||
private String treatMissingData = "missing";
|
||||
@@ -102,8 +109,8 @@ public class MetricAlarm {
|
||||
public int getEvaluationPeriods() { return evaluationPeriods; }
|
||||
public void setEvaluationPeriods(int evaluationPeriods) { this.evaluationPeriods = evaluationPeriods; }
|
||||
|
||||
public int getDatapointsToAlarm() { return datapointsToAlarm; }
|
||||
public void setDatapointsToAlarm(int datapointsToAlarm) { this.datapointsToAlarm = datapointsToAlarm; }
|
||||
public Integer getDatapointsToAlarm() { return datapointsToAlarm; }
|
||||
public void setDatapointsToAlarm(Integer datapointsToAlarm) { this.datapointsToAlarm = datapointsToAlarm; }
|
||||
|
||||
public double getThreshold() { return threshold; }
|
||||
public void setThreshold(double threshold) { this.threshold = threshold; }
|
||||
|
||||
+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