Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

LocalFileTracer.ts

112 lines3.2 KB

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