Skip to content

Commit

Permalink
feat(ecs): add metrics for Fargate services (#2798)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jogold authored and rix0rrr committed Jun 11, 2019
1 parent 1ae11c0 commit acf015d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 32 deletions.
20 changes: 19 additions & 1 deletion packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,29 @@ export abstract class BaseService extends Resource
return new cloudwatch.Metric({
namespace: 'AWS/ECS',
metricName,
dimensions: { ServiceName: this.serviceName },
dimensions: { ClusterName: this.clusterName, ServiceName: this.serviceName },
...props
});
}

/**
* Metric for cluster Memory utilization
*
* @default average over 5 minutes
*/
public metricMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('MemoryUtilization', props);
}

/**
* Metric for cluster CPU utilization
*
* @default average over 5 minutes
*/
public metricCpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('CPUUtilization', props);
}

/**
* Set up AWSVPC networking for this construct
*/
Expand Down
31 changes: 0 additions & 31 deletions packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cloudwatch = require ('@aws-cdk/aws-cloudwatch');
import ec2 = require('@aws-cdk/aws-ec2');
import elb = require('@aws-cdk/aws-elasticloadbalancing');
import { Construct, Lazy, Resource } from '@aws-cdk/cdk';
Expand Down Expand Up @@ -242,36 +241,6 @@ export class Ec2Service extends BaseService implements IEc2Service, elb.ILoadBal
});
}

/**
* Return the given named metric for this Service
*/
public metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
namespace: 'AWS/ECS',
metricName,
dimensions: { ClusterName: this.clusterName, ServiceName: this.serviceName },
...props
});
}

/**
* Metric for cluster Memory utilization
*
* @default average over 5 minutes
*/
public metricMemoryUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('MemoryUtilization', props );
}

/**
* Metric for cluster CPU utilization
*
* @default average over 5 minutes
*/
public metricCpuUtilization(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('CPUUtilization', props);
}

/**
* Validate this Ec2Service
*/
Expand Down
32 changes: 32 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,5 +1007,37 @@ export = {

test.done();
},
},

'Metric'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });
const taskDefinition = new ecs.Ec2TaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('Container', {
image: ecs.ContainerImage.fromRegistry('hello')
});

// WHEN
const service = new ecs.Ec2Service(stack, 'Service', {
cluster,
taskDefinition,
});

// THEN
test.deepEqual(stack.resolve(service.metricMemoryUtilization()), {
dimensions: {
ClusterName: { Ref: 'EcsCluster97242B84' },
ServiceName: { 'Fn::GetAtt': ['ServiceD69D759B', 'Name'] }
},
namespace: 'AWS/ECS',
metricName: 'MemoryUtilization',
periodSec: 300,
statistic: 'Average'
});

test.done();
}
};
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/fargate/test.fargate-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,5 +467,36 @@ export = {

test.done();
},
},

'Metric'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'FargateTaskDef');
taskDefinition.addContainer('Container', {
image: ecs.ContainerImage.fromRegistry('hello')
});

// WHEN
const service = new ecs.FargateService(stack, 'Service', {
cluster,
taskDefinition,
});

// THEN
test.deepEqual(stack.resolve(service.metricCpuUtilization()), {
dimensions: {
ClusterName: { Ref: 'EcsCluster97242B84' },
ServiceName: { 'Fn::GetAtt': ['ServiceD69D759B', 'Name'] }
},
namespace: 'AWS/ECS',
metricName: 'CPUUtilization',
periodSec: 300,
statistic: 'Average'
});

test.done();
}
};

0 comments on commit acf015d

Please sign in to comment.