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:
Matheus OliveiraandPriamo Germosen authored and GitHub committed 2026-10-31 05:30:43 -04:00
1 parent 6d32605370
commit 2ad35bba1d
5 files changed
+371 -15

No files matched your search

@@ -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;