-
Notifications
You must be signed in to change notification settings - Fork 309
/
writer.js
109 lines (90 loc) · 3.22 KB
/
writer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict'
const request = require('../common/request')
const { startupLog } = require('../../startup-log')
const runtimeMetrics = require('../../runtime_metrics')
const log = require('../../log')
const tracerVersion = require('../../../../../package.json').version
const BaseWriter = require('../common/writer')
const METRIC_PREFIX = 'datadog.tracer.node.exporter.agent'
class Writer extends BaseWriter {
constructor ({ prioritySampler, lookup, protocolVersion, headers }) {
super(...arguments)
const AgentEncoder = getEncoder(protocolVersion)
this._prioritySampler = prioritySampler
this._lookup = lookup
this._protocolVersion = protocolVersion
this._encoder = new AgentEncoder(this)
this._headers = headers
}
_sendPayload (data, count, done) {
runtimeMetrics.increment(`${METRIC_PREFIX}.requests`, true)
const { _headers, _lookup, _protocolVersion, _url } = this
makeRequest(_protocolVersion, data, count, _url, _headers, _lookup, true, (err, res, status) => {
if (status) {
runtimeMetrics.increment(`${METRIC_PREFIX}.responses`, true)
runtimeMetrics.increment(`${METRIC_PREFIX}.responses.by.status`, `status:${status}`, true)
} else if (err) {
runtimeMetrics.increment(`${METRIC_PREFIX}.errors`, true)
runtimeMetrics.increment(`${METRIC_PREFIX}.errors.by.name`, `name:${err.name}`, true)
if (err.code) {
runtimeMetrics.increment(`${METRIC_PREFIX}.errors.by.code`, `code:${err.code}`, true)
}
}
startupLog({ agentError: err })
if (err) {
log.error(err)
done()
return
}
log.debug(`Response from the agent: ${res}`)
try {
this._prioritySampler.update(JSON.parse(res).rate_by_service)
} catch (e) {
log.error(e)
runtimeMetrics.increment(`${METRIC_PREFIX}.errors`, true)
runtimeMetrics.increment(`${METRIC_PREFIX}.errors.by.name`, `name:${e.name}`, true)
}
done()
})
}
}
function setHeader (headers, key, value) {
if (value) {
headers[key] = value
}
}
function getEncoder (protocolVersion) {
if (protocolVersion === '0.5') {
return require('../../encode/0.5').AgentEncoder
} else {
return require('../../encode/0.4').AgentEncoder
}
}
function makeRequest (version, data, count, url, headers, lookup, needsStartupLog, cb) {
const options = {
path: `/v${version}/traces`,
method: 'PUT',
headers: {
...headers,
'Content-Type': 'application/msgpack',
'Datadog-Meta-Tracer-Version': tracerVersion,
'X-Datadog-Trace-Count': String(count)
},
lookup,
url
}
setHeader(options.headers, 'Datadog-Meta-Lang', 'nodejs')
setHeader(options.headers, 'Datadog-Meta-Lang-Version', process.version)
setHeader(options.headers, 'Datadog-Meta-Lang-Interpreter', process.jsEngine || 'v8')
log.debug(() => `Request to the agent: ${JSON.stringify(options)}`)
request(data, options, (err, res, status) => {
if (needsStartupLog) {
// Note that logging will only happen once, regardless of how many times this is called.
startupLog({
agentError: status !== 404 && status !== 200 ? err : undefined
})
}
cb(err, res, status)
})
}
module.exports = Writer