Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add capacity information when applicable to dynamodb spans #1365

Merged
merged 7 commits into from
Aug 12, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,16 @@ export class DynamodbServiceExtension implements ServiceExtension {
responseHook(
response: NormalizedResponse,
span: Span,
tracer: Tracer,
config: AwsSdkInstrumentationConfig
_tracer: Tracer,
_config: AwsSdkInstrumentationConfig
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
) {
const operation = response.request.commandName;

if (operation === 'BatchGetItem') {
if (Array.isArray(response.data?.ConsumedCapacity)) {
span.setAttribute(
SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY,
response.data.ConsumedCapacity.map(
(x: { [DictionaryKey: string]: any }) => JSON.stringify(x)
)
);
}
if (response.data?.ConsumedCapacity) {
span.setAttribute(
SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY,
toArray(response.data.ConsumedCapacity).map(
(x: { [DictionaryKey: string]: any }) => JSON.stringify(x)
)
);
}

if (response.data?.ItemCollectionMetrics) {
Expand Down Expand Up @@ -241,3 +237,7 @@ export class DynamodbServiceExtension implements ServiceExtension {
}
}
}

function toArray<T>(values: T | T[]): T[] {
return Array.isArray(values) ? values : [values];
}
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,78 @@ describe('DynamoDB', () => {
);
});
});

describe('ConsumedCapacity', () => {
it('should populate ConsumedCapacity attributes when they exist', done => {
mockV2AwsSend(responseMockSuccess, {
ConsumedCapacity: {
TableName: 'test-table',
CapacityUnits: 0.5,
Table: { CapacityUnits: 0.5 },
},
} as AWS.DynamoDB.Types.PutItemOutput);

const dynamodb = new AWS.DynamoDB.DocumentClient();
dynamodb.put(
{
TableName: 'test-table',
Item: { key1: 'val1' },
ReturnConsumedCapacity: 'INDEXES',
},
(err: AWSError, data: AWS.DynamoDB.DocumentClient.PutItemOutput) => {
const spans = getTestSpans();
expect(spans.length).toStrictEqual(1);
const attrs = spans[0].attributes;
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual(
DbSystemValues.DYNAMODB
);
expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual(
'PutItem'
);
expect(
attrs[SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY]
).toStrictEqual([
JSON.stringify({
TableName: 'test-table',
CapacityUnits: 0.5,
Table: { CapacityUnits: 0.5 },
}),
]);
expect(err).toBeFalsy();
done();
}
);
});

it('should not populate ConsumedCapacity attributes when it is not returned', done => {
mockV2AwsSend(responseMockSuccess, {
ConsumedCapacity: undefined,
} as AWS.DynamoDB.Types.PutItemOutput);

const dynamodb = new AWS.DynamoDB.DocumentClient();
dynamodb.put(
{
TableName: 'test-table',
Item: { key1: 'val1' },
ReturnConsumedCapacity: 'NONE',
},
(err: AWSError, data: AWS.DynamoDB.DocumentClient.PutItemOutput) => {
const spans = getTestSpans();
expect(spans.length).toStrictEqual(1);
const attrs = spans[0].attributes;
expect(attrs[SemanticAttributes.DB_SYSTEM]).toStrictEqual(
DbSystemValues.DYNAMODB
);
expect(attrs[SemanticAttributes.DB_OPERATION]).toStrictEqual(
'PutItem'
);
expect(attrs).not.toHaveProperty(
SemanticAttributes.AWS_DYNAMODB_CONSUMED_CAPACITY
);
expect(err).toBeFalsy();
done();
}
);
});
});
});