|
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'; |
3 | 3 |
|
4 | 4 | class Tracer {
|
5 |
| - protected activeSpans: Map<string, string> = new Map(); |
| 5 | + protected activeSpans: Map<SpanId, string> = new Map(); |
6 | 6 | protected queue: Array<SpanEvent> = [];
|
7 | 7 | protected resolveWaitChunksP: (() => void) | undefined;
|
8 | 8 | protected ended: boolean = false;
|
9 | 9 | protected idGen = new IdSortable();
|
10 | 10 |
|
11 |
| - protected nextId(): string { |
| 11 | + protected nextId(): SpanId { |
12 | 12 | const result = this.idGen.next();
|
13 | 13 | if (result.done || result.value == null) {
|
14 | 14 | throw new Error('Unexpected end of id generator');
|
15 | 15 | }
|
16 |
| - return idUtils.toMultibase(result.value, 'base64'); |
| 16 | + return result.value.toMultibase('base32hex'); |
17 | 17 | }
|
18 | 18 |
|
19 | 19 | protected queueSpanEvent(evt: SpanEvent) {
|
20 | 20 | this.queue.push(evt);
|
21 | 21 | if (this.resolveWaitChunksP != null) this.resolveWaitChunksP();
|
22 | 22 | }
|
23 | 23 |
|
24 |
| - public startSpan(name: string, parentSpanId?: string): string { |
| 24 | + public startSpan(name: string, parentSpanId?: SpanId): SpanId { |
25 | 25 | const spanId = this.nextId();
|
26 | 26 | this.activeSpans.set(spanId, name);
|
27 | 27 | this.queueSpanEvent({
|
28 | 28 | type: 'start',
|
29 |
| - id: this.nextId(), |
30 |
| - spanId: spanId, |
31 |
| - parentSpanId: parentSpanId, |
| 29 | + id: spanId, |
| 30 | + parentId: parentSpanId, |
32 | 31 | name: name,
|
33 | 32 | });
|
34 | 33 | return spanId;
|
35 | 34 | }
|
36 | 35 |
|
37 |
| - public endSpan(spanId: string): void { |
| 36 | + public endSpan(spanId: SpanId): void { |
38 | 37 | const name = this.activeSpans.get(spanId);
|
39 | 38 | if (!name) return;
|
40 | 39 | this.activeSpans.delete(spanId);
|
41 | 40 | this.queueSpanEvent({
|
42 |
| - type: 'end', |
| 41 | + type: 'stop', |
43 | 42 | id: this.nextId(),
|
44 |
| - spanId: spanId, |
45 |
| - name: name, |
| 43 | + startId: spanId, |
46 | 44 | });
|
47 | 45 | }
|
48 | 46 |
|
|
0 commit comments