Skip to content

Commit 73015bc

Browse files
authored
fix(js): include project/session name in distributed tracing (#1667)
1 parent dfbac69 commit 73015bc

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "langsmith",
3-
"version": "0.3.16",
3+
"version": "0.3.17",
44
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
55
"packageManager": "[email protected]",
66
"files": [

js/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ export { RunTree, type RunTreeConfig } from "./run_trees.js";
1818
export { overrideFetchImplementation } from "./singletons/fetch.js";
1919

2020
// Update using yarn bump-version
21-
export const __version__ = "0.3.16";
21+
export const __version__ = "0.3.17";

js/src/run_trees.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,35 @@ interface HeadersLike {
115115
class Baggage {
116116
metadata: KVMap | undefined;
117117
tags: string[] | undefined;
118-
119-
constructor(metadata: KVMap | undefined, tags: string[] | undefined) {
118+
project_name: string | undefined;
119+
constructor(
120+
metadata: KVMap | undefined,
121+
tags: string[] | undefined,
122+
project_name: string | undefined
123+
) {
120124
this.metadata = metadata;
121125
this.tags = tags;
126+
this.project_name = project_name;
122127
}
123128

124129
static fromHeader(value: string) {
125130
const items = value.split(",");
126131
let metadata: KVMap = {};
127132
let tags: string[] = [];
133+
let project_name: string | undefined;
128134
for (const item of items) {
129135
const [key, uriValue] = item.split("=");
130136
const value = decodeURIComponent(uriValue);
131137
if (key === "langsmith-metadata") {
132138
metadata = JSON.parse(value);
133139
} else if (key === "langsmith-tags") {
134140
tags = value.split(",");
141+
} else if (key === "langsmith-project") {
142+
project_name = value;
135143
}
136144
}
137145

138-
return new Baggage(metadata, tags);
146+
return new Baggage(metadata, tags, project_name);
139147
}
140148

141149
toHeader(): string {
@@ -150,6 +158,10 @@ class Baggage {
150158
if (this.tags && this.tags.length > 0) {
151159
items.push(`langsmith-tags=${encodeURIComponent(this.tags.join(","))}`);
152160
}
161+
if (this.project_name) {
162+
items.push(`langsmith-project=${encodeURIComponent(this.project_name)}`);
163+
}
164+
153165
return items.join(",");
154166
}
155167
}
@@ -556,6 +568,7 @@ export class RunTree implements BaseRun {
556568
const baggage = Baggage.fromHeader(rawHeaders["baggage"]);
557569
config.metadata = baggage.metadata;
558570
config.tags = baggage.tags;
571+
config.project_name = baggage.project_name;
559572
}
560573

561574
return new RunTree(config);
@@ -564,7 +577,11 @@ export class RunTree implements BaseRun {
564577
toHeaders(headers?: HeadersLike) {
565578
const result = {
566579
"langsmith-trace": this.dotted_order,
567-
baggage: new Baggage(this.extra?.metadata, this.tags).toHeader(),
580+
baggage: new Baggage(
581+
this.extra?.metadata,
582+
this.tags,
583+
this.project_name
584+
).toHeader(),
568585
};
569586

570587
if (headers) {

js/src/tests/run_trees.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ test("distributed", () => {
9595
name: "parent_1",
9696
id: "00000000-0000-0000-0000-00000000000",
9797
start_time: Date.parse("2021-05-03T00:00:00.000Z"),
98+
project_name: "test_project",
9899
});
99100

100101
const serialized = parent.toHeaders();
102+
expect(serialized.baggage).toContain("test_project");
101103

102104
const child2 = RunTree.fromHeaders(serialized)?.createChild({
103105
name: "child_2",
@@ -108,6 +110,7 @@ test("distributed", () => {
108110
expect(JSON.parse(JSON.stringify(child2))).toMatchObject({
109111
name: "child_2",
110112
run_type: "chain",
113+
session_name: "test_project",
111114
dotted_order:
112115
"20210503T000000000001Z00000000-0000-0000-0000-00000000000.20210503T000001000002Z00000000-0000-0000-0000-00000000001",
113116
});

0 commit comments

Comments
 (0)