Skip to content

Commit d400bfb

Browse files
xendoJerzy 'Cedric' Zagorski
andauthored
fix: monitorEc2Instances for instanceId list failing (#663)
Fixes # In the previous PR where I added monitoring for network egress ingress I started setting color to 'Total Network In" unfortunately this is not caught during test or synthesis but only during deployment, and I only did e2e testing for ASG monitoring. <img width="1795" height="672" alt="per-instance-network" src="https://github.com/user-attachments/assets/3d73854b-ccac-47db-ab8e-7b3eb09b7714" /> <img width="1895" height="233" alt="per-instance" src="https://github.com/user-attachments/assets/6db15921-2a4f-48ba-9d8d-c144efce6e56" /> <img width="1801" height="670" alt="per-instance-network-multiple" src="https://github.com/user-attachments/assets/c9d21efa-a9de-439a-bc35-452d64349bd7" /> <img width="1117" height="493" alt="network-monitoring-multiple" src="https://github.com/user-attachments/assets/ecd08a54-3fd4-40bd-b0c7-9da2971441f7" /> --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_ --------- Co-authored-by: Jerzy 'Cedric' Zagorski <jzagorsk@amazon.com>
1 parent 4e6f2ff commit d400bfb

File tree

4 files changed

+934
-29
lines changed

4 files changed

+934
-29
lines changed

lib/monitoring/aws-ec2/EC2MetricFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ class SelectedInstancesStrategy implements IEC2MetricFactoryStrategy {
8080
return metricFactory.createMetric(
8181
metricName,
8282
statistic,
83-
`${metricName} (${instanceId})`,
83+
`${label ?? metricName} (${instanceId})`,
8484
resolveDimensions(this.autoScalingGroup, instanceId),
85-
label,
85+
undefined,
8686
EC2Namespace,
8787
undefined,
8888
region,

lib/monitoring/aws-ec2/EC2Monitoring.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ export class EC2Monitoring extends Monitoring {
115115
const alarmProps =
116116
props.addNetworkInTotalBytesExceedThresholdAlarm[disambiguator];
117117

118-
const createdAlarms = this.networkInMetrics.map((metric) => {
118+
const createdAlarms = this.networkInSumMetrics.map((metric, idx) => {
119+
const indexedDisambiguator =
120+
idx > 0 ? `${disambiguator}-${idx}` : disambiguator;
119121
const createdAlarm = this.ec2AlarmFactory.addNetworkInAlarm(
120122
metric,
121123
alarmProps,
122-
disambiguator,
124+
indexedDisambiguator,
123125
);
124126
this.addAlarm(createdAlarm);
125127
return createdAlarm;
@@ -133,11 +135,13 @@ export class EC2Monitoring extends Monitoring {
133135
for (const disambiguator in props.addNetworkOutTotalBytesExceedThresholdAlarm) {
134136
const alarmProps =
135137
props.addNetworkOutTotalBytesExceedThresholdAlarm[disambiguator];
136-
const createdAlarms = this.networkOutSumMetrics.map((metric) => {
138+
const createdAlarms = this.networkOutSumMetrics.map((metric, idx) => {
139+
const indexedDisambiguator =
140+
idx > 0 ? `${disambiguator}-${idx}` : disambiguator;
137141
const createdAlarm = this.ec2AlarmFactory.addNetworkOutAlarm(
138142
metric,
139143
alarmProps,
140-
disambiguator,
144+
indexedDisambiguator,
141145
);
142146
this.addAlarm(createdAlarm);
143147
return createdAlarm;

test/monitoring/aws-ec2/EC2Monitoring.test.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,114 @@ test("snapshot test: all instances, network alarms", () => {
102102
expect(numAlarmsCreated).toBe(2);
103103
expect(Template.fromStack(stack)).toMatchSnapshot();
104104
});
105+
106+
test("snapshot test: ASG, network alarms", () => {
107+
const stack = new Stack();
108+
109+
const scope = new TestMonitoringScope(stack, "Scope");
110+
111+
let numAlarmsCreated = 0;
112+
113+
const monitoring = new EC2Monitoring(scope, {
114+
alarmFriendlyName: "EC2",
115+
autoScalingGroup: AutoScalingGroup.fromAutoScalingGroupName(
116+
stack,
117+
"DummyASG",
118+
"DummyASG",
119+
),
120+
addNetworkInTotalBytesExceedThresholdAlarm: {
121+
WARNING: {
122+
maxNetworkInBytes: 100,
123+
period: Duration.days(1),
124+
},
125+
},
126+
addNetworkOutTotalBytesExceedThresholdAlarm: {
127+
ERROR: {
128+
maxNetworkOutBytes: 200,
129+
period: Duration.days(4),
130+
},
131+
},
132+
useCreatedAlarms: {
133+
consume(alarms: AlarmWithAnnotation[]) {
134+
numAlarmsCreated = alarms.length;
135+
},
136+
},
137+
});
138+
139+
addMonitoringDashboardsToStack(stack, monitoring);
140+
expect(numAlarmsCreated).toBe(2);
141+
expect(Template.fromStack(stack)).toMatchSnapshot();
142+
});
143+
144+
test("snapshot test: instance filter, network alarms", () => {
145+
const stack = new Stack();
146+
147+
const scope = new TestMonitoringScope(stack, "Scope");
148+
149+
let numAlarmsCreated = 0;
150+
151+
const monitoring = new EC2Monitoring(scope, {
152+
alarmFriendlyName: "EC2",
153+
instanceIds: ["instanceId1", "instanceId2"],
154+
addNetworkInTotalBytesExceedThresholdAlarm: {
155+
WARNING: {
156+
maxNetworkInBytes: 100,
157+
period: Duration.days(1),
158+
},
159+
},
160+
addNetworkOutTotalBytesExceedThresholdAlarm: {
161+
ERROR: {
162+
maxNetworkOutBytes: 200,
163+
period: Duration.days(4),
164+
},
165+
},
166+
useCreatedAlarms: {
167+
consume(alarms: AlarmWithAnnotation[]) {
168+
numAlarmsCreated = alarms.length;
169+
},
170+
},
171+
});
172+
173+
addMonitoringDashboardsToStack(stack, monitoring);
174+
expect(numAlarmsCreated).toBe(4);
175+
expect(Template.fromStack(stack)).toMatchSnapshot();
176+
});
177+
178+
test("snapshot test: instance filter + ASG, network alarms", () => {
179+
const stack = new Stack();
180+
181+
const scope = new TestMonitoringScope(stack, "Scope");
182+
183+
let numAlarmsCreated = 0;
184+
185+
const monitoring = new EC2Monitoring(scope, {
186+
alarmFriendlyName: "EC2",
187+
instanceIds: ["instanceId1", "instanceId2"],
188+
autoScalingGroup: AutoScalingGroup.fromAutoScalingGroupName(
189+
stack,
190+
"DummyASG",
191+
"DummyASG",
192+
),
193+
addNetworkInTotalBytesExceedThresholdAlarm: {
194+
WARNING: {
195+
maxNetworkInBytes: 100,
196+
period: Duration.days(1),
197+
},
198+
},
199+
addNetworkOutTotalBytesExceedThresholdAlarm: {
200+
ERROR: {
201+
maxNetworkOutBytes: 200,
202+
period: Duration.days(4),
203+
},
204+
},
205+
useCreatedAlarms: {
206+
consume(alarms: AlarmWithAnnotation[]) {
207+
numAlarmsCreated = alarms.length;
208+
},
209+
},
210+
});
211+
212+
addMonitoringDashboardsToStack(stack, monitoring);
213+
expect(numAlarmsCreated).toBe(4);
214+
expect(Template.fromStack(stack)).toMatchSnapshot();
215+
});

0 commit comments

Comments
 (0)