-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathdirect.js
More file actions
64 lines (56 loc) · 1.7 KB
/
Copy pathdirect.js
File metadata and controls
64 lines (56 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict'
const { format } = require('node:url')
const { HttpsProxyAgent } = require('https-proxy-agent')
const { getProxyForUrl } = require('proxy-from-env')
const log = require('../log')
/**
* @typedef {object} DirectEVPRoute
* @property {URL} url - Direct intake URL
* @property {string} basePath - Direct intake base path
* @property {object} headers - Direct intake authentication headers
* @property {import('node:https').Agent} [agent] - Optional HTTPS proxy agent
*/
/**
* Creates an authenticated direct EVP intake route.
*
* This helper does not perform local receiver discovery.
*
* @param {import('../config/config-base')} config - Tracer configuration
* @param {string} intake - EVP intake subdomain
* @returns {DirectEVPRoute|undefined} Direct route when credentials and site are available
*/
function createDirectEVPRoute (config, intake) {
const apiKey = config.DD_API_KEY
if (!apiKey || !config.site) return
try {
const hostname = `${intake}.${config.site}`.toLowerCase()
const url = new URL(format({
protocol: 'https:',
hostname,
}))
if (
url.hostname !== hostname ||
url.username ||
url.password ||
url.port ||
url.pathname !== '/' ||
url.search ||
url.hash
) {
throw new Error('Invalid direct EVP intake URL')
}
const proxyUrl = getProxyForUrl(url.href)
const agent = proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined
return {
url,
basePath: '',
headers: {
'DD-API-KEY': apiKey,
},
...(agent && { agent }),
}
} catch (error) {
log.debug('Unable to configure direct EVP intake: %s', error.message)
}
}
module.exports = { createDirectEVPRoute }