typescript - 如何将带有警报注释的图形小部件添加到 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'"
},
]
谁能给我一些关于如何解决这个问题的指示?我们还有其他没有警报注释的小部件,它们可以工作。
解决方案
问题是小部件缺少此处所述的指标:
{
"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);
推荐阅读
- javascript - 如何跳过值
- python - Python opencv 缺陷检测
- google-sheets - 如何使用 vlookup 在另一个表(谷歌表)中根据时间查找值?
- javascript - 在这种情况下如何编写 onChange 函数?
- node.js - NPM 默认注册表配置到私有仓库不起作用
- uwp - 如何在 UWP 中默认启用选择文件夹
- python - 在 Python 中计算原始时刻
- java - 如何在调用抛出异常的方法时避免在 try catch 块中进行虚拟返回?
- flutter - 最新的 Flutter 开发版本有很多错误
- excel - 如何用 Excel VBA 中的特定值替换列中的所有值?