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-patterns): allow imported IBaseService #471

Merged
merged 2 commits into from
Jan 2, 2024
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
18 changes: 9 additions & 9 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 28 additions & 15 deletions lib/monitoring/aws-ecs-patterns/BaseServiceMetricFactory.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
import { BaseService } from "aws-cdk-lib/aws-ecs";
import { DimensionsMap } from "aws-cdk-lib/aws-cloudwatch";
import { IBaseService } from "aws-cdk-lib/aws-ecs";

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

const EcsNamespace = "AWS/ECS";
const EcsContainerInsightsNamespace = "ECS/ContainerInsights";

/**
* Props to create BaseServiceMetricFactory.
*/
export interface BaseServiceMetricFactoryProps {
readonly service: BaseService;
readonly service: IBaseService;
}

/**
* Metric factory for a base service (parent class for e.g. Fargate and EC2 services).
*/
export class BaseServiceMetricFactory {
protected readonly metricFactory: MetricFactory;
protected readonly service: BaseService;
protected readonly dimensionsMap: DimensionsMap;
/**
* @deprecated This isn't required by cdk-monitoring-constructs anymore; use your own reference.
*/
protected readonly service: IBaseService;

constructor(
metricFactory: MetricFactory,
props: BaseServiceMetricFactoryProps
) {
this.metricFactory = metricFactory;
this.dimensionsMap = {
ClusterName: props.service.cluster.clusterName,
ServiceName: props.service.serviceName,
};
this.service = props.service;
}

metricClusterCpuUtilisationInPercent() {
return this.metricFactory.adaptMetric(
this.service.metricCpuUtilization({
label: "Cluster CPU Utilization",
})
return this.metricFactory.createMetric(
"CPUUtilization",
MetricStatistic.AVERAGE,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the metricFactory used to adapt this metric to Average stats before? If not, then would it be a breaking change in regards to the default stats of this metric?

Copy link
Member Author

@echeung-amzn echeung-amzn Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a regular Metric (code ref), which defaults to Average (code ref).

So this is the same thing.

"Cluster CPU Utilization",
this.dimensionsMap,
undefined,
EcsNamespace
);
}

metricClusterMemoryUtilisationInPercent() {
return this.metricFactory.adaptMetric(
this.service.metricMemoryUtilization({
label: "Cluster Memory Utilization",
})
return this.metricFactory.createMetric(
"MemoryUtilization",
MetricStatistic.AVERAGE,
"Cluster Memory Utilization",
this.dimensionsMap,
undefined,
EcsNamespace
);
}

Expand All @@ -47,10 +63,7 @@ export class BaseServiceMetricFactory {
"RunningTaskCount",
MetricStatistic.AVERAGE,
"Running Tasks",
{
ServiceName: this.service.serviceName,
ClusterName: this.service.cluster.clusterName,
},
this.dimensionsMap,
undefined,
EcsContainerInsightsNamespace
);
Expand Down
4 changes: 2 additions & 2 deletions lib/monitoring/aws-ecs-patterns/Ec2ServiceMonitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IMetric,
IWidget,
} from "aws-cdk-lib/aws-cloudwatch";
import { Ec2Service } from "aws-cdk-lib/aws-ecs";
import { Ec2Service, IBaseService } from "aws-cdk-lib/aws-ecs";
import {
ApplicationLoadBalancedEc2Service,
NetworkLoadBalancedEc2Service,
Expand Down Expand Up @@ -147,7 +147,7 @@ export interface Ec2ApplicationLoadBalancerMonitoringProps

export interface CustomEc2ServiceMonitoringProps
extends BaseLoadBalancedEc2ServiceMonitoringProps {
readonly ec2Service: Ec2Service;
readonly ec2Service: IBaseService;
readonly loadBalancer?: IApplicationLoadBalancer | INetworkLoadBalancer;
readonly targetGroup?: IApplicationTargetGroup | INetworkTargetGroup;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/monitoring/aws-ecs-patterns/FargateServiceMonitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IMetric,
IWidget,
} from "aws-cdk-lib/aws-cloudwatch";
import { FargateService } from "aws-cdk-lib/aws-ecs";
import { FargateService, IBaseService } from "aws-cdk-lib/aws-ecs";
import {
ApplicationLoadBalancedFargateService,
NetworkLoadBalancedFargateService,
Expand Down Expand Up @@ -147,7 +147,7 @@ export interface FargateApplicationLoadBalancerMonitoringProps

export interface CustomFargateServiceMonitoringProps
extends BaseLoadBalancedFargateServiceMonitoringProps {
readonly fargateService: FargateService;
readonly fargateService: IBaseService;
readonly loadBalancer?: IApplicationLoadBalancer | INetworkLoadBalancer;
readonly targetGroup?: IApplicationTargetGroup | INetworkTargetGroup;
}
Expand Down
28 changes: 27 additions & 1 deletion test/monitoring/aws-ecs-patterns/Ec2ServiceMonitoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Stack } from "aws-cdk-lib";
import { Template } from "aws-cdk-lib/assertions";
import { InstanceType } from "aws-cdk-lib/aws-ec2";
import { Repository } from "aws-cdk-lib/aws-ecr";
import { Cluster, EcrImage } from "aws-cdk-lib/aws-ecs";
import { Cluster, Ec2Service, EcrImage } from "aws-cdk-lib/aws-ecs";
import {
ApplicationLoadBalancedEc2Service,
NetworkLoadBalancedEc2Service,
Expand Down Expand Up @@ -261,3 +261,29 @@ import { TestMonitoringScope } from "../TestMonitoringScope";
});
}
);

test("snapshot test: with imported service", () => {
const stack = new Stack();

const importedService = Ec2Service.fromEc2ServiceAttributes(
stack,
"ImportedEc2Service",
{
cluster: Cluster.fromClusterArn(
stack,
"ImportedCluster",
"arn:aws:ecs:us-west-2:123456789012:cluster/DummyCluster"
),
serviceName: "DummyService",
}
);

const scope = new TestMonitoringScope(stack, "Scope");
const monitoring = new Ec2ServiceMonitoring(scope, {
ec2Service: importedService,
alarmFriendlyName: "DummyEc2Service",
});

addMonitoringDashboardsToStack(stack, monitoring);
expect(Template.fromStack(stack)).toMatchSnapshot();
});
26 changes: 26 additions & 0 deletions test/monitoring/aws-ecs-patterns/FargateServiceMonitoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,29 @@ import { TestMonitoringScope } from "../TestMonitoringScope";
});
}
);

test("snapshot test: with imported service", () => {
const stack = new Stack();

const importedService = FargateService.fromFargateServiceAttributes(
stack,
"ImportedEc2Service",
{
cluster: Cluster.fromClusterArn(
stack,
"ImportedCluster",
"arn:aws:ecs:us-west-2:123456789012:cluster/DummyCluster"
),
serviceName: "DummyService",
}
);

const scope = new TestMonitoringScope(stack, "Scope");
const monitoring = new FargateServiceMonitoring(scope, {
fargateService: importedService,
alarmFriendlyName: "DummyFargateService",
});

addMonitoringDashboardsToStack(stack, monitoring);
expect(Template.fromStack(stack)).toMatchSnapshot();
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading