Skip to content

Commit f493b19

Browse files
authored
Add config tests for OOM heap profiler (#2943)
* Use isTrue in profiling config * Add OOM heap profiler config tests * Deduplicate OOM export strategies
1 parent fcca1ad commit f493b19

File tree

2 files changed

+86
-26
lines changed

2 files changed

+86
-26
lines changed

packages/dd-trace/src/profiling/config.js

+27-26
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,32 @@ const WallProfiler = require('./profilers/wall')
1212
const SpaceProfiler = require('./profilers/space')
1313
const { oomExportStrategies, snapshotKinds } = require('./constants')
1414
const { tagger } = require('./tagger')
15-
16-
const {
17-
DD_PROFILING_ENABLED,
18-
DD_PROFILING_PROFILERS,
19-
DD_PROFILING_ENDPOINT_COLLECTION_ENABLED,
20-
DD_ENV,
21-
DD_TAGS,
22-
DD_SERVICE,
23-
DD_VERSION,
24-
DD_TRACE_AGENT_URL,
25-
DD_AGENT_HOST,
26-
DD_TRACE_AGENT_PORT,
27-
DD_PROFILING_UPLOAD_TIMEOUT,
28-
DD_PROFILING_SOURCE_MAP,
29-
DD_PROFILING_UPLOAD_PERIOD,
30-
DD_PROFILING_PPROF_PREFIX,
31-
DD_PROFILING_EXPERIMENTAL_OOM_MONITORING_ENABLED,
32-
DD_PROFILING_EXPERIMENTAL_OOM_HEAP_LIMIT_EXTENSION_SIZE,
33-
DD_PROFILING_EXPERIMENTAL_OOM_MAX_HEAP_EXTENSION_COUNT,
34-
DD_PROFILING_EXPERIMENTAL_OOM_EXPORT_STRATEGIES
35-
} = process.env
15+
const { isTrue } = require('../util')
3616

3717
class Config {
3818
constructor (options = {}) {
39-
const enabled = coalesce(options.enabled, DD_PROFILING_ENABLED, true)
19+
const {
20+
DD_PROFILING_ENABLED,
21+
DD_PROFILING_PROFILERS,
22+
DD_PROFILING_ENDPOINT_COLLECTION_ENABLED,
23+
DD_ENV,
24+
DD_TAGS,
25+
DD_SERVICE,
26+
DD_VERSION,
27+
DD_TRACE_AGENT_URL,
28+
DD_AGENT_HOST,
29+
DD_TRACE_AGENT_PORT,
30+
DD_PROFILING_UPLOAD_TIMEOUT,
31+
DD_PROFILING_SOURCE_MAP,
32+
DD_PROFILING_UPLOAD_PERIOD,
33+
DD_PROFILING_PPROF_PREFIX,
34+
DD_PROFILING_EXPERIMENTAL_OOM_MONITORING_ENABLED,
35+
DD_PROFILING_EXPERIMENTAL_OOM_HEAP_LIMIT_EXTENSION_SIZE,
36+
DD_PROFILING_EXPERIMENTAL_OOM_MAX_HEAP_EXTENSION_COUNT,
37+
DD_PROFILING_EXPERIMENTAL_OOM_EXPORT_STRATEGIES
38+
} = process.env
39+
40+
const enabled = isTrue(coalesce(options.enabled, DD_PROFILING_ENABLED, true))
4041
const env = coalesce(options.env, DD_ENV)
4142
const service = options.service || DD_SERVICE || 'node'
4243
const host = os.hostname()
@@ -53,7 +54,7 @@ class Config {
5354
const pprofPrefix = coalesce(options.pprofPrefix,
5455
DD_PROFILING_PPROF_PREFIX)
5556

56-
this.enabled = String(enabled) !== 'false'
57+
this.enabled = enabled
5758
this.service = service
5859
this.env = env
5960
this.host = host
@@ -84,8 +85,8 @@ class Config {
8485
new AgentExporter(this)
8586
], this)
8687

87-
const oomMonitoringEnabled = coalesce(options.oomMonitoring,
88-
DD_PROFILING_EXPERIMENTAL_OOM_MONITORING_ENABLED, false)
88+
const oomMonitoringEnabled = isTrue(coalesce(options.oomMonitoring,
89+
DD_PROFILING_EXPERIMENTAL_OOM_MONITORING_ENABLED, false))
8990
const heapLimitExtensionSize = coalesce(options.oomHeapLimitExtensionSize,
9091
Number(DD_PROFILING_EXPERIMENTAL_OOM_HEAP_LIMIT_EXTENSION_SIZE), 0)
9192
const maxHeapExtensionCount = coalesce(options.oomMaxHeapExtensionCount,
@@ -136,7 +137,7 @@ function ensureOOMExportStrategies (strategies, options) {
136137
}
137138
}
138139

139-
return strategies
140+
return [ ...new Set(strategies) ]
140141
}
141142

142143
function getExporter (name, options) {

packages/dd-trace/test/profiling/config.spec.js

+59
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require('../setup/tap')
44

55
const { expect } = require('chai')
66
const os = require('os')
7+
const path = require('path')
78
const { AgentExporter } = require('../../src/profiling/exporters/agent')
89
const { FileExporter } = require('../../src/profiling/exporters/file')
910
const CpuProfiler = require('../../src/profiling/profilers/cpu')
@@ -13,9 +14,16 @@ const { ConsoleLogger } = require('../../src/profiling/loggers/console')
1314

1415
describe('config', () => {
1516
let Config
17+
let env
1618

1719
beforeEach(() => {
1820
Config = require('../../src/profiling/config').Config
21+
env = process.env
22+
process.env = {}
23+
})
24+
25+
afterEach(() => {
26+
process.env = env
1927
})
2028

2129
it('should have the correct defaults', () => {
@@ -136,4 +144,55 @@ describe('config', () => {
136144

137145
expect(exporterUrl).to.equal(expectedUrl)
138146
})
147+
148+
it('should disable OOM heap profiler by default', () => {
149+
const config = new Config()
150+
expect(config.oomMonitoring).to.deep.equal({
151+
enabled: false,
152+
heapLimitExtensionSize: 0,
153+
maxHeapExtensionCount: 0,
154+
exportStrategies: [],
155+
exportCommand: undefined
156+
})
157+
})
158+
159+
it('should support OOM heap profiler configuration', () => {
160+
process.env = {
161+
DD_PROFILING_EXPERIMENTAL_OOM_MONITORING_ENABLED: 'false'
162+
}
163+
const config = new Config({})
164+
165+
expect(config.oomMonitoring).to.deep.equal({
166+
enabled: false,
167+
heapLimitExtensionSize: 0,
168+
maxHeapExtensionCount: 0,
169+
exportStrategies: [],
170+
exportCommand: undefined
171+
})
172+
})
173+
174+
it('should support OOM heap profiler configuration', () => {
175+
process.env = {
176+
DD_PROFILING_EXPERIMENTAL_OOM_MONITORING_ENABLED: '1',
177+
DD_PROFILING_EXPERIMENTAL_OOM_HEAP_LIMIT_EXTENSION_SIZE: '1000000',
178+
DD_PROFILING_EXPERIMENTAL_OOM_MAX_HEAP_EXTENSION_COUNT: '2',
179+
DD_PROFILING_EXPERIMENTAL_OOM_EXPORT_STRATEGIES: 'process,interrupt,async,interrupt'
180+
}
181+
182+
const config = new Config({})
183+
184+
expect(config.oomMonitoring).to.deep.equal({
185+
enabled: true,
186+
heapLimitExtensionSize: 1000000,
187+
maxHeapExtensionCount: 2,
188+
exportStrategies: ['process', 'interrupt', 'async'],
189+
exportCommand: [
190+
process.execPath,
191+
path.normalize(path.join(__dirname, '../../src/profiling', 'exporter_cli.js')),
192+
'http://localhost:8126/',
193+
`host:${config.host},service:node,snapshot:on_oom`,
194+
'space'
195+
]
196+
})
197+
})
139198
})

0 commit comments

Comments
 (0)