Skip to content

Commit

Permalink
fix(ecs): Alarms with custom dimensions should be processed
Browse files Browse the repository at this point in the history
  • Loading branch information
christosarvanitis committed Dec 23, 2024
1 parent 53f11a8 commit 9833161
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -77,7 +79,16 @@ public List<EcsMetricAlarm> getMetricAlarms(

String glob = Keys.getAlarmKey(accountName, region, "*", ecsClusterName);
Collection<String> metricAlarmsIds = filterIdentifiers(glob);
Collection<EcsMetricAlarm> allMetricAlarms = getAll(metricAlarmsIds);
String globEmptyDimension = Keys.getAlarmKey(accountName, region, "*", "");
Collection<String> otherMetricAlarmsIds = filterIdentifiers(globEmptyDimension);

Collection<String> combinedMetricIds =
Stream.of(metricAlarmsIds, otherMetricAlarmsIds)
.filter(m -> m != null)
.flatMap(Collection::stream)
.collect(Collectors.toList());

Collection<EcsMetricAlarm> allMetricAlarms = getAll(combinedMetricIds);

for (EcsMetricAlarm metricAlarm : allMetricAlarms) {
if (metricAlarm.getAlarmActions().stream().anyMatch(action -> action.contains(serviceName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Subject

import java.util.stream.Collectors
import java.util.stream.Stream

class EcsCloudWatchAlarmCacheClientSpec extends Specification {
@Subject
EcsCloudWatchAlarmCacheClient client
Expand Down Expand Up @@ -246,4 +249,58 @@ def 'should return metric alarms with actions matching the service'() {
)
}


def 'should return metric alarms for a service - single cluster with Custom Alarms/Cloudwatch Dimensions'() {
given:
def serviceName = 'my-service'
def serviceName2 = 'not-matching-service'

def ecsClusterName = 'my-cluster'
def metricAlarms = Set.of(
new MetricAlarm().withAlarmName("alarm-name").withAlarmArn("alarmArn")
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName}")
.withDimensions([new Dimension().withName("ClusterName").withValue(ecsClusterName)]),
new MetricAlarm().withAlarmName("alarm-name-2").withAlarmArn("alarmArn2")
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName}")
.withDimensions([new Dimension().withName("ClusterName").withValue(ecsClusterName)]),
new MetricAlarm().withAlarmName("alarm-name").withAlarmArn("alarmArn3")
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName2}")
.withDimensions([new Dimension().withName("ClusterName").withValue(ecsClusterName)])
)
def metricAlarmCustomDimension = Set.of (
new MetricAlarm().withAlarmName("alarm-name-2-custom").withAlarmArn("alarmArn2-custom")
.withAlarmActions("arn:aws:sns:us-west-1:123456789012:${serviceName}")
.withDimensions([new Dimension().withName("CustomDimension").withValue("customValue")]),
)

def keys = metricAlarms.collect { alarm ->
def key = Keys.getAlarmKey(ACCOUNT, REGION, alarm.getAlarmArn(), ecsClusterName)
def attributes = agent.convertMetricAlarmToAttributes(alarm, ACCOUNT, REGION)
[key, new DefaultCacheData(key, attributes, [:])]
}
def keysCustom = metricAlarmCustomDimension.collect { alarm ->
def key = Keys.getAlarmKey(ACCOUNT, REGION, alarm.getAlarmArn(), "")
def attributes = agent.convertMetricAlarmToAttributes(alarm, ACCOUNT, REGION)
[key, new DefaultCacheData(key, attributes, [:])]
}

cacheView.filterIdentifiers(Keys.Namespace.ALARMS.ns, Keys.getAlarmKey(ACCOUNT, REGION, "*", ecsClusterName)) >> keys*.first()
cacheView.filterIdentifiers(Keys.Namespace.ALARMS.ns, Keys.getAlarmKey(ACCOUNT, REGION, "*", "")) >> keysCustom*.first()
def combinedMetricIds = Stream.of( keys*.first(), keysCustom*.first())
.filter { it != null }
.flatMap { it.stream() }
.collect(Collectors.toList())

cacheView.getAll(Keys.Namespace.ALARMS.ns, combinedMetricIds) >> keys*.last() + keysCustom*.last()

when:
def metricAlarmsReturned = client.getMetricAlarms(serviceName, ACCOUNT, REGION, ecsClusterName)

then:
metricAlarmsReturned.size() == 3
metricAlarmsReturned*.alarmName.containsAll(["alarm-name", "alarm-name-2", "alarm-name-2-custom"])
metricAlarmsReturned*.alarmArn.containsAll(["alarmArn", "alarmArn2","alarmArn2-custom"])
!metricAlarmsReturned*.alarmArn.contains(["alarmArn3"])
}

}

0 comments on commit 9833161

Please sign in to comment.