-
Notifications
You must be signed in to change notification settings - Fork 303
/
config.js
146 lines (126 loc) · 4.03 KB
/
config.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict'
const coalesce = require('koalas')
const os = require('os')
const { URL } = require('url')
const { AgentExporter } = require('./exporters/agent')
const { FileExporter } = require('./exporters/file')
const { ConsoleLogger } = require('./loggers/console')
const CpuProfiler = require('./profilers/cpu')
const WallProfiler = require('./profilers/wall')
const SpaceProfiler = require('./profilers/space')
const { tagger } = require('./tagger')
const {
DD_PROFILING_ENABLED,
DD_PROFILING_PROFILERS,
DD_PROFILING_ENDPOINT_COLLECTION_ENABLED,
DD_ENV,
DD_TAGS,
DD_SERVICE,
DD_VERSION,
DD_TRACE_AGENT_URL,
DD_AGENT_HOST,
DD_TRACE_AGENT_PORT,
DD_PROFILING_UPLOAD_TIMEOUT,
DD_PROFILING_SOURCE_MAP
} = process.env
class Config {
constructor (options = {}) {
const enabled = coalesce(options.enabled, DD_PROFILING_ENABLED, true)
const env = coalesce(options.env, DD_ENV)
const service = options.service || DD_SERVICE || 'node'
const host = os.hostname()
const version = coalesce(options.version, DD_VERSION)
// Must be longer than one minute so pad with five seconds
const flushInterval = coalesce(options.interval, 65 * 1000)
const uploadTimeout = coalesce(options.uploadTimeout,
DD_PROFILING_UPLOAD_TIMEOUT, 60 * 1000)
const sourceMap = coalesce(options.sourceMap,
DD_PROFILING_SOURCE_MAP, true)
const endpointCollection = coalesce(options.endpointCollection,
DD_PROFILING_ENDPOINT_COLLECTION_ENABLED, false)
this.enabled = String(enabled) !== 'false'
this.service = service
this.env = env
this.host = host
this.version = version
this.tags = Object.assign(
tagger.parse(DD_TAGS),
tagger.parse(options.tags),
tagger.parse({ env, host, service, version })
)
this.logger = ensureLogger(options.logger)
this.flushInterval = flushInterval
this.uploadTimeout = uploadTimeout
this.sourceMap = sourceMap
this.endpointCollection = endpointCollection
const hostname = coalesce(options.hostname, DD_AGENT_HOST, 'localhost')
const port = coalesce(options.port, DD_TRACE_AGENT_PORT, 8126)
this.url = new URL(coalesce(options.url, DD_TRACE_AGENT_URL,
`http://${hostname || 'localhost'}:${port || 8126}`))
this.exporters = ensureExporters(options.exporters || [
new AgentExporter(this)
], this)
const profilers = coalesce(options.profilers, DD_PROFILING_PROFILERS, [
new WallProfiler(this),
new SpaceProfiler(this)
])
this.profilers = ensureProfilers(profilers, this)
}
}
module.exports = { Config }
function getExporter (name, options) {
switch (name) {
case 'agent':
return new AgentExporter(options)
case 'file':
return new FileExporter(options)
}
}
function ensureExporters (exporters, options) {
if (typeof exporters === 'string') {
exporters = exporters.split(',')
}
for (let i = 0; i < exporters.length; i++) {
const exporter = exporters[i]
if (typeof exporter === 'string') {
exporters[i] = getExporter(exporter, options)
}
}
return exporters
}
function getProfiler (name, options) {
switch (name) {
case 'cpu':
case 'wall':
return new WallProfiler(options)
case 'space':
return new SpaceProfiler(options)
case 'cpu-experimental':
return new CpuProfiler(options)
default:
options.logger.error(`Unknown profiler "${name}"`)
}
}
function ensureProfilers (profilers, options) {
if (typeof profilers === 'string') {
profilers = profilers.split(',')
}
for (let i = 0; i < profilers.length; i++) {
const profiler = profilers[i]
if (typeof profiler === 'string') {
profilers[i] = getProfiler(profiler, options)
}
}
// Filter out any invalid profilers
return profilers.filter(v => v)
}
function ensureLogger (logger) {
if (typeof logger !== 'object' ||
typeof logger.debug !== 'function' ||
typeof logger.info !== 'function' ||
typeof logger.warn !== 'function' ||
typeof logger.error !== 'function') {
return new ConsoleLogger()
}
return logger
}