Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.

Commit f4a6da2

Browse files
authored
Merge pull request #29 from tylfin/throttleMetric
Add throttle metrics to describeTableConsumedCapacityAsync
2 parents 6665b89 + e025d1d commit f4a6da2

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/capacity/CapacityCalculatorBase.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,42 @@ export default class CapacityCalculatorBase {
5454
let gsiWrites = (params.GlobalSecondaryIndexes || [])
5555
.map(gsi => this.getConsumedCapacityAsync(false, params.TableName, gsi.IndexName));
5656

57+
let tableTRead = this.getThrottledEventsAsync(true, params.TableName, null)
58+
let tableTWrites = this.getThrottledEventsAsync(false, params.TableName, null)
59+
60+
let gsiTReads = (params.GlobalSecondaryIndexes || [])
61+
.map(gsi => this.getThrottledEventsAsync(true, params.TableName, gsi.IndexName));
62+
63+
let gsiTWrites = (params.GlobalSecondaryIndexes || [])
64+
.map(gsi => this.getThrottledEventsAsync(false, params.TableName, gsi.IndexName));
65+
5766
// Await on the results
5867
let tableConsumedRead = await tableRead;
5968
let tableConsumedWrite = await tableWrite;
6069
let gsiConsumedReads = await Promise.all(gsiReads);
6170
let gsiConsumedWrites = await Promise.all(gsiWrites);
6271

72+
// Await on throttled info
73+
let tableThrottledRead = await tableTRead;
74+
let tableThrottledWrite = await tableTWrites;
75+
let gsiThrottledReads = await Promise.all(gsiTReads);
76+
let gsiThrottledWrites = await Promise.all(gsiTWrites);
77+
6378
// Format results
6479
let gsis = gsiConsumedReads.map((read, i) => {
6580
let write = gsiConsumedWrites[i];
81+
let throttledWrite = gsiThrottledWrites[i];
82+
let throttledRead = gsiThrottledReads[i]
6683
return {
6784
// $FlowIgnore: The indexName is not null in this case
6885
IndexName: read.globalSecondaryIndexName,
6986
ConsumedThroughput: {
7087
ReadCapacityUnits: read.value,
7188
WriteCapacityUnits: write.value
89+
},
90+
ThrottledEvents: {
91+
ThrottledReadEvents: throttledRead,
92+
ThrottledWriteEvents: throttledRead
7293
}
7394
};
7495
});
@@ -79,6 +100,10 @@ export default class CapacityCalculatorBase {
79100
ReadCapacityUnits: tableConsumedRead.value,
80101
WriteCapacityUnits: tableConsumedWrite.value
81102
},
103+
ThrottledEvents: {
104+
ThrottledReadEvents: tableThrottledRead,
105+
ThrottledWriteEvents: tableThrottledWrite
106+
},
82107
GlobalSecondaryIndexes: gsis
83108
};
84109
} catch (ex) {
@@ -138,6 +163,46 @@ export default class CapacityCalculatorBase {
138163
}
139164
}
140165

166+
async getThrottledEventsAsync(
167+
isRead: boolean, tableName: string, globalSecondaryIndexName: ?string):
168+
Promise<number> {
169+
try {
170+
invariant(isRead != null, 'Parameter \'isRead\' is not set');
171+
invariant(tableName != null, 'Parameter \'tableName\' is not set');
172+
173+
let settings = this.getStatisticSettings();
174+
175+
let EndTime = new Date();
176+
let StartTime = new Date();
177+
StartTime.setTime(EndTime - (60000 * settings.spanMinutes * settings.count));
178+
let MetricName = isRead ? 'ReadThrottleEvents' : 'WriteThrottleEvents';
179+
let Dimensions = this.getDimensions(tableName, globalSecondaryIndexName);
180+
let period = (settings.spanMinutes * 60);
181+
let params = {
182+
Namespace: 'AWS/DynamoDB',
183+
MetricName,
184+
Dimensions,
185+
StartTime,
186+
EndTime,
187+
Period: period,
188+
Statistics: [ settings.type ],
189+
Unit: 'Count'
190+
};
191+
192+
let statistics = await this.cw.getMetricStatisticsAsync(params);
193+
let value = this.getProjectedValue(settings, statistics);
194+
195+
return value;
196+
} catch (ex) {
197+
warning(JSON.stringify({
198+
class: 'CapacityCalculator',
199+
function: 'getThrottledEventsAsync',
200+
isRead, tableName, globalSecondaryIndexName,
201+
}, null, json.padding));
202+
throw ex;
203+
}
204+
}
205+
141206
getDimensions(tableName: string, globalSecondaryIndexName: ?string): Dimension[] {
142207
if (globalSecondaryIndexName) {
143208
return [

src/flow/FlowTypes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
/* @flow */
22
import type { ProvisionedThroughput, Throughput } from 'aws-sdk-promise';
33

4+
export type ThrottledEventsDescription = {
5+
ThrottledReadEvents: number,
6+
ThrottledWriteEvents: number
7+
}
8+
49
export type TableProvisionedAndConsumedThroughput = {
510
TableName: string,
611
IndexName?: string,
712
ProvisionedThroughput: ProvisionedThroughput,
813
ConsumedThroughput: Throughput,
14+
ThrottledEvents: ThrottledEventsDescription
915
};
1016

1117
export type GlobalSecondaryIndexConsumedThroughput = {
@@ -17,6 +23,7 @@ export type TableConsumedCapacityDescription = {
1723
TableName: string,
1824
ConsumedThroughput: Throughput,
1925
GlobalSecondaryIndexes: GlobalSecondaryIndexConsumedThroughput[],
26+
ThrottledEvents: ThrottledEventsDescription
2027
};
2128

2229
export type ConsumedCapacityDesc = {

src/provisioning/ProvisionerConfigurableBase.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export default class ProvisionerConfigurableBase extends ProvisionerBase {
7070
let tableData = {
7171
TableName: tableDescription.TableName,
7272
ProvisionedThroughput: tableDescription.ProvisionedThroughput,
73-
ConsumedThroughput: tableConsumedCapacityDescription.ConsumedThroughput
73+
ConsumedThroughput: tableConsumedCapacityDescription.ConsumedThroughput,
74+
ThrottledEvents: tableConsumedCapacityDescription.ThrottledEvents
7475
};
7576

7677
let provisionedThroughput = this.getUpdatedProvisionedThroughput(tableData);
@@ -177,7 +178,8 @@ export default class ProvisionerConfigurableBase extends ProvisionerBase {
177178
TableName: tableDescription.TableName,
178179
IndexName: gsicc.IndexName,
179180
ProvisionedThroughput: gsi.ProvisionedThroughput,
180-
ConsumedThroughput: gsicc.ConsumedThroughput
181+
ConsumedThroughput: gsicc.ConsumedThroughput,
182+
ThrottledEvents: gsicc.ThrottledEvents
181183
});
182184

183185
// eslint-disable-next-line eqeqeq

0 commit comments

Comments
 (0)