diff --git a/packages/dd-trace/src/profiling/exporters/agent.js b/packages/dd-trace/src/profiling/exporters/agent.js index b34ab3c9d94..01363d6d2c5 100644 --- a/packages/dd-trace/src/profiling/exporters/agent.js +++ b/packages/dd-trace/src/profiling/exporters/agent.js @@ -195,11 +195,13 @@ class AgentExporter { }) sendRequest(options, form, (err, response) => { - if (operation.retry(err)) { - this._logger.error(`Error from the agent: ${err.message}`) - return - } else if (err) { - reject(err) + if (err) { + const { status } = err + if ((typeof status !== 'number' || status >= 500 || status === 429) && operation.retry(err)) { + this._logger.error(`Error from the agent: ${err.message}`) + } else { + reject(err) + } return } diff --git a/packages/dd-trace/test/profiling/exporters/agent.spec.js b/packages/dd-trace/test/profiling/exporters/agent.spec.js index b318456eebd..8391e14d613 100644 --- a/packages/dd-trace/test/profiling/exporters/agent.spec.js +++ b/packages/dd-trace/test/profiling/exporters/agent.spec.js @@ -303,7 +303,7 @@ describe('exporters/agent', function () { /^Adding wall profile to agent export:( [0-9a-f]{2})+$/, /^Adding space profile to agent export:( [0-9a-f]{2})+$/, /^Submitting profiler agent report attempt #1 to:/i, - /^Error from the agent: HTTP Error 400$/, + /^Error from the agent: HTTP Error 500$/, /^Submitting profiler agent report attempt #2 to:/i, /^Agent export response: ([0-9a-f]{2}( |$))*/ ] @@ -344,7 +344,7 @@ describe('exporters/agent', function () { return } const data = Buffer.from(json) - res.writeHead(400, { + res.writeHead(500, { 'content-type': 'application/json', 'content-length': data.length }) @@ -356,6 +356,43 @@ describe('exporters/agent', function () { waitForResponse ]) }) + + it('should not retry on 4xx errors', async function () { + const exporter = newAgentExporter({ url, logger: { debug: () => {}, error: () => {} } }) + const start = new Date() + const end = new Date() + const tags = { foo: 'bar' } + + const [wall, space] = await Promise.all([ + createProfile(['wall', 'microseconds']), + createProfile(['space', 'bytes']) + ]) + + const profiles = { + wall, + space + } + + let tries = 0 + const json = JSON.stringify({ error: 'some error' }) + app.post('/profiling/v1/input', upload.any(), (_, res) => { + tries++ + const data = Buffer.from(json) + res.writeHead(400, { + 'content-type': 'application/json', + 'content-length': data.length + }) + res.end(data) + }) + + try { + await exporter.export({ profiles, start, end, tags }) + throw new Error('should have thrown') + } catch (err) { + expect(err.message).to.equal('HTTP Error 400') + } + expect(tries).to.equal(1) + }) }) describe('using ipv6', () => {