Skip to content

ci: merge staging to master #58

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 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default } from './Logger.js';
export { default as Handler } from './Handler.js';
export { default as tracer } from './tracer/index.js';
export * as formatting from './formatting.js';
export * from './handlers/index.js';
export * from './tracer/index.js';
Expand Down
35 changes: 17 additions & 18 deletions src/tracer/Tracer.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
import type { SpanEvent, SpanId } from './types.js';
import type { SpanEvent } from './types.js';
import { IdSortable, utils as idUtils } from '@matrixai/id';

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

protected queueSpanEvent(evt: SpanEvent) {
// Convert the binary id to base64-encoded id
if (evt.id instanceof IdSortable) {
evt.id = idUtils.toMultibase(evt.id.get(), 'base64');
}
if (evt.spanId instanceof IdSortable) {
evt.spanId = idUtils.toMultibase(evt.spanId.get(), 'base64');
}
if (evt.parentSpanId instanceof IdSortable) {
evt.parentSpanId = idUtils.toMultibase(evt.parentSpanId.get(), 'base64');
protected nextId(): string {
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');
}

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

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

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

public async traced<T>(
name: string,
parentSpanId: SpanId | undefined,
parentSpanId: string | undefined,
fn: () => T | Promise<T>,
): Promise<T> {
const fnProm = async () => {
Expand Down
13 changes: 4 additions & 9 deletions src/tracer/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import type { IdSortable } from '@matrixai/id';

type SpanId = IdSortable | string;
type EventId = IdSortable | string;

type Span = {
spanId: SpanId;
spanId: string;
name: string;
parentSpanId?: SpanId;
parentSpanId?: string;
};

type SpanEvent = Span & {
type: 'start' | 'end';
id: EventId;
id: string;
};

export type { SpanId, EventId, Span, SpanEvent };
export type { Span, SpanEvent };
9 changes: 4 additions & 5 deletions tests/asciinemaTest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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<SpanId> = [];
let nestedIds: Array<string> = [];

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

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