|
| 1 | +import process from 'node:process'; |
1 | 2 | import { isEnvVarEnabled } from '../env.js'; |
2 | | -import { PerformanceObserverSink } from '../performance-observer.js'; |
3 | | -import type { Recoverable, Sink } from '../sink-source.type.js'; |
4 | 3 | import { |
5 | 4 | type ActionTrackConfigs, |
6 | 5 | type MeasureCtxOptions, |
@@ -96,22 +95,20 @@ export class Profiler<T extends ActionTrackConfigs> { |
96 | 95 | /** |
97 | 96 | * Sets enabled state for this profiler. |
98 | 97 | * |
99 | | - * Note: This only affects the current profiler instance and does not modify environment variables. |
100 | | - * Environment variables are read-only configuration that should be set before application startup. |
| 98 | + * Also sets the `CP_PROFILING` environment variable. |
| 99 | + * This means any future {@link Profiler} instantiations (including child processes) will use the same enabled state. |
101 | 100 | * |
102 | 101 | * @param enabled - Whether profiling should be enabled |
103 | 102 | */ |
104 | 103 | setEnabled(enabled: boolean): void { |
105 | | - if (this.#enabled === enabled) { |
106 | | - return; |
107 | | - } |
| 104 | + process.env[PROFILER_ENABLED_ENV_VAR] = `${enabled}`; |
108 | 105 | this.#enabled = enabled; |
109 | 106 | } |
110 | 107 |
|
111 | 108 | /** |
112 | 109 | * Is profiling enabled? |
113 | 110 | * |
114 | | - * Profiling is enabled by {@link setEnabled} call or by the `CP_PROFILING` environment variable at instantiation. |
| 111 | + * Profiling is enabled by {@link setEnabled} call or `CP_PROFILING` environment variable. |
115 | 112 | * |
116 | 113 | * @returns Whether profiling is currently enabled |
117 | 114 | */ |
@@ -229,108 +226,3 @@ export class Profiler<T extends ActionTrackConfigs> { |
229 | 226 | } |
230 | 227 | } |
231 | 228 | } |
232 | | - |
233 | | -/** |
234 | | - * Options for configuring a NodejsProfiler instance. |
235 | | - * |
236 | | - * Extends ProfilerOptions with a required sink parameter. |
237 | | - * |
238 | | - * @template Tracks - Record type defining available track names and their configurations |
239 | | - */ |
240 | | -export type NodejsProfilerOptions< |
241 | | - DomainEvents, |
242 | | - Tracks extends Record<string, ActionTrackEntryPayload>, |
243 | | -> = ProfilerOptions<Tracks> & { |
244 | | - /** Sink for buffering and flushing performance data */ |
245 | | - sink: Sink<DomainEvents, unknown> & Recoverable<unknown>; |
246 | | - /** Encoder that converts PerformanceEntry to domain events */ |
247 | | - // eslint-disable-next-line n/no-unsupported-features/node-builtins |
248 | | - encode: (entry: PerformanceEntry) => DomainEvents[]; |
249 | | -}; |
250 | | - |
251 | | -/** |
252 | | - * Performance profiler with automatic process exit handling for buffered performance data. |
253 | | - * |
254 | | - * This class extends the base {@link Profiler} with automatic flushing of performance data |
255 | | - * when the process exits. It accepts a {@link PerformanceObserverSink} that buffers performance |
256 | | - * entries and ensures they are written out during process termination, even for unexpected exits. |
257 | | - * |
258 | | - * The sink defines the output format for performance data, enabling flexible serialization |
259 | | - * to various formats such as DevTools TraceEvent JSON, OpenTelemetry protocol buffers, |
260 | | - * or custom domain-specific formats. |
261 | | - * |
262 | | - * The profiler automatically subscribes to the performance observer when enabled and installs |
263 | | - * exit handlers that flush buffered data on process termination (signals, fatal errors, or normal exit). |
264 | | - * |
265 | | - */ |
266 | | -export class NodejsProfiler< |
267 | | - DomainEvents, |
268 | | - Tracks extends Record<string, ActionTrackEntryPayload> = Record< |
269 | | - string, |
270 | | - ActionTrackEntryPayload |
271 | | - >, |
272 | | -> extends Profiler<Tracks> { |
273 | | - #sink: Sink<DomainEvents, unknown> & Recoverable<unknown>; |
274 | | - #performanceObserverSink: PerformanceObserverSink<DomainEvents>; |
275 | | - |
276 | | - /** |
277 | | - * Creates a new NodejsProfiler instance with automatic exit handling. |
278 | | - * |
279 | | - * @param options - Configuration options including the sink |
280 | | - * @param options.sink - Sink for buffering and flushing performance data |
281 | | - * @param options.tracks - Custom track configurations merged with defaults |
282 | | - * @param options.prefix - Prefix for all measurement names |
283 | | - * @param options.track - Default track name for measurements |
284 | | - * @param options.trackGroup - Default track group for organization |
285 | | - * @param options.color - Default color for track entries |
286 | | - * @param options.enabled - Whether profiling is enabled (defaults to CP_PROFILING env var) |
287 | | - * |
288 | | - */ |
289 | | - constructor(options: NodejsProfilerOptions<DomainEvents, Tracks>) { |
290 | | - const { sink, encode, ...profilerOptions } = options; |
291 | | - |
292 | | - super(profilerOptions); |
293 | | - |
294 | | - this.#sink = sink; |
295 | | - |
296 | | - this.#performanceObserverSink = new PerformanceObserverSink<DomainEvents>({ |
297 | | - sink, |
298 | | - encode, |
299 | | - }); |
300 | | - |
301 | | - this.#setObserving(this.isEnabled()); |
302 | | - } |
303 | | - |
304 | | - #setObserving(observing: boolean): void { |
305 | | - if (observing) { |
306 | | - this.#sink.open(); |
307 | | - this.#performanceObserverSink.subscribe(); |
308 | | - } else { |
309 | | - this.#performanceObserverSink.unsubscribe(); |
310 | | - this.#performanceObserverSink.flush(); |
311 | | - this.#sink.close(); |
312 | | - } |
313 | | - } |
314 | | - |
315 | | - /** |
316 | | - * Sets enabled state for this profiler and manages sink/observer lifecycle. |
317 | | - * |
318 | | - * Design: Environment = default, Runtime = override |
319 | | - * - Environment variables define defaults (read once at construction) |
320 | | - * - This method provides runtime control without mutating globals |
321 | | - * - Child processes are unaffected by runtime enablement changes |
322 | | - * |
323 | | - * Invariant: enabled ↔ sink + observer state |
324 | | - * - enabled === true → sink open + observer subscribed |
325 | | - * - enabled === false → sink closed + observer unsubscribed |
326 | | - * |
327 | | - * @param enabled - Whether profiling should be enabled |
328 | | - */ |
329 | | - setEnabled(enabled: boolean): void { |
330 | | - if (this.isEnabled() === enabled) { |
331 | | - return; |
332 | | - } |
333 | | - super.setEnabled(enabled); |
334 | | - this.#setObserving(enabled); |
335 | | - } |
336 | | -} |
0 commit comments