Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-wrap-traced-no-trace-io.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"braintrust": patch
---

fix: Don't ignore `noTraceIO` for `wrapTraced` around plain functions
65 changes: 65 additions & 0 deletions js/src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,71 @@ describe("isGeneratorFunction and isAsyncGeneratorFunction utilities", () => {
});
});

describe("wrapTraced noTraceIO", () => {
let memoryLogger: any;

beforeEach(async () => {
await _exportsForTestingOnly.simulateLoginForTests();
memoryLogger = _exportsForTestingOnly.useTestBackgroundLogger();
});

afterEach(() => {
_exportsForTestingOnly.clearTestBackgroundLogger();
_exportsForTestingOnly.simulateLogoutForTests();
});

test("preserves manually logged input and output for async functions", async () => {
initLogger({ projectName: "test", projectId: "test-project-id" });

const callModel = wrapTraced(
async (request: { input: string }) => {
const response = { output: "manual output", metadata: "extra" };
currentSpan().log({
input: request.input,
output: response.output,
});
return response;
},
{ noTraceIO: true },
);

await callModel({ input: "manual input" });

await memoryLogger.flush();
const logs = await memoryLogger.drain();
expect(logs).toHaveLength(1);
expect(logs[0].input).toBe("manual input");
expect(logs[0].output).toBe("manual output");
});

test("preserves manually logged input and output for synchronous functions", async () => {
initLogger({ projectName: "test", projectId: "test-project-id" });

const callModel = wrapTraced(
(request: { input: string }) => {
const response = { output: "manual output", metadata: "extra" };
currentSpan().log({
input: request.input,
output: response.output,
});
return response;
},
{ noTraceIO: true, asyncFlush: true },
);

expect(callModel({ input: "manual input" })).toEqual({
output: "manual output",
metadata: "extra",
});

await memoryLogger.flush();
const logs = await memoryLogger.drain();
expect(logs).toHaveLength(1);
expect(logs[0].input).toBe("manual input");
expect(logs[0].output).toBe("manual output");
});
});

describe("wrapTraced generator support", () => {
let memoryLogger: any;
let originalEnv: string | undefined;
Expand Down
8 changes: 4 additions & 4 deletions js/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6289,13 +6289,13 @@ export function wrapTraced<
if (args?.asyncFlush) {
return ((...fnArgs: Parameters<F>) =>
traced((span) => {
if (!hasExplicitInput) {
if (!args?.noTraceIO && !hasExplicitInput) {
span.log({ input: fnArgs });
}

const output = fn(...fnArgs);

if (!hasExplicitOutput) {
if (!args?.noTraceIO && !hasExplicitOutput) {
if (output instanceof Promise) {
return (async () => {
const result = await output;
Expand All @@ -6312,15 +6312,15 @@ export function wrapTraced<
} else {
return ((...fnArgs: Parameters<F>) =>
traced(async (span) => {
if (!hasExplicitInput) {
if (!args?.noTraceIO && !hasExplicitInput) {
span.log({ input: fnArgs });
}

const outputResult = fn(...fnArgs);

const output = await outputResult;

if (!hasExplicitOutput) {
if (!args?.noTraceIO && !hasExplicitOutput) {
span.log({ output });
}

Expand Down
Loading