@@ -66,6 +66,165 @@ describe('util.excludedFromInstrumentation', () => {
6666 expect ( isExcluded ( '/usr/local/npmx.js' ) ) . to . be . false ;
6767 } ) ;
6868
69+ describe ( 'pino thread-stream worker detection' , ( ) => {
70+ let originalRequire ;
71+
72+ beforeEach ( ( ) => {
73+ originalRequire = require . cache [ require . resolve ( 'worker_threads' ) ] ;
74+ } ) ;
75+
76+ afterEach ( ( ) => {
77+ if ( originalRequire ) {
78+ require . cache [ require . resolve ( 'worker_threads' ) ] = originalRequire ;
79+ } else {
80+ delete require . cache [ require . resolve ( 'worker_threads' ) ] ;
81+ }
82+ delete require . cache [ require . resolve ( '../../src/util/excludedFromInstrumentation' ) ] ;
83+ } ) ;
84+
85+ it ( 'should exclude Pino thread-stream worker with valid structure' , ( ) => {
86+ mockWorkerThreads ( {
87+ isMainThread : false ,
88+ workerData : {
89+ filename : '/path/to/worker.js' ,
90+ workerData : {
91+ $context : {
92+ threadStreamVersion : '2.0.0'
93+ } ,
94+ target : 'pino-pretty'
95+ }
96+ }
97+ } ) ;
98+
99+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
100+ expect ( checkExclusion ( ) ) . to . be . true ;
101+ } ) ;
102+
103+ it ( 'should exclude Pino thread-stream worker with targets property' , ( ) => {
104+ mockWorkerThreads ( {
105+ isMainThread : false ,
106+ workerData : {
107+ filename : '/path/to/worker.js' ,
108+ workerData : {
109+ $context : {
110+ threadStreamVersion : '2.1.0'
111+ } ,
112+ targets : [ { target : 'pino-pretty' } ]
113+ }
114+ }
115+ } ) ;
116+
117+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
118+ expect ( checkExclusion ( ) ) . to . be . true ;
119+ } ) ;
120+
121+ it ( 'should not exclude main thread' , ( ) => {
122+ mockWorkerThreads ( {
123+ isMainThread : true ,
124+ workerData : {
125+ filename : '/path/to/worker.js' ,
126+ workerData : {
127+ $context : {
128+ threadStreamVersion : '2.0.0'
129+ }
130+ }
131+ }
132+ } ) ;
133+
134+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
135+ expect ( checkExclusion ( ) ) . to . be . false ;
136+ } ) ;
137+
138+ it ( 'should not exclude worker without workerData' , ( ) => {
139+ mockWorkerThreads ( {
140+ isMainThread : false ,
141+ workerData : null
142+ } ) ;
143+
144+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
145+ expect ( checkExclusion ( ) ) . to . be . false ;
146+ } ) ;
147+
148+ it ( 'should not exclude worker without filename' , ( ) => {
149+ mockWorkerThreads ( {
150+ isMainThread : false ,
151+ workerData : {
152+ workerData : {
153+ $context : {
154+ threadStreamVersion : '2.0.0'
155+ }
156+ }
157+ }
158+ } ) ;
159+
160+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
161+ expect ( checkExclusion ( ) ) . to . be . false ;
162+ } ) ;
163+
164+ it ( 'should not exclude worker without nested workerData' , ( ) => {
165+ mockWorkerThreads ( {
166+ isMainThread : false ,
167+ workerData : {
168+ filename : '/path/to/worker.js'
169+ }
170+ } ) ;
171+
172+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
173+ expect ( checkExclusion ( ) ) . to . be . false ;
174+ } ) ;
175+
176+ it ( 'should not exclude worker without $context' , ( ) => {
177+ mockWorkerThreads ( {
178+ isMainThread : false ,
179+ workerData : {
180+ filename : '/path/to/worker.js' ,
181+ workerData : {
182+ someOtherProperty : 'value'
183+ }
184+ }
185+ } ) ;
186+
187+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
188+ expect ( checkExclusion ( ) ) . to . be . false ;
189+ } ) ;
190+
191+ it ( 'should not exclude worker without threadStreamVersion' , ( ) => {
192+ mockWorkerThreads ( {
193+ isMainThread : false ,
194+ workerData : {
195+ filename : '/path/to/worker.js' ,
196+ workerData : {
197+ $context : {
198+ someOtherProperty : 'value'
199+ }
200+ }
201+ }
202+ } ) ;
203+
204+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
205+ expect ( checkExclusion ( ) ) . to . be . false ;
206+ } ) ;
207+
208+ it ( 'should not exclude non-Pino worker thread' , ( ) => {
209+ mockWorkerThreads ( {
210+ isMainThread : false ,
211+ workerData : {
212+ customData : 'some other worker'
213+ }
214+ } ) ;
215+
216+ const checkExclusion = require ( '../../src/util/excludedFromInstrumentation' ) ;
217+ expect ( checkExclusion ( ) ) . to . be . false ;
218+ } ) ;
219+
220+ function mockWorkerThreads ( mockData ) {
221+ const mockModule = {
222+ exports : mockData
223+ } ;
224+ require . cache [ require . resolve ( 'worker_threads' ) ] = mockModule ;
225+ }
226+ } ) ;
227+
69228 function isExcluded ( argv1 ) {
70229 process . argv [ 1 ] = argv1 ;
71230 return isExcludedFromInstrumentation ( ) ;
0 commit comments