Repository: cdklabs/cdk-monitoring-constructs (language: python)

Issue:
DynamoDB GSI Consumed Metrics are Wrong
### Version

v4.0.3

### Steps and/or minimal code example to reproduce

Using `.monitorDynamoTableGlobalSecondaryIndex(…)` creates a widget and monitors that use an incorrect metric for the consumed values.

### Expected behavior

As with the consumed metrics when using `.monitorDynamoTable(…)`, the metrics need to be divided by the number of periods:

https://github.com/cdklabs/cdk-monitoring-constructs/blob/main/lib/monitoring/aws-dynamo/DynamoTableMetricFactory.ts#L58-L80

### Actual behavior

This logic is missing for the consumed metrics for GSIs:

https://github.com/cdklabs/cdk-monitoring-constructs/blob/main/lib/monitoring/aws-dynamo/DynamoTableGlobalSecondaryIndexMetricFactory.ts#L50-L70

### Other details

_No response_


Discussion / hints:

Fix patch:
diff --git a/lib/monitoring/aws-dynamo/DynamoTableGlobalSecondaryIndexMetricFactory.ts b/lib/monitoring/aws-dynamo/DynamoTableGlobalSecondaryIndexMetricFactory.ts
index dd58f726..ead3114d 100644
--- a/lib/monitoring/aws-dynamo/DynamoTableGlobalSecondaryIndexMetricFactory.ts
+++ b/lib/monitoring/aws-dynamo/DynamoTableGlobalSecondaryIndexMetricFactory.ts
@@ -12,6 +12,7 @@ export interface DynamoTableGlobalSecondaryIndexMetricFactoryProps {
 
 export class DynamoTableGlobalSecondaryIndexMetricFactory {
   protected readonly metricFactory: MetricFactory;
+  protected readonly table: ITable;
   protected readonly dimensionsMap: DimensionsMap;
 
   constructor(
@@ -19,6 +20,7 @@ export class DynamoTableGlobalSecondaryIndexMetricFactory {
     props: DynamoTableGlobalSecondaryIndexMetricFactoryProps
   ) {
     this.metricFactory = metricFactory;
+    this.table = props.table;
     this.dimensionsMap = {
       TableName: props.table.tableName,
       GlobalSecondaryIndexName: props.globalSecondaryIndexName,
@@ -48,24 +50,28 @@ export class DynamoTableGlobalSecondaryIndexMetricFactory {
   }
 
   metricConsumedReadCapacityUnits() {
-    return this.metricFactory.createMetric(
-      "ConsumedReadCapacityUnits",
-      MetricStatistic.SUM,
-      "Consumed",
-      this.dimensionsMap,
-      undefined,
-      DynamoDbNamespace
+    return this.metricFactory.createMetricMath(
+      "consumed_rcu_sum/PERIOD(consumed_rcu_sum)",
+      {
+        consumed_rcu_sum: this.table.metricConsumedReadCapacityUnits({
+          statistic: MetricStatistic.SUM,
+          dimensionsMap: this.dimensionsMap,
+        }),
+      },
+      "Consumed"
     );
   }
 
   metricConsumedWriteCapacityUnits() {
-    return this.metricFactory.createMetric(
-      "ConsumedWriteCapacityUnits",
-      MetricStatistic.SUM,
-      "Consumed",
-      this.dimensionsMap,
-      undefined,
-      DynamoDbNamespace
+    return this.metricFactory.createMetricMath(
+      "consumed_wcu_sum/PERIOD(consumed_wcu_sum)",
+      {
+        consumed_wcu_sum: this.table.metricConsumedWriteCapacityUnits({
+          statistic: MetricStatistic.SUM,
+          dimensionsMap: this.dimensionsMap,
+        }),
+      },
+      "Consumed"
     );
   }
 


Test patch:
diff --git a/test/monitoring/aws-dynamo/__snapshots__/DynamoTableGlobalSecondaryIndexMonitoring.test.ts.snap b/test/monitoring/aws-dynamo/__snapshots__/DynamoTableGlobalSecondaryIndexMonitoring.test.ts.snap
index f39a02d8..6d2fdcfa 100644
--- a/test/monitoring/aws-dynamo/__snapshots__/DynamoTableGlobalSecondaryIndexMonitoring.test.ts.snap
+++ b/test/monitoring/aws-dynamo/__snapshots__/DynamoTableGlobalSecondaryIndexMonitoring.test.ts.snap
@@ -30,11 +30,11 @@ Object {
               Object {
                 "Ref": "AWS::Region",
               },
-              "\\",\\"metrics\\":[[\\"AWS/DynamoDB\\",\\"ConsumedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",\\"metrics\\":[[{\\"label\\":\\"Consumed\\",\\"expression\\":\\"consumed_rcu_sum/PERIOD(consumed_rcu_sum)\\"}],[\\"AWS/DynamoDB\\",\\"ConsumedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },
-              "\\",{\\"label\\":\\"Consumed\\",\\"stat\\":\\"Sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",{\\"stat\\":\\"Sum\\",\\"visible\\":false,\\"id\\":\\"consumed_rcu_sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },
@@ -42,11 +42,11 @@ Object {
               Object {
                 "Ref": "AWS::Region",
               },
-              "\\",\\"metrics\\":[[\\"AWS/DynamoDB\\",\\"ConsumedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",\\"metrics\\":[[{\\"label\\":\\"Consumed\\",\\"expression\\":\\"consumed_wcu_sum/PERIOD(consumed_wcu_sum)\\"}],[\\"AWS/DynamoDB\\",\\"ConsumedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },
-              "\\",{\\"label\\":\\"Consumed\\",\\"stat\\":\\"Sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",{\\"stat\\":\\"Sum\\",\\"visible\\":false,\\"id\\":\\"consumed_wcu_sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },
@@ -91,11 +91,11 @@ Object {
               Object {
                 "Ref": "AWS::Region",
               },
-              "\\",\\"metrics\\":[[\\"AWS/DynamoDB\\",\\"ConsumedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",\\"metrics\\":[[{\\"label\\":\\"Consumed\\",\\"expression\\":\\"consumed_rcu_sum/PERIOD(consumed_rcu_sum)\\"}],[\\"AWS/DynamoDB\\",\\"ConsumedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },
-              "\\",{\\"label\\":\\"Consumed\\",\\"stat\\":\\"Sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",{\\"stat\\":\\"Sum\\",\\"visible\\":false,\\"id\\":\\"consumed_rcu_sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedReadCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },
@@ -103,11 +103,11 @@ Object {
               Object {
                 "Ref": "AWS::Region",
               },
-              "\\",\\"metrics\\":[[\\"AWS/DynamoDB\\",\\"ConsumedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",\\"metrics\\":[[{\\"label\\":\\"Consumed\\",\\"expression\\":\\"consumed_wcu_sum/PERIOD(consumed_wcu_sum)\\"}],[\\"AWS/DynamoDB\\",\\"ConsumedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },
-              "\\",{\\"label\\":\\"Consumed\\",\\"stat\\":\\"Sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
+              "\\",{\\"stat\\":\\"Sum\\",\\"visible\\":false,\\"id\\":\\"consumed_wcu_sum\\"}],[\\"AWS/DynamoDB\\",\\"ProvisionedWriteCapacityUnits\\",\\"GlobalSecondaryIndexName\\",\\"non-existing-index\\",\\"TableName\\",\\"",
               Object {
                 "Ref": "TableCD117FA1",
               },