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

cloudwatch: Unrecognized statistic warning when using Stats helper. #29465

Closed
everett1992 opened this issue Mar 12, 2024 · 4 comments · Fixed by #29498 · May be fixed by NOUIY/aws-solutions-constructs#98, NOUIY/aws-solutions-constructs#99 or NOUIY/aws-solutions-constructs#101
Assignees
Labels
@aws-cdk/aws-cloudwatch Related to Amazon CloudWatch bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@everett1992
Copy link
Contributor

Describe the bug

CDK can generate Unrecognized statistic warnings even when using the Stats helper.

Expected Behavior

The warning says to use the stats helper, and the helper is (somtimes, more on that later) generating valid statistics so there should not be any warnings.

Current Behavior

  new Metric({
    namespace: "example",
    metricName: "example",
    statistic: Stats.percentileRank(123),
  }).warningsV2
Unrecognized statistic "PR(:0)" for metric with namespace "example" and metric name "example". Preferably use the `aws_cloudwatch.Stats` helper class to specify a statistic. You can ignore this warning if your statistic is valid but not yet supported by the `aws_cloudwatch.Stats` helper class.
  • PR(:0) is a valid statistic
  • It was generated with the Stats helper class.

Reproduction Steps

import { Metric, Stats } from 'aws-cdk-lib/aws-cloudwatch';

// Stats can create valid percentile ranks but Metric will incorrectly warn on them.
it.each([
  Stats.percentileRank(0),
  Stats.percentileRank(0, 1),
  Stats.percentileRank(0, undefined),
])('Stats can create valid statistics %s that causes warnings', (statistic) => {
  const metric = new Metric({
    namespace: "example",
    metricName: "example",
    statistic,
  })

  expect(metric.warningsV2).toEqual({
    "CloudWatch:Alarm:UnrecognizedStatistic": expect.stringContaining(`Unrecognized statistic \"${statistic}\"`),
  })
});

Additionally while I was writing this bug report I realized that Stats can generate actually invalid statitsics without a type error if you pass Infinity, or NaN.

// Stats can create some statistics that are not valid, mainly because TypeScript's number
// includes special floating point values, NaN, Infinity, ect.
// Stats creates these and Metric correctly (but accidenttaly) warns on them.
it.each([
  Stats.percentileRank(0, Infinity),
  Stats.percentileRank(0, NaN),
  Stats.percentileRank(NaN),
  Stats.percentileRank(NaN),
])('Stats can create _invalid_ statistics %s that causes warnings', (statistic) => {
  const metric = new Metric({
    namespace: "example",
    metricName: "example",
    statistic,
  })

  expect(metric.warningsV2).toEqual({
    "CloudWatch:Alarm:UnrecognizedStatistic": expect.stringContaining(`Unrecognized statistic \"${statistic}\"`),
  })
});

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.132.1 (build 9df7dd3)

Framework Version

2.132.1

Node.js Version

v20.10.0

OS

Ubuntu

Language

TypeScript

Language Version

No response

Other information

No response

@everett1992 everett1992 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 12, 2024
@github-actions github-actions bot added the @aws-cdk/aws-cloudwatch Related to Amazon CloudWatch label Mar 12, 2024
@everett1992
Copy link
Contributor Author

I also found that there's no way to generate open upper bounds, like PR(1:), or trimmed stats that used a fixed value, like TC(10:) vs TC(10%:), that's probably a seprate feature request.

@pahud
Copy link
Contributor

pahud commented Mar 13, 2024

yes we probably should fix here

const parsedStat = parseStatistic(props.statistic || Stats.AVERAGE);
if (parsedStat.type === 'generic') {
// Unrecognized statistic, do not throw, just warn
// There may be a new statistic that this lib does not support yet
const label = props.label ? `, label "${props.label}"`: '';
const warning = `Unrecognized statistic "${props.statistic}" for metric with namespace "${props.namespace}"${label} and metric name "${props.metricName}".` +
' Preferably use the `aws_cloudwatch.Stats` helper class to specify a statistic.' +
' You can ignore this warning if your statistic is valid but not yet supported by the `aws_cloudwatch.Stats` helper class.';
this.warningsV2 = {
'CloudWatch:Alarm:UnrecognizedStatistic': warning,
};
this.warnings = [warning];
}
this.statistic = normalizeStatistic(parsedStat);

@pahud pahud added p2 effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. p2 labels Mar 13, 2024
@xazhao xazhao self-assigned this Mar 14, 2024
@xazhao
Copy link
Contributor

xazhao commented Mar 14, 2024

Had a quick look the percentileRank statistic is missing from here:
https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-cloudwatch/lib/private/statistic.ts#L187

Adding the percentileRank should fix the issue. I will draft a PR later

@mergify mergify bot closed this as completed in #29498 Mar 15, 2024
mergify bot pushed a commit that referenced this issue Mar 15, 2024
…Rank statistic in Stats helper (#29498)

### Issue # (if applicable)

Closes #29465.

### Reason for this change

There shouldn't be a warning when `Stats.percentileRank`

### Description of changes

Add a new parser for percentileRank statistic

### Description of how you validated changes

unit test

### Checklist
- [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

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

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment