Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sample collection when using worker threads #309

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions injects/sampler.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
'use strict'

// Important to keep in mind that every Node.js worker thread
// will run it's own instance of the sample.js.

const fs = require('fs')
const makeDir = require('mkdirp')
const systemInfo = require('../collect/system-info.js')
const ProcessStat = require('../collect/process-stat.js')
const getLoggingPaths = require('@nearform/clinic-common').getLoggingPaths('doctor')
const ProcessStatEncoder = require('../format/process-stat-encoder.js')
const { isMainThread, threadId } = require('worker_threads')

// create encoding files and directory
const paths = getLoggingPaths({ path: process.env.NODE_CLINIC_DOCTOR_DATA_PATH, identifier: process.pid })

makeDir.sync(paths['/'])

// write system file
fs.writeFileSync(paths['/systeminfo'], JSON.stringify(systemInfo(), null, 2))
// write system file only on the main thread
if (isMainThread) {
fs.writeFileSync(paths['/systeminfo'], JSON.stringify(systemInfo(), null, 2))
}

const processStatEncoder = new ProcessStatEncoder()
const out = processStatEncoder.pipe(fs.createWriteStream(paths['/processstat']))

// If sampling in a worker thread, we have to write samples
// out to a separate processstat file.
let outpath = paths['/processstat']
if (!isMainThread) {
outpath += `-${threadId}`
}

const out = processStatEncoder.pipe(fs.createWriteStream(outpath))

// sample every 10ms
const processStat = new ProcessStat(parseInt(
Expand Down
37 changes: 37 additions & 0 deletions test/cmd-collect-analysing-worker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'

const { test } = require('tap')
const rimraf = require('rimraf')
const ClinicDoctor = require('../index.js')

test('cmd - test collect - emits "analysing" event', function (t) {
const tool = new ClinicDoctor()

function cleanup (err, dirname) {
t.ifError(err)
t.match(dirname, /^[0-9]+\.clinic-doctor/)
rimraf(dirname, (err) => {
t.ifError(err)
t.end()
})
}

let seenAnalysing = false
tool.on('analysing', () => {
seenAnalysing = true
})

const expr = 'setTimeout(() => {}, 123);' +
'const { Worker } = require("worker_threads");' +
'new Worker("setTimeout(() => {}, 123);", { eval: true });'

tool.collect(
[process.execPath, '-e', expr],
function (err, dirname) {
if (err) return cleanup(err, dirname)

t.ok(seenAnalysing) // should've happened before this callback
cleanup(null, dirname)
}
)
})