Skip to content

Commit 7a22126

Browse files
committed
chore: updated spans to event-based
1 parent d1a4ab6 commit 7a22126

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/bin/cli.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ const options = program.opts();
1515
const sampleMode = options.sample;
1616

1717
async function loadSpans(): Promise<Array<Span>> {
18-
if (!fs.existsSync('spans.jsonl')) return [];
1918
const spans: Array<Span> = [];
20-
const file = await fs.promises.open('testSpans.jsonl', 'r');
19+
const file = await fs.promises.open('span.jsonl', 'r');
2120
for await (const line of file.readLines()) {
2221
spans.push(JSON.parse(line));
2322
}

src/lib/Tracer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
import type { SpanEvent } from './types.js';
12
import Span from './Span.js';
23

34
class Tracer {
45
protected activeSpans: Map<string, Span> = new Map();
5-
protected queue: Array<Span> = [];
6+
protected queue: Array<SpanEvent> = [];
67
protected resolveWaitChunksP: (() => void) | undefined;
78
protected ended: boolean = false;
89

9-
protected queueSpan(span: Span) {
10+
protected queueSpanEvent(span: SpanEvent) {
1011
this.queue.push(span);
1112
if (this.resolveWaitChunksP != null) this.resolveWaitChunksP();
1213
}
1314

1415
public startSpan(name: string, parentSpanId?: string): string {
1516
const span = new Span(name, parentSpanId);
1617
this.activeSpans.set(span.spanId, span);
17-
1818
if (parentSpanId && this.activeSpans.has(parentSpanId)) {
1919
this.activeSpans.get(parentSpanId)!.children.push(span);
2020
}
21-
21+
this.queueSpanEvent({ type: 'start', span: span.toJSON() });
2222
return span.spanId;
2323
}
2424

@@ -27,7 +27,7 @@ class Tracer {
2727
if (!span) return;
2828

2929
span.close();
30-
this.queueSpan(span);
30+
this.queueSpanEvent({ type: 'stop', span: span.toJSON() });
3131
return span;
3232
}
3333

@@ -56,7 +56,7 @@ class Tracer {
5656
this.ended = true;
5757
}
5858

59-
public async *streamEvents(): AsyncGenerator<Span, void, void> {
59+
public async *streamEvents(): AsyncGenerator<SpanEvent, void, void> {
6060
while (true) {
6161
const value = this.queue.shift();
6262
if (value == null) {

src/lib/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ type SpanJSON = {
88
children: Array<SpanJSON>;
99
};
1010

11-
export type { SpanJSON };
11+
type SpanEvent = {
12+
type: 'start' | 'stop';
13+
span: SpanJSON;
14+
};
15+
16+
export type { SpanJSON, SpanEvent };

tests/asciinemaTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ setInterval(async () => {
159159
}
160160

161161
step++;
162-
process.stderr.write('generated data step');
162+
process.stderr.write('generated data step\n');
163163
}, 500);
164164

165165
await saveToFileP;

0 commit comments

Comments
 (0)