首页 > 解决方案 > 如何将带有警报注释的图形小部件添加到 Cloudwatch 仪表板

问题描述

我需要向我们的 Cloudwatch 仪表板添加一些小部件。我的代码是这样的:

MetricAlarm班级

import {
  Alarm, DimensionHash, Metric, Statistic, Unit,
} from '@aws-cdk/aws-cloudwatch';
import { Construct } from '@aws-cdk/core';
// MetricAlarmProps type definition here
export class MetricAlarm {
  public readonly alarm: Alarm;

  constructor(scope: Construct, id: string, {
    namespace,
    metricName,
    label,
    dimensions,
    statistic = Statistic.AVERAGE,
    threshold,
    alarmName,
    alarmDescription,
    unit = Unit.COUNT,
    evaluationPeriods = 1,
  }: MetricAlarmProps) {
    const metric = new Metric({
      namespace,
      metricName,
      label,
      dimensions,
      statistic,
      unit,
    });
    this.alarm = metric.createAlarm(scope, id, {
      evaluationPeriods,
      threshold,
      alarmName,
      alarmDescription,
    });
  }
}

AlarmMetricWidget班级:

// AlarmMetricWidgetProps definition here
export default class AlarmMetricWidget extends GraphWidget {
  constructor({
    alarmAnnotation,
    title,
    size = 'standard',
  }: AlarmMetricWidgetProps) {
    super({
      title,
      leftAnnotations: [alarmAnnotation],
      width: (size === 'wide' ? 12 : 6),
    });
  }
}

执行:

    // we have a dashboard object here defined up here
    const { alarm } = new ECSServiceAlarm(scope, id, {
      namespace: 'AWS/ECS',
      metricName: 'CPUUtilization',
      statistic: Statistic.AVERAGE,
      dimensions: {
        ClusterName: ecsClusterName,
        ServiceName: ecsServiceName,
      },
      label,
      threshold,
      alarmName,
      alarmDescription,
    });
    const widget = new AlarmMetricWidget({
      alarmAnnotation: alarm.toAnnotation(),
      title: 'My alarm widget',
    });
    dashboard.addWidgets(widget);

cdk synth通过。但是在 Codepipeline 中,它会引发以下错误:

The dashboard body is invalid, there are 2 validation errors: [
  {
    "dataPath": "/widgets/7/properties",
    "message": "The metric widget should have specified a region and a data source or an alarm annotation"
  },
  {
    "dataPath": "/widgets/7/properties",
    "message": "Should have required property 'metrics' or 'insightRule'"
  },
]

谁能给我一些关于如何解决这个问题的指示?我们还有其他没有警报注释的小部件,它们可以工作。

标签: typescriptamazon-web-servicesaws-cdk

解决方案


问题是小部件缺少此处所述的指标:

  {
    "dataPath": "/widgets/7/properties",
    "message": "Should have required property 'metrics' or 'insightRule'"
  },

我需要导出指标,然后将指标和警报注释添加到小部件以使其工作:

import {
  Alarm, DimensionHash, Metric, Statistic, Unit,
} from '@aws-cdk/aws-cloudwatch';
import { Construct } from '@aws-cdk/core';
// MetricAlarmProps type definition here
export class MetricAlarm {
  public readonly alarm: Alarm;
  public readonly metric: Metric;
  constructor(scope: Construct, id: string, {
    namespace,
    metricName,
    label,
    dimensions,
    statistic = Statistic.AVERAGE,
    threshold,
    alarmName,
    alarmDescription,
    unit = Unit.COUNT,
    evaluationPeriods = 1,
  }: MetricAlarmProps) {
    const metric = new Metric({
      namespace,
      metricName,
      label,
      dimensions,
      statistic,
      unit,
    });
    this.alarm = metric.createAlarm(scope, id, {
      evaluationPeriods,
      threshold,
      alarmName,
      alarmDescription,
    });
    this.metric = metric;
  }
}
// AlarmMetricWidgetProps definition here
export default class AlarmMetricWidget extends GraphWidget {
  constructor({
    alarmAnnotation,
    title,
    metric,
    size = 'standard',
  }: AlarmMetricWidgetProps) {
    super({
      title,
      left: [metric],
      leftAnnotations: [alarmAnnotation],
      width: (size === 'wide' ? 12 : 6),
    });
  }
}
   // we have a dashboard object here defined up here
    const { alarm, metric } = new ECSServiceAlarm(scope, id, {
      namespace: 'AWS/ECS',
      metricName: 'CPUUtilization',
      statistic: Statistic.AVERAGE,
      dimensions: {
        ClusterName: ecsClusterName,
        ServiceName: ecsServiceName,
      },
      label,
      threshold,
      alarmName,
      alarmDescription,
    });
    const widget = new AlarmMetricWidget({
      alarmAnnotation: alarm.toAnnotation(),
      metric,
      title: 'My alarm widget',
    });
    dashboard.addWidgets(widget);

推荐阅读