如何将带有警报注释的图形小部件添加到 Cloudwatch 仪表板
How to add a graph widget with an alarm annotation to Cloudwatch dashboard
我需要向我们的 Cloudwatch 仪表板添加一些小部件。我的代码是这样的:
MetricAlarm
class
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
class:
// 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);
我需要向我们的 Cloudwatch 仪表板添加一些小部件。我的代码是这样的:
MetricAlarm
class
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
class:
// 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);