|
| 1 | +import { |
| 2 | + afterEach, |
| 3 | + beforeEach, |
| 4 | + describe, |
| 5 | + expect, |
| 6 | + expectTypeOf, |
| 7 | + test, |
| 8 | +} from "vitest"; |
| 9 | +import { |
| 10 | + _exportsForTestingOnly, |
| 11 | + initLogger, |
| 12 | + type TestBackgroundLogger, |
| 13 | +} from "./logger"; |
| 14 | +import { configureInstrumentation, registry } from "./instrumentation/registry"; |
| 15 | +import { configureNode } from "./node/config"; |
| 16 | +import { |
| 17 | + INSTRUMENTATION_NAMES, |
| 18 | + withSpanInstrumentationName, |
| 19 | +} from "./span-origin"; |
| 20 | +import type { SpanCustomizer, SpanExportData } from "./exports"; |
| 21 | + |
| 22 | +configureNode(); |
| 23 | + |
| 24 | +test("customizers expose only the outgoing-record export hook", () => { |
| 25 | + expectTypeOf<SpanCustomizer>().toEqualTypeOf<{ |
| 26 | + onSpanExport?(data: SpanExportData): SpanExportData; |
| 27 | + }>(); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("onSpanExport", () => { |
| 31 | + let memoryLogger: TestBackgroundLogger; |
| 32 | + |
| 33 | + beforeEach(async () => { |
| 34 | + registry.disable(); |
| 35 | + await _exportsForTestingOnly.simulateLoginForTests(); |
| 36 | + memoryLogger = _exportsForTestingOnly.useTestBackgroundLogger(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + configureInstrumentation({ spanCustomizers: [] }); |
| 41 | + _exportsForTestingOnly.clearTestBackgroundLogger(); |
| 42 | + }); |
| 43 | + |
| 44 | + function startInstrumentedSpan() { |
| 45 | + return initLogger({ |
| 46 | + projectName: "customizer-project", |
| 47 | + projectId: "customizer-project", |
| 48 | + }).startSpan( |
| 49 | + withSpanInstrumentationName( |
| 50 | + { name: "provider.call" }, |
| 51 | + INSTRUMENTATION_NAMES.OPENAI, |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + test("adds a field to outgoing span records", async () => { |
| 57 | + configureInstrumentation({ |
| 58 | + spanCustomizers: [ |
| 59 | + { |
| 60 | + onSpanExport(data) { |
| 61 | + data.custom_field = "added"; |
| 62 | + return data; |
| 63 | + }, |
| 64 | + }, |
| 65 | + ], |
| 66 | + }); |
| 67 | + |
| 68 | + const span = startInstrumentedSpan(); |
| 69 | + span.log({ output: "result" }); |
| 70 | + span.end(); |
| 71 | + |
| 72 | + const events = await memoryLogger.drain(); |
| 73 | + expect(events).toEqual([ |
| 74 | + expect.objectContaining({ |
| 75 | + id: span.id, |
| 76 | + project_id: "customizer-project", |
| 77 | + output: "result", |
| 78 | + custom_field: "added", |
| 79 | + }), |
| 80 | + ]); |
| 81 | + }); |
| 82 | + |
| 83 | + test("alters an existing field in outgoing span records", async () => { |
| 84 | + configureInstrumentation({ |
| 85 | + spanCustomizers: [ |
| 86 | + { |
| 87 | + onSpanExport(data) { |
| 88 | + if ("output" in data) data.output = "[redacted]"; |
| 89 | + return data; |
| 90 | + }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }); |
| 94 | + |
| 95 | + const span = startInstrumentedSpan(); |
| 96 | + span.log({ output: "sensitive response" }); |
| 97 | + span.end(); |
| 98 | + |
| 99 | + expect(await memoryLogger.drain()).toEqual([ |
| 100 | + expect.objectContaining({ id: span.id, output: "[redacted]" }), |
| 101 | + ]); |
| 102 | + }); |
| 103 | + |
| 104 | + test("deletes a field from outgoing span records", async () => { |
| 105 | + configureInstrumentation({ |
| 106 | + spanCustomizers: [ |
| 107 | + { |
| 108 | + onSpanExport(data) { |
| 109 | + delete data.error; |
| 110 | + return data; |
| 111 | + }, |
| 112 | + }, |
| 113 | + ], |
| 114 | + }); |
| 115 | + |
| 116 | + const span = startInstrumentedSpan(); |
| 117 | + span.log({ error: "sensitive error", output: "safe response" }); |
| 118 | + span.end(); |
| 119 | + |
| 120 | + const events = await memoryLogger.drain(); |
| 121 | + expect(events).toEqual([ |
| 122 | + expect.objectContaining({ id: span.id, output: "safe response" }), |
| 123 | + ]); |
| 124 | + expect(events[0]).not.toHaveProperty("error"); |
| 125 | + }); |
| 126 | + |
| 127 | + test("passes replacement records through later customizers despite errors", async () => { |
| 128 | + configureInstrumentation({ |
| 129 | + spanCustomizers: [ |
| 130 | + { |
| 131 | + onSpanExport(data) { |
| 132 | + return "output" in data ? { ...data, output: "replacement" } : data; |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + onSpanExport() { |
| 137 | + throw new Error("customizer failed"); |
| 138 | + }, |
| 139 | + }, |
| 140 | + { |
| 141 | + onSpanExport(data) { |
| 142 | + if (typeof data.output === "string") { |
| 143 | + data.output = data.output.toUpperCase(); |
| 144 | + } |
| 145 | + return data; |
| 146 | + }, |
| 147 | + }, |
| 148 | + ], |
| 149 | + }); |
| 150 | + |
| 151 | + const span = startInstrumentedSpan(); |
| 152 | + span.log({ output: "original" }); |
| 153 | + span.end(); |
| 154 | + |
| 155 | + expect(await memoryLogger.drain()).toEqual([ |
| 156 | + expect.objectContaining({ |
| 157 | + id: span.id, |
| 158 | + output: "REPLACEMENT", |
| 159 | + metrics: expect.objectContaining({ end: expect.any(Number) }), |
| 160 | + }), |
| 161 | + ]); |
| 162 | + }); |
| 163 | + |
| 164 | + test("does not customize manually created spans", async () => { |
| 165 | + configureInstrumentation({ |
| 166 | + spanCustomizers: [ |
| 167 | + { |
| 168 | + onSpanExport(data) { |
| 169 | + data.tags = ["customized"]; |
| 170 | + return data; |
| 171 | + }, |
| 172 | + }, |
| 173 | + ], |
| 174 | + }); |
| 175 | + |
| 176 | + const instrumented = startInstrumentedSpan(); |
| 177 | + const manual = instrumented.startSpan({ name: "manual child" }); |
| 178 | + manual.log({ output: "manual result" }); |
| 179 | + manual.end(); |
| 180 | + instrumented.end(); |
| 181 | + |
| 182 | + const events = await memoryLogger.drain(); |
| 183 | + expect(events.find((event) => event.id === instrumented.id)).toMatchObject({ |
| 184 | + tags: ["customized"], |
| 185 | + }); |
| 186 | + const manualEvent = events.find((event) => event.id === manual.id); |
| 187 | + expect(manualEvent).toMatchObject({ output: "manual result" }); |
| 188 | + expect(manualEvent).not.toHaveProperty("tags"); |
| 189 | + }); |
| 190 | +}); |
0 commit comments