首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS获取辅助索引和指标

AWS获取辅助索引和指标
EN

Stack Overflow用户
提问于 2021-01-18 10:28:18
回答 1查看 588关注 0票数 3

我有一个带有DDB表的cdk堆栈,上面有一些警报。现在已经成功地为它添加了一个二级索引。

但是,如何以编程方式列出表所具有的辅助索引,以及如何获得表的指标。

后台,我们有一个模块,为我们的DDB表创建警报。该模块接收cdk表对象,并使用metricConsumedWriteCapacityUnits等方法在其上创建一些告警。

我希望扩展这个警报创建者模块,以便也为表的索引创建告警,为此,我需要读取辅助索引(更具体地说是全局索引),以检查表是否有;然后它是否创建警报。这些警报应该超出容量消耗和限制请求(但可能扩展到其他指标)。

  1. 给出了一个表cdk对象,我如何列出它所具有的辅助索引?
  2. 检索了辅助索引,如果它们是本地索引还是全局索引,现在如何才能?
  3. 检索了全局辅助索引,如何获得与它相关的指标;容量使用和受限制的请求?
EN

回答 1

Stack Overflow用户

发布于 2021-05-13 20:45:51

给出了一个表cdk对象,我如何列出它所具有的辅助索引?

这是个棘手的问题。索引在ITable上不可用,只有CfnTable可用。从ITableCfnTable可以通过table.node.defaultChild完成。然后,必须在CDK堆栈中解析带有索引的变量。

检索了次要索引之后,如何知道它们是本地索引还是全局索引?

这是最简单的一点。索引位于单独的变量globalSecondaryIndexeslocalSecondaryIndexes中。

检索了全局辅助索引之后,如何获得与其相关的度量标准:容量使用和节流请求?

通过将{ GlobalSecondaryIndexName: <indexName> }传递给度量调用的dimensions参数。

下面是一个基于我刚为工作而建的类,它完成了以上所有的工作。它编译,甚至应该运行(不能100%确定它会运行,因为我从我们的解决方案中移除了一些中间脚手架,并且还没有测试这个精确的代码)。

代码语言:javascript
复制
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,
      });
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65772736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档