Skip to content

Commit 46db106

Browse files
authored
fix(aws-sdk): do not patch client config deserialization multiple times (#6991)
Fixes: #6985
1 parent 4b11d91 commit 46db106

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

integration-tests/helpers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@ async function spawnPluginIntegrationTestProc (cwd, serverFile, agentPort, stdio
578578
env = { ...process.env, ...env, ...additionalEnvArgs }
579579
return spawnProc(path.join(cwd, serverFile), {
580580
cwd,
581-
env
581+
env,
582+
execArgv: additionalEnvArgs.execArgv
582583
}, stdioHandler)
583584
}
584585

packages/datadog-instrumentations/src/aws-sdk.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const { channel, addHook } = require('./helpers/instrument')
44
const shimmer = require('../../datadog-shimmer')
55

6+
const patchedClientConfigProtocols = new WeakSet()
7+
68
function wrapRequest (send) {
79
return function wrappedRequest (cb) {
810
if (!this.service) return send.apply(this, arguments)
@@ -67,12 +69,14 @@ function wrapSmithySend (send) {
6769

6870
if (typeof command.deserialize === 'function') {
6971
shimmer.wrap(command, 'deserialize', deserialize => wrapDeserialize(deserialize, channelSuffix))
70-
} else if (this.config?.protocol?.deserializeResponse) {
72+
} else if (this.config?.protocol?.deserializeResponse && !patchedClientConfigProtocols.has(this.config.protocol)) {
7173
shimmer.wrap(
7274
this.config.protocol,
7375
'deserializeResponse',
7476
deserializeResponse => wrapDeserialize(deserializeResponse, channelSuffix, 2)
7577
)
78+
79+
patchedClientConfigProtocols.add(this.config.protocol)
7680
}
7781

7882
const ctx = {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'dd-trace/init.js'
2+
import {
3+
SQSClient,
4+
CreateQueueCommand,
5+
ReceiveMessageCommand,
6+
} from '@aws-sdk/client-sqs'
7+
8+
const client = new SQSClient({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' })
9+
10+
// Create queue first
11+
const createRes = await client.send(new CreateQueueCommand({ QueueName: 'test-queue' }))
12+
const queueUrl = createRes.QueueUrl
13+
14+
// Send many receive message commands to trigger recursion
15+
for (let i = 0; i < 500; i++) {
16+
client.send(new ReceiveMessageCommand({ QueueUrl: queueUrl, MessageAttributeNames: ['.*'] }))
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict'
2+
3+
const {
4+
FakeAgent,
5+
sandboxCwd,
6+
useSandbox,
7+
checkSpansForServiceName,
8+
spawnPluginIntegrationTestProc
9+
} = require('../../../../integration-tests/helpers')
10+
const { withVersions } = require('../../../dd-trace/test/setup/mocha')
11+
12+
const assert = require('node:assert')
13+
14+
describe('recursion regression test', () => {
15+
let agent
16+
let proc
17+
18+
withVersions('aws-sdk', ['@aws-sdk/smithy-client'], version => {
19+
useSandbox([`'@aws-sdk/client-sqs'@${version}'`], false, [
20+
'./packages/datadog-plugin-aws-sdk/test/integration-test/*'])
21+
22+
beforeEach(async () => {
23+
agent = await new FakeAgent().start()
24+
})
25+
26+
afterEach(async () => {
27+
proc && proc.kill()
28+
await agent.stop()
29+
})
30+
31+
it('does not cause a recursion error when many commands are sent', async () => {
32+
const res = agent.assertMessageReceived(({ headers, payload }) => {
33+
assert.equal(headers.host, `127.0.0.1:${agent.port}`)
34+
assert.ok(Array.isArray(payload))
35+
assert.strictEqual(checkSpansForServiceName(payload, 'aws.request'), true)
36+
})
37+
38+
proc = await spawnPluginIntegrationTestProc(sandboxCwd(), 'recursion.mjs', agent.port, undefined,
39+
{
40+
AWS_SECRET_ACCESS_KEY: '0000000000/00000000000000000000000000000',
41+
AWS_ACCESS_KEY_ID: '00000000000000000000',
42+
execArgv: ['--stack-size=128']
43+
}
44+
)
45+
46+
await res
47+
}).timeout(20000)
48+
})
49+
})

0 commit comments

Comments
 (0)