Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

otel-capture.ts

113 lines2.8 KB

1import type {
2 AttributeValue,
3 Attributes,
4 SpanStatusCode,
5} from '@opentelemetry/api'
6​
7export interface CapturedEvent {
8 name: string
9 attributes?: Attributes
11​
12export interface CapturedException {
13 exception: string
14 attributes?: Attributes
16​
17export interface CapturedSpan {
18 id: string
19 name: string
20 kind?: number
21 attributes: Record<string, AttributeValue>
22 status: SpanStatusCode
23 statusMessage?: string
24 events: Array<CapturedEvent>
25 exceptions: Array<CapturedException>
26 ended: boolean
28​
29export interface CapturedHistogram {
30 name: string
31 value: number
32 attributes?: Attributes
33 unit?: string
35​
36export interface OtelCapture {
37 spans: Array<CapturedSpan>
38 histograms: Array<CapturedHistogram>
40​
41// Keyed by testId. Lives as a module-global — Nitro's dev server keeps a
42// single JS context per process, which is all these tests need.
43const captures: Map<string, OtelCapture> = new Map()
44​
45function bucketFor(captureId: string): OtelCapture {
46 let bucket = captures.get(captureId)
47 if (!bucket) {
48 bucket = { spans: [], histograms: [] }
49 captures.set(captureId, bucket)
50 }
51 return bucket
53​
54export function resetOtelCapture(captureId: string): void {
55 captures.set(captureId, { spans: [], histograms: [] })
57​
58export function getOtelCapture(captureId: string): OtelCapture {
59 return bucketFor(captureId)
61​
62/**
63 * Append a new span or patch an existing one.
64 *
65 * Patches are applied with a shallow `Object.assign`, so nested objects and
66 * arrays (e.g. `attributes`, `events`, `exceptions`) are replaced, not merged.
67 * Callers that want to extend those fields must pre-merge before passing the
68 * patch (see how `api.middleware-test.ts` spreads `{ ...attrs }`). Prefer
69 * `recordOtelEvent` / `recordOtelException` for those fields — they append
70 * rather than replace.
71 */
72export function recordOtelSpan(
73 captureId: string,
74 entry:
75 | CapturedSpan
76 | { id: string; patch: Partial<Omit<CapturedSpan, 'id'>> },
77): void {
78 const bucket = bucketFor(captureId)
79 if ('patch' in entry) {
80 const existing = bucket.spans.find((s) => s.id === entry.id)
81 if (!existing) return
82 Object.assign(existing, entry.patch)
83 return
84 }
85 bucket.spans.push(entry)
87​
88export function recordOtelEvent(
89 captureId: string,
90 spanId: string,
91 event: CapturedEvent,
92): void {
93 const existing = bucketFor(captureId).spans.find((s) => s.id === spanId)
94 if (!existing) return
95 existing.events.push(event)
97​
98export function recordOtelException(
99 captureId: string,
100 spanId: string,
101 exception: CapturedException,
102): void {
103 const existing = bucketFor(captureId).spans.find((s) => s.id === spanId)
104 if (!existing) return
105 existing.exceptions.push(exception)
107​
108export function recordOtelHistogram(
109 captureId: string,
110 entry: CapturedHistogram,
111): void {
112 bucketFor(captureId).histograms.push(entry)