Skip to content

Commit 5c644c2

Browse files
committed
refactor: wip
1 parent 8c3171e commit 5c644c2

File tree

6 files changed

+22
-57
lines changed

6 files changed

+22
-57
lines changed

packages/utils/src/lib/performance-observer.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,6 @@ export class PerformanceObserverSink<T> {
316316
this.processPerformanceEntries(list.getEntries());
317317
});
318318

319-
this.#observer.observe({
320-
entryTypes: OBSERVED_TYPES,
321-
buffered: this.#buffered,
322-
});
323-
324319
// When buffered mode is enabled, Node.js PerformanceObserver invokes
325320
// the callback synchronously with all buffered entries before observe() returns.
326321
// However, entries created before any observer existed may not be buffered by Node.js.
@@ -332,6 +327,12 @@ export class PerformanceObserverSink<T> {
332327
const allEntries = [...existingMarks, ...existingMeasures];
333328
this.processPerformanceEntries(allEntries);
334329
}
330+
331+
this.#observer.observe({
332+
entryTypes: OBSERVED_TYPES,
333+
// @NOTE: This is for unknown reasons not working, and we manually do it above
334+
// buffered: this.#buffered,
335+
});
335336
}
336337

337338
/**

packages/utils/src/lib/profiler/profiler-node.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ export type PersistOptions<DomainEvents extends object> = {
3939
*/
4040
outDir?: string;
4141

42-
/**
43-
* File path for the WriteAheadLogFile sink.
44-
* If not provided, defaults to `trace.json` in the current working directory.
45-
*/
46-
filename?: string;
4742
/**
4843
* Override the base name for WAL files (overrides format.baseName).
4944
* If provided, this value will be merged into the format configuration.
@@ -127,7 +122,6 @@ export class NodejsProfiler<
127122
outDir = PROFILER_PERSIST_OUT_DIR,
128123
enabled,
129124
debug,
130-
filename,
131125
...profilerOptions
132126
} = allButBufferOptions;
133127

@@ -141,7 +135,6 @@ export class NodejsProfiler<
141135
coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR,
142136
measureNameEnvVar: PROFILER_MEASURE_NAME_ENV_VAR,
143137
groupId: measureName,
144-
filename,
145138
});
146139
this.#sharder.ensureCoordinator();
147140

packages/utils/src/lib/profiler/profiler-node.unit.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,21 +368,19 @@ describe('NodejsProfiler', () => {
368368
expect(profiler.stats.shardPath).toMatch(/\.jsonl$/);
369369
});
370370

371-
it('should use provided filename when specified', () => {
372-
const customPath = path.join(process.cwd(), 'custom-trace.json');
371+
it('should use measureName for final file path', () => {
373372
const profiler = createProfiler({
374373
measureName: 'custom-filename',
375-
filename: customPath,
376374
});
377375
const shardPath = profiler.stats.shardPath;
378376
// shardPath uses the shard ID format: baseName.shardId.jsonl
379377
expect(shardPath).toContain('tmp/profiles/custom-filename');
380378
expect(shardPath).toMatch(
381379
/trace\.\d{8}-\d{6}-\d{3}\.\d+\.\d+\.\d+\.jsonl$/,
382380
);
383-
// finalFilePath uses the custom filename
381+
// finalFilePath uses measureName as the identifier
384382
expect(profiler.stats.finalFilePath).toBe(
385-
`${PROFILER_PERSIST_OUT_DIR}/custom-filename/trace.custom-trace.json`,
383+
`${PROFILER_PERSIST_OUT_DIR}/custom-filename/trace.custom-filename.json`,
386384
);
387385
});
388386

@@ -420,7 +418,6 @@ describe('NodejsProfiler', () => {
420418
it('get stats() getter should return current stats', () => {
421419
const profiler = createProfiler({
422420
measureName: 'stats-getter',
423-
filename: 'stats-getter-trace',
424421
enabled: false,
425422
});
426423

@@ -438,7 +435,7 @@ describe('NodejsProfiler', () => {
438435
isCoordinator: true, // When no coordinator env var is set, this profiler becomes coordinator
439436
isFinalized: false,
440437
isCleaned: false,
441-
finalFilePath: `${PROFILER_PERSIST_OUT_DIR}/stats-getter/trace.stats-getter-trace.json`,
438+
finalFilePath: `${PROFILER_PERSIST_OUT_DIR}/stats-getter/trace.stats-getter.json`,
442439
shardFileCount: 0,
443440
shardFiles: [],
444441
shardOpen: false,

packages/utils/src/lib/profiler/trace-file.type.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// ─────────────────────────────────────────────────────────────
2-
// Core trace event model
3-
// ─────────────────────────────────────────────────────────────
41
import type {
52
MarkerPayload,
63
TrackEntryPayload,

packages/utils/src/lib/wal-sharded.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class ShardedWal<T extends object = object> {
8282
readonly #dir: string = process.cwd();
8383
readonly #coordinatorIdEnvVar: string;
8484
#state: 'active' | 'finalized' | 'cleaned' = 'active';
85-
#filename?: string;
8685

8786
/**
8887
* Initialize the origin PID environment variable if not already set.
@@ -121,24 +120,16 @@ export class ShardedWal<T extends object = object> {
121120
* @param opt.groupId - Group ID for sharding (defaults to generated group ID)
122121
* @param opt.coordinatorIdEnvVar - Environment variable name for storing coordinator ID (defaults to CP_SHARDED_WAL_COORDINATOR_ID)
123122
* @param opt.measureNameEnvVar - Environment variable name for coordinating groupId across processes (optional)
124-
* @param opt.filename - Filename to use for final output file (optional)
125123
*/
126124
constructor(opt: {
127125
dir?: string;
128126
format: Partial<WalFormat<T>>;
129127
groupId?: string;
130128
coordinatorIdEnvVar: string;
131129
measureNameEnvVar?: string;
132-
filename?: string;
133130
}) {
134-
const {
135-
dir,
136-
format,
137-
groupId,
138-
coordinatorIdEnvVar,
139-
measureNameEnvVar,
140-
filename,
141-
} = opt;
131+
const { dir, format, groupId, coordinatorIdEnvVar, measureNameEnvVar } =
132+
opt;
142133

143134
// Determine groupId: use provided, then env var, or generate
144135
let resolvedGroupId: string;
@@ -165,7 +156,6 @@ export class ShardedWal<T extends object = object> {
165156
}
166157
this.#format = parseWalFormat<T>(format);
167158
this.#coordinatorIdEnvVar = coordinatorIdEnvVar;
168-
this.#filename = filename;
169159
}
170160

171161
/**
@@ -257,31 +247,24 @@ export class ShardedWal<T extends object = object> {
257247

258248
/**
259249
* Generates a filename for the final merged output file.
260-
* Uses the stored filename if available, otherwise falls back to groupId.
250+
* Uses the groupId as the identifier in the final filename.
261251
*
262252
* Example with baseName "trace" and groupId "20240101-120000-000":
263253
* Filename: trace.20240101-120000-000.json
264254
*
265-
* Example with baseName "trace" and filename "custom-trace.json":
266-
* Filename: trace.custom-trace.json
255+
* Example with baseName "trace" and groupId "measureName":
256+
* Filename: trace.measureName.json
267257
*
268258
* @returns The filename for the final merged output file
269259
*/
270260
getFinalFilePath() {
271261
const groupIdDir = path.join(this.#dir, this.groupId);
272262
const { baseName, finalExtension } = this.#format;
273263

274-
// Use stored filename if available, otherwise use groupId
275-
let identifier: string;
276-
if (this.#filename) {
277-
// Extract basename if it's a full path, and remove extension
278-
const basename = path.basename(this.#filename);
279-
identifier = basename.replace(/\.[^.]*$/, ''); // Remove extension
280-
} else {
281-
identifier = this.groupId;
282-
}
283-
284-
return path.join(groupIdDir, `${baseName}.${identifier}${finalExtension}`);
264+
return path.join(
265+
groupIdDir,
266+
`${baseName}.${this.groupId}${finalExtension}`,
267+
);
285268
}
286269

287270
shard() {

packages/utils/src/lib/wal-sharded.unit.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const getShardedWal = (overrides?: {
1313
format?: Partial<
1414
Parameters<typeof ShardedWal.prototype.constructor>[0]['format']
1515
>;
16-
filename?: string;
1716
}) =>
1817
new ShardedWal({
1918
dir: '/test/shards',
@@ -41,20 +40,15 @@ describe('ShardedWal', () => {
4140
it('should create shard with correct file path', () => {
4241
const sw = getShardedWal({
4342
format: { baseName: 'trace', walExtension: '.log' },
44-
filename: '20231114-221320-000.1.2.3',
4543
});
4644
const shard = sw.shard();
4745
expect(shard).toBeInstanceOf(WriteAheadLogFile);
4846
// Shard files use getShardId() format (timestamp.pid.threadId.counter)
49-
// Filename is stored but not used in shard path
50-
expect(shard.getPath()).toStartWithPath(
51-
'/test/shards/20231114-221320-000/trace.20231114-221320-000.',
52-
);
53-
expect(shard.getPath()).toEndWithPath('.log');
54-
// Verify it matches the getShardId() pattern: timestamp.pid.threadId.counter.log
47+
// The groupId is auto-generated and used in the shard path
5548
expect(shard.getPath()).toMatch(
56-
/^\/test\/shards\/20231114-221320-000\/trace\.20231114-221320-000\.\d+\.\d+\.\d+\.log$/,
49+
/^\/test\/shards\/\d{8}-\d{6}-\d{3}\/trace\.\d{8}-\d{6}-\d{3}\.\d+\.\d+\.\d+\.log$/,
5750
);
51+
expect(shard.getPath()).toEndWithPath('.log');
5852
});
5953

6054
it('should create shard with default shardId when no argument provided', () => {

0 commit comments

Comments
 (0)