Skip to content

Commit 1b947c9

Browse files
committed
multi projec tracing
1 parent 38a6c52 commit 1b947c9

3 files changed

Lines changed: 68 additions & 14 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"braintrust": patch
3+
---
4+
5+
Allow spans started with an exported parent to write to the receiver object while preserving trace lineage.

js/src/logger.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,68 @@ describe("parent precedence", () => {
24292429
expect(byName.forced.span_parents).toContain(byName.outer.span_id);
24302430
expect(byName.forced.span_parents).not.toContain(byName.inner.span_id);
24312431
});
2432+
2433+
test("logger.startSpan with exported parent uses receiver project", async () => {
2434+
const primaryLogger = initLogger({
2435+
projectName: "primary",
2436+
projectId: "project-a",
2437+
});
2438+
const secondaryLogger = initLogger({
2439+
projectName: "secondary",
2440+
projectId: "project-b",
2441+
});
2442+
2443+
const root = primaryLogger.startSpan({ name: "root" });
2444+
const parentStr = await root.export();
2445+
root.end();
2446+
2447+
const child = secondaryLogger.startSpan({
2448+
name: "child",
2449+
parent: parentStr,
2450+
});
2451+
child.end();
2452+
2453+
await memory.flush();
2454+
const events = await memory.drain();
2455+
const byName: any = Object.fromEntries(
2456+
events.map((e: any) => [e.span_attributes?.name, e]),
2457+
);
2458+
2459+
expect(byName.child.project_id).toBe("project-b");
2460+
expect(byName.child.root_span_id).toBe(byName.root.root_span_id);
2461+
expect(byName.child.span_parents).toContain(byName.root.span_id);
2462+
});
2463+
2464+
test("experiment.startSpan with exported parent uses receiver experiment", async () => {
2465+
const primaryExperiment = _exportsForTestingOnly.initTestExperiment(
2466+
"experiment-a",
2467+
"project-a",
2468+
);
2469+
const secondaryExperiment = _exportsForTestingOnly.initTestExperiment(
2470+
"experiment-b",
2471+
"project-b",
2472+
);
2473+
2474+
const root = primaryExperiment.startSpan({ name: "root" });
2475+
const parentStr = await root.export();
2476+
root.end();
2477+
2478+
const child = secondaryExperiment.startSpan({
2479+
name: "child",
2480+
parent: parentStr,
2481+
});
2482+
child.end();
2483+
2484+
await memory.flush();
2485+
const events = await memory.drain();
2486+
const byName: any = Object.fromEntries(
2487+
events.map((e: any) => [e.span_attributes?.name, e]),
2488+
);
2489+
2490+
expect(byName.child.experiment_id).toBe("experiment-b");
2491+
expect(byName.child.root_span_id).toBe(byName.root.root_span_id);
2492+
expect(byName.child.span_parents).toContain(byName.root.span_id);
2493+
});
24322494
});
24332495

24342496
test("attachment with unreadable path logs warning", () => {

js/src/logger.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,20 +2236,7 @@ function startSpanParentArgs(args: {
22362236
);
22372237
}
22382238

2239-
const parentComponentsObjectIdLambda = spanComponentsToObjectIdLambda(
2240-
args.state,
2241-
parentComponents,
2242-
);
2243-
const computeParentObjectId = async () => {
2244-
const parentComponentsObjectId = await parentComponentsObjectIdLambda();
2245-
if ((await args.parentObjectId.get()) !== parentComponentsObjectId) {
2246-
throw new Error(
2247-
`Mismatch between expected span parent object id ${await args.parentObjectId.get()} and provided id ${parentComponentsObjectId}`,
2248-
);
2249-
}
2250-
return await args.parentObjectId.get();
2251-
};
2252-
argParentObjectId = new LazyValue(computeParentObjectId);
2239+
argParentObjectId = args.parentObjectId;
22532240
if (parentComponents.data.row_id) {
22542241
argParentSpanIds = {
22552242
spanId: parentComponents.data.span_id,

0 commit comments

Comments
 (0)