|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import type { Settings } from "../config/settings.js"; |
| 4 | +import { clear, end, snapshot, start, type PerfSpan } from "./index.js"; |
| 5 | +import { |
| 6 | + buildOtlpPayload, |
| 7 | + flushPerfToOtel, |
| 8 | + flushToOtel, |
| 9 | + monoToUnixNano, |
| 10 | + otelSpanId, |
| 11 | + otlpTracesUrl, |
| 12 | + tagsToOtlpAttributes, |
| 13 | +} from "./otel-sink.js"; |
| 14 | +import type { EnabledOtelExportConfig } from "./otel-config.js"; |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + clear(); |
| 18 | +}); |
| 19 | + |
| 20 | +const enabledConfig = ( |
| 21 | + overrides: Partial<EnabledOtelExportConfig> = {}, |
| 22 | +): EnabledOtelExportConfig => ({ |
| 23 | + enabled: true, |
| 24 | + endpoint: "http://localhost:4318", |
| 25 | + headers: {}, |
| 26 | + serviceName: "corbits-code", |
| 27 | + resourceAttributes: { "service.name": "corbits-code" }, |
| 28 | + ...overrides, |
| 29 | +}); |
| 30 | + |
| 31 | +const baseSettings = (otel?: Settings["otel"]): Settings => ({ |
| 32 | + providers: {}, |
| 33 | + ...(otel !== undefined ? { otel } : {}), |
| 34 | +}); |
| 35 | + |
| 36 | +const mockFetch = (impl: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>) => |
| 37 | + impl as unknown as typeof fetch; |
| 38 | + |
| 39 | +describe("otlpTracesUrl", () => { |
| 40 | + test("appends /v1/traces to base endpoint", () => { |
| 41 | + expect(otlpTracesUrl("http://localhost:4318")).toBe("http://localhost:4318/v1/traces"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("does not double-append when path already ends with /v1/traces", () => { |
| 45 | + expect(otlpTracesUrl("https://app.phoenix.arize.com/v1/traces")).toBe( |
| 46 | + "https://app.phoenix.arize.com/v1/traces", |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + test("strips trailing slash before appending", () => { |
| 51 | + expect(otlpTracesUrl("http://localhost:4318/")).toBe("http://localhost:4318/v1/traces"); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe("otelSpanId", () => { |
| 56 | + test("is 16 hex chars and stable", () => { |
| 57 | + const a = otelSpanId("1"); |
| 58 | + const b = otelSpanId("1"); |
| 59 | + expect(a).toMatch(/^[0-9a-f]{16}$/); |
| 60 | + expect(a).toBe(b); |
| 61 | + expect(otelSpanId("2")).not.toBe(a); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("tagsToOtlpAttributes", () => { |
| 66 | + test("maps string and integer tags", () => { |
| 67 | + const attrs = tagsToOtlpAttributes({ |
| 68 | + provider_id: "openai", |
| 69 | + count: 3, |
| 70 | + transport: "ws", |
| 71 | + }); |
| 72 | + expect(attrs).toEqual([ |
| 73 | + { key: "count", value: { intValue: "3" } }, |
| 74 | + { key: "provider_id", value: { stringValue: "openai" } }, |
| 75 | + { key: "transport", value: { stringValue: "ws" } }, |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + test("re-sanitizes forbidden keys at export", () => { |
| 80 | + const attrs = tagsToOtlpAttributes({ |
| 81 | + provider_id: "xai", |
| 82 | + prompt: "secret", |
| 83 | + path: "/tmp/x", |
| 84 | + } as never); |
| 85 | + expect(attrs).toEqual([{ key: "provider_id", value: { stringValue: "xai" } }]); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe("buildOtlpPayload", () => { |
| 90 | + test("maps parent links, names, and times", () => { |
| 91 | + const turnId = start("turn"); |
| 92 | + const infId = start("inference", { parentId: turnId, tags: { model_id: "m1" } }); |
| 93 | + end(infId); |
| 94 | + end(turnId); |
| 95 | + const spans: PerfSpan[] = [ |
| 96 | + { |
| 97 | + id: turnId, |
| 98 | + name: "turn", |
| 99 | + startNs: 1000n, |
| 100 | + endNs: 5000n, |
| 101 | + }, |
| 102 | + { |
| 103 | + id: infId, |
| 104 | + name: "inference", |
| 105 | + parentId: turnId, |
| 106 | + startNs: 2000n, |
| 107 | + endNs: 4000n, |
| 108 | + tags: { model_id: "m1" }, |
| 109 | + }, |
| 110 | + ]; |
| 111 | + |
| 112 | + const anchor = { monoNs: 0n, unixNs: 1_000_000_000_000n }; |
| 113 | + const payload = buildOtlpPayload(spans, enabledConfig(), { |
| 114 | + wallAnchor: anchor, |
| 115 | + traceId: "a".repeat(32), |
| 116 | + }); |
| 117 | + |
| 118 | + const otlpSpans = payload.resourceSpans[0]!.scopeSpans[0]!.spans; |
| 119 | + expect(otlpSpans).toHaveLength(2); |
| 120 | + |
| 121 | + const turn = otlpSpans.find((s) => s.name === "turn")!; |
| 122 | + const inf = otlpSpans.find((s) => s.name === "inference")!; |
| 123 | + expect(turn.traceId).toBe("a".repeat(32)); |
| 124 | + expect(turn.spanId).toBe(otelSpanId(turnId)); |
| 125 | + expect(turn.parentSpanId).toBeUndefined(); |
| 126 | + expect(turn.startTimeUnixNano).toBe(monoToUnixNano(1000n, anchor).toString()); |
| 127 | + expect(turn.endTimeUnixNano).toBe(monoToUnixNano(5000n, anchor).toString()); |
| 128 | + |
| 129 | + expect(inf.parentSpanId).toBe(otelSpanId(turnId)); |
| 130 | + expect(inf.attributes).toEqual([{ key: "model_id", value: { stringValue: "m1" } }]); |
| 131 | + |
| 132 | + const resource = payload.resourceSpans[0]!.resource.attributes; |
| 133 | + expect( |
| 134 | + resource.some( |
| 135 | + (a) => a.key === "service.name" && "stringValue" in a.value && a.value.stringValue === "corbits-code", |
| 136 | + ), |
| 137 | + ).toBe(true); |
| 138 | + }); |
| 139 | + |
| 140 | + test("open spans use nowMonoNs as end", () => { |
| 141 | + const spans: PerfSpan[] = [{ id: "open1", name: "session", startNs: 10n }]; |
| 142 | + const anchor = { monoNs: 0n, unixNs: 0n }; |
| 143 | + const payload = buildOtlpPayload(spans, enabledConfig(), { |
| 144 | + wallAnchor: anchor, |
| 145 | + nowMonoNs: () => 99n, |
| 146 | + traceId: "b".repeat(32), |
| 147 | + }); |
| 148 | + const span = payload.resourceSpans[0]!.scopeSpans[0]!.spans[0]!; |
| 149 | + expect(span.startTimeUnixNano).toBe("10"); |
| 150 | + expect(span.endTimeUnixNano).toBe("99"); |
| 151 | + }); |
| 152 | +}); |
| 153 | + |
| 154 | +describe("flushToOtel", () => { |
| 155 | + test("POSTs OTLP JSON with headers to /v1/traces", async () => { |
| 156 | + const calls: Array<{ url: string; init: RequestInit }> = []; |
| 157 | + const fetchFn = mockFetch(async (input, init) => { |
| 158 | + calls.push({ url: String(input), init: init ?? {} }); |
| 159 | + return new Response(null, { status: 200 }); |
| 160 | + }); |
| 161 | + |
| 162 | + const spans: PerfSpan[] = [{ id: "1", name: "turn", startNs: 1n, endNs: 2n }]; |
| 163 | + await flushToOtel( |
| 164 | + spans, |
| 165 | + enabledConfig({ |
| 166 | + endpoint: "https://collector.example", |
| 167 | + headers: { Authorization: "Bearer secret" }, |
| 168 | + }), |
| 169 | + { fetchFn, traceId: "c".repeat(32), wallAnchor: { monoNs: 0n, unixNs: 0n } }, |
| 170 | + ); |
| 171 | + |
| 172 | + expect(calls).toHaveLength(1); |
| 173 | + expect(calls[0]!.url).toBe("https://collector.example/v1/traces"); |
| 174 | + const headers = calls[0]!.init.headers as Record<string, string>; |
| 175 | + expect(headers["content-type"]).toBe("application/json"); |
| 176 | + expect(headers.Authorization).toBe("Bearer secret"); |
| 177 | + |
| 178 | + const body = JSON.parse(String(calls[0]!.init.body)) as { |
| 179 | + resourceSpans: unknown[]; |
| 180 | + }; |
| 181 | + expect(body.resourceSpans).toHaveLength(1); |
| 182 | + }); |
| 183 | + |
| 184 | + test("empty spans do not call fetch", async () => { |
| 185 | + let called = 0; |
| 186 | + const fetchFn = mockFetch(async () => { |
| 187 | + called += 1; |
| 188 | + return new Response(null, { status: 200 }); |
| 189 | + }); |
| 190 | + await flushToOtel([], enabledConfig(), { fetchFn }); |
| 191 | + expect(called).toBe(0); |
| 192 | + }); |
| 193 | + |
| 194 | + test("network errors are swallowed", async () => { |
| 195 | + const fetchFn = mockFetch(async () => { |
| 196 | + throw new Error("ECONNREFUSED"); |
| 197 | + }); |
| 198 | + await expect( |
| 199 | + flushToOtel([{ id: "1", name: "turn", startNs: 1n, endNs: 2n }], enabledConfig(), { |
| 200 | + fetchFn, |
| 201 | + }), |
| 202 | + ).resolves.toBeUndefined(); |
| 203 | + }); |
| 204 | + |
| 205 | + test("non-2xx responses are swallowed", async () => { |
| 206 | + const fetchFn = mockFetch(async () => new Response("nope", { status: 503 })); |
| 207 | + await expect( |
| 208 | + flushToOtel([{ id: "1", name: "turn", startNs: 1n, endNs: 2n }], enabledConfig(), { |
| 209 | + fetchFn, |
| 210 | + }), |
| 211 | + ).resolves.toBeUndefined(); |
| 212 | + }); |
| 213 | +}); |
| 214 | + |
| 215 | +describe("flushPerfToOtel", () => { |
| 216 | + test("disabled config performs zero network", async () => { |
| 217 | + let called = 0; |
| 218 | + const fetchFn = mockFetch(async () => { |
| 219 | + called += 1; |
| 220 | + return new Response(null, { status: 200 }); |
| 221 | + }); |
| 222 | + |
| 223 | + const id = start("turn"); |
| 224 | + end(id); |
| 225 | + await flushPerfToOtel(baseSettings(), {}, { fetchFn }); |
| 226 | + expect(called).toBe(0); |
| 227 | + }); |
| 228 | + |
| 229 | + test("enabled config snapshots and POSTs", async () => { |
| 230 | + const calls: string[] = []; |
| 231 | + const fetchFn = mockFetch(async (input) => { |
| 232 | + calls.push(String(input)); |
| 233 | + return new Response(null, { status: 200 }); |
| 234 | + }); |
| 235 | + |
| 236 | + const id = start("session"); |
| 237 | + end(id); |
| 238 | + |
| 239 | + await flushPerfToOtel(baseSettings({ endpoint: "http://127.0.0.1:4318" }), {}, { fetchFn, getSpans: snapshot }); |
| 240 | + expect(calls).toEqual(["http://127.0.0.1:4318/v1/traces"]); |
| 241 | + }); |
| 242 | + |
| 243 | + test("invalid config does not throw and does not fetch", async () => { |
| 244 | + let called = 0; |
| 245 | + const fetchFn = mockFetch(async () => { |
| 246 | + called += 1; |
| 247 | + return new Response(null, { status: 200 }); |
| 248 | + }); |
| 249 | + |
| 250 | + await expect( |
| 251 | + flushPerfToOtel(baseSettings({ enabled: true }), {}, { fetchFn }), |
| 252 | + ).resolves.toBeUndefined(); |
| 253 | + expect(called).toBe(0); |
| 254 | + }); |
| 255 | + |
| 256 | + test("explicit spans override ring snapshot", async () => { |
| 257 | + const bodies: string[] = []; |
| 258 | + const fetchFn = mockFetch(async (_input, init) => { |
| 259 | + bodies.push(String(init?.body ?? "")); |
| 260 | + return new Response(null, { status: 200 }); |
| 261 | + }); |
| 262 | + |
| 263 | + // Ring has a session span — should be ignored when spans is provided. |
| 264 | + start("session"); |
| 265 | + const only: PerfSpan[] = [ |
| 266 | + { id: "only", name: "tool", startNs: 1n, endNs: 2n, tags: { tool_id: "t1" } }, |
| 267 | + ]; |
| 268 | + await flushPerfToOtel( |
| 269 | + baseSettings({ endpoint: "http://localhost:4318" }), |
| 270 | + {}, |
| 271 | + { |
| 272 | + fetchFn, |
| 273 | + spans: only, |
| 274 | + traceId: "d".repeat(32), |
| 275 | + wallAnchor: { monoNs: 0n, unixNs: 0n }, |
| 276 | + }, |
| 277 | + ); |
| 278 | + expect(bodies).toHaveLength(1); |
| 279 | + const parsed = JSON.parse(bodies[0]!) as { |
| 280 | + resourceSpans: Array<{ |
| 281 | + scopeSpans: Array<{ spans: Array<{ name: string }> }>; |
| 282 | + }>; |
| 283 | + }; |
| 284 | + const names = parsed.resourceSpans[0]!.scopeSpans[0]!.spans.map((s) => s.name); |
| 285 | + expect(names).toEqual(["tool"]); |
| 286 | + }); |
| 287 | +}); |
0 commit comments