Skip to content

Commit 026a25b

Browse files
committed
fix: skip thread stream
1 parent d2712cc commit 026a25b

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

packages/collector/src/index.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,27 @@ if (isNodeJsTooOld()) {
3333
// @ts-ignore TS1108 (return can only be used within a function body)
3434
return;
3535
}
36-
37-
let isMainThread = true;
38-
try {
39-
isMainThread = require('worker_threads').isMainThread;
40-
} catch (err) {
41-
// Worker threads are not available, so we know that this is the main thread.
42-
}
43-
36+
const skipPinoWorker = require('./util/skipPinoWorker');
4437
// Skip Instana instrumentation in Pino thread-stream workers.
4538
// Pino uses internal worker threads for its "thread-stream" transport.
4639
// These threads are not part of the main application logic and should
4740
// not be instrumented.
4841
//
4942
// This prevents errors when the collector is loaded via NODE_OPTIONS in worker threads.
50-
const { workerData } = require('worker_threads');
51-
if (!isMainThread && workerData && typeof workerData === 'object') {
52-
const dataString = Object.values(workerData).join(' ').toLowerCase();
53-
if (dataString.includes('thread-stream')) {
54-
// eslint-disable-next-line no-console
55-
console.warn('[Instana] Skipping instrumentation in thread-stream worker.');
56-
module.exports = function noOp() {};
57-
module.exports.default = function noOp() {};
58-
// @ts-ignore
59-
return;
60-
}
43+
if (skipPinoWorker()) {
44+
// eslint-disable-next-line no-console
45+
console.warn('[Instana] Skipping instrumentation in Pino thread-stream worker.');
46+
module.exports = function noOp() {};
47+
module.exports.default = function noOp() {};
48+
// @ts-ignore
49+
return;
50+
}
51+
52+
let isMainThread = true;
53+
try {
54+
isMainThread = require('worker_threads').isMainThread;
55+
} catch (err) {
56+
// Worker threads are not available, so we know that this is the main thread.
6157
}
6258

6359
const path = require('path');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
'use strict';
6+
7+
/**
8+
* Detects whether the current process is a Pino thread-stream worker thread.
9+
* Returns true if we should skip instrumentation for this worker.
10+
*/
11+
function skipPinoWorker() {
12+
try {
13+
const { isMainThread, workerData } = require('worker_threads');
14+
if (isMainThread) return false;
15+
16+
// Check workerData properties for pino thread-stream
17+
if (workerData && typeof workerData === 'object') {
18+
const dataString = Object.values(workerData).join(' ').toLowerCase();
19+
if (dataString.includes('thread-stream')) {
20+
return true;
21+
}
22+
}
23+
24+
return false;
25+
} catch {
26+
return false;
27+
}
28+
}
29+
30+
module.exports = skipPinoWorker;

0 commit comments

Comments
 (0)