-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathsend-data.js
62 lines (55 loc) · 1.55 KB
/
send-data.js
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
const request = require('../exporters/common/request')
function getHeaders (config, application, reqType) {
const headers = {
'content-type': 'application/json',
'dd-telemetry-api-version': 'v1',
'dd-telemetry-request-type': reqType,
'dd-client-library-language': application.language_name,
'dd-client-library-version': application.tracer_version
}
const debug = config.telemetry && config.telemetry.debug
if (debug) {
headers['dd-telemetry-debug-enabled'] = 'true'
}
return headers
}
let seqId = 0
function getPayload (payload) {
// Some telemetry endpoints payloads accept collections of elements such as the 'logs' endpoint.
// 'logs' request type payload is meant to send library logs to Datadog’s backend.
if (Array.isArray(payload)) {
return payload
} else {
const { logger, tags, serviceMapping, ...trimmedPayload } = payload
return trimmedPayload
}
}
function sendData (config, application, host, reqType, payload = {}) {
const {
hostname,
port,
url
} = config
const options = {
url,
hostname,
port,
method: 'POST',
path: '/telemetry/proxy/api/v2/apmtelemetry',
headers: getHeaders(config, application, reqType)
}
const data = JSON.stringify({
api_version: 'v1',
request_type: reqType,
tracer_time: Math.floor(Date.now() / 1000),
runtime_id: config.tags['runtime-id'],
seq_id: ++seqId,
payload: getPayload(payload),
application,
host
})
request(data, options, () => {
// ignore errors
})
}
module.exports = { sendData }