Skip to content

Commit ef677fc

Browse files
authored
Merge pull request #61 from MatrixAI/feature-eng-612-span-format
Reduce redundancy in the span format
2 parents ecc4664 + 85cdaaa commit ef677fc

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

src/tracer/Tracer.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
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();
10+
protected shouldTrace = false;
1011

11-
protected nextId(): string {
12+
protected nextId(): SpanId {
1213
const result = this.idGen.next();
1314
if (result.done || result.value == null) {
1415
throw new Error('Unexpected end of id generator');
1516
}
16-
return idUtils.toMultibase(result.value, 'base64');
17+
return result.value.toMultibase('base32hex') as SpanId;
1718
}
1819

19-
protected queueSpanEvent(evt: SpanEvent) {
20+
protected queueSpanEvent(evt: SpanEvent): void {
21+
if (!this.shouldTrace) return;
2022
this.queue.push(evt);
2123
if (this.resolveWaitChunksP != null) this.resolveWaitChunksP();
2224
}
2325

24-
public startSpan(name: string, parentSpanId?: string): string {
26+
public startSpan(name: string, parentSpanId?: SpanId): SpanId {
2527
const spanId = this.nextId();
2628
this.activeSpans.set(spanId, name);
2729
this.queueSpanEvent({
2830
type: 'start',
29-
id: this.nextId(),
30-
spanId: spanId,
31-
parentSpanId: parentSpanId,
31+
id: spanId,
32+
parentId: parentSpanId,
3233
name: name,
3334
});
3435
return spanId;
3536
}
3637

37-
public endSpan(spanId: string): void {
38+
public endSpan(spanId: SpanId): void {
3839
const name = this.activeSpans.get(spanId);
3940
if (!name) return;
4041
this.activeSpans.delete(spanId);
4142
this.queueSpanEvent({
42-
type: 'end',
43+
type: 'stop',
4344
id: this.nextId(),
44-
spanId: spanId,
45-
name: name,
45+
startId: spanId,
4646
});
4747
}
4848

4949
public async traced<T>(
5050
name: string,
51-
parentSpanId: string | undefined,
51+
parentSpanId: SpanId | undefined,
5252
fn: () => T | Promise<T>,
5353
): Promise<T> {
5454
const fnProm = async () => {
@@ -77,6 +77,13 @@ class Tracer {
7777
yield value;
7878
}
7979
}
80+
81+
public disableTracing(): void {
82+
this.shouldTrace = false;
83+
this.ended = true;
84+
this.resolveWaitChunksP?.();
85+
this.queue = [];
86+
}
8087
}
8188

8289
export default Tracer;

src/tracer/types.ts

+22-8
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 & { readonly brand: unique symbol };
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 };

tests/asciinemaTest.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import type { SpanId } from '#tracer/index.js';
12
import fs from 'fs';
23
import * as fc from 'fast-check';
34
import tracer from '#tracer/index.js';
45

56
let parentIndex = 0;
67
let step = 0;
7-
let nestedIds: Array<string> = [];
8+
let nestedIds: Array<SpanId> = [];
89

910
type Flags = {
1011
hasForkA: boolean;
@@ -18,9 +19,9 @@ type Flags = {
1819
};
1920

2021
const current: {
21-
parentId?: string;
22-
forkAId?: string;
23-
forkBId?: string;
22+
parentId?: SpanId;
23+
forkAId?: SpanId;
24+
forkBId?: SpanId;
2425
flags: Flags;
2526
} = {
2627
flags: {

0 commit comments

Comments
 (0)