-
Notifications
You must be signed in to change notification settings - Fork 39
fix(pino): skip instrumentation for pino thread-stream workers #2130
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
base: main
Are you sure you want to change the base?
Conversation
| * Pino uses background worker threads for "thread-stream" logging, which should not be instrumented. | ||
| * @returns {boolean} | ||
| */ | ||
| function isPinoThreadStreamWorker() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So yeah.
I think the decision in #517 fixed the metrics bug (wrong process name).
But I think we have to reconsider how we trace worker threads.
IMO Worker Threads should only be traced if its part of the customer application. And that should happen automatically.
We should not trace workers who are created from dependencies (such as pino).
This is a general problem - a lot of libraries nowadays create a worker to outsource stuff.
IMO adding an exception for pino does not solve the underlying problem and its hard to manage that long-term. Do you wanne add an exception for all libraries now who span a worker?
I'd suggest:
- Hook into worker threads as early as possible
const workerThreads = require('worker_threads');
class InstanaWorkerOverrideClass extends workerThreads.Worker {
// @ts-ignore
constructor(filename, options) {
if (filename.includes('node_modules') {
if (!options) {
options = {};
}
if (options.workerData !== undefined) {
options.workerData.instanaTracing = false;
} else {
options.workerData = { instanaTracing: false };
}
}
super(filename, options);
}
}
workerThreads.Worker = InstanaWorkerOverrideClass;
- Checking the worker thread tracing:
const skipWorkerThreadTracing = !workerThreads.isMainThread && workerThreads.workerData?.instanaTracing === false;If any customer wants to trace worker treads in node_modules, then we can also add an env.
Before doing so, we should raise a PR to move the worker in packages/collector/test/metrics/appWithWorkerThread/test.js to the app root.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick fix for the customer could look like this:
INSTANA_PARENT_THREAD_COMMUNICATION=false
if (!process.env.INSTANA_PARENT_THREAD_COMMUNICATION || process.env.INSTANA_PARENT_THREAD_COMMUNICATION !== 'true') {
parentPort.postMessage('instana.collector.initialized');
}The error
Error: this should not happen: undefined
Only occurs because thread-stream does not follow the postMessage format.
This error is actually a bug in this library. They expect a specific format - otherwise they throw an error.
Value can be anything - also a string.
https://github.com/pinojs/thread-stream/blob/82e011281b8895bf5e8f70008e0242927fcaf0cb/index.js#L163
https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage
Still, this fix is more like a workaround - but its quick and easy to ship. But its not solving the root cause.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correction:
INSTANA_PARENT_PROCESS_COMMUNICATION=false
if (process.env.INSTANA_PARENT_PROCESS_COMMUNICATION !== 'false') {
parentPort.postMessage('instana.collector.initialized');
}or alternatively, disable worker thread tracing completely. This is maybe a more useful env anyway. (personally preferred)
if (!isMainThread && disableWorkerThreadTracing) {
return
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
created a separate card for this https://jsw.ibm.com/browse/INSTA-63554
kirrg001
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4533eea to
4db557b
Compare
|


When collector is loaded via:
NODE_OPTIONS="--require /path/to/@instana/collector/src/immediate"it executes before the main application code. If a Pino thread-stream worker is spawned, this worker also inherits the NODE_OPTIONS environment variable and attempts to initialize Instana.
The Pino worker is not a real application; it only exists to handle log messages. No need to instrument this process
Error
refs https://jsw.ibm.com/browse/INSTA-63554
https://github.com/pinojs/pino/blob/main/docs/transports.md