Skip to content

Commit 342d301

Browse files
authored
feat: added ability to disable worker thread tracing (#2131)
Controlled via INSTANA_DISABLE_WORKER_THREADS environment variable. Use case: Disable tracing for worker threads not part of the customer application. Workers spawned by dependencies (e.g., pino) are no longer monitored. refs https://jsw.ibm.com/browse/INSTA-62583
1 parent f01bede commit 342d301

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

packages/collector/src/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ try {
4141
// Worker threads are not available, so we know that this is the main thread.
4242
}
4343

44+
// Check if worker threads should be disabled via environment variable.
45+
// Disabling worker threads may be necessary in environments where
46+
// multi-threading causes issues or monitoring of worker threads is not required.
47+
const disableWorkerThreads = process.env.INSTANA_DISABLE_WORKER_THREADS?.toLowerCase() === 'true';
48+
49+
if (disableWorkerThreads && !isMainThread) {
50+
// eslint-disable-next-line no-console
51+
console.warn(
52+
'Worker threads are disabled via INSTANA_DISABLE_WORKER_THREADS. ' +
53+
'This worker thread will not be monitored by Instana.'
54+
);
55+
56+
module.exports = function noOp() {};
57+
module.exports.default = function noOp() {};
58+
59+
// @ts-ignore
60+
return;
61+
}
62+
4463
const path = require('path');
4564
const instanaNodeJsCore = require('@instana/core');
4665
const instanaSharedMetrics = require('@instana/shared-metrics');

packages/collector/test/tracing/logging/pino/app.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ process.on('SIGTERM', () => {
1414
});
1515

1616
require('./mockVersion');
17-
require('../../../..')();
17+
18+
if (!process.env.NODE_OPTIONS || !process.env.NODE_OPTIONS.includes('src/immediate')) {
19+
require('../../../..')();
20+
}
1821

1922
const fetch = require('node-fetch-v2');
2023
const bodyParser = require('body-parser');
@@ -212,6 +215,22 @@ app.get('/express-pino-error-random-object-and-string', (req, res) => {
212215
finish(res);
213216
});
214217

218+
app.get('/thread-stream-test', (req, res) => {
219+
try {
220+
const mode = process.env.PINO_WORKER_MODE || 'transport';
221+
222+
const logger =
223+
mode === 'transport'
224+
? pino({ transport: { target: 'pino-pretty', options: { destination: 1 } } })
225+
: pino({ destination: 1 });
226+
logger.error('Pino worker test error log');
227+
228+
res.sendStatus(200);
229+
} catch (e) {
230+
res.status(500).send(`Failed: ${e.message}`);
231+
}
232+
});
233+
215234
function finish(res) {
216235
fetch(`http://127.0.0.1:${agentPort}`).then(() => {
217236
res.sendStatus(200);

packages/collector/test/tracing/logging/pino/test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,52 @@ describe('tracing/logging/pino', function () {
102102
});
103103
});
104104
});
105+
describe('pino thread-stream worker', function () {
106+
let controls;
107+
108+
before(async () => {
109+
controls = new ProcessControls({
110+
dirname: __dirname,
111+
useGlobalAgent: true,
112+
env: {
113+
PINO_WORKER_MODE: 'transport',
114+
PINO_EXPRESS: 'false',
115+
NODE_OPTIONS: `--require ${path.join(__dirname, '../../../..', 'src', 'immediate.js')}`,
116+
INSTANA_DISABLE_WORKER_THREADS: 'true'
117+
}
118+
});
119+
120+
await controls.startAndWaitForAgentConnection();
121+
});
122+
123+
beforeEach(async () => {
124+
await agentControls.clearReceivedTraceData();
125+
});
126+
127+
after(async () => {
128+
await controls.stop();
129+
});
130+
131+
it('must trace without errors', async () => {
132+
await controls.sendRequest({
133+
method: 'GET',
134+
path: '/thread-stream-test'
135+
});
136+
await testUtils.delay(1000);
137+
const spans = await agentControls.getSpans();
138+
139+
const logSpan = spans.find(s => s.n === 'log.pino');
140+
expect(logSpan).to.exist;
141+
expect(logSpan.data.log.message).to.equal('Pino worker test error log');
142+
143+
const httpSpan = spans.find(s => s.n === 'node.http.server');
144+
expect(httpSpan).to.exist;
145+
expect(httpSpan.data.http.path_tpl).to.equal('/thread-stream-test');
146+
expect(httpSpan.data.http.status).to.equal(200);
147+
148+
expect(spans).to.have.lengthOf(2);
149+
});
150+
});
105151

106152
function runTests(pinoVersion, useExpressPino) {
107153
const suffix = useExpressPino ? ' (express-pino)' : '';

0 commit comments

Comments
 (0)