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

feat(ecs): add metrics for Fargate service #2798

Merged
merged 1 commit into from
Jun 11, 2019
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
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, Resource, Token } from '@aws-cdk/cdk';
Expand Down Expand Up @@ -235,36 +234,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 @@ -979,5 +979,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();
}
};