Skip to content

Commit

Permalink
Improve metrics logging to derive RCU and WCU from CU where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
henhal committed Jun 25, 2024
1 parent eb268c2 commit fbe9941
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/DynamoWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ type DynamoDbInput = ServiceInputTypes;
type DynamoDbOutput = ServiceOutputTypes;
type DynamoDbCommandExecutor<I extends DynamoDbInput, O extends DynamoDbOutput> = (client: DynamoDBDocumentClient, cmd: DynamoDbCommand<I, O>) => Promise<O>;

function getCommandType(command: string) {
switch (command) {
case 'BatchGetItemCommandInput':
case 'GetItemCommandInput':
case 'QueryCommandInput':
case 'ScanCommandInput':
return 'read';

case 'BatchWriteItemCommandInput':
case 'DeleteItemCommandInput':
case 'PutItemCommandInput':
case 'UpdateItemCommandInput':
return 'write';
}
}

function truncate(data: unknown): unknown {
if (data == null) {
return data;
Expand Down Expand Up @@ -51,7 +67,7 @@ export abstract class DynamoWrapper {
this.logger?.debug({input: truncate(input)}, `DynamoDB ${command} input`);
const output = await executor(this.client.dc, cmd);
if ('ConsumedCapacity' in output) {
this.logConsumedCapacity(output.ConsumedCapacity);
this.logConsumedCapacity(command, output.ConsumedCapacity);
}
this.logger?.debug({output: truncate(output)}, `DynamoDB ${command} output`);
return output;
Expand All @@ -61,16 +77,17 @@ export abstract class DynamoWrapper {
}
}

private logConsumedCapacity(consumedCapacity: ConsumedCapacity | ConsumedCapacity[] = []): void {
private logConsumedCapacity(command: string, consumedCapacity: ConsumedCapacity | ConsumedCapacity[] = []): void {
const type = getCommandType(command);
const items = Array.isArray(consumedCapacity) ? consumedCapacity : [consumedCapacity];
const tableMetrics = this.client.getTableMetrics();

for (const item of items) {
const {
TableName: tableName,
ReadCapacityUnits: rcu ,
WriteCapacityUnits: wcu ,
CapacityUnits: cu
CapacityUnits: cu,
ReadCapacityUnits: rcu = type === 'read' ? cu : undefined ,
WriteCapacityUnits: wcu = type === 'write' ? cu : undefined
} = item;

if (tableName) {
Expand Down

0 comments on commit fbe9941

Please sign in to comment.