88import { objectToEntries } from '../transform.js' ;
99import {
1010 asOptions ,
11+ errorToMarkerPayload ,
1112 markerPayload ,
12- errorToMarkerPayload } from '../user-timing-extensibility-api-utils.js' ;
13+ } from '../user-timing-extensibility-api-utils.js' ;
1314import type {
1415 ActionTrackEntryPayload ,
1516 MarkerPayload ,
@@ -109,27 +110,44 @@ export class NodejsProfiler<
109110 */
110111
111112 constructor ( options : NodejsProfilerOptions < DomainEvents , Tracks > ) {
113+ // Pick ProfilerBufferOptions
112114 const {
113115 captureBufferedEntries,
114116 flushThreshold,
115117 maxQueueSize,
118+ ...allButBufferOptions
119+ } = options ;
120+ // Pick ProfilerPersistOptions
121+ const {
116122 format : profilerFormat ,
117123 measureName,
118124 outDir = PROFILER_PERSIST_OUT_DIR ,
119125 enabled,
120126 debug,
121127 ...profilerOptions
122- } = options ;
128+ } = allButBufferOptions ;
123129
124130 super ( { ...profilerOptions , enabled, debug } ) ;
125131
126- this . #initializeStorage( profilerFormat , {
132+ const { encodePerfEntry, ...format } = profilerFormat ;
133+
134+ this . #sharder = new ShardedWal < DomainEvents > ( {
135+ debug,
136+ dir : process . env [ PROFILER_OUT_DIR_ENV_VAR ] ?? outDir ,
137+ format : parseWalFormat < DomainEvents > ( format ) ,
138+ coordinatorIdEnvVar : PROFILER_SHARDER_ID_ENV_VAR ,
139+ measureNameEnvVar : PROFILER_MEASURE_NAME_ENV_VAR ,
140+ groupId : measureName ,
141+ } ) ;
142+
143+ this . #shard = this . #sharder. shard ( ) ;
144+ this . #performanceObserverSink = new PerformanceObserverSink ( {
145+ sink : this . #shard,
146+ encodePerfEntry,
127147 captureBufferedEntries,
128148 flushThreshold,
129149 maxQueueSize,
130- measureName,
131- outDir,
132- debug,
150+ debug : this . isDebugMode ( ) ,
133151 } ) ;
134152
135153 this . #unsubscribeExitHandlers = subscribeProcessExit ( {
@@ -151,40 +169,6 @@ export class NodejsProfiler<
151169 }
152170 }
153171
154- #initializeStorage(
155- profilerFormat : ProfilerFormat < DomainEvents > ,
156- options : {
157- captureBufferedEntries ?: boolean ;
158- flushThreshold ?: number ;
159- maxQueueSize ?: number ;
160- measureName ?: string ;
161- outDir : string ;
162- debug ?: boolean ;
163- } ,
164- ) {
165- const { encodePerfEntry, ...format } = profilerFormat ;
166- const { captureBufferedEntries, flushThreshold, maxQueueSize, measureName, outDir, debug } = options ;
167-
168- this . #sharder = new ShardedWal < DomainEvents > ( {
169- debug,
170- dir : process . env [ PROFILER_OUT_DIR_ENV_VAR ] ?? outDir ,
171- format : parseWalFormat < DomainEvents > ( format ) ,
172- coordinatorIdEnvVar : PROFILER_SHARDER_ID_ENV_VAR ,
173- measureNameEnvVar : PROFILER_MEASURE_NAME_ENV_VAR ,
174- groupId : measureName ,
175- } ) ;
176-
177- this . #shard = this . #sharder. shard ( ) ;
178- this . #performanceObserverSink = new PerformanceObserverSink ( {
179- sink : this . #shard,
180- encodePerfEntry,
181- captureBufferedEntries,
182- flushThreshold,
183- maxQueueSize,
184- debug : this . isDebugMode ( ) ,
185- } ) ;
186- }
187-
188172 /**
189173 * Creates a performance marker for a profiler state transition.
190174 */
@@ -255,22 +239,22 @@ export class NodejsProfiler<
255239 switch ( transition ) {
256240 case 'idle->running' :
257241 super . setEnabled ( true ) ;
258- this . #shard. open ( ) ;
259- this . #performanceObserverSink. subscribe ( ) ;
242+ this . #shard? .open ( ) ;
243+ this . #performanceObserverSink? .subscribe ( ) ;
260244 break ;
261245
262246 case 'running->idle' :
263247 super . setEnabled ( false ) ;
264- this . #performanceObserverSink. unsubscribe ( ) ;
265- this . #shard. close ( ) ;
248+ this . #performanceObserverSink? .unsubscribe ( ) ;
249+ this . #shard? .close ( ) ;
266250 break ;
267251
268252 case 'running->closed' :
269253 case 'idle->closed' :
270254 super . setEnabled ( false ) ;
271- this . #performanceObserverSink. unsubscribe ( ) ;
272- this . #shard. close ( ) ;
273- this . #sharder. finalizeIfCoordinator ( ) ;
255+ this . #performanceObserverSink? .unsubscribe ( ) ;
256+ this . #shard? .close ( ) ;
257+ this . #sharder? .finalizeIfCoordinator ( ) ;
274258 this . #unsubscribeExitHandlers?.( ) ;
275259 break ;
276260
@@ -317,17 +301,17 @@ export class NodejsProfiler<
317301 state : sharderState ,
318302 isCoordinator,
319303 ...sharderStats
320- } = this . #sharder. stats ;
304+ } = this . #sharder? .stats ?? { } ;
321305
322306 return {
323307 profilerState : this . #state,
324308 debug : this . isDebugMode ( ) ,
325309 sharderState,
326310 ...sharderStats ,
327311 isCoordinator,
328- shardOpen : ! this . #shard. isClosed ( ) ,
329- shardPath : this . #shard. getPath ( ) ,
330- ...this . #performanceObserverSink. getStats ( ) ,
312+ shardOpen : this . #shard? .isClosed ( ) ,
313+ shardPath : this . #shard? .getPath ( ) ,
314+ ...this . #performanceObserverSink? .getStats ( ) ,
331315 } ;
332316 }
333317
@@ -336,6 +320,6 @@ export class NodejsProfiler<
336320 if ( this . #state === 'closed' ) {
337321 return ; // No-op if closed
338322 }
339- this . #performanceObserverSink. flush ( ) ;
323+ this . #performanceObserverSink? .flush ( ) ;
340324 }
341325}
0 commit comments