From 56661dca77fa91375bb80610670a89d8c121043c Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Wed, 30 Apr 2025 17:01:05 +1000 Subject: [PATCH] fix: inconsistent id generation --- src/index.ts | 1 + src/tracer/Tracer.ts | 35 +++++++++++++++++------------------ src/tracer/types.ts | 13 ++++--------- tests/asciinemaTest.ts | 9 ++++----- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/index.ts b/src/index.ts index a81d094..08fd4c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ export { default } from './Logger.js'; export { default as Handler } from './Handler.js'; +export { default as tracer } from './tracer/index.js'; export * as formatting from './formatting.js'; export * from './handlers/index.js'; export * from './tracer/index.js'; diff --git a/src/tracer/Tracer.ts b/src/tracer/Tracer.ts index 804359b..71d81d0 100644 --- a/src/tracer/Tracer.ts +++ b/src/tracer/Tracer.ts @@ -1,33 +1,32 @@ -import type { SpanEvent, SpanId } from './types.js'; +import type { SpanEvent } from './types.js'; import { IdSortable, utils as idUtils } from '@matrixai/id'; class Tracer { - protected activeSpans: Map = new Map(); + protected activeSpans: Map = new Map(); protected queue: Array = []; protected resolveWaitChunksP: (() => void) | undefined; protected ended: boolean = false; + protected idGen = new IdSortable(); - protected queueSpanEvent(evt: SpanEvent) { - // Convert the binary id to base64-encoded id - if (evt.id instanceof IdSortable) { - evt.id = idUtils.toMultibase(evt.id.get(), 'base64'); - } - if (evt.spanId instanceof IdSortable) { - evt.spanId = idUtils.toMultibase(evt.spanId.get(), 'base64'); - } - if (evt.parentSpanId instanceof IdSortable) { - evt.parentSpanId = idUtils.toMultibase(evt.parentSpanId.get(), 'base64'); + protected nextId(): string { + const result = this.idGen.next(); + if (result.done || result.value == null) { + throw new Error('Unexpected end of id generator'); } + return idUtils.toMultibase(result.value, 'base64'); + } + + protected queueSpanEvent(evt: SpanEvent) { this.queue.push(evt); if (this.resolveWaitChunksP != null) this.resolveWaitChunksP(); } - public startSpan(name: string, parentSpanId?: SpanId): SpanId { - const spanId = new IdSortable(); + public startSpan(name: string, parentSpanId?: string): string { + const spanId = this.nextId(); this.activeSpans.set(spanId, name); this.queueSpanEvent({ type: 'start', - id: new IdSortable(), + id: this.nextId(), spanId: spanId, parentSpanId: parentSpanId, name: name, @@ -35,13 +34,13 @@ class Tracer { return spanId; } - public endSpan(spanId: SpanId): void { + public endSpan(spanId: string): void { const name = this.activeSpans.get(spanId); if (!name) return; this.activeSpans.delete(spanId); this.queueSpanEvent({ type: 'end', - id: new IdSortable(), + id: this.nextId(), spanId: spanId, name: name, }); @@ -49,7 +48,7 @@ class Tracer { public async traced( name: string, - parentSpanId: SpanId | undefined, + parentSpanId: string | undefined, fn: () => T | Promise, ): Promise { const fnProm = async () => { diff --git a/src/tracer/types.ts b/src/tracer/types.ts index a52db4c..74b75d4 100644 --- a/src/tracer/types.ts +++ b/src/tracer/types.ts @@ -1,17 +1,12 @@ -import type { IdSortable } from '@matrixai/id'; - -type SpanId = IdSortable | string; -type EventId = IdSortable | string; - type Span = { - spanId: SpanId; + spanId: string; name: string; - parentSpanId?: SpanId; + parentSpanId?: string; }; type SpanEvent = Span & { type: 'start' | 'end'; - id: EventId; + id: string; }; -export type { SpanId, EventId, Span, SpanEvent }; +export type { Span, SpanEvent }; diff --git a/tests/asciinemaTest.ts b/tests/asciinemaTest.ts index 2b268ca..88b6e2c 100644 --- a/tests/asciinemaTest.ts +++ b/tests/asciinemaTest.ts @@ -1,11 +1,10 @@ -import type { SpanId } from '#tracer/index.js'; import fs from 'fs'; import * as fc from 'fast-check'; import tracer from '#tracer/index.js'; let parentIndex = 0; let step = 0; -let nestedIds: Array = []; +let nestedIds: Array = []; type Flags = { hasForkA: boolean; @@ -19,9 +18,9 @@ type Flags = { }; const current: { - parentId?: SpanId; - forkAId?: SpanId; - forkBId?: SpanId; + parentId?: string; + forkAId?: string; + forkBId?: string; flags: Flags; } = { flags: {