Skip to content

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

@everett1992

Description

@everett1992

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

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-cloudwatchRelated to Amazon CloudWatchbugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions