Skip to content

Commit 110348b

Browse files
committed
Start profilers synchronously within tracer initialization. Source maps are still loaded asynchronously.
1 parent 9b3d7fd commit 110348b

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,11 @@ class Profiler extends EventEmitter {
5353
this.endpointCounts = new Map()
5454
}
5555

56-
start (options) {
57-
return this._start(options).catch((err) => {
58-
logError(options.logger, 'Error starting profiler. For troubleshooting tips, see ' +
59-
'<https://dtdg.co/nodejs-profiler-troubleshooting>', err)
60-
return false
61-
})
62-
}
63-
6456
_logError (err) {
6557
logError(this._logger, err)
6658
}
6759

68-
async _start (options) {
60+
start (options) {
6961
if (this._enabled) return true
7062

7163
const config = this._config = new Config(options)
@@ -77,19 +69,34 @@ class Profiler extends EventEmitter {
7769
// Log errors if the source map finder fails, but don't prevent the rest
7870
// of the profiler from running without source maps.
7971
let mapper
80-
try {
81-
const { setLogger, SourceMapper } = require('@datadog/pprof')
82-
setLogger(config.logger)
83-
84-
mapper = await maybeSourceMap(config.sourceMap, SourceMapper, config.debugSourceMaps)
85-
if (config.sourceMap && config.debugSourceMaps) {
86-
this._logger.debug(() => {
87-
return mapper.infoMap.size === 0
88-
? 'Found no source maps'
89-
: `Found source maps for following files: [${[...mapper.infoMap.keys()].join(', ')}]`
72+
if (config.sourceMap) {
73+
try {
74+
const { setLogger, SourceMapper } = require('@datadog/pprof')
75+
setLogger(config.logger)
76+
77+
let loadedMapper
78+
maybeSourceMap(SourceMapper, config.debugSourceMaps).then((sourceMap) => {
79+
loadedMapper = sourceMap
80+
if (config.debugSourceMaps) {
81+
this._logger.debug(() => {
82+
return sourceMap.infoMap.size === 0
83+
? 'Found no source maps'
84+
: `Found source maps for following files: [${[...mapper.infoMap.keys()].join(', ')}]`
85+
})
86+
}
87+
}).catch((err) => {
88+
this._logError(err)
9089
})
90+
mapper = {
91+
hasMappingInfo: (p) => loadedMapper?.hasMappingInfo(p) ?? false,
92+
mappingInfo: (l) => loadedMapper?.mappingInfo(l) ?? l
93+
}
94+
} catch (err) {
95+
this._logError(err)
9196
}
97+
}
9298

99+
try {
93100
const clevel = config.uploadCompression.level
94101
switch (config.uploadCompression.method) {
95102
case 'gzip':

packages/dd-trace/src/proxy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ class Tracer extends NoopProxy {
159159
ssiHeuristics.start()
160160
let mockProfiler = null
161161
if (config.profiling.enabled === 'true') {
162-
this._profilerStarted = this._startProfiler(config)
162+
this._startProfiler(config)
163+
this._profilerStarted = Promise.resolve(true)
163164
} else if (ssiHeuristics.emitsTelemetry) {
164165
// Start a mock profiler that emits mock profile-submitted events for the telemetry.
165166
// It will be stopped if the real profiler is started by the heuristics.

0 commit comments

Comments
 (0)