Skip to content

Commit 3acfd0f

Browse files
author
Eugene Cheung
authored
feat(ecs-patterns): allow imported IBaseService (#471)
The existing logic to get metrics on `BaseService` [is pretty simple](https://github.com/aws/aws-cdk/blob/v2.115.0/packages/aws-cdk-lib/aws-ecs/lib/base/base-service.ts#L1195-L1220) and can probably be lifted up to `IBaseService` upstream (similar [to the change for load balancers](aws/aws-cdk@cb889bc)), but I don't have bandwidth to tackle that right now. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent fadb7b5 commit 3acfd0f

File tree

8 files changed

+288
-29
lines changed

8 files changed

+288
-29
lines changed

API.md

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/monitoring/aws-ecs-patterns/BaseServiceMetricFactory.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
1-
import { BaseService } from "aws-cdk-lib/aws-ecs";
1+
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";
2+
import { IBaseService } from "aws-cdk-lib/aws-ecs";
23

34
import { MetricFactory, MetricStatistic } from "../../common";
45

6+
const EcsNamespace = "AWS/ECS";
57
const EcsContainerInsightsNamespace = "ECS/ContainerInsights";
68

79
/**
810
* Props to create BaseServiceMetricFactory.
911
*/
1012
export interface BaseServiceMetricFactoryProps {
11-
readonly service: BaseService;
13+
readonly service: IBaseService;
1214
}
1315

1416
/**
1517
* Metric factory for a base service (parent class for e.g. Fargate and EC2 services).
1618
*/
1719
export class BaseServiceMetricFactory {
1820
protected readonly metricFactory: MetricFactory;
19-
protected readonly service: BaseService;
21+
protected readonly dimensionsMap: DimensionsMap;
22+
/**
23+
* @deprecated This isn't required by cdk-monitoring-constructs anymore; use your own reference.
24+
*/
25+
protected readonly service: IBaseService;
2026

2127
constructor(
2228
metricFactory: MetricFactory,
2329
props: BaseServiceMetricFactoryProps
2430
) {
2531
this.metricFactory = metricFactory;
32+
this.dimensionsMap = {
33+
ClusterName: props.service.cluster.clusterName,
34+
ServiceName: props.service.serviceName,
35+
};
2636
this.service = props.service;
2737
}
2838

2939
metricClusterCpuUtilisationInPercent() {
30-
return this.metricFactory.adaptMetric(
31-
this.service.metricCpuUtilization({
32-
label: "Cluster CPU Utilization",
33-
})
40+
return this.metricFactory.createMetric(
41+
"CPUUtilization",
42+
MetricStatistic.AVERAGE,
43+
"Cluster CPU Utilization",
44+
this.dimensionsMap,
45+
undefined,
46+
EcsNamespace
3447
);
3548
}
3649

3750
metricClusterMemoryUtilisationInPercent() {
38-
return this.metricFactory.adaptMetric(
39-
this.service.metricMemoryUtilization({
40-
label: "Cluster Memory Utilization",
41-
})
51+
return this.metricFactory.createMetric(
52+
"MemoryUtilization",
53+
MetricStatistic.AVERAGE,
54+
"Cluster Memory Utilization",
55+
this.dimensionsMap,
56+
undefined,
57+
EcsNamespace
4258
);
4359
}
4460

@@ -47,10 +63,7 @@ export class BaseServiceMetricFactory {
4763
"RunningTaskCount",
4864
MetricStatistic.AVERAGE,
4965
"Running Tasks",
50-
{
51-
ServiceName: this.service.serviceName,
52-
ClusterName: this.service.cluster.clusterName,
53-
},
66+
this.dimensionsMap,
5467
undefined,
5568
EcsContainerInsightsNamespace
5669
);

lib/monitoring/aws-ecs-patterns/Ec2ServiceMonitoring.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
IMetric,
55
IWidget,
66
} from "aws-cdk-lib/aws-cloudwatch";
7-
import { Ec2Service } from "aws-cdk-lib/aws-ecs";
7+
import { Ec2Service, IBaseService } from "aws-cdk-lib/aws-ecs";
88
import {
99
ApplicationLoadBalancedEc2Service,
1010
NetworkLoadBalancedEc2Service,
@@ -147,7 +147,7 @@ export interface Ec2ApplicationLoadBalancerMonitoringProps
147147

148148
export interface CustomEc2ServiceMonitoringProps
149149
extends BaseLoadBalancedEc2ServiceMonitoringProps {
150-
readonly ec2Service: Ec2Service;
150+
readonly ec2Service: IBaseService;
151151
readonly loadBalancer?: IApplicationLoadBalancer | INetworkLoadBalancer;
152152
readonly targetGroup?: IApplicationTargetGroup | INetworkTargetGroup;
153153
}

lib/monitoring/aws-ecs-patterns/FargateServiceMonitoring.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
IMetric,
55
IWidget,
66
} from "aws-cdk-lib/aws-cloudwatch";
7-
import { FargateService } from "aws-cdk-lib/aws-ecs";
7+
import { FargateService, IBaseService } from "aws-cdk-lib/aws-ecs";
88
import {
99
ApplicationLoadBalancedFargateService,
1010
NetworkLoadBalancedFargateService,
@@ -147,7 +147,7 @@ export interface FargateApplicationLoadBalancerMonitoringProps
147147

148148
export interface CustomFargateServiceMonitoringProps
149149
extends BaseLoadBalancedFargateServiceMonitoringProps {
150-
readonly fargateService: FargateService;
150+
readonly fargateService: IBaseService;
151151
readonly loadBalancer?: IApplicationLoadBalancer | INetworkLoadBalancer;
152152
readonly targetGroup?: IApplicationTargetGroup | INetworkTargetGroup;
153153
}

test/monitoring/aws-ecs-patterns/Ec2ServiceMonitoring.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Stack } from "aws-cdk-lib";
22
import { Template } from "aws-cdk-lib/assertions";
33
import { InstanceType } from "aws-cdk-lib/aws-ec2";
44
import { Repository } from "aws-cdk-lib/aws-ecr";
5-
import { Cluster, EcrImage } from "aws-cdk-lib/aws-ecs";
5+
import { Cluster, Ec2Service, EcrImage } from "aws-cdk-lib/aws-ecs";
66
import {
77
ApplicationLoadBalancedEc2Service,
88
NetworkLoadBalancedEc2Service,
@@ -261,3 +261,29 @@ import { TestMonitoringScope } from "../TestMonitoringScope";
261261
});
262262
}
263263
);
264+
265+
test("snapshot test: with imported service", () => {
266+
const stack = new Stack();
267+
268+
const importedService = Ec2Service.fromEc2ServiceAttributes(
269+
stack,
270+
"ImportedEc2Service",
271+
{
272+
cluster: Cluster.fromClusterArn(
273+
stack,
274+
"ImportedCluster",
275+
"arn:aws:ecs:us-west-2:123456789012:cluster/DummyCluster"
276+
),
277+
serviceName: "DummyService",
278+
}
279+
);
280+
281+
const scope = new TestMonitoringScope(stack, "Scope");
282+
const monitoring = new Ec2ServiceMonitoring(scope, {
283+
ec2Service: importedService,
284+
alarmFriendlyName: "DummyEc2Service",
285+
});
286+
287+
addMonitoringDashboardsToStack(stack, monitoring);
288+
expect(Template.fromStack(stack)).toMatchSnapshot();
289+
});

test/monitoring/aws-ecs-patterns/FargateServiceMonitoring.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,29 @@ import { TestMonitoringScope } from "../TestMonitoringScope";
457457
});
458458
}
459459
);
460+
461+
test("snapshot test: with imported service", () => {
462+
const stack = new Stack();
463+
464+
const importedService = FargateService.fromFargateServiceAttributes(
465+
stack,
466+
"ImportedEc2Service",
467+
{
468+
cluster: Cluster.fromClusterArn(
469+
stack,
470+
"ImportedCluster",
471+
"arn:aws:ecs:us-west-2:123456789012:cluster/DummyCluster"
472+
),
473+
serviceName: "DummyService",
474+
}
475+
);
476+
477+
const scope = new TestMonitoringScope(stack, "Scope");
478+
const monitoring = new FargateServiceMonitoring(scope, {
479+
fargateService: importedService,
480+
alarmFriendlyName: "DummyFargateService",
481+
});
482+
483+
addMonitoringDashboardsToStack(stack, monitoring);
484+
expect(Template.fromStack(stack)).toMatchSnapshot();
485+
});

test/monitoring/aws-ecs-patterns/__snapshots__/Ec2ServiceMonitoring.test.ts.snap

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)