Skip to content

Commit

Permalink
feat(ecs-patterns): improve ALB/NLB type guards (#480)
Browse files Browse the repository at this point in the history
Fixes #476

This is an alternative to PR #477 without a breaking API change.

---

_By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license_
  • Loading branch information
echeung-amzn authored Jan 19, 2024
1 parent b915c4a commit cb1b92f
Show file tree
Hide file tree
Showing 4 changed files with 838 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": ["javascript", "typescript"]
}
12 changes: 5 additions & 7 deletions lib/monitoring/aws-loadbalancing/LoadBalancerMetricFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,27 @@ import { MetricFactory, MetricWithAlarmSupport } from "../../common";
// It's not possible to use instanceOf with typescript interfaces, so we have to use type guards to differentiate
// between the interfaces. As another problem, the LoadBalancer/TargetGroup for both Network and Application types
// don't have distinguished fields that could be used to differentiate between both types, so we resort to using
// the name of the class below.
//
// Ideally the 2 interfaces would provide a specific field to distinguish both types.
// checking for unique methods in their metrics interfaces.
function isApplicationLoadBalancer(
loadBalancer: INetworkLoadBalancer | IApplicationLoadBalancer
): loadBalancer is IApplicationLoadBalancer {
return loadBalancer.constructor.name.indexOf("Application") != -1;
return !!(loadBalancer.metrics as any).httpRedirectCount;
}
function isNetworkLoadBalancer(
loadBalancer: INetworkLoadBalancer | IApplicationLoadBalancer
): loadBalancer is INetworkLoadBalancer {
return loadBalancer.constructor.name.indexOf("Network") != -1;
return !isApplicationLoadBalancer(loadBalancer);
}

function isApplicationTargetGroup(
targetGroup: INetworkTargetGroup | IApplicationTargetGroup
): targetGroup is IApplicationTargetGroup {
return targetGroup.constructor.name.indexOf("Application") != -1;
return !!(targetGroup.metrics as any).httpCodeTarget;
}
function isNetworkTargetGroup(
targetGroup: INetworkTargetGroup | IApplicationTargetGroup
): targetGroup is INetworkTargetGroup {
return targetGroup.constructor.name.indexOf("Network") != -1;
return !isApplicationTargetGroup(targetGroup);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
ApplicationLoadBalancedFargateService,
NetworkLoadBalancedFargateService,
} from "aws-cdk-lib/aws-ecs-patterns";
import { NetworkLoadBalancer } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import {
NetworkLoadBalancer,
NetworkTargetGroup,
} from "aws-cdk-lib/aws-elasticloadbalancingv2";

import { AlarmWithAnnotation, FargateServiceMonitoring } from "../../../lib";
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
Expand Down Expand Up @@ -483,3 +486,46 @@ test("snapshot test: with imported service", () => {
addMonitoringDashboardsToStack(stack, monitoring);
expect(Template.fromStack(stack)).toMatchSnapshot();
});

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

const scope = new TestMonitoringScope(stack, "Scope");

const cluster = new Cluster(stack, "Cluster");
const image = new EcrImage(new Repository(stack, "Repository"), "DummyImage");
const taskDefinition = new FargateTaskDefinition(stack, "TaskDef", {});

taskDefinition
.addContainer("Container", { image })
.addPortMappings({ containerPort: 8080 });

const fargateService = new FargateService(stack, "Service", {
cluster,
taskDefinition,
});
const networkLoadBalancer =
NetworkLoadBalancer.fromNetworkLoadBalancerAttributes(stack, "NLB", {
loadBalancerArn:
"arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/net/LoadBalancer/123",
});
const networkTargetGroup = NetworkTargetGroup.fromTargetGroupAttributes(
stack,
"NTG",
{
targetGroupArn:
"arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/TargetGroup/123",
loadBalancerArns: networkLoadBalancer.loadBalancerArn,
}
);

const monitoring = new FargateServiceMonitoring(scope, {
fargateService,
loadBalancer: networkLoadBalancer,
targetGroup: networkTargetGroup,
alarmFriendlyName: "DummyFargateService",
});

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

0 comments on commit cb1b92f

Please sign in to comment.