Skip to content

Commit b6f6e6d

Browse files
committed
chore: cleaned up span format
1 parent ecc4664 commit b6f6e6d

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/tracer/Tracer.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
1-
import type { SpanEvent } from './types.js';
2-
import { IdSortable, utils as idUtils } from '@matrixai/id';
1+
import type { SpanEvent, SpanId } from './types.js';
2+
import { IdSortable } from '@matrixai/id';
33

44
class Tracer {
5-
protected activeSpans: Map<string, string> = new Map();
5+
protected activeSpans: Map<SpanId, string> = new Map();
66
protected queue: Array<SpanEvent> = [];
77
protected resolveWaitChunksP: (() => void) | undefined;
88
protected ended: boolean = false;
99
protected idGen = new IdSortable();
1010

11-
protected nextId(): string {
11+
protected nextId(): SpanId {
1212
const result = this.idGen.next();
1313
if (result.done || result.value == null) {
1414
throw new Error('Unexpected end of id generator');
1515
}
16-
return idUtils.toMultibase(result.value, 'base64');
16+
return result.value.toMultibase('base32hex');
1717
}
1818

1919
protected queueSpanEvent(evt: SpanEvent) {
2020
this.queue.push(evt);
2121
if (this.resolveWaitChunksP != null) this.resolveWaitChunksP();
2222
}
2323

24-
public startSpan(name: string, parentSpanId?: string): string {
24+
public startSpan(name: string, parentSpanId?: SpanId): SpanId {
2525
const spanId = this.nextId();
2626
this.activeSpans.set(spanId, name);
2727
this.queueSpanEvent({
2828
type: 'start',
29-
id: this.nextId(),
30-
spanId: spanId,
31-
parentSpanId: parentSpanId,
29+
id: spanId,
30+
parentId: parentSpanId,
3231
name: name,
3332
});
3433
return spanId;
3534
}
3635

37-
public endSpan(spanId: string): void {
36+
public endSpan(spanId: SpanId): void {
3837
const name = this.activeSpans.get(spanId);
3938
if (!name) return;
4039
this.activeSpans.delete(spanId);
4140
this.queueSpanEvent({
42-
type: 'end',
41+
type: 'stop',
4342
id: this.nextId(),
44-
spanId: spanId,
45-
name: name,
43+
startId: spanId,
4644
});
4745
}
4846

src/tracer/types.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
type Span = {
2-
spanId: string;
3-
name: string;
4-
parentSpanId?: string;
1+
type SpanId = string;
2+
3+
/**
4+
* A span is a virtual concept, not an actual object. A span is made up of
5+
* multiple events. Each event gets its own ID. Each start event must be
6+
* associated with a stop event, otherwise the span is considered to be
7+
* ongoing.
8+
*/
9+
10+
type SpanStart = {
11+
type: 'start';
12+
id: SpanId;
13+
parentId?: SpanId;
14+
name?: string;
515
};
616

7-
type SpanEvent = Span & {
8-
type: 'start' | 'end';
9-
id: string;
17+
type SpanStop = {
18+
type: 'stop';
19+
id: SpanId;
20+
startId: SpanId;
21+
parentId?: SpanId;
1022
};
1123

12-
export type { Span, SpanEvent };
24+
type SpanEvent = SpanStart | SpanStop;
25+
26+
export type { SpanId, SpanEvent };

0 commit comments

Comments
 (0)