我有一个带有DDB表的cdk堆栈,上面有一些警报。现在已经成功地为它添加了一个二级索引。
但是,如何以编程方式列出表所具有的辅助索引,以及如何获得表的指标。
后台,我们有一个模块,为我们的DDB表创建警报。该模块接收cdk表对象,并使用metricConsumedWriteCapacityUnits等方法在其上创建一些告警。
我希望扩展这个警报创建者模块,以便也为表的索引创建告警,为此,我需要读取辅助索引(更具体地说是全局索引),以检查表是否有;然后它是否创建警报。这些警报应该超出容量消耗和限制请求(但可能扩展到其他指标)。
发布于 2021-05-13 20:45:51
给出了一个表cdk对象,我如何列出它所具有的辅助索引?
这是个棘手的问题。索引在ITable上不可用,只有CfnTable可用。从ITable到CfnTable可以通过table.node.defaultChild完成。然后,必须在CDK堆栈中解析带有索引的变量。
检索了次要索引之后,如何知道它们是本地索引还是全局索引?
这是最简单的一点。索引位于单独的变量globalSecondaryIndexes和localSecondaryIndexes中。
检索了全局辅助索引之后,如何获得与其相关的度量标准:容量使用和节流请求?
通过将{ GlobalSecondaryIndexName: <indexName> }传递给度量调用的dimensions参数。
下面是一个基于我刚为工作而建的类,它完成了以上所有的工作。它编译,甚至应该运行(不能100%确定它会运行,因为我从我们的解决方案中移除了一些中间脚手架,并且还没有测试这个精确的代码)。
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
import * as dynamo from '@aws-cdk/aws-dynamodb';
import { Construct, Duration } from '@aws-cdk/core';
export class DynamoMonitor extends Construct {
private static getIndexNames(dynamoTable: dynamo.ITable) {
// pull the names of any indexes on the current table from construct
const table = dynamoTable.node.defaultChild as dynamo.CfnTable;
const indexes = dynamoTable.stack.resolve(table.globalSecondaryIndexes) as
Array<dynamo.CfnTable.GlobalSecondaryIndexProperty> | undefined;
const indexNames: string[] = [];
if (indexes) {
for (const index of indexes) {
indexNames.push(index.indexName);
}
}
return indexNames;
}
constructor(scope: Construct, id: string, dynamoTable: dynamo.ITable) {
super(scope, id);
const period = Duration.seconds(30);
const threshold = 50;
const evaluationPeriods = 5;
const indexNames = DynamoMonitor.getIndexNames(dynamoTable);
for (const indexName of indexNames) {
const throttleEvents = dynamoTable.metric('WriteThrottleEvents', {
period: period,
dimensions: { GlobalSecondaryIndexName: indexName },
statistic: cloudwatch.Statistic.SAMPLE_COUNT,
unit: cloudwatch.Unit.COUNT,
});
const consumedWriteCapacityUnits = dynamoTable.metricConsumedWriteCapacityUnits({
label: 'ConsumedWriteCapacityUnits',
dimensions: { GlobalSecondaryIndexName: indexName },
period: period,
statistic: cloudwatch.Statistic.SAMPLE_COUNT,
unit: cloudwatch.Unit.COUNT,
});
const throttleRate = new cloudwatch.MathExpression({
expression: '(throttleEvents/consumedWriteCapacityUnits) * 100',
label: 'WriteThrottleRate',
usingMetrics: {
throttleEvents: throttleEvents,
consumedWriteCapacityUnits: consumedWriteCapacityUnits,
},
});
throttleRate.createAlarm(this, `WriteIndex${indexName}ThrottleRateAlarm`, {
threshold: threshold,
evaluationPeriods: evaluationPeriods,
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
alarmDescription: 'this.description',
treatMissingData: cloudwatch.TreatMissingData.NOT_BREACHING,
});
}
}
}https://stackoverflow.com/questions/65772736
复制相似问题