Skip to content

Commit acf015d

Browse files
jogoldrix0rrr
authored andcommitted
feat(ecs): add metrics for Fargate services (#2798)
The `BaseService` class had a `metric()` implementation missing the `clusterName` dimension (it was overriden in `Ec2Service` with a correct implementation). Moved correct `metric()`, `metricCpuUtilization()` and `metricMemoryUtilization()` methods to `BaseService`. Both `Ec2Service` and `FargateService` now have correct `metricXxx()` methods. Added tests for `metricXxx()` methods.
1 parent 1ae11c0 commit acf015d

File tree

4 files changed

+82
-32
lines changed

4 files changed

+82
-32
lines changed

packages/@aws-cdk/aws-ecs/lib/base/base-service.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,29 @@ export abstract class BaseService extends Resource
227227
return new cloudwatch.Metric({
228228
namespace: 'AWS/ECS',
229229
metricName,
230-
dimensions: { ServiceName: this.serviceName },
230+
dimensions: { ClusterName: this.clusterName, ServiceName: this.serviceName },
231231
...props
232232
});
233233
}
234234

235+
/**
236+
* Metric for cluster Memory utilization
237+
*
238+
* @default average over 5 minutes
239+
*/
240+
public metricMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
241+
return this.metric('MemoryUtilization', props);
242+
}
243+
244+
/**
245+
* Metric for cluster CPU utilization
246+
*
247+
* @default average over 5 minutes
248+
*/
249+
public metricCpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
250+
return this.metric('CPUUtilization', props);
251+
}
252+
235253
/**
236254
* Set up AWSVPC networking for this construct
237255
*/

packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cloudwatch = require ('@aws-cdk/aws-cloudwatch');
21
import ec2 = require('@aws-cdk/aws-ec2');
32
import elb = require('@aws-cdk/aws-elasticloadbalancing');
43
import { Construct, Lazy, Resource } from '@aws-cdk/cdk';
@@ -242,36 +241,6 @@ export class Ec2Service extends BaseService implements IEc2Service, elb.ILoadBal
242241
});
243242
}
244243

245-
/**
246-
* Return the given named metric for this Service
247-
*/
248-
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
249-
return new cloudwatch.Metric({
250-
namespace: 'AWS/ECS',
251-
metricName,
252-
dimensions: { ClusterName: this.clusterName, ServiceName: this.serviceName },
253-
...props
254-
});
255-
}
256-
257-
/**
258-
* Metric for cluster Memory utilization
259-
*
260-
* @default average over 5 minutes
261-
*/
262-
public metricMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
263-
return this.metric('MemoryUtilization', props );
264-
}
265-
266-
/**
267-
* Metric for cluster CPU utilization
268-
*
269-
* @default average over 5 minutes
270-
*/
271-
public metricCpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
272-
return this.metric('CPUUtilization', props);
273-
}
274-
275244
/**
276245
* Validate this Ec2Service
277246
*/

packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,5 +1007,37 @@ export = {
10071007

10081008
test.done();
10091009
},
1010+
},
1011+
1012+
'Metric'(test: Test) {
1013+
// GIVEN
1014+
const stack = new cdk.Stack();
1015+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
1016+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
1017+
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
1018+
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'FargateTaskDef');
1019+
taskDefinition.addContainer('Container', {
1020+
image: ecs.ContainerImage.fromRegistry('hello')
1021+
});
1022+
1023+
// WHEN
1024+
const service = new ecs.Ec2Service(stack, 'Service', {
1025+
cluster,
1026+
taskDefinition,
1027+
});
1028+
1029+
// THEN
1030+
test.deepEqual(stack.resolve(service.metricMemoryUtilization()), {
1031+
dimensions: {
1032+
ClusterName: { Ref: 'EcsCluster97242B84' },
1033+
ServiceName: { 'Fn::GetAtt': ['ServiceD69D759B', 'Name'] }
1034+
},
1035+
namespace: 'AWS/ECS',
1036+
metricName: 'MemoryUtilization',
1037+
periodSec: 300,
1038+
statistic: 'Average'
1039+
});
1040+
1041+
test.done();
10101042
}
10111043
};

packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,5 +467,36 @@ export = {
467467

468468
test.done();
469469
},
470+
},
471+
472+
'Metric'(test: Test) {
473+
// GIVEN
474+
const stack = new cdk.Stack();
475+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
476+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
477+
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
478+
taskDefinition.addContainer('Container', {
479+
image: ecs.ContainerImage.fromRegistry('hello')
480+
});
481+
482+
// WHEN
483+
const service = new ecs.FargateService(stack, 'Service', {
484+
cluster,
485+
taskDefinition,
486+
});
487+
488+
// THEN
489+
test.deepEqual(stack.resolve(service.metricCpuUtilization()), {
490+
dimensions: {
491+
ClusterName: { Ref: 'EcsCluster97242B84' },
492+
ServiceName: { 'Fn::GetAtt': ['ServiceD69D759B', 'Name'] }
493+
},
494+
namespace: 'AWS/ECS',
495+
metricName: 'CPUUtilization',
496+
periodSec: 300,
497+
statistic: 'Average'
498+
});
499+
500+
test.done();
470501
}
471502
};

0 commit comments

Comments
 (0)