diff --git a/src/bin/cli.tsx b/src/bin/cli.tsx index b93e7e7..507dfe4 100644 --- a/src/bin/cli.tsx +++ b/src/bin/cli.tsx @@ -15,9 +15,8 @@ const options = program.opts(); const sampleMode = options.sample; async function loadSpans(): Promise> { - if (!fs.existsSync('spans.jsonl')) return []; const spans: Array = []; - const file = await fs.promises.open('testSpans.jsonl', 'r'); + const file = await fs.promises.open('span.jsonl', 'r'); for await (const line of file.readLines()) { spans.push(JSON.parse(line)); } diff --git a/src/lib/Tracer.ts b/src/lib/Tracer.ts index b434626..e4679a1 100644 --- a/src/lib/Tracer.ts +++ b/src/lib/Tracer.ts @@ -1,12 +1,13 @@ +import type { SpanEvent } from './types.js'; import Span from './Span.js'; class Tracer { protected activeSpans: Map = new Map(); - protected queue: Array = []; + protected queue: Array = []; protected resolveWaitChunksP: (() => void) | undefined; protected ended: boolean = false; - protected queueSpan(span: Span) { + protected queueSpanEvent(span: SpanEvent) { this.queue.push(span); if (this.resolveWaitChunksP != null) this.resolveWaitChunksP(); } @@ -14,11 +15,10 @@ class Tracer { public startSpan(name: string, parentSpanId?: string): string { const span = new Span(name, parentSpanId); this.activeSpans.set(span.spanId, span); - if (parentSpanId && this.activeSpans.has(parentSpanId)) { this.activeSpans.get(parentSpanId)!.children.push(span); } - + this.queueSpanEvent({ type: 'start', span: span.toJSON() }); return span.spanId; } @@ -27,7 +27,7 @@ class Tracer { if (!span) return; span.close(); - this.queueSpan(span); + this.queueSpanEvent({ type: 'stop', span: span.toJSON() }); return span; } @@ -56,7 +56,7 @@ class Tracer { this.ended = true; } - public async *streamEvents(): AsyncGenerator { + public async *streamEvents(): AsyncGenerator { while (true) { const value = this.queue.shift(); if (value == null) { diff --git a/src/lib/types.ts b/src/lib/types.ts index 09811b1..d2ee4c0 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -8,4 +8,9 @@ type SpanJSON = { children: Array; }; -export type { SpanJSON }; +type SpanEvent = { + type: 'start' | 'stop'; + span: SpanJSON; +}; + +export type { SpanJSON, SpanEvent }; diff --git a/tests/asciinemaTest.ts b/tests/asciinemaTest.ts index 056d9e7..aafed31 100644 --- a/tests/asciinemaTest.ts +++ b/tests/asciinemaTest.ts @@ -159,7 +159,7 @@ setInterval(async () => { } step++; - process.stderr.write('generated data step'); + process.stderr.write('generated data step\n'); }, 500); await saveToFileP;