Skip to content

Commit 9281ba3

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 9281ba3

2 files changed

Lines changed: 49 additions & 10 deletions

File tree

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,25 @@ 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+
return
191+
}
186192
const backendHeader = { ...options.headers, 'DD-API-KEY': config.apiKey }
187193
const backendOptions = {
188194
...options,
189195
url: backendUrl,
190196
headers: backendHeader,
191197
path: '/api/v2/apmtelemetry',
192198
}
193-
if (backendUrl) {
194-
request(data, backendOptions, (error) => {
195-
if (error) {
196-
log.error('Error sending telemetry data', error)
197-
}
198-
})
199-
} else {
200-
log.error('Invalid Telemetry URL')
201-
}
199+
request(data, backendOptions, (error) => {
200+
if (error) {
201+
log.error('Error sending telemetry data', error)
202+
}
203+
})
202204
}
203205

204206
if (!error && !agentTelemetry) {

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)