Skip to content

Commit 48ae52e

Browse files
committed
refactor(telemetry): send the agentless backend URL as a URL object
The agent-telemetry fallback passed the agentless intake endpoint to `request` as a string while the primary agentless path already constructed a `URL`. Build it with `new URL` up front and guard it the same way, so an invalid endpoint is logged and the request skipped rather than throwing from inside the request helper.
1 parent 6125505 commit 48ae52e

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

packages/dd-trace/src/telemetry/send-data.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ function sendData (config, application, host, reqType, payload = {}, cb = () =>
182182
agentTelemetry = false
183183
}
184184
// figure out which data center to send to
185-
const backendUrl = getAgentlessTelemetryEndpoint(config.site)
185+
let backendUrl
186+
try {
187+
backendUrl = new URL(getAgentlessTelemetryEndpoint(config.site))
188+
} catch {
189+
log.error('Invalid Telemetry URL')
190+
}
186191
const backendHeader = { ...options.headers, 'DD-API-KEY': config.apiKey }
187192
const backendOptions = {
188193
...options,
@@ -196,8 +201,6 @@ function sendData (config, application, host, reqType, payload = {}, cb = () =>
196201
log.error('Error sending telemetry data', error)
197202
}
198203
})
199-
} else {
200-
log.error('Invalid Telemetry URL')
201204
}
202205
}
203206

packages/dd-trace/test/telemetry/send-data.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,41 @@ describe('sendData', () => {
199199
const { url } = options
200200
assert.deepStrictEqual(url, new URL('https://my-intake.example/'))
201201
})
202+
203+
it('sends the agentless backend telemetry with a URL object when the agent request fails', () => {
204+
request.yields(new Error('agent unreachable'))
205+
206+
sendDataModule.sendData(
207+
{
208+
apiKey: 'secret-key',
209+
site: 'datadoghq.eu',
210+
tags: { 'runtime-id': '123' },
211+
},
212+
application,
213+
host,
214+
'req-type'
215+
)
216+
217+
assert.strictEqual(request.callCount, 2)
218+
const backendOptions = request.getCall(1).args[1]
219+
assert.deepStrictEqual(backendOptions.url, new URL('https://instrumentation-telemetry-intake.datadoghq.eu'))
220+
assert.strictEqual(backendOptions.headers['DD-API-KEY'], 'secret-key')
221+
})
222+
223+
it('skips the agentless backend request when the endpoint URL is invalid', () => {
224+
request.yields(new Error('agent unreachable'))
225+
226+
sendDataModule.sendData(
227+
{
228+
apiKey: 'secret-key',
229+
site: 'x:notaport',
230+
tags: { 'runtime-id': '123' },
231+
},
232+
application,
233+
host,
234+
'req-type'
235+
)
236+
237+
assert.strictEqual(request.callCount, 1)
238+
})
202239
})

0 commit comments

Comments
 (0)