Open History to see the last commit
1import type * as Exit from "effect/Exit";
2import { Effect, Option, Tracer } from "effect";
3
4import { EffectTraceRecord, spanToTraceRecord } from "./TraceRecord.ts";
5import { makeTraceSink, type TraceSink } from "./TraceSink.ts";
6
7export interface LocalFileTracerOptions {
8 readonly filePath: string;
9 readonly maxBytes: number;
10 readonly maxFiles: number;
11 readonly batchWindowMs: number;
12 readonly delegate?: Tracer.Tracer;
13 readonly sink?: TraceSink;
14}
15
16class LocalFileSpan implements Tracer.Span {
17 readonly _tag = "Span";
18 readonly name: string;
19 readonly spanId: string;
20 readonly traceId: string;
21 readonly parent: Option.Option<Tracer.AnySpan>;
22 readonly annotations: Tracer.Span["annotations"];
23 readonly links: Array<Tracer.SpanLink>;
24 readonly sampled: boolean;
25 readonly kind: Tracer.SpanKind;
26
27 status: Tracer.SpanStatus;
28 attributes: Map<string, unknown>;
29 events: Array<[name: string, startTime: bigint, attributes: Record<string, unknown>]>;
30
31 constructor(
32 options: Parameters<Tracer.Tracer["span"]>[0],
33 private readonly delegate: Tracer.Span,
34 private readonly push: (record: EffectTraceRecord) => void,
35 ) {
36 this.name = delegate.name;
37 this.spanId = delegate.spanId;
38 this.traceId = delegate.traceId;
39 this.parent = options.parent;
40 this.annotations = options.annotations;
41 this.links = [...options.links];
42 this.sampled = delegate.sampled;
43 this.kind = delegate.kind;
44 this.status = {
45 _tag: "Started",
46 startTime: options.startTime,
47 };
48 this.attributes = new Map();
49 this.events = [];
50 }
51
52 end(endTime: bigint, exit: Exit.Exit<unknown, unknown>): void {
53 this.status = {
54 _tag: "Ended",
55 startTime: this.status.startTime,
56 endTime,
57 exit,
58 };
59 this.delegate.end(endTime, exit);
60
61 if (this.sampled) {
62 this.push(spanToTraceRecord(this));
63 }
64 }
65
66 attribute(key: string, value: unknown): void {
67 this.attributes.set(key, value);
68 this.delegate.attribute(key, value);
69 }
70
71 event(name: string, startTime: bigint, attributes?: Record<string, unknown>): void {
72 const nextAttributes = attributes ?? {};
73 this.events.push([name, startTime, nextAttributes]);
74 this.delegate.event(name, startTime, nextAttributes);
75 }
76
77 addLinks(links: ReadonlyArray<Tracer.SpanLink>): void {
78 this.links.push(...links);
79 this.delegate.addLinks(links);
80 }
81}
82
83export const makeLocalFileTracer = Effect.fn("makeLocalFileTracer")(function* (
84 options: LocalFileTracerOptions,
85) {
86 const sink =
87 options.sink ??
88 (yield* makeTraceSink({
89 filePath: options.filePath,
90 maxBytes: options.maxBytes,
91 maxFiles: options.maxFiles,
92 batchWindowMs: options.batchWindowMs,
93 }));
94
95 const delegate =
96 options.delegate ??
97 Tracer.make({
98 span: (spanOptions) => new Tracer.NativeSpan(spanOptions),
99 });
100
101 return Tracer.make({
102 span(spanOptions) {
103 return new LocalFileSpan(spanOptions, delegate.span(spanOptions), sink.push);
104 },
105 ...(delegate.context ? { context: delegate.context } : {}),
106 });
107});