Skip to content

Commit

Permalink
Mark crashes that happen during collecting profiles as `profiler_seri…
Browse files Browse the repository at this point in the history
…alizing:1` (#5096)
  • Loading branch information
szegedi authored Jan 16, 2025
1 parent 6523d94 commit 26722b3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"node": ">=18"
},
"dependencies": {
"@datadog/libdatadog": "^0.3.0",
"@datadog/libdatadog": "^0.4.0",
"@datadog/native-appsec": "8.4.0",
"@datadog/native-iast-rewriter": "2.6.1",
"@datadog/native-iast-taint-tracking": "3.2.0",
Expand Down
9 changes: 9 additions & 0 deletions packages/dd-trace/src/crashtracking/crashtracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ class Crashtracker {
}
}

withProfilerSerializing (f) {
binding.beginProfilerSerializing()
try {
return f()
} finally {
binding.endProfilerSerializing()
}
}

// TODO: Send only configured values when defaults are fixed.
_getConfig (config) {
const { hostname = '127.0.0.1', port = 8126 } = config
Expand Down
3 changes: 3 additions & 0 deletions packages/dd-trace/src/crashtracking/noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
class NoopCrashtracker {
configure () {}
start () {}
withProfilerSerializing (f) {
return f()
}
}

module.exports = new NoopCrashtracker()
19 changes: 11 additions & 8 deletions packages/dd-trace/src/profiling/profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { snapshotKinds } = require('./constants')
const { threadNamePrefix } = require('./profilers/shared')
const { isWebServerSpan, endpointNameFromTags, getStartedSpans } = require('./webspan-utils')
const dc = require('dc-polyfill')
const crashtracker = require('../crashtracking')

const profileSubmittedChannel = dc.channel('datadog:profiling:profile-submitted')
const spanFinishedChannel = dc.channel('dd-trace:span:finish')
Expand Down Expand Up @@ -197,15 +198,17 @@ class Profiler extends EventEmitter {
throw new Error('No profile types configured.')
}

// collect profiles synchronously so that profilers can be safely stopped asynchronously
for (const profiler of this._config.profilers) {
const profile = profiler.profile(restart, startDate, endDate)
if (!restart) {
this._logger.debug(`Stopped ${profiler.type} profiler in ${threadNamePrefix} thread`)
crashtracker.withProfilerSerializing(() => {
// collect profiles synchronously so that profilers can be safely stopped asynchronously
for (const profiler of this._config.profilers) {
const profile = profiler.profile(restart, startDate, endDate)
if (!restart) {
this._logger.debug(`Stopped ${profiler.type} profiler in ${threadNamePrefix} thread`)
}
if (!profile) continue
profiles.push({ profiler, profile })
}
if (!profile) continue
profiles.push({ profiler, profile })
}
})

if (restart) {
this._capture(this._timeoutInterval, endDate)
Expand Down

0 comments on commit 26722b3

Please sign in to comment.