Skip to content

Commit b070889

Browse files
authored
Add parallelism information to profiles (#4765)
1 parent 9e36df0 commit b070889

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

packages/dd-trace/src/profiling/exporters/event_serializer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ const os = require('os')
22
const perf = require('perf_hooks').performance
33
const version = require('../../../../../package.json').version
44

5+
const libuvThreadPoolSize = (() => {
6+
const ss = process.env.UV_THREADPOOL_SIZE
7+
if (ss === undefined) {
8+
// Backend will apply the default size based on Node version.
9+
return undefined
10+
}
11+
// libuv uses atoi to parse the value, which is almost the same as parseInt, except that parseInt
12+
// will return NaN on invalid input, while atoi will return 0. This is handled at return.
13+
const s = parseInt(ss)
14+
// We dont' interpret the value further here in the library. Backend will interpret the number
15+
// based on Node version. In all currently known Node versions, 0 results in 1 worker thread,
16+
// negative values (because they're assigned to an unsigned int) become very high positive values,
17+
// and the value is finally capped at 1024.
18+
return isNaN(s) ? 0 : s
19+
})()
20+
521
class EventSerializer {
622
constructor ({ env, host, service, version, libraryInjected, activation } = {}) {
723
this._env = env
@@ -56,11 +72,16 @@ class EventSerializer {
5672
version
5773
},
5874
runtime: {
75+
// os.availableParallelism only available in node 18.14.0/19.4.0 and above
76+
available_processors: typeof os.availableParallelism === 'function'
77+
? os.availableParallelism()
78+
: os.cpus().length,
5979
// Using `nodejs` for consistency with the existing `runtime` tag.
6080
// Note that the event `family` property uses `node`, as that's what's
6181
// proscribed by the Intake API, but that's an internal enum and is
6282
// not customer visible.
6383
engine: 'nodejs',
84+
libuv_threadpool_size: libuvThreadPoolSize,
6485
// strip off leading 'v'. This makes the format consistent with other
6586
// runtimes (e.g. Ruby) but not with the existing `runtime_version` tag.
6687
// We'll keep it like this as we want cross-engine consistency. We

packages/dd-trace/test/profiling/exporters/agent.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ describe('exporters/agent', function () {
115115
expect(event.info.profiler.ssi).to.have.property('mechanism', 'none')
116116
expect(event.info.profiler).to.have.property('version', version)
117117
expect(event.info).to.have.property('runtime')
118-
expect(Object.keys(event.info.runtime)).to.have.length(2)
118+
expect(Object.keys(event.info.runtime)).to.have.length(3)
119+
expect(event.info.runtime).to.have.property('available_processors')
119120
expect(event.info.runtime).to.have.property('engine', 'nodejs')
120121
expect(event.info.runtime).to.have.property('version', process.version.substring(1))
121122

0 commit comments

Comments
 (0)