Skip to content

Commit

Permalink
fix(opentelemetry-instrumentation-aws-sdk): error when ReturnConsume…
Browse files Browse the repository at this point in the history
…dCapacity is set to None (#899)

* DynamoDB metrics for BatchGetItem fails when ReturnConsumedCapacity is set to NONE

* fix(unit): add unit test and change if

* fix(formatting): wrong formatting

* fix(formatting): formatting

Co-authored-by: Amir Blum <amir@aspecto.io>
  • Loading branch information
pauloprestes and Amir Blum committed Feb 28, 2022
1 parent f436601 commit e7ab4d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Expand Up @@ -64,7 +64,7 @@ export class DynamodbServiceExtension implements ServiceExtension {
const operation = response.request.commandName;

if (operation === 'BatchGetItem') {
if ('ConsumedCapacity' in response.data) {
if (Array.isArray(response.data?.ConsumedCapacity)) {
span.setAttribute(
SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY,
response.data.ConsumedCapacity.map(
Expand Down
Expand Up @@ -196,5 +196,50 @@ describe('DynamoDB', () => {
}
);
});

it('should populate BatchGetIem when consumedCapacity is undefined', done => {
mockV2AwsSend(responseMockSuccess, {
Responses: { 'test-table': [{ key1: { S: 'val1' } }] },
UnprocessedKeys: {},
ConsumedCapacity: undefined,
} as AWS.DynamoDB.Types.BatchGetItemOutput);

const dynamodb = new AWS.DynamoDB.DocumentClient();
const dynamodb_params = {
RequestItems: {
'test-table': {
Keys: [{ key1: { S: 'val1' } }],
ProjectionExpression: 'id',
},
},
ReturnConsumedCapacity: 'NONE',
};
dynamodb.batchGet(
dynamodb_params,
(
err: AWSError,
data: AWS.DynamoDB.DocumentClient.BatchGetItemOutput
) => {
const spans = getTestSpans();
expect(spans.length).toStrictEqual(1);
const attrs = spans[0].attributes;
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual('dynamodb');
expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual(
'BatchGetItem'
);
expect(
attrs[SemanticAttributes.AWS_DYNAMODB_TABLE_NAMES]
).toStrictEqual(['test-table']);
expect(
attrs[SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY]
).toBeUndefined();
expect(
JSON.parse(attrs[SemanticAttributes.DB_STATEMENT] as string)
).toEqual(dynamodb_params);
expect(err).toBeFalsy();
done();
}
);
});
});
});

0 comments on commit e7ab4d0

Please sign in to comment.