Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ecs): Alarms with custom dimensions should be processed #6324

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"])
}

}
Loading