|
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 | + protected shouldTrace = false; |
10 | 11 |
|
11 |
| - protected nextId(): string { |
| 12 | + protected nextId(): SpanId { |
12 | 13 | const result = this.idGen.next();
|
13 | 14 | if (result.done || result.value == null) {
|
14 | 15 | throw new Error('Unexpected end of id generator');
|
15 | 16 | }
|
16 |
| - return idUtils.toMultibase(result.value, 'base64'); |
| 17 | + return result.value.toMultibase('base32hex') as SpanId; |
17 | 18 | }
|
18 | 19 |
|
19 |
| - protected queueSpanEvent(evt: SpanEvent) { |
| 20 | + protected queueSpanEvent(evt: SpanEvent): void { |
| 21 | + if (!this.shouldTrace) return; |
20 | 22 | this.queue.push(evt);
|
21 | 23 | if (this.resolveWaitChunksP != null) this.resolveWaitChunksP();
|
22 | 24 | }
|
23 | 25 |
|
24 |
| - public startSpan(name: string, parentSpanId?: string): string { |
| 26 | + public startSpan(name: string, parentSpanId?: SpanId): SpanId { |
25 | 27 | const spanId = this.nextId();
|
26 | 28 | this.activeSpans.set(spanId, name);
|
27 | 29 | this.queueSpanEvent({
|
28 | 30 | type: 'start',
|
29 |
| - id: this.nextId(), |
30 |
| - spanId: spanId, |
31 |
| - parentSpanId: parentSpanId, |
| 31 | + id: spanId, |
| 32 | + parentId: parentSpanId, |
32 | 33 | name: name,
|
33 | 34 | });
|
34 | 35 | return spanId;
|
35 | 36 | }
|
36 | 37 |
|
37 |
| - public endSpan(spanId: string): void { |
| 38 | + public endSpan(spanId: SpanId): void { |
38 | 39 | const name = this.activeSpans.get(spanId);
|
39 | 40 | if (!name) return;
|
40 | 41 | this.activeSpans.delete(spanId);
|
41 | 42 | this.queueSpanEvent({
|
42 |
| - type: 'end', |
| 43 | + type: 'stop', |
43 | 44 | id: this.nextId(),
|
44 |
| - spanId: spanId, |
45 |
| - name: name, |
| 45 | + startId: spanId, |
46 | 46 | });
|
47 | 47 | }
|
48 | 48 |
|
49 | 49 | public async traced<T>(
|
50 | 50 | name: string,
|
51 |
| - parentSpanId: string | undefined, |
| 51 | + parentSpanId: SpanId | undefined, |
52 | 52 | fn: () => T | Promise<T>,
|
53 | 53 | ): Promise<T> {
|
54 | 54 | const fnProm = async () => {
|
@@ -77,6 +77,13 @@ class Tracer {
|
77 | 77 | yield value;
|
78 | 78 | }
|
79 | 79 | }
|
| 80 | + |
| 81 | + public disableTracing(): void { |
| 82 | + this.shouldTrace = false; |
| 83 | + this.ended = true; |
| 84 | + this.resolveWaitChunksP?.(); |
| 85 | + this.queue = []; |
| 86 | + } |
80 | 87 | }
|
81 | 88 |
|
82 | 89 | export default Tracer;
|
0 commit comments