Skip to content

Commit b9ec9c9

Browse files
committed
fix(exporters): isolate per-request connection agents
1 parent bd1e67f commit b9ec9c9

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

packages/dd-trace/src/exporters/common/request.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ function request (data, options, callback) {
9393

9494
docker.inject(options.headers)
9595

96-
options.agent ??= isSecure ? httpsAgent : httpAgent
96+
const connectionOptions = {
97+
...options,
98+
agent: options.agent ?? (isSecure ? httpsAgent : httpAgent),
99+
}
97100

98101
/**
99102
* @param {import('node:http').IncomingMessage} res
@@ -218,7 +221,7 @@ function request (data, options, callback) {
218221
}
219222
}
220223

221-
const req = client.request(options, (res) => onResponse(res, complete, handleError))
224+
const req = client.request(connectionOptions, (res) => onResponse(res, complete, handleError))
222225

223226
req.once('close', finalize)
224227
req.once('timeout', finalize)

packages/dd-trace/test/exporters/common/request.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ describe('request', function () {
133133
})
134134
})
135135

136+
it('selects a new default agent when callers reuse options with another protocol', (done) => {
137+
const options = {
138+
url: new URL('http://test:123'),
139+
path: '/path',
140+
method: 'GET',
141+
}
142+
nock('http://test:123').get('/path').reply(200, 'OK')
143+
144+
request(Buffer.from(''), options, (httpError) => {
145+
if (httpError) return done(httpError)
146+
147+
assert.strictEqual(options.agent, undefined)
148+
options.url = new URL('https://test:443')
149+
nock('https://test:443').get('/path').reply(200, 'OK')
150+
151+
request(Buffer.from(''), options, (httpsError) => {
152+
assert.strictEqual(options.agent, undefined)
153+
done(httpsError)
154+
})
155+
})
156+
})
157+
136158
it('does not retry when retries are disabled', (done) => {
137159
maxAttempts = 5
138160
const error = Object.assign(new Error('ECONNRESET'), { code: 'ECONNRESET' })

0 commit comments

Comments
 (0)