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(cloudwatch): add support for cross-account alarms #16007

Merged
merged 12 commits into from
Aug 25, 2021
Merged

feat(cloudwatch): add support for cross-account alarms #16007

merged 12 commits into from
Aug 25, 2021

Conversation

kaizencc
Copy link
Contributor

Allows accountId to be specified in a CloudWatch Alarm. This opens up the ability to create cross-account alarms, which was just announced.

closes #15959.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@kaizencc kaizencc requested review from rix0rrr and madeline-k August 11, 2021 22:06
@gitpod-io
Copy link

gitpod-io bot commented Aug 11, 2021

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 11, 2021
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

LGTM, leaving to Madeline to decide final vote

@@ -257,7 +257,8 @@ export class Alarm extends AlarmBase {
return dispatchMetric(metric, {
withStat(stat, conf) {
self.validateMetricStat(stat, metric);
if (conf.renderingProperties?.label == undefined) {
if (conf.renderingProperties?.label == undefined &&
Copy link
Contributor

Choose a reason for hiding this comment

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

💅 for readability, it might make sense to assign this expression to a variable to indicate what and why this is happening.

const canRenderAsLegacyMetric = /* ...expression... */;

// Do this to disturb existing templates as little as possible
if (canRenderAsLegacyMetric) {
  return /* ... */;
}

@faizalsomani
Copy link

Hi, can we merge and close the ticket?

@peterwoodworth peterwoodworth added @aws-cdk/aws-cloudwatch Related to Amazon CloudWatch p1 effort/small Small work item – less than a day of effort labels Aug 19, 2021
packages/@aws-cdk/aws-cloudwatch/README.md Outdated Show resolved Hide resolved
@@ -257,7 +257,10 @@ export class Alarm extends AlarmBase {
return dispatchMetric(metric, {
withStat(stat, conf) {
self.validateMetricStat(stat, metric);
if (conf.renderingProperties?.label == undefined) {
const canRenderAsLegacyMetric = conf.renderingProperties?.label == undefined &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain what you mean by the term "legacy" here? This doesn't quite make sense to me, since the accountId is only set if this is true, which would imply that a cross-account alarm only works if the metric is in a "legacy" format of some kind. But that doesn't make sense, since cross-account alarms are a new feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The "legacy" term refers to the fact that cloudwatch has two ways to provision metrics on alarms, and only one way is being updated with new features like cross-account alarms. We want to avoid causing unnecessary diffs, so we shepherd metrics that do not require these "new" features to the "legacy" properties - the new way to provision metrics is via the CfnAlarm.MetricDataQueryProperty.

Here accountId is only set if canRenderAsLegacyMetric is false, not true. So it is correctly named here. (what seems to be the confusion is that github is hiding the unchanged lines and makes it seem like accountId is inside the if (canRenderAsLegacyMetric) scope when it is actually not.)

Copy link
Contributor

Choose a reason for hiding this comment

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

aha, thank you for the explanation! The github diff was confusing me here as well :)

@@ -112,6 +113,36 @@ describe('cross environment', () => {


});

test('metric attached to stack3 will render in stack1 ', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test that uses the method of setting the account property on the metric instead of using attachTo? There should be a unit test for each way that it is possible to use the feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added an additional test that uses the account property, but I do want to push back on this a bit (or ask for clarification). Under the hood, metric.AttachTo() simply updates the metric object with the account property, so I see it as a convenience method and not an additional way to use this feature. To me, it seems like the second test duplicates the first. Does this still warrant an additional unit test?

Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose it is somewhat of a grey area. But I still think the extra test is helpful. Your reasoning for the additional unit test being unnecessary is based on an implementation detail. What if that changes? In this particular scenario, it doesn't seem likely. But in general, unit tests should test the API in all ways that a customer could use it, and make sure that the correct behaviour/result happens in all of them. So that if someone accidentally breaks something by changing an implementation detail, then the unit tests will catch it.

@kaizencc kaizencc requested a review from madeline-k August 23, 2021 17:45
Copy link
Contributor

@madeline-k madeline-k left a comment

Choose a reason for hiding this comment

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

Looks great! 👍

@@ -257,7 +257,10 @@ export class Alarm extends AlarmBase {
return dispatchMetric(metric, {
withStat(stat, conf) {
self.validateMetricStat(stat, metric);
if (conf.renderingProperties?.label == undefined) {
const canRenderAsLegacyMetric = conf.renderingProperties?.label == undefined &&
Copy link
Contributor

Choose a reason for hiding this comment

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

aha, thank you for the explanation! The github diff was confusing me here as well :)

@@ -112,6 +113,36 @@ describe('cross environment', () => {


});

test('metric attached to stack3 will render in stack1 ', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose it is somewhat of a grey area. But I still think the extra test is helpful. Your reasoning for the additional unit test being unnecessary is based on an implementation detail. What if that changes? In this particular scenario, it doesn't seem likely. But in general, unit tests should test the API in all ways that a customer could use it, and make sure that the correct behaviour/result happens in all of them. So that if someone accidentally breaks something by changing an implementation detail, then the unit tests will catch it.

@mergify
Copy link
Contributor

mergify bot commented Aug 24, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: e82b5b1
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit e547ba0 into aws:master Aug 25, 2021
@mergify
Copy link
Contributor

mergify bot commented Aug 25, 2021

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@wangzlei
Copy link

Looking forward to this function, may I know when will this change get delivered in monocdk?

hollanddd pushed a commit to hollanddd/aws-cdk that referenced this pull request Aug 26, 2021
Allows `accountId` to be specified in a CloudWatch Alarm. This opens up the ability to create cross-account alarms, which was just [announced](https://aws.amazon.com/about-aws/whats-new/2021/08/announcing-amazon-cloudwatch-cross-account-alarms/).

closes aws#15959.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Sep 6, 2021
Allows `accountId` to be specified in a CloudWatch Alarm. This opens up the ability to create cross-account alarms, which was just [announced](https://aws.amazon.com/about-aws/whats-new/2021/08/announcing-amazon-cloudwatch-cross-account-alarms/).

closes aws#15959.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
david-doyle-as24 pushed a commit to david-doyle-as24/aws-cdk that referenced this pull request Sep 7, 2021
Allows `accountId` to be specified in a CloudWatch Alarm. This opens up the ability to create cross-account alarms, which was just [announced](https://aws.amazon.com/about-aws/whats-new/2021/08/announcing-amazon-cloudwatch-cross-account-alarms/).

closes aws#15959.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@kaizencc kaizencc deleted the xa-alarms branch November 17, 2021 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cloudwatch Related to Amazon CloudWatch contribution/core This is a PR that came from AWS. effort/small Small work item – less than a day of effort p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-cloudwatch) Support cross-account alarms
7 participants