|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { format } = require('node:url') |
| 4 | + |
| 5 | +const { HttpsProxyAgent } = require('https-proxy-agent') |
| 6 | +const { getProxyForUrl } = require('proxy-from-env') |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {object} DirectEVPRoute |
| 10 | + * @property {'direct'} mode - Route mode |
| 11 | + * @property {URL} url - Direct intake URL |
| 12 | + * @property {string} basePath - Direct intake base path |
| 13 | + * @property {object} headers - Direct intake authentication headers |
| 14 | + * @property {import('node:https').Agent} [agent] - Optional HTTPS proxy agent |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates an authenticated direct EVP intake route. |
| 19 | + * |
| 20 | + * This helper does not perform local receiver discovery. |
| 21 | + * |
| 22 | + * @param {import('../config/config-base')} config - Tracer configuration |
| 23 | + * @param {string} intake - EVP intake subdomain |
| 24 | + * @returns {DirectEVPRoute|undefined} Direct route when credentials and site are available |
| 25 | + */ |
| 26 | +function createDirectEVPRoute (config, intake) { |
| 27 | + const apiKey = config.DD_API_KEY |
| 28 | + if (!apiKey || !config.site) return |
| 29 | + |
| 30 | + try { |
| 31 | + const url = new URL(format({ |
| 32 | + protocol: 'https:', |
| 33 | + hostname: `${intake}.${config.site}`, |
| 34 | + })) |
| 35 | + const proxyUrl = getProxyForUrl(url.href) |
| 36 | + const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined |
| 37 | + |
| 38 | + return { |
| 39 | + mode: 'direct', |
| 40 | + url, |
| 41 | + basePath: '', |
| 42 | + headers: { |
| 43 | + 'DD-API-KEY': apiKey, |
| 44 | + }, |
| 45 | + ...(agent && { agent }), |
| 46 | + } |
| 47 | + } catch {} |
| 48 | +} |
| 49 | + |
| 50 | +module.exports = { createDirectEVPRoute } |
0 commit comments