Skip to content

Commit 56661dc

Browse files
committed
fix: inconsistent id generation
1 parent 3131221 commit 56661dc

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default } from './Logger.js';
22
export { default as Handler } from './Handler.js';
3+
export { default as tracer } from './tracer/index.js';
34
export * as formatting from './formatting.js';
45
export * from './handlers/index.js';
56
export * from './tracer/index.js';

src/tracer/Tracer.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
1-
import type { SpanEvent, SpanId } from './types.js';
1+
import type { SpanEvent } from './types.js';
22
import { IdSortable, utils as idUtils } from '@matrixai/id';
33

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

10-
protected queueSpanEvent(evt: SpanEvent) {
11-
// Convert the binary id to base64-encoded id
12-
if (evt.id instanceof IdSortable) {
13-
evt.id = idUtils.toMultibase(evt.id.get(), 'base64');
14-
}
15-
if (evt.spanId instanceof IdSortable) {
16-
evt.spanId = idUtils.toMultibase(evt.spanId.get(), 'base64');
17-
}
18-
if (evt.parentSpanId instanceof IdSortable) {
19-
evt.parentSpanId = idUtils.toMultibase(evt.parentSpanId.get(), 'base64');
11+
protected nextId(): string {
12+
const result = this.idGen.next();
13+
if (result.done || result.value == null) {
14+
throw new Error('Unexpected end of id generator');
2015
}
16+
return idUtils.toMultibase(result.value, 'base64');
17+
}
18+
19+
protected queueSpanEvent(evt: SpanEvent) {
2120
this.queue.push(evt);
2221
if (this.resolveWaitChunksP != null) this.resolveWaitChunksP();
2322
}
2423

25-
public startSpan(name: string, parentSpanId?: SpanId): SpanId {
26-
const spanId = new IdSortable();
24+
public startSpan(name: string, parentSpanId?: string): string {
25+
const spanId = this.nextId();
2726
this.activeSpans.set(spanId, name);
2827
this.queueSpanEvent({
2928
type: 'start',
30-
id: new IdSortable(),
29+
id: this.nextId(),
3130
spanId: spanId,
3231
parentSpanId: parentSpanId,
3332
name: name,
3433
});
3534
return spanId;
3635
}
3736

38-
public endSpan(spanId: SpanId): void {
37+
public endSpan(spanId: string): void {
3938
const name = this.activeSpans.get(spanId);
4039
if (!name) return;
4140
this.activeSpans.delete(spanId);
4241
this.queueSpanEvent({
4342
type: 'end',
44-
id: new IdSortable(),
43+
id: this.nextId(),
4544
spanId: spanId,
4645
name: name,
4746
});
4847
}
4948

5049
public async traced<T>(
5150
name: string,
52-
parentSpanId: SpanId | undefined,
51+
parentSpanId: string | undefined,
5352
fn: () => T | Promise<T>,
5453
): Promise<T> {
5554
const fnProm = async () => {

src/tracer/types.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import type { IdSortable } from '@matrixai/id';
2-
3-
type SpanId = IdSortable | string;
4-
type EventId = IdSortable | string;
5-
61
type Span = {
7-
spanId: SpanId;
2+
spanId: string;
83
name: string;
9-
parentSpanId?: SpanId;
4+
parentSpanId?: string;
105
};
116

127
type SpanEvent = Span & {
138
type: 'start' | 'end';
14-
id: EventId;
9+
id: string;
1510
};
1611

17-
export type { SpanId, EventId, Span, SpanEvent };
12+
export type { Span, SpanEvent };

tests/asciinemaTest.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import type { SpanId } from '#tracer/index.js';
21
import fs from 'fs';
32
import * as fc from 'fast-check';
43
import tracer from '#tracer/index.js';
54

65
let parentIndex = 0;
76
let step = 0;
8-
let nestedIds: Array<SpanId> = [];
7+
let nestedIds: Array<string> = [];
98

109
type Flags = {
1110
hasForkA: boolean;
@@ -19,9 +18,9 @@ type Flags = {
1918
};
2019

2120
const current: {
22-
parentId?: SpanId;
23-
forkAId?: SpanId;
24-
forkBId?: SpanId;
21+
parentId?: string;
22+
forkAId?: string;
23+
forkBId?: string;
2524
flags: Flags;
2625
} = {
2726
flags: {

0 commit comments

Comments
 (0)