Skip to content

Commit

Permalink
feat: Add capacity information when applicable to dynamodb spans (#1365)
Browse files Browse the repository at this point in the history
Co-authored-by: Haddas Bronfman <85441461+haddasbronfman@users.noreply.github.com>
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
4 people authored Aug 12, 2023
1 parent 704f76f commit ad94c5c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 13 deletions.
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
) {
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();
}
);
});
});
});

0 comments on commit ad94c5c

Please sign in to comment.