Skip to content

Commit

Permalink
feat(elbv2): add metrics to INetworkTargetGroup and IApplicationTarge…
Browse files Browse the repository at this point in the history
…tGroup

By moving the metrics methods to the `INetworkTargetGroup` and
`IApplicationTargetGroup` interfaces it allows to create these metrics also for
Target Groups that are imported via the `fromTargetGroupAttributes()` method.

To create the metrics for Target Groups requires (1) the full name of the Target
Group and (2) the full name of the Load Balancer.

For (1): it is readily available given that all imported Target Groups need to
provide its ARN.
For (2), it is an optional value, so the `.metrics` parameter will throw an
error if it was not provided.

To solve this problem I did:

- Introduce a new interface for each TG type: `INetworkTargetGroupMetrics`,
`IApplicationTargetGroupMetrics`
- Create a concrete implementation for the new interfaces (1 for each):
`NetworkTargetGroupMetrics` and `ApplicationTargetGroupMetrics`
- Make each concrete implementation of each Load Balancer to also provide a
`metrics` field. The concrete implementations of the load balancers are:
`ImportedApplicationTargetGroup`, and `ApplicationLoadBalancer` (and the same
for the NLB classes).

I chose to create a new interface because code can be reused across the 3
concrete implementations of each Load Balancer. I deprecated the `metricXXX()`
methods of each load balancer because I think it is cleaner to access metrics
through the new `metrics` attribute/interface.

There is a small **gotcha** here because the parameter of the
`fromTargetGroupAttributes()` method that refers to the LB is:
`loadBalancerArns`, which has its documentation as:

> A Token representing the list of ARNs for the load balancer routing to this
target group

I'm not treating this parameter as a collection of ARNs, but as a single AR.
Also, I'm not treating it only as a token, but hardcoded ARNs can also be
supplied, which "sort of" violates its interface. This attribute is weird though
because Target Groups cannot have multiple Load Balancers as of today, although
its documentation doesn't clearly express that is the case.

fix: #10850

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing
guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process
described
[here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies)

### New Features

* [x] Have you added the new feature to an [integration
test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the
snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache-2.0 license*
  • Loading branch information
Gustavo Muenz committed Feb 7, 2023
1 parent 6fdf0b0 commit 5f9d157
Show file tree
Hide file tree
Showing 14 changed files with 1,146 additions and 90 deletions.
38 changes: 37 additions & 1 deletion packages/@aws-cdk/aws-elasticloadbalancingv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,47 @@ const listener = elbv2.NetworkListener.fromLookup(this, 'ALBListener', {

## Metrics

You may create metrics for each Load Balancer through the `metrics` attribute:
You may create metrics for Load Balancers and Target Groups through the `metrics` attribute:

**Load Balancer:**

```ts
declare const alb: elbv2.IApplicationLoadBalancer;

const albMetrics: elbv2.IApplicationLoadBalancerMetrics = alb.metrics;
const metricConnectionCount: cloudwatch.Metric = albMetrics.activeConnectionCount();
```

**Target Group:**

```ts
declare const targetGroup: elbv2.IApplicationTargetGroup;

const targetGroupMetrics: elbv2.IApplicationTargetGroupMetrics = targetGroup.metrics;
const metricHealthyHostCount: cloudwatch.Metric = targetGroupMetrics.healthyHostCount();
```

Metrics are also available to imported resources:

```ts
declare const stack: Stack;

const targetGroup = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'MyTargetGroup', {
targetGroupArn: Fn.importValue('TargetGroupArn'),
loadBalancerArns: Fn.importValue('LoadBalancerArn'),
});

const targetGroupMetrics: elbv2.IApplicationTargetGroupMetrics = targetGroup.metrics;
```

Notice that TargetGroups must be imported by supplying the Load Balancer too, otherwise accessing the `metrics` will
throw an error:

```ts
declare const stack: Stack;
const targetGroup = elbv2.ApplicationTargetGroup.fromTargetGroupAttributes(stack, 'MyTargetGroup', {
targetGroupArn: Fn.importValue('TargetGroupArn'),
});

const targetGroupMetrics: elbv2.IApplicationTargetGroupMetrics = targetGroup.metrics; // throws an Error()
```
Loading

0 comments on commit 5f9d157

Please sign in to comment.