简体   繁体   中英

CDK v2 - access metric from CloudFormation Queue (CfnQueue)

I am using the cdk v2.33.0 for Typescript and I am trying to get the NumberOfMessageSent metric from a Cloud Formation Queue I created using the CfnQueue class. The end goal being to use this metric for an alarm I want to create.

While I am able to do myStandardQueue.metricNumberOfMessagesSent() with a Queue object, it seems CfnQueue doesn't have this method.

So my question is, is there another way to do this with CfnQueue objects?

To illustrate:

import { CfnQueue, Queue } from 'aws-cdk-lib/aws-sqs'


// THIS PART IS OK
const myStandardQueue = new Queue(..some parameters...);

const myStandardQueueAlarm = new Alarm(myScope, 'someIdForMyAlarm', {
      alarmName: `myStandardQueueAlarm`,
      comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
      evaluationPeriods: 1,
      threshold: 1,
      // this works because Queue has the metricNumberOfMessagesSent method -->
      metric: myStandardQueue.metricNumberOfMessagesSent({
        period: Duration.seconds(60),
        statistic: 'Sum',
      })
    });


// THIS PART IS NOT OK
const myCfnQueue = new CfnQueue(...some parameters...);

const myCfnQueueAlarm = new Alarm(myScope, 'someOtherIdForMyOtherAlarm', {
      alarmName: `myCfnQueueAlarm`,
      comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
      evaluationPeriods: 1,
      threshold: 1,
      // this does not works because CfnQueue does not have 
      // a metricNumberOfMessagesSent method -->
      metric: myStandardQueue.metricNumberOfMessagesSent({
        period: Duration.seconds(60),
        statistic: 'Sum',
      })
    });

Thanks :)

You can construct it manually. For metricNumberOfMessagesSent , the equivalent Metric would be:

const myMetric = new Metric({
    dimensions: { QueueName: myCfnQueue.attrQueueName },
    namespace: 'AWS/SQS',
    metricName: 'NumberOfMessagesSent',
    period: Duration.minutes(5),
    statistic: 'Sum',
  });

You can do this for any metrics exposed by SQS.

The metric methods available in L2 are generated from spec files - here's the one for SQS: https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/cfnspec/lib/augmentations/AWS_SQS_Queue.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM