mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(applicationautoscaling): target-tracking alarms watch the real metric, not the policy type name (#6902)
* fix(applicationautoscaling): target-tracking alarms watch the real metric, not the policy type name CustomizedMetricSpecification passes Namespace, MetricName, Dimensions, Statistic and Unit onto the AlarmHigh and AlarmLow alarms as given, and the ECSServiceAverageCPUUtilization, ECSServiceAverageMemoryUtilization and ALBRequestCountPerTarget predefined types map onto the documented AWS/ECS and AWS/ApplicationELB metric identities, so the alarms see data published the way AWS (or the caller) publishes it. Closes #6853 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> * fix(applicationautoscaling): read ALB target-tracking alarms through Sum RequestCountPerTarget data points already carry the per-minute average across the target group, and CloudWatch names Sum as the only valid statistic for the metric, so an Average alarm compares the target against a second average and misses scale-outs (two samples of 600 read 600 instead of 1200 against a target of 1000). The statistic now travels with each predefined metric mapping, keeping Average for the ECS utilisation gauges. Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> --------- Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com> Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
This commit is contained in:
1 parent
6d32605370
commit
2ad35bba1d
5 files changed
+371
-15
No files matched your search
@@ -72,7 +72,25 @@ TargetTracking-<resourceId>-AlarmLow-<uuid>
|
||||
```
|
||||
|
||||
They are visible through `DescribeAlarms` and are deleted when the policy is deleted or
|
||||
its scalable target is deregistered.
|
||||
its scalable target is deregistered, exactly as on AWS.
|
||||
|
||||
Each alarm watches the CloudWatch metric it scales on, as real AWS's alarms do:
|
||||
|
||||
- A `CustomizedMetricSpecification` passes the caller's `Namespace`, `MetricName`,
|
||||
`Dimensions`, `Statistic` (defaulting to `Average`) and `Unit` straight onto the
|
||||
alarms, so data published under the identity the policy named is what the alarms
|
||||
read.
|
||||
- `ECSServiceAverageCPUUtilization` maps onto the documented AWS/ECS service-level
|
||||
metric: `CPUUtilization` with the `ClusterName`/`ServiceName` dimensions taken from
|
||||
the resource ID, and `ECSServiceAverageMemoryUtilization` maps to `MemoryUtilization`
|
||||
the same way.
|
||||
- `ALBRequestCountPerTarget` maps onto AWS/ApplicationELB `RequestCountPerTarget` with
|
||||
the `LoadBalancer`/`TargetGroup` dimensions encoded in the policy's `ResourceLabel`.
|
||||
The alarm reads the `Sum` statistic, the only valid one for that metric on CloudWatch
|
||||
(its data points are already the per-minute average across the group's targets).
|
||||
- Any other predefined type keeps the service namespace's CloudWatch namespace under the
|
||||
type's own name, since that type's metric identity is not one Floci derives; push the
|
||||
metric under that name to exercise such a policy.
|
||||
|
||||
For `ecs` targets, the alarm is created with the same `ClusterName`/`ServiceName`
|
||||
dimensions real AWS attaches to the metric, so a metric you push with
|
||||
|
||||
+105
-13
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.SignedMarkers;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.Alarm;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.PredefinedMetricSpecification;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.ScalableTarget;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.ScalingActivity;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.ScalingPolicy;
|
||||
@@ -392,10 +393,7 @@ public class ApplicationAutoScalingService {
|
||||
alarm.setAlarmName(alarmName);
|
||||
alarm.setAlarmDescription("DO NOT EDIT OR DELETE. For TargetTrackingScaling policy "
|
||||
+ policy.getPolicyArn() + ".");
|
||||
alarm.setNamespace(cloudWatchNamespace(policy.getServiceNamespace()));
|
||||
alarm.setMetricName(predefinedMetricType(policy));
|
||||
alarm.setDimensions(resolveDimensions(policy));
|
||||
alarm.setStatistic("Average");
|
||||
applyMetricSpecification(alarm, policy);
|
||||
alarm.setPeriod(60);
|
||||
alarm.setEvaluationPeriods("AlarmHigh".equals(suffix) ? 3 : 15);
|
||||
alarm.setThreshold(targetValue(policy));
|
||||
@@ -439,6 +437,109 @@ public class ApplicationAutoScalingService {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* The alarm must watch the metric the policy scales on, exactly as AWS's own target
|
||||
* tracking alarms do, otherwise data published the way AWS (or the caller) publishes it
|
||||
* never matches the alarm and the policy never scales. A customized specification
|
||||
* carries the caller's namespace, metric name, dimensions and statistic through; a
|
||||
* predefined type maps onto the CloudWatch identity its backing service documents.
|
||||
*/
|
||||
private void applyMetricSpecification(MetricAlarm alarm, ScalingPolicy policy) {
|
||||
TargetTrackingConfiguration config = policy.getTargetTrackingConfiguration();
|
||||
if (config != null && config.getCustomizedMetricSpecification() != null) {
|
||||
applyCustomizedSpecification(alarm, config.getCustomizedMetricSpecification(), policy);
|
||||
} else {
|
||||
applyPredefinedSpecification(alarm, policy,
|
||||
config == null ? null : config.getPredefinedMetricSpecification());
|
||||
}
|
||||
}
|
||||
|
||||
private static void applyCustomizedSpecification(MetricAlarm alarm, Map<String, Object> spec,
|
||||
ScalingPolicy policy) {
|
||||
alarm.setNamespace(metricOrDefault(textValue(spec.get("Namespace")),
|
||||
cloudWatchNamespace(policy.getServiceNamespace())));
|
||||
alarm.setMetricName(metricOrDefault(textValue(spec.get("MetricName")), "CustomizedMetric"));
|
||||
alarm.setDimensions(customizedDimensions(spec));
|
||||
alarm.setStatistic(metricOrDefault(textValue(spec.get("Statistic")), "Average"));
|
||||
String unit = textValue(spec.get("Unit"));
|
||||
if (unit != null) {
|
||||
alarm.setUnit(unit);
|
||||
}
|
||||
}
|
||||
|
||||
private void applyPredefinedSpecification(MetricAlarm alarm, ScalingPolicy policy,
|
||||
PredefinedMetricSpecification spec) {
|
||||
String type = spec == null ? null : spec.getPredefinedMetricType();
|
||||
PredefinedMetric metric = type == null ? null : PREDEFINED_METRICS.get(type);
|
||||
alarm.setStatistic(metric == null ? "Average" : metric.statistic());
|
||||
if (metric != null) {
|
||||
alarm.setNamespace(metric.namespace());
|
||||
alarm.setMetricName(metric.metricName());
|
||||
alarm.setDimensions("ALBRequestCountPerTarget".equals(type)
|
||||
? resourceLabelDimensions(spec.getResourceLabel()) : resolveDimensions(policy));
|
||||
} else {
|
||||
alarm.setNamespace(cloudWatchNamespace(policy.getServiceNamespace()));
|
||||
alarm.setMetricName(type != null ? type : "CustomizedMetric");
|
||||
alarm.setDimensions(resolveDimensions(policy));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The documented CloudWatch identity of each predefined type Floci resolves, exactly as
|
||||
* the backing service publishes it: {@code ECSServiceAverageCPUUtilization} and
|
||||
* {@code ECSServiceAverageMemoryUtilization} read the AWS/ECS service-level metrics
|
||||
* keyed by {@code ClusterName}/{@code ServiceName}, and {@code ALBRequestCountPerTarget}
|
||||
* reads the AWS/ApplicationELB per-target request count keyed by the load balancer and
|
||||
* target group inside {@code ResourceLabel}. RequestCountPerTarget's only valid
|
||||
* CloudWatch statistic is Sum (the value is already the per-minute average across
|
||||
* targets), so each mapping carries the statistic the alarm must read. Types without a
|
||||
* mapping Floci is certain of
|
||||
* are left onto the service namespace's own namespace under the type's name rather than
|
||||
* silently pointing at a metric the backing service never publishes.
|
||||
*/
|
||||
private static final Map<String, PredefinedMetric> PREDEFINED_METRICS = Map.of(
|
||||
"ECSServiceAverageCPUUtilization", new PredefinedMetric("AWS/ECS", "CPUUtilization", "Average"),
|
||||
"ECSServiceAverageMemoryUtilization", new PredefinedMetric("AWS/ECS", "MemoryUtilization", "Average"),
|
||||
"ALBRequestCountPerTarget", new PredefinedMetric("AWS/ApplicationELB", "RequestCountPerTarget", "Sum"));
|
||||
|
||||
private record PredefinedMetric(String namespace, String metricName, String statistic) {
|
||||
}
|
||||
|
||||
private static String metricOrDefault(String value, String fallback) {
|
||||
return value != null ? value : fallback;
|
||||
}
|
||||
|
||||
private static String textValue(Object value) {
|
||||
return value instanceof String text && !text.isBlank() ? text : null;
|
||||
}
|
||||
|
||||
private static List<Dimension> customizedDimensions(Map<String, Object> spec) {
|
||||
List<Dimension> dimensions = new ArrayList<>();
|
||||
Object entries = spec.get("Dimensions");
|
||||
if (entries instanceof List<?> list) {
|
||||
for (Object entry : list) {
|
||||
if (entry instanceof Map<?, ?> dimension
|
||||
&& dimension.get("Name") instanceof String name
|
||||
&& dimension.get("Value") instanceof String value) {
|
||||
dimensions.add(new Dimension(name, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return List.copyOf(dimensions);
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS's {@code ResourceLabel} for the ALB per-target metric is the tail of the two ARNs,
|
||||
* {@code app/{name}/{id}/targetgroup/{name}/{id}}, which are verbatim the {@code
|
||||
* LoadBalancer} and {@code TargetGroup} dimension values the metric is published under.
|
||||
*/
|
||||
private static List<Dimension> resourceLabelDimensions(String resourceLabel) {
|
||||
int split = resourceLabel == null ? -1 : resourceLabel.indexOf("/targetgroup/");
|
||||
return split <= 0 ? List.of() : List.of(
|
||||
new Dimension("LoadBalancer", resourceLabel.substring(0, split)),
|
||||
new Dimension("TargetGroup", resourceLabel.substring(split + 1)));
|
||||
}
|
||||
|
||||
private static String cloudWatchNamespace(String serviceNamespace) {
|
||||
return switch (serviceNamespace) {
|
||||
case "ecs" -> "AWS/ECS";
|
||||
@@ -451,15 +552,6 @@ public class ApplicationAutoScalingService {
|
||||
};
|
||||
}
|
||||
|
||||
private static String predefinedMetricType(ScalingPolicy policy) {
|
||||
TargetTrackingConfiguration config = policy.getTargetTrackingConfiguration();
|
||||
if (config != null && config.getPredefinedMetricSpecification() != null
|
||||
&& config.getPredefinedMetricSpecification().getPredefinedMetricType() != null) {
|
||||
return config.getPredefinedMetricSpecification().getPredefinedMetricType();
|
||||
}
|
||||
return "CustomizedMetric";
|
||||
}
|
||||
|
||||
private static double targetValue(ScalingPolicy policy) {
|
||||
TargetTrackingConfiguration config = policy.getTargetTrackingConfiguration();
|
||||
return config != null && config.getTargetValue() != null ? config.getTargetValue() : 0.0d;
|
||||
|
||||
+1
-1
@@ -115,7 +115,7 @@ class ApplicationAutoScalingEcsControlLoopIntegrationTest {
|
||||
}
|
||||
metricData.append("""
|
||||
{
|
||||
"MetricName": "ECSServiceAverageCPUUtilization",
|
||||
"MetricName": "CPUUtilization",
|
||||
"Value": 90.0,
|
||||
"Unit": "Percent",
|
||||
"Timestamp": %d,
|
||||
|
||||
+145
@@ -35,8 +35,13 @@ class ApplicationAutoScalingIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
private static final String TARGET_PREFIX = "AnyScaleFrontendService.";
|
||||
private static final String CLOUDWATCH_TARGET = "GraniteServiceVersion20100801.";
|
||||
private static final String CLOUDWATCH_JSON_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String RESOURCE_ID = "service/aas-it-cluster/aas-it-service";
|
||||
private static final String PAGED_RESOURCE_ID = "service/aas-it-cluster/aas-it-service-paged";
|
||||
private static final String ALARM_RESOURCE_ID = "service/aas-it-cluster/aas-it-alarm-service";
|
||||
private static final String ALARM_CLUSTER = "aas-it-cluster";
|
||||
private static final String ALARM_SERVICE = "aas-it-alarm-service";
|
||||
private static final String DIMENSION = "ecs:service:DesiredCount";
|
||||
|
||||
private static String scalableTargetArn;
|
||||
@@ -50,6 +55,10 @@ class ApplicationAutoScalingIntegrationTest {
|
||||
return given().header("X-Amz-Target", TARGET_PREFIX + action).contentType(CONTENT_TYPE);
|
||||
}
|
||||
|
||||
private static RequestSpecification cloudwatchCall(String action) {
|
||||
return given().header("X-Amz-Target", CLOUDWATCH_TARGET + action).contentType(CLOUDWATCH_JSON_CONTENT_TYPE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void registerScalableTargetReturnsScalableTargetArn() {
|
||||
@@ -217,6 +226,14 @@ class ApplicationAutoScalingIntegrationTest {
|
||||
.body("Alarms", hasSize(2))
|
||||
.body("Alarms[0].AlarmName", startsWith("TargetTracking-" + RESOURCE_ID))
|
||||
.body("Alarms[0].AlarmARN", startsWith("arn:aws:cloudwatch:"));
|
||||
|
||||
cloudwatchCall("DescribeAlarms")
|
||||
.body("{ \"AlarmNamePrefix\": \"TargetTracking-" + RESOURCE_ID + "\" }")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("MetricAlarms.find { it.MetricName == 'RequestCountPerTarget' }.Namespace",
|
||||
equalTo("AWS/ApplicationELB"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'RequestCountPerTarget' }.Statistic",
|
||||
equalTo("Sum"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -408,4 +425,132 @@ class ApplicationAutoScalingIntegrationTest {
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
void customizedTargetTrackingAlarmsWatchTheMetricIdentityTheCallerGave() {
|
||||
call("RegisterScalableTarget")
|
||||
.body("""
|
||||
{
|
||||
"ServiceNamespace": "ecs",
|
||||
"ResourceId": "%s",
|
||||
"ScalableDimension": "%s",
|
||||
"MinCapacity": 1,
|
||||
"MaxCapacity": 3
|
||||
}
|
||||
""".formatted(ALARM_RESOURCE_ID, DIMENSION))
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
call("PutScalingPolicy")
|
||||
.body("""
|
||||
{
|
||||
"PolicyName": "aas-it-custom-metric",
|
||||
"PolicyType": "TargetTrackingScaling",
|
||||
"ServiceNamespace": "ecs",
|
||||
"ResourceId": "%s",
|
||||
"ScalableDimension": "%s",
|
||||
"TargetTrackingScalingPolicyConfiguration": {
|
||||
"TargetValue": 10.0,
|
||||
"CustomizedMetricSpecification": {
|
||||
"Namespace": "App/Workers",
|
||||
"MetricName": "JobsPerTask",
|
||||
"Statistic": "Average",
|
||||
"Dimensions": [{ "Name": "ServiceName", "Value": "web" }]
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(ALARM_RESOURCE_ID, DIMENSION))
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
cloudwatchCall("DescribeAlarms")
|
||||
.body("{ \"AlarmNamePrefix\": \"TargetTracking-" + ALARM_RESOURCE_ID + "\" }")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("MetricAlarms.findAll { it.MetricName == 'JobsPerTask' }.size()", equalTo(2))
|
||||
.body("MetricAlarms.find { it.MetricName == 'JobsPerTask' }.Namespace", equalTo("App/Workers"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'JobsPerTask' }.Statistic", equalTo("Average"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'JobsPerTask' }.Dimensions.size()", equalTo(1))
|
||||
.body("MetricAlarms.find { it.MetricName == 'JobsPerTask' }.Dimensions[0].Name", equalTo("ServiceName"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'JobsPerTask' }.Dimensions[0].Value", equalTo("web"))
|
||||
// The metric the alarm watches must be the caller's, not the ECS-shaped fallback.
|
||||
.body("MetricAlarms.findAll { it.Namespace == 'AWS/ECS' }.size()", equalTo(0));
|
||||
|
||||
call("DeregisterScalableTarget")
|
||||
.body("""
|
||||
{ "ServiceNamespace": "ecs", "ResourceId": "%s", "ScalableDimension": "%s" }
|
||||
""".formatted(ALARM_RESOURCE_ID, DIMENSION))
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void ecsPredefinedTargetTrackingAlarmsWatchTheRealMetricTheServicePublishes() {
|
||||
call("RegisterScalableTarget")
|
||||
.body("""
|
||||
{
|
||||
"ServiceNamespace": "ecs",
|
||||
"ResourceId": "%s",
|
||||
"ScalableDimension": "%s",
|
||||
"MinCapacity": 1,
|
||||
"MaxCapacity": 3
|
||||
}
|
||||
""".formatted(ALARM_RESOURCE_ID, DIMENSION))
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
for (String policy : List.of("""
|
||||
{
|
||||
"PolicyName": "aas-it-cpu",
|
||||
"PolicyType": "TargetTrackingScaling",
|
||||
"ServiceNamespace": "ecs",
|
||||
"ResourceId": "%s",
|
||||
"ScalableDimension": "%s",
|
||||
"TargetTrackingScalingPolicyConfiguration": {
|
||||
"TargetValue": 70.0,
|
||||
"PredefinedMetricSpecification": {
|
||||
"PredefinedMetricType": "ECSServiceAverageCPUUtilization"
|
||||
}
|
||||
}
|
||||
}
|
||||
""", """
|
||||
{
|
||||
"PolicyName": "aas-it-memory",
|
||||
"PolicyType": "TargetTrackingScaling",
|
||||
"ServiceNamespace": "ecs",
|
||||
"ResourceId": "%s",
|
||||
"ScalableDimension": "%s",
|
||||
"TargetTrackingScalingPolicyConfiguration": {
|
||||
"TargetValue": 80.0,
|
||||
"PredefinedMetricSpecification": {
|
||||
"PredefinedMetricType": "ECSServiceAverageMemoryUtilization"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")) {
|
||||
call("PutScalingPolicy").body(policy.formatted(ALARM_RESOURCE_ID, DIMENSION))
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
cloudwatchCall("DescribeAlarms")
|
||||
.body("{ \"AlarmNamePrefix\": \"TargetTracking-" + ALARM_RESOURCE_ID + "\" }")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("MetricAlarms.findAll { it.MetricName == 'CPUUtilization' }.size()", equalTo(2))
|
||||
.body("MetricAlarms.find { it.MetricName == 'CPUUtilization' }.Namespace", equalTo("AWS/ECS"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'CPUUtilization' }.Dimensions.size()", equalTo(2))
|
||||
.body("MetricAlarms.find { it.MetricName == 'CPUUtilization' }.Dimensions[0].Name", equalTo("ClusterName"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'CPUUtilization' }.Dimensions[0].Value", equalTo(ALARM_CLUSTER))
|
||||
.body("MetricAlarms.find { it.MetricName == 'CPUUtilization' }.Dimensions[1].Name", equalTo("ServiceName"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'CPUUtilization' }.Dimensions[1].Value", equalTo(ALARM_SERVICE))
|
||||
.body("MetricAlarms.findAll { it.MetricName == 'MemoryUtilization' }.size()", equalTo(2))
|
||||
.body("MetricAlarms.find { it.MetricName == 'MemoryUtilization' }.Namespace", equalTo("AWS/ECS"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'MemoryUtilization' }.Dimensions[0].Name", equalTo("ClusterName"))
|
||||
.body("MetricAlarms.find { it.MetricName == 'MemoryUtilization' }.Dimensions[1].Name", equalTo("ServiceName"))
|
||||
// Nothing watches the policy type name itself, as real AWS would never publish it.
|
||||
.body("MetricAlarms.findAll { it.MetricName == 'ECSServiceAverageCPUUtilization' }.size()", equalTo(0))
|
||||
.body("MetricAlarms.findAll { it.MetricName == 'ECSServiceAverageMemoryUtilization' }.size()", equalTo(0));
|
||||
|
||||
call("DeregisterScalableTarget")
|
||||
.body("""
|
||||
{ "ServiceNamespace": "ecs", "ResourceId": "%s", "ScalableDimension": "%s" }
|
||||
""".formatted(ALARM_RESOURCE_ID, DIMENSION))
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
+101
@@ -9,12 +9,14 @@ import io.github.hectorvent.floci.services.applicationautoscaling.model.Scalable
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.ScalingPolicy;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.TargetTrackingConfiguration;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricAlarm;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -44,16 +46,21 @@ class ApplicationAutoScalingServiceTest {
|
||||
/** Alarm names currently registered in the stubbed CloudWatch. */
|
||||
private final Set<String> alarmsInCloudWatch = new LinkedHashSet<>();
|
||||
|
||||
/** Alarm objects currently registered in the stubbed CloudWatch, in creation order. */
|
||||
private final List<MetricAlarm> createdAlarms = new ArrayList<>();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, "000000000000");
|
||||
alarmsInCloudWatch.clear();
|
||||
createdAlarms.clear();
|
||||
|
||||
cloudWatch = mock(CloudWatchMetricsService.class);
|
||||
doAnswer(invocation -> {
|
||||
MetricAlarm alarm = invocation.getArgument(0);
|
||||
alarm.setAlarmArn("arn:aws:cloudwatch:" + REGION + ":000000000000:alarm:" + alarm.getAlarmName());
|
||||
alarmsInCloudWatch.add(alarm.getAlarmName());
|
||||
createdAlarms.add(alarm);
|
||||
return null;
|
||||
}).when(cloudWatch).putMetricAlarm(any(MetricAlarm.class), anyString());
|
||||
doAnswer(invocation -> {
|
||||
@@ -406,6 +413,100 @@ class ApplicationAutoScalingServiceTest {
|
||||
assertNotNull(policy.getAlarms().get(0).getAlarmArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizedMetricAlarmsWatchTheMetricIdentityTheCallerGave() {
|
||||
register();
|
||||
TargetTrackingConfiguration config = new TargetTrackingConfiguration();
|
||||
config.setTargetValue(10.0d);
|
||||
config.setCustomizedMetricSpecification(Map.of(
|
||||
"Namespace", "App/Workers",
|
||||
"MetricName", "JobsPerTask",
|
||||
"Statistic", "Maximum",
|
||||
"Unit", "Count",
|
||||
"Dimensions", List.of(
|
||||
Map.of("Name", "ServiceName", "Value", "web"),
|
||||
Map.of("Name", "Queue", "Value", "jobs"))));
|
||||
service.putScalingPolicy("custom", "TargetTrackingScaling",
|
||||
NAMESPACE, RESOURCE_ID, DIMENSION, config, null, REGION);
|
||||
|
||||
assertEquals(2, createdAlarms.size());
|
||||
for (MetricAlarm alarm : createdAlarms) {
|
||||
assertEquals("App/Workers", alarm.getNamespace(), "the caller's namespace must reach the alarm");
|
||||
assertEquals("JobsPerTask", alarm.getMetricName(), "the caller's metric name must reach the alarm");
|
||||
assertEquals("Maximum", alarm.getStatistic(), "the caller's statistic must reach the alarm");
|
||||
assertEquals("Count", alarm.getUnit(), "the caller's unit must reach the alarm");
|
||||
assertEquals(List.of(new Dimension("ServiceName", "web"), new Dimension("Queue", "jobs")),
|
||||
alarm.getDimensions(), "the caller's dimensions must reach the alarm");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizedMetricWithoutAStatisticKeepsTheAlarmDefaultAverage() {
|
||||
register();
|
||||
TargetTrackingConfiguration config = new TargetTrackingConfiguration();
|
||||
config.setTargetValue(10.0d);
|
||||
config.setCustomizedMetricSpecification(Map.of(
|
||||
"Namespace", "App/Workers",
|
||||
"MetricName", "JobsPerTask"));
|
||||
service.putScalingPolicy("custom", "TargetTrackingScaling",
|
||||
NAMESPACE, RESOURCE_ID, DIMENSION, config, null, REGION);
|
||||
|
||||
assertEquals("Average", createdAlarms.get(0).getStatistic());
|
||||
assertNull(createdAlarms.get(0).getUnit());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ecsCpuPredefinedAlarmsWatchTheRealAwsEcsMetricIdentity() {
|
||||
register();
|
||||
service.putScalingPolicy("cpu", "TargetTrackingScaling",
|
||||
NAMESPACE, RESOURCE_ID, DIMENSION,
|
||||
targetTracking(65.0, "ECSServiceAverageCPUUtilization", null), null, REGION);
|
||||
|
||||
for (MetricAlarm alarm : createdAlarms) {
|
||||
assertEquals("AWS/ECS", alarm.getNamespace());
|
||||
assertEquals("CPUUtilization", alarm.getMetricName(),
|
||||
"the alarm must watch the metric ECS publishes, not the policy type name");
|
||||
assertEquals(List.of(new Dimension("ClusterName", "my-cluster"), new Dimension("ServiceName", "my-service")),
|
||||
alarm.getDimensions());
|
||||
assertEquals("Average", alarm.getStatistic());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void ecsMemoryPredefinedAlarmsWatchTheRealAwsEcsMetricIdentity() {
|
||||
register();
|
||||
service.putScalingPolicy("memory", "TargetTrackingScaling",
|
||||
NAMESPACE, RESOURCE_ID, DIMENSION,
|
||||
targetTracking(70.0, "ECSServiceAverageMemoryUtilization", null), null, REGION);
|
||||
|
||||
for (MetricAlarm alarm : createdAlarms) {
|
||||
assertEquals("AWS/ECS", alarm.getNamespace());
|
||||
assertEquals("MemoryUtilization", alarm.getMetricName(),
|
||||
"the alarm must watch the metric ECS publishes, not the policy type name");
|
||||
assertEquals(List.of(new Dimension("ClusterName", "my-cluster"), new Dimension("ServiceName", "my-service")),
|
||||
alarm.getDimensions());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void albPredefinedAlarmsWatchThePerTargetMetricInsideTheResourceLabel() {
|
||||
register();
|
||||
service.putScalingPolicy("alb", "TargetTrackingScaling", NAMESPACE, RESOURCE_ID, DIMENSION,
|
||||
targetTracking(1000.0, "ALBRequestCountPerTarget", "app/my-lb/abc123/targetgroup/my-tg/def456"),
|
||||
null, REGION);
|
||||
|
||||
for (MetricAlarm alarm : createdAlarms) {
|
||||
assertEquals("AWS/ApplicationELB", alarm.getNamespace());
|
||||
assertEquals("RequestCountPerTarget", alarm.getMetricName());
|
||||
assertEquals(List.of(
|
||||
new Dimension("LoadBalancer", "app/my-lb/abc123"),
|
||||
new Dimension("TargetGroup", "targetgroup/my-tg/def456")),
|
||||
alarm.getDimensions());
|
||||
assertEquals("Sum", alarm.getStatistic(),
|
||||
"RequestCountPerTarget's only valid CloudWatch statistic is Sum");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deregisterCascadesToPoliciesAndAlarms() {
|
||||
register();
|
||||
|
||||
Reference in new issue
Block a user