Skip to content

Commit 4ada111

Browse files
fix(ecs): Alarms with custom dimensions should be processed (spinnaker#6324)
1 parent d5f80cf commit 4ada111

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

clouddriver-ecs/src/main/java/com/netflix/spinnaker/clouddriver/ecs/cache/client/EcsCloudWatchAlarmCacheClient.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.LinkedList;
2828
import java.util.List;
2929
import java.util.Map;
30+
import java.util.stream.Collectors;
31+
import java.util.stream.Stream;
3032
import org.springframework.beans.factory.annotation.Autowired;
3133
import org.springframework.stereotype.Component;
3234

@@ -77,7 +79,16 @@ public List<EcsMetricAlarm> getMetricAlarms(
7779

7880
String glob = Keys.getAlarmKey(accountName, region, "*", ecsClusterName);
7981
Collection<String> metricAlarmsIds = filterIdentifiers(glob);
80-
Collection<EcsMetricAlarm> allMetricAlarms = getAll(metricAlarmsIds);
82+
String globEmptyDimension = Keys.getAlarmKey(accountName, region, "*", "");
83+
Collection<String> otherMetricAlarmsIds = filterIdentifiers(globEmptyDimension);
84+
85+
Collection<String> combinedMetricIds =
86+
Stream.of(metricAlarmsIds, otherMetricAlarmsIds)
87+
.filter(m -> m != null)
88+
.flatMap(Collection::stream)
89+
.collect(Collectors.toList());
90+
91+
Collection<EcsMetricAlarm> allMetricAlarms = getAll(combinedMetricIds);
8192

8293
for (EcsMetricAlarm metricAlarm : allMetricAlarms) {
8394
if (metricAlarm.getAlarmActions().stream().anyMatch(action -> action.contains(serviceName))

clouddriver-ecs/src/test/groovy/com/netflix/spinnaker/clouddriver/ecs/cache/EcsCloudWatchAlarmCacheClientSpec.groovy

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ import spock.lang.Shared
3232
import spock.lang.Specification
3333
import spock.lang.Subject
3434

35+
import java.util.stream.Collectors
36+
import java.util.stream.Stream
37+
3538
class EcsCloudWatchAlarmCacheClientSpec extends Specification {
3639
@Subject
3740
EcsCloudWatchAlarmCacheClient client
@@ -246,4 +249,58 @@ def 'should return metric alarms with actions matching the service'() {
246249
)
247250
}
248251

252+
253+
def 'should return metric alarms for a service - single cluster with Custom Alarms/Cloudwatch Dimensions'() {
254+
given:
255+
def serviceName = 'my-service'
256+
def serviceName2 = 'not-matching-service'
257+
258+
def ecsClusterName = 'my-cluster'
259+
def metricAlarms = Set.of(
260+
new MetricAlarm().withAlarmName("alarm-name").withAlarmArn("alarmArn")
261+
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName}")
262+
.withDimensions([new Dimension().withName("ClusterName").withValue(ecsClusterName)]),
263+
new MetricAlarm().withAlarmName("alarm-name-2").withAlarmArn("alarmArn2")
264+
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName}")
265+
.withDimensions([new Dimension().withName("ClusterName").withValue(ecsClusterName)]),
266+
new MetricAlarm().withAlarmName("alarm-name").withAlarmArn("alarmArn3")
267+
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName2}")
268+
.withDimensions([new Dimension().withName("ClusterName").withValue(ecsClusterName)])
269+
)
270+
def metricAlarmCustomDimension = Set.of (
271+
new MetricAlarm().withAlarmName("alarm-name-2-custom").withAlarmArn("alarmArn2-custom")
272+
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName}")
273+
.withDimensions([new Dimension().withName("CustomDimension").withValue("customValue")]),
274+
)
275+
276+
def keys = metricAlarms.collect { alarm ->
277+
def key = Keys.getAlarmKey(ACCOUNT, REGION, alarm.getAlarmArn(), ecsClusterName)
278+
def attributes = agent.convertMetricAlarmToAttributes(alarm, ACCOUNT, REGION)
279+
[key, new DefaultCacheData(key, attributes, [:])]
280+
}
281+
def keysCustom = metricAlarmCustomDimension.collect { alarm ->
282+
def key = Keys.getAlarmKey(ACCOUNT, REGION, alarm.getAlarmArn(), "")
283+
def attributes = agent.convertMetricAlarmToAttributes(alarm, ACCOUNT, REGION)
284+
[key, new DefaultCacheData(key, attributes, [:])]
285+
}
286+
287+
cacheView.filterIdentifiers(Keys.Namespace.ALARMS.ns, Keys.getAlarmKey(ACCOUNT, REGION, "*", ecsClusterName)) >> keys*.first()
288+
cacheView.filterIdentifiers(Keys.Namespace.ALARMS.ns, Keys.getAlarmKey(ACCOUNT, REGION, "*", "")) >> keysCustom*.first()
289+
def combinedMetricIds = Stream.of( keys*.first(), keysCustom*.first())
290+
.filter { it != null }
291+
.flatMap { it.stream() }
292+
.collect(Collectors.toList())
293+
294+
cacheView.getAll(Keys.Namespace.ALARMS.ns, combinedMetricIds) >> keys*.last() + keysCustom*.last()
295+
296+
when:
297+
def metricAlarmsReturned = client.getMetricAlarms(serviceName, ACCOUNT, REGION, ecsClusterName)
298+
299+
then:
300+
metricAlarmsReturned.size() == 3
301+
metricAlarmsReturned*.alarmName.containsAll(["alarm-name", "alarm-name-2", "alarm-name-2-custom"])
302+
metricAlarmsReturned*.alarmArn.containsAll(["alarmArn", "alarmArn2","alarmArn2-custom"])
303+
!metricAlarmsReturned*.alarmArn.contains(["alarmArn3"])
304+
}
305+
249306
}

0 commit comments

Comments
 (0)