Skip to content
Open
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/bump-temporal-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@braintrust/temporal": minor
---

feat: Bump support for temporal
10 changes: 5 additions & 5 deletions integrations/temporal-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"author": "Braintrust Data Inc.",
"license": "MIT",
"devDependencies": {
"@temporalio/activity": "^1.14.1",
"@temporalio/client": "^1.14.1",
"@temporalio/common": "^1.14.1",
"@temporalio/worker": "^1.14.1",
"@temporalio/workflow": "^1.14.1",
"@temporalio/activity": "^1.25.0",
"@temporalio/client": "^1.25.0",
"@temporalio/common": "^1.25.0",
"@temporalio/worker": "^1.25.0",
"@temporalio/workflow": "^1.25.0",
"@types/node": "^22.15.21",
"braintrust": "workspace:*",
"tsup": "^8.5.0",
Expand Down
73 changes: 73 additions & 0 deletions integrations/temporal-js/src/interceptors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import type { Context } from "@temporalio/activity";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { createBraintrustActivityInterceptor } from "./interceptors";
import { BRAINTRUST_SPAN_HEADER, serializeHeaderValue } from "./utils";

const mock = vi.hoisted(() => {
const span = { log: vi.fn(), end: vi.fn() };
return { span, startSpan: vi.fn(() => span) };
});

vi.mock("braintrust", () => ({
startSpan: mock.startSpan,
withCurrent: <R>(_span: unknown, callback: () => R): R => callback(),
}));

beforeEach(() => vi.clearAllMocks());

describe.each([
{
name: "workflow",
workflowExecution: { workflowId: "workflow", runId: "run" },
},
{ name: "standalone", workflowExecution: undefined },
])("$name activity", ({ workflowExecution }) => {
const context = {
info: {
activityId: "activity",
activityType: "generate",
workflowExecution,
},
} as Context;

test("runs the activity and preserves parent context and output", async () => {
const interceptor = createBraintrustActivityInterceptor(context).inbound!;
const input = {
args: ["prompt"],
headers: { [BRAINTRUST_SPAN_HEADER]: serializeHeaderValue("parent") },
};
const output = { text: "result" };
const next = vi.fn().mockResolvedValue(output);

await expect(interceptor.execute!(input, next)).resolves.toBe(output);
expect(next).toHaveBeenCalledExactlyOnceWith(input);
expect(mock.startSpan).toHaveBeenCalledWith(
expect.objectContaining({
parent: "parent",
event: {
metadata: {
"temporal.activity_type": "generate",
"temporal.activity_id": "activity",
"temporal.workflow_id": workflowExecution?.workflowId,
"temporal.workflow_run_id": workflowExecution?.runId,
},
},
}),
);
expect(mock.span.log).toHaveBeenCalledWith({ output });
expect(mock.span.end).toHaveBeenCalledOnce();
});

test("preserves activity errors and ends the span", async () => {
const interceptor = createBraintrustActivityInterceptor(context).inbound!;
const error = new Error("activity failed");
const next = vi.fn().mockRejectedValue(error);

await expect(
interceptor.execute!({ args: [], headers: {} }, next),
).rejects.toBe(error);
expect(next).toHaveBeenCalledOnce();
expect(mock.span.log).toHaveBeenCalledWith({ error: String(error) });
expect(mock.span.end).toHaveBeenCalledOnce();
});
});
6 changes: 3 additions & 3 deletions integrations/temporal-js/src/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ class BraintrustActivityInterceptor implements ActivityInboundCallsInterceptor {
next: Next<ActivityInboundCallsInterceptor, "execute">,
): Promise<unknown> {
const info = this.ctx.info;
const runId = info.workflowExecution.runId;
const runId = info.workflowExecution?.runId;

// Try to get workflow span export - first check local Map, then headers
let parent: string | undefined;

// Check if we have the workflow span export locally (same worker as workflow)
const spanExportPromise = getWorkflowSpanExport(runId);
const spanExportPromise = runId ? getWorkflowSpanExport(runId) : undefined;
if (spanExportPromise) {
try {
parent = await spanExportPromise;
Expand Down Expand Up @@ -159,7 +159,7 @@ class BraintrustActivityInterceptor implements ActivityInboundCallsInterceptor {
metadata: {
"temporal.activity_type": info.activityType,
"temporal.activity_id": info.activityId,
"temporal.workflow_id": info.workflowExecution.workflowId,
"temporal.workflow_id": info.workflowExecution?.workflowId,
"temporal.workflow_run_id": runId,
},
},
Expand Down
Loading
Loading