Skip to content

Reduce redundancy in the span format #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/tracer/Tracer.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
import type { SpanEvent } from './types.js';
import { IdSortable, utils as idUtils } from '@matrixai/id';
import type { SpanEvent, SpanId } from './types.js';
import { IdSortable } from '@matrixai/id';

class Tracer {
protected activeSpans: Map<string, string> = new Map();
protected activeSpans: Map<SpanId, string> = new Map();
protected queue: Array<SpanEvent> = [];
protected resolveWaitChunksP: (() => void) | undefined;
protected ended: boolean = false;
protected idGen = new IdSortable();
protected shouldTrace = false;

protected nextId(): string {
protected nextId(): SpanId {
const result = this.idGen.next();
if (result.done || result.value == null) {
throw new Error('Unexpected end of id generator');
}
return idUtils.toMultibase(result.value, 'base64');
return result.value.toMultibase('base32hex') as SpanId;
}

protected queueSpanEvent(evt: SpanEvent) {
protected queueSpanEvent(evt: SpanEvent): void {
if (!this.shouldTrace) return;
this.queue.push(evt);
if (this.resolveWaitChunksP != null) this.resolveWaitChunksP();
}

public startSpan(name: string, parentSpanId?: string): string {
public startSpan(name: string, parentSpanId?: SpanId): SpanId {
const spanId = this.nextId();
this.activeSpans.set(spanId, name);
this.queueSpanEvent({
type: 'start',
id: this.nextId(),
spanId: spanId,
parentSpanId: parentSpanId,
id: spanId,
parentId: parentSpanId,
name: name,
});
return spanId;
}

public endSpan(spanId: string): void {
public endSpan(spanId: SpanId): void {
const name = this.activeSpans.get(spanId);
if (!name) return;
this.activeSpans.delete(spanId);
this.queueSpanEvent({
type: 'end',
type: 'stop',
id: this.nextId(),
spanId: spanId,
name: name,
startId: spanId,
});
}

public async traced<T>(
name: string,
parentSpanId: string | undefined,
parentSpanId: SpanId | undefined,
fn: () => T | Promise<T>,
): Promise<T> {
const fnProm = async () => {
@@ -77,6 +77,13 @@ class Tracer {
yield value;
}
}

public disableTracing(): void {
this.shouldTrace = false;
this.ended = true;
this.resolveWaitChunksP?.();
this.queue = [];
}
}

export default Tracer;
30 changes: 22 additions & 8 deletions src/tracer/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
type Span = {
spanId: string;
name: string;
parentSpanId?: string;
type SpanId = string & { readonly brand: unique symbol };

/**
* A span is a virtual concept, not an actual object. A span is made up of
* multiple events. Each event gets its own ID. Each start event must be
* associated with a stop event, otherwise the span is considered to be
* ongoing.
*/

type SpanStart = {
type: 'start';
id: SpanId;
parentId?: SpanId;
name?: string;
};

type SpanEvent = Span & {
type: 'start' | 'end';
id: string;
type SpanStop = {
type: 'stop';
id: SpanId;
startId: SpanId;
parentId?: SpanId;
};

export type { Span, SpanEvent };
type SpanEvent = SpanStart | SpanStop;

export type { SpanId, SpanEvent };
9 changes: 5 additions & 4 deletions tests/asciinemaTest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { SpanId } from '#tracer/index.js';
import fs from 'fs';
import * as fc from 'fast-check';
import tracer from '#tracer/index.js';

let parentIndex = 0;
let step = 0;
let nestedIds: Array<string> = [];
let nestedIds: Array<SpanId> = [];

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

const current: {
parentId?: string;
forkAId?: string;
forkBId?: string;
parentId?: SpanId;
forkAId?: SpanId;
forkBId?: SpanId;
flags: Flags;
} = {
flags: {