langsmith 0.3.35-rc.0 → 0.3.35-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.cjs +1 -8
- package/dist/client.js +1 -8
- package/dist/experimental/otel/exporter.cjs +1 -1
- package/dist/experimental/otel/exporter.js +1 -1
- package/dist/experimental/otel/setup.cjs +44 -42
- package/dist/experimental/otel/setup.js +44 -42
- package/dist/experimental/otel/translator.cjs +6 -22
- package/dist/experimental/otel/translator.d.ts +1 -3
- package/dist/experimental/otel/translator.js +6 -22
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/traceable.cjs +5 -5
- package/dist/traceable.d.ts +2 -0
- package/dist/traceable.js +5 -5
- package/package.json +1 -1
package/dist/client.cjs
CHANGED
|
@@ -391,14 +391,7 @@ class Client {
|
|
|
391
391
|
this.fetchOptions = config.fetchOptions || {};
|
|
392
392
|
this.manualFlushMode = config.manualFlushMode ?? this.manualFlushMode;
|
|
393
393
|
if ((0, env_js_1.getEnvironmentVariable)("OTEL_ENABLED") === "true") {
|
|
394
|
-
|
|
395
|
-
const existingTracerProvider = otel_trace.getTracerProvider();
|
|
396
|
-
const { tracerProvider: langSmithTracerProvider } = (0, otel_js_1.getDefaultOTLPTracerComponents)() ?? {};
|
|
397
|
-
// If user has set global tracer before, this fails and returns false
|
|
398
|
-
const globalSuccessfullyOverridden = otel_trace.setGlobalTracerProvider(langSmithTracerProvider);
|
|
399
|
-
this.langSmithToOTELTranslator = new translator_js_1.LangSmithToOTELTranslator(globalSuccessfullyOverridden
|
|
400
|
-
? langSmithTracerProvider
|
|
401
|
-
: existingTracerProvider);
|
|
394
|
+
this.langSmithToOTELTranslator = new translator_js_1.LangSmithToOTELTranslator();
|
|
402
395
|
}
|
|
403
396
|
}
|
|
404
397
|
static getDefaultClientConfig() {
|
package/dist/client.js
CHANGED
|
@@ -353,14 +353,7 @@ export class Client {
|
|
|
353
353
|
this.fetchOptions = config.fetchOptions || {};
|
|
354
354
|
this.manualFlushMode = config.manualFlushMode ?? this.manualFlushMode;
|
|
355
355
|
if (getEnvironmentVariable("OTEL_ENABLED") === "true") {
|
|
356
|
-
|
|
357
|
-
const existingTracerProvider = otel_trace.getTracerProvider();
|
|
358
|
-
const { tracerProvider: langSmithTracerProvider } = getDefaultOTLPTracerComponents() ?? {};
|
|
359
|
-
// If user has set global tracer before, this fails and returns false
|
|
360
|
-
const globalSuccessfullyOverridden = otel_trace.setGlobalTracerProvider(langSmithTracerProvider);
|
|
361
|
-
this.langSmithToOTELTranslator = new LangSmithToOTELTranslator(globalSuccessfullyOverridden
|
|
362
|
-
? langSmithTracerProvider
|
|
363
|
-
: existingTracerProvider);
|
|
356
|
+
this.langSmithToOTELTranslator = new LangSmithToOTELTranslator();
|
|
364
357
|
}
|
|
365
358
|
}
|
|
366
359
|
static getDefaultClientConfig() {
|
|
@@ -40,7 +40,7 @@ const env_js_1 = require("../../env.cjs");
|
|
|
40
40
|
class LangSmithOTLPTraceExporter extends exporter_trace_otlp_proto_1.OTLPTraceExporter {
|
|
41
41
|
export(spans, resultCallback) {
|
|
42
42
|
if (!(0, env_js_1.isTracingEnabled)()) {
|
|
43
|
-
return;
|
|
43
|
+
return resultCallback({ code: 0 });
|
|
44
44
|
}
|
|
45
45
|
for (const span of spans) {
|
|
46
46
|
if (!span.attributes[constants.GENAI_PROMPT]) {
|
|
@@ -4,7 +4,7 @@ import { isTracingEnabled } from "../../env.js";
|
|
|
4
4
|
export class LangSmithOTLPTraceExporter extends OTLPTraceExporter {
|
|
5
5
|
export(spans, resultCallback) {
|
|
6
6
|
if (!isTracingEnabled()) {
|
|
7
|
-
return;
|
|
7
|
+
return resultCallback({ code: 0 });
|
|
8
8
|
}
|
|
9
9
|
for (const span of spans) {
|
|
10
10
|
if (!span.attributes[constants.GENAI_PROMPT]) {
|
|
@@ -9,14 +9,6 @@ const sdk_trace_base_1 = require("@opentelemetry/sdk-trace-base");
|
|
|
9
9
|
const env_js_1 = require("../../utils/env.cjs");
|
|
10
10
|
const exporter_js_1 = require("./exporter.cjs");
|
|
11
11
|
const otel_js_1 = require("../../singletons/otel.cjs");
|
|
12
|
-
const otel = {
|
|
13
|
-
trace: api_1.trace,
|
|
14
|
-
context: api_1.context,
|
|
15
|
-
};
|
|
16
|
-
(0, otel_js_1.setOTELInstances)(otel);
|
|
17
|
-
const contextManager = new context_async_hooks_1.AsyncHooksContextManager();
|
|
18
|
-
contextManager.enable();
|
|
19
|
-
api_1.context.setGlobalContextManager(contextManager);
|
|
20
12
|
/**
|
|
21
13
|
* Convert headers string in format "name=value,name2=value2" to object
|
|
22
14
|
*/
|
|
@@ -32,40 +24,50 @@ function parseHeadersString(headersStr) {
|
|
|
32
24
|
});
|
|
33
25
|
return headers;
|
|
34
26
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
27
|
+
// Set LangSmith-specific defaults if not already set in environment
|
|
28
|
+
if (!(0, env_js_1.getEnvironmentVariable)("OTEL_EXPORTER_OTLP_ENDPOINT")) {
|
|
29
|
+
const lsEndpoint = (0, env_js_1.getLangSmithEnvironmentVariable)("ENDPOINT") ||
|
|
30
|
+
"https://api.smith.langchain.com";
|
|
31
|
+
const baseUrl = lsEndpoint.replace(/\/$/, "");
|
|
32
|
+
(0, env_js_1.setEnvironmentVariable)("OTEL_EXPORTER_OTLP_ENDPOINT", `${baseUrl}/otel/v1/traces`);
|
|
33
|
+
}
|
|
34
|
+
// Configure headers with API key and project if available
|
|
35
|
+
if (!(0, env_js_1.getEnvironmentVariable)("OTEL_EXPORTER_OTLP_HEADERS")) {
|
|
36
|
+
const apiKey = (0, env_js_1.getLangSmithEnvironmentVariable)("API_KEY");
|
|
37
|
+
if (!apiKey) {
|
|
38
|
+
throw new Error("LANGSMITH_API_KEY or LANGCHAIN_API_KEY environment variable is required");
|
|
42
39
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
throw new Error("LANGSMITH_API_KEY or LANGCHAIN_API_KEY environment variable is required");
|
|
48
|
-
}
|
|
49
|
-
let headers = `x-api-key=${apiKey}`;
|
|
50
|
-
const project = (0, env_js_1.getLangSmithEnvironmentVariable)("PROJECT");
|
|
51
|
-
if (project) {
|
|
52
|
-
headers += `,Langsmith-Project=${project}`;
|
|
53
|
-
}
|
|
54
|
-
(0, env_js_1.setEnvironmentVariable)("OTEL_EXPORTER_OTLP_HEADERS", headers);
|
|
40
|
+
let headers = `x-api-key=${apiKey}`;
|
|
41
|
+
const project = (0, env_js_1.getLangSmithEnvironmentVariable)("PROJECT");
|
|
42
|
+
if (project) {
|
|
43
|
+
headers += `,Langsmith-Project=${project}`;
|
|
55
44
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
45
|
+
(0, env_js_1.setEnvironmentVariable)("OTEL_EXPORTER_OTLP_HEADERS", headers);
|
|
46
|
+
}
|
|
47
|
+
const headersStr = (0, env_js_1.getEnvironmentVariable)("OTEL_EXPORTER_OTLP_HEADERS") || "";
|
|
48
|
+
const headersObj = parseHeadersString(headersStr);
|
|
49
|
+
const langsmithSpanExporter = new exporter_js_1.LangSmithOTLPTraceExporter({
|
|
50
|
+
url: (0, env_js_1.getEnvironmentVariable)("OTEL_EXPORTER_OTLP_ENDPOINT"),
|
|
51
|
+
headers: headersObj,
|
|
52
|
+
});
|
|
53
|
+
const spanProcessor = new sdk_trace_base_1.BatchSpanProcessor(langsmithSpanExporter);
|
|
54
|
+
const otel = {
|
|
55
|
+
trace: api_1.trace,
|
|
56
|
+
context: api_1.context,
|
|
57
|
+
};
|
|
58
|
+
(0, otel_js_1.setOTELInstances)(otel);
|
|
59
|
+
const contextManager = new context_async_hooks_1.AsyncHooksContextManager();
|
|
60
|
+
contextManager.enable();
|
|
61
|
+
api_1.context.setGlobalContextManager(contextManager);
|
|
62
|
+
const defaultComponents = {
|
|
63
|
+
spanProcessor,
|
|
64
|
+
tracerProvider: new sdk_trace_base_1.BasicTracerProvider({
|
|
65
|
+
spanProcessors: [spanProcessor],
|
|
66
|
+
}),
|
|
67
|
+
langsmithSpanExporter,
|
|
68
|
+
};
|
|
69
|
+
// If user has set global tracer before, this fails and returns false
|
|
70
|
+
const globalSuccessfullyOverridden = api_1.trace.setGlobalTracerProvider(defaultComponents.tracerProvider);
|
|
71
|
+
if (globalSuccessfullyOverridden) {
|
|
72
|
+
(0, otel_js_1.setDefaultOTLPTracerComponents)(defaultComponents);
|
|
70
73
|
}
|
|
71
|
-
(0, otel_js_1.setDefaultOTLPTracerComponents)(getDefaultOTLPTracerComponents());
|
|
@@ -7,14 +7,6 @@ import { BatchSpanProcessor, BasicTracerProvider, } from "@opentelemetry/sdk-tra
|
|
|
7
7
|
import { getEnvironmentVariable, getLangSmithEnvironmentVariable, setEnvironmentVariable, } from "../../utils/env.js";
|
|
8
8
|
import { LangSmithOTLPTraceExporter } from "./exporter.js";
|
|
9
9
|
import { setDefaultOTLPTracerComponents, setOTELInstances, } from "../../singletons/otel.js";
|
|
10
|
-
const otel = {
|
|
11
|
-
trace: otel_trace,
|
|
12
|
-
context: otel_context,
|
|
13
|
-
};
|
|
14
|
-
setOTELInstances(otel);
|
|
15
|
-
const contextManager = new AsyncHooksContextManager();
|
|
16
|
-
contextManager.enable();
|
|
17
|
-
otel_context.setGlobalContextManager(contextManager);
|
|
18
10
|
/**
|
|
19
11
|
* Convert headers string in format "name=value,name2=value2" to object
|
|
20
12
|
*/
|
|
@@ -30,40 +22,50 @@ function parseHeadersString(headersStr) {
|
|
|
30
22
|
});
|
|
31
23
|
return headers;
|
|
32
24
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
// Set LangSmith-specific defaults if not already set in environment
|
|
26
|
+
if (!getEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT")) {
|
|
27
|
+
const lsEndpoint = getLangSmithEnvironmentVariable("ENDPOINT") ||
|
|
28
|
+
"https://api.smith.langchain.com";
|
|
29
|
+
const baseUrl = lsEndpoint.replace(/\/$/, "");
|
|
30
|
+
setEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", `${baseUrl}/otel/v1/traces`);
|
|
31
|
+
}
|
|
32
|
+
// Configure headers with API key and project if available
|
|
33
|
+
if (!getEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS")) {
|
|
34
|
+
const apiKey = getLangSmithEnvironmentVariable("API_KEY");
|
|
35
|
+
if (!apiKey) {
|
|
36
|
+
throw new Error("LANGSMITH_API_KEY or LANGCHAIN_API_KEY environment variable is required");
|
|
40
37
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
throw new Error("LANGSMITH_API_KEY or LANGCHAIN_API_KEY environment variable is required");
|
|
46
|
-
}
|
|
47
|
-
let headers = `x-api-key=${apiKey}`;
|
|
48
|
-
const project = getLangSmithEnvironmentVariable("PROJECT");
|
|
49
|
-
if (project) {
|
|
50
|
-
headers += `,Langsmith-Project=${project}`;
|
|
51
|
-
}
|
|
52
|
-
setEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS", headers);
|
|
38
|
+
let headers = `x-api-key=${apiKey}`;
|
|
39
|
+
const project = getLangSmithEnvironmentVariable("PROJECT");
|
|
40
|
+
if (project) {
|
|
41
|
+
headers += `,Langsmith-Project=${project}`;
|
|
53
42
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
43
|
+
setEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS", headers);
|
|
44
|
+
}
|
|
45
|
+
const headersStr = getEnvironmentVariable("OTEL_EXPORTER_OTLP_HEADERS") || "";
|
|
46
|
+
const headersObj = parseHeadersString(headersStr);
|
|
47
|
+
const langsmithSpanExporter = new LangSmithOTLPTraceExporter({
|
|
48
|
+
url: getEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT"),
|
|
49
|
+
headers: headersObj,
|
|
50
|
+
});
|
|
51
|
+
const spanProcessor = new BatchSpanProcessor(langsmithSpanExporter);
|
|
52
|
+
const otel = {
|
|
53
|
+
trace: otel_trace,
|
|
54
|
+
context: otel_context,
|
|
55
|
+
};
|
|
56
|
+
setOTELInstances(otel);
|
|
57
|
+
const contextManager = new AsyncHooksContextManager();
|
|
58
|
+
contextManager.enable();
|
|
59
|
+
otel_context.setGlobalContextManager(contextManager);
|
|
60
|
+
const defaultComponents = {
|
|
61
|
+
spanProcessor,
|
|
62
|
+
tracerProvider: new BasicTracerProvider({
|
|
63
|
+
spanProcessors: [spanProcessor],
|
|
64
|
+
}),
|
|
65
|
+
langsmithSpanExporter,
|
|
66
|
+
};
|
|
67
|
+
// If user has set global tracer before, this fails and returns false
|
|
68
|
+
const globalSuccessfullyOverridden = otel_trace.setGlobalTracerProvider(defaultComponents.tracerProvider);
|
|
69
|
+
if (globalSuccessfullyOverridden) {
|
|
70
|
+
setDefaultOTLPTracerComponents(defaultComponents);
|
|
68
71
|
}
|
|
69
|
-
setDefaultOTLPTracerComponents(getDefaultOTLPTracerComponents());
|
|
@@ -34,7 +34,6 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.LangSmithToOTELTranslator = void 0;
|
|
37
|
-
const index_js_1 = require("../../index.cjs");
|
|
38
37
|
const constants = __importStar(require("./constants.cjs"));
|
|
39
38
|
const otel_js_1 = require("../../singletons/otel.cjs");
|
|
40
39
|
const WELL_KNOWN_OPERATION_NAMES = {
|
|
@@ -48,25 +47,15 @@ function getOperationName(runType) {
|
|
|
48
47
|
return WELL_KNOWN_OPERATION_NAMES[runType] || runType;
|
|
49
48
|
}
|
|
50
49
|
class LangSmithToOTELTranslator {
|
|
51
|
-
constructor(
|
|
52
|
-
Object.defineProperty(this, "tracer", {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
configurable: true,
|
|
55
|
-
writable: true,
|
|
56
|
-
value: void 0
|
|
57
|
-
});
|
|
50
|
+
constructor() {
|
|
58
51
|
Object.defineProperty(this, "spans", {
|
|
59
52
|
enumerable: true,
|
|
60
53
|
configurable: true,
|
|
61
54
|
writable: true,
|
|
62
55
|
value: new Map()
|
|
63
56
|
});
|
|
64
|
-
this.tracer = (tracerProvider ?? (0, otel_js_1.getOTELTrace)()).getTracer("langsmith", index_js_1.__version__);
|
|
65
57
|
}
|
|
66
58
|
exportBatch(operations, otelContextMap) {
|
|
67
|
-
if (!this.tracer) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
59
|
for (const op of operations) {
|
|
71
60
|
try {
|
|
72
61
|
if (!op.run) {
|
|
@@ -74,7 +63,7 @@ class LangSmithToOTELTranslator {
|
|
|
74
63
|
}
|
|
75
64
|
if (op.operation === "post") {
|
|
76
65
|
const span = this.createSpanForRun(op, op.run, otelContextMap.get(op.id));
|
|
77
|
-
if (span) {
|
|
66
|
+
if (span && !op.run.end_time) {
|
|
78
67
|
this.spans.set(op.id, span);
|
|
79
68
|
}
|
|
80
69
|
}
|
|
@@ -88,23 +77,19 @@ class LangSmithToOTELTranslator {
|
|
|
88
77
|
}
|
|
89
78
|
}
|
|
90
79
|
createSpanForRun(op, runInfo, otelContext) {
|
|
91
|
-
if (!this.tracer) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
80
|
const activeSpan = otelContext && (0, otel_js_1.getOTELTrace)().getSpan(otelContext);
|
|
95
81
|
if (!activeSpan) {
|
|
96
82
|
return;
|
|
97
83
|
}
|
|
98
84
|
try {
|
|
99
|
-
|
|
100
|
-
return this.finishSpanSetup(activeSpan, runInfo, op, endTime);
|
|
85
|
+
return this.finishSpanSetup(activeSpan, runInfo, op);
|
|
101
86
|
}
|
|
102
87
|
catch (e) {
|
|
103
88
|
console.error(`Failed to create span for run ${op.id}:`, e);
|
|
104
89
|
return undefined;
|
|
105
90
|
}
|
|
106
91
|
}
|
|
107
|
-
finishSpanSetup(span, runInfo, op
|
|
92
|
+
finishSpanSetup(span, runInfo, op) {
|
|
108
93
|
// Set all attributes
|
|
109
94
|
this.setSpanAttributes(span, runInfo, op);
|
|
110
95
|
// Set status based on error
|
|
@@ -116,8 +101,8 @@ class LangSmithToOTELTranslator {
|
|
|
116
101
|
span.setStatus({ code: 1 }); // OK status
|
|
117
102
|
}
|
|
118
103
|
// End the span if end_time is present
|
|
119
|
-
if (
|
|
120
|
-
span.end(
|
|
104
|
+
if (runInfo.end_time) {
|
|
105
|
+
span.end(runInfo.end_time);
|
|
121
106
|
}
|
|
122
107
|
return span;
|
|
123
108
|
}
|
|
@@ -142,7 +127,6 @@ class LangSmithToOTELTranslator {
|
|
|
142
127
|
const endTime = runInfo.end_time;
|
|
143
128
|
if (endTime) {
|
|
144
129
|
span.end(endTime);
|
|
145
|
-
// Remove the span from our dictionary
|
|
146
130
|
this.spans.delete(op.id);
|
|
147
131
|
}
|
|
148
132
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { OTELContext
|
|
1
|
+
import type { OTELContext } from "./types.js";
|
|
2
2
|
import type { RunCreate, RunUpdate } from "../../schemas.js";
|
|
3
3
|
export type SerializedRunOperation<T extends "post" | "patch" = "post" | "patch"> = {
|
|
4
4
|
operation: T;
|
|
@@ -7,9 +7,7 @@ export type SerializedRunOperation<T extends "post" | "patch" = "post" | "patch"
|
|
|
7
7
|
run: T extends "post" ? RunCreate : RunUpdate;
|
|
8
8
|
};
|
|
9
9
|
export declare class LangSmithToOTELTranslator {
|
|
10
|
-
private tracer?;
|
|
11
10
|
private spans;
|
|
12
|
-
constructor(tracerProvider?: OTELTracerProvider);
|
|
13
11
|
exportBatch(operations: SerializedRunOperation[], otelContextMap: Map<string, OTELContext>): void;
|
|
14
12
|
private createSpanForRun;
|
|
15
13
|
private finishSpanSetup;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { __version__ } from "../../index.js";
|
|
2
1
|
import * as constants from "./constants.js";
|
|
3
2
|
import { getOTELTrace } from "../../singletons/otel.js";
|
|
4
3
|
const WELL_KNOWN_OPERATION_NAMES = {
|
|
@@ -12,25 +11,15 @@ function getOperationName(runType) {
|
|
|
12
11
|
return WELL_KNOWN_OPERATION_NAMES[runType] || runType;
|
|
13
12
|
}
|
|
14
13
|
export class LangSmithToOTELTranslator {
|
|
15
|
-
constructor(
|
|
16
|
-
Object.defineProperty(this, "tracer", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
configurable: true,
|
|
19
|
-
writable: true,
|
|
20
|
-
value: void 0
|
|
21
|
-
});
|
|
14
|
+
constructor() {
|
|
22
15
|
Object.defineProperty(this, "spans", {
|
|
23
16
|
enumerable: true,
|
|
24
17
|
configurable: true,
|
|
25
18
|
writable: true,
|
|
26
19
|
value: new Map()
|
|
27
20
|
});
|
|
28
|
-
this.tracer = (tracerProvider ?? getOTELTrace()).getTracer("langsmith", __version__);
|
|
29
21
|
}
|
|
30
22
|
exportBatch(operations, otelContextMap) {
|
|
31
|
-
if (!this.tracer) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
23
|
for (const op of operations) {
|
|
35
24
|
try {
|
|
36
25
|
if (!op.run) {
|
|
@@ -38,7 +27,7 @@ export class LangSmithToOTELTranslator {
|
|
|
38
27
|
}
|
|
39
28
|
if (op.operation === "post") {
|
|
40
29
|
const span = this.createSpanForRun(op, op.run, otelContextMap.get(op.id));
|
|
41
|
-
if (span) {
|
|
30
|
+
if (span && !op.run.end_time) {
|
|
42
31
|
this.spans.set(op.id, span);
|
|
43
32
|
}
|
|
44
33
|
}
|
|
@@ -52,23 +41,19 @@ export class LangSmithToOTELTranslator {
|
|
|
52
41
|
}
|
|
53
42
|
}
|
|
54
43
|
createSpanForRun(op, runInfo, otelContext) {
|
|
55
|
-
if (!this.tracer) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
44
|
const activeSpan = otelContext && getOTELTrace().getSpan(otelContext);
|
|
59
45
|
if (!activeSpan) {
|
|
60
46
|
return;
|
|
61
47
|
}
|
|
62
48
|
try {
|
|
63
|
-
|
|
64
|
-
return this.finishSpanSetup(activeSpan, runInfo, op, endTime);
|
|
49
|
+
return this.finishSpanSetup(activeSpan, runInfo, op);
|
|
65
50
|
}
|
|
66
51
|
catch (e) {
|
|
67
52
|
console.error(`Failed to create span for run ${op.id}:`, e);
|
|
68
53
|
return undefined;
|
|
69
54
|
}
|
|
70
55
|
}
|
|
71
|
-
finishSpanSetup(span, runInfo, op
|
|
56
|
+
finishSpanSetup(span, runInfo, op) {
|
|
72
57
|
// Set all attributes
|
|
73
58
|
this.setSpanAttributes(span, runInfo, op);
|
|
74
59
|
// Set status based on error
|
|
@@ -80,8 +65,8 @@ export class LangSmithToOTELTranslator {
|
|
|
80
65
|
span.setStatus({ code: 1 }); // OK status
|
|
81
66
|
}
|
|
82
67
|
// End the span if end_time is present
|
|
83
|
-
if (
|
|
84
|
-
span.end(
|
|
68
|
+
if (runInfo.end_time) {
|
|
69
|
+
span.end(runInfo.end_time);
|
|
85
70
|
}
|
|
86
71
|
return span;
|
|
87
72
|
}
|
|
@@ -106,7 +91,6 @@ export class LangSmithToOTELTranslator {
|
|
|
106
91
|
const endTime = runInfo.end_time;
|
|
107
92
|
if (endTime) {
|
|
108
93
|
span.end(endTime);
|
|
109
|
-
// Remove the span from our dictionary
|
|
110
94
|
this.spans.delete(op.id);
|
|
111
95
|
}
|
|
112
96
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -10,4 +10,4 @@ Object.defineProperty(exports, "overrideFetchImplementation", { enumerable: true
|
|
|
10
10
|
var project_js_1 = require("./utils/project.cjs");
|
|
11
11
|
Object.defineProperty(exports, "getDefaultProjectName", { enumerable: true, get: function () { return project_js_1.getDefaultProjectName; } });
|
|
12
12
|
// Update using yarn bump-version
|
|
13
|
-
exports.__version__ = "0.3.35-rc.
|
|
13
|
+
exports.__version__ = "0.3.35-rc.2";
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ export type { Dataset, Example, TracerSession, Run, Feedback, RetrieverOutput, }
|
|
|
3
3
|
export { RunTree, type RunTreeConfig } from "./run_trees.js";
|
|
4
4
|
export { overrideFetchImplementation } from "./singletons/fetch.js";
|
|
5
5
|
export { getDefaultProjectName } from "./utils/project.js";
|
|
6
|
-
export declare const __version__ = "0.3.35-rc.
|
|
6
|
+
export declare const __version__ = "0.3.35-rc.2";
|
package/dist/index.js
CHANGED
|
@@ -3,4 +3,4 @@ export { RunTree } from "./run_trees.js";
|
|
|
3
3
|
export { overrideFetchImplementation } from "./singletons/fetch.js";
|
|
4
4
|
export { getDefaultProjectName } from "./utils/project.js";
|
|
5
5
|
// Update using yarn bump-version
|
|
6
|
-
export const __version__ = "0.3.35-rc.
|
|
6
|
+
export const __version__ = "0.3.35-rc.2";
|
package/dist/traceable.cjs
CHANGED
|
@@ -9,14 +9,14 @@ const traceable_js_1 = require("./singletons/traceable.cjs");
|
|
|
9
9
|
const constants_js_1 = require("./singletons/constants.cjs");
|
|
10
10
|
const asserts_js_1 = require("./utils/asserts.cjs");
|
|
11
11
|
const env_js_2 = require("./utils/env.cjs");
|
|
12
|
-
const utils_js_1 = require("./experimental/otel/utils.cjs");
|
|
13
12
|
const index_js_1 = require("./index.cjs");
|
|
14
13
|
const otel_js_1 = require("./singletons/otel.cjs");
|
|
14
|
+
const utils_js_1 = require("./experimental/otel/utils.cjs");
|
|
15
15
|
traceable_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
|
|
16
16
|
/**
|
|
17
17
|
* Create OpenTelemetry context manager from RunTree if OTEL is enabled.
|
|
18
18
|
*/
|
|
19
|
-
function maybeCreateOtelContext(runTree) {
|
|
19
|
+
function maybeCreateOtelContext(runTree, tracer) {
|
|
20
20
|
if (!runTree || (0, env_js_2.getEnvironmentVariable)("OTEL_ENABLED") !== "true") {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
@@ -25,7 +25,7 @@ function maybeCreateOtelContext(runTree) {
|
|
|
25
25
|
try {
|
|
26
26
|
const spanContext = (0, utils_js_1.createOtelSpanContextFromRun)(runTree);
|
|
27
27
|
return (fn) => {
|
|
28
|
-
const resolvedTracer = otel_trace.getTracer("langsmith", index_js_1.__version__);
|
|
28
|
+
const resolvedTracer = tracer ?? otel_trace.getTracer("langsmith", index_js_1.__version__);
|
|
29
29
|
return resolvedTracer.startActiveSpan(runTree.name, {
|
|
30
30
|
attributes: {
|
|
31
31
|
"langsmith.traceable": "true",
|
|
@@ -383,7 +383,7 @@ function traceable(wrappedFunc, config) {
|
|
|
383
383
|
const currentRunTree = getTracingRunTree(firstArg === traceable_js_1.ROOT
|
|
384
384
|
? new run_trees_js_1.RunTree(ensuredConfig)
|
|
385
385
|
: firstArg.createChild(ensuredConfig), restArgs, config?.getInvocationParams, processInputsFn, extractAttachmentsFn);
|
|
386
|
-
return [currentRunTree, restArgs];
|
|
386
|
+
return [currentRunTree, [currentRunTree, ...restArgs]];
|
|
387
387
|
}
|
|
388
388
|
// Node.JS uses AsyncLocalStorage (ALS) and AsyncResource
|
|
389
389
|
// to allow storing context
|
|
@@ -406,7 +406,7 @@ function traceable(wrappedFunc, config) {
|
|
|
406
406
|
}
|
|
407
407
|
return [currentRunTree, processedArgs];
|
|
408
408
|
})();
|
|
409
|
-
const otelContextManager = maybeCreateOtelContext(currentRunTree);
|
|
409
|
+
const otelContextManager = maybeCreateOtelContext(currentRunTree, config?.tracer);
|
|
410
410
|
const otel_context = (0, otel_js_1.getOTELContext)();
|
|
411
411
|
const runWithContext = () => {
|
|
412
412
|
const postRunPromise = currentRunTree?.postRun();
|
package/dist/traceable.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RunTreeConfig } from "./run_trees.js";
|
|
2
2
|
import { Attachments, InvocationParamsSchema, KVMap } from "./schemas.js";
|
|
3
3
|
import { TraceableFunction } from "./singletons/types.js";
|
|
4
|
+
import { OTELTracer } from "./experimental/otel/types.js";
|
|
4
5
|
/**
|
|
5
6
|
* Higher-order function that takes function as input and returns a
|
|
6
7
|
* "TraceableFunction" - a wrapped version of the input that
|
|
@@ -18,6 +19,7 @@ import { TraceableFunction } from "./singletons/types.js";
|
|
|
18
19
|
export declare function traceable<Func extends (...args: any[]) => any>(wrappedFunc: Func, config?: Partial<Omit<RunTreeConfig, "inputs" | "outputs">> & {
|
|
19
20
|
aggregator?: (args: any[]) => any;
|
|
20
21
|
argsConfigPath?: [number] | [number, string];
|
|
22
|
+
tracer?: OTELTracer;
|
|
21
23
|
__finalTracedIteratorKey?: string;
|
|
22
24
|
/**
|
|
23
25
|
* Extract attachments from args and return remaining args.
|
package/dist/traceable.js
CHANGED
|
@@ -5,14 +5,14 @@ import { ROOT, AsyncLocalStorageProviderSingleton, } from "./singletons/traceabl
|
|
|
5
5
|
import { _LC_CONTEXT_VARIABLES_KEY } from "./singletons/constants.js";
|
|
6
6
|
import { isKVMap, isReadableStream, isAsyncIterable, isIteratorLike, isThenable, isGenerator, isPromiseMethod, } from "./utils/asserts.js";
|
|
7
7
|
import { getEnvironmentVariable } from "./utils/env.js";
|
|
8
|
-
import { createOtelSpanContextFromRun } from "./experimental/otel/utils.js";
|
|
9
8
|
import { __version__ } from "./index.js";
|
|
10
9
|
import { getOTELTrace, getOTELContext } from "./singletons/otel.js";
|
|
10
|
+
import { createOtelSpanContextFromRun } from "./experimental/otel/utils.js";
|
|
11
11
|
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
12
12
|
/**
|
|
13
13
|
* Create OpenTelemetry context manager from RunTree if OTEL is enabled.
|
|
14
14
|
*/
|
|
15
|
-
function maybeCreateOtelContext(runTree) {
|
|
15
|
+
function maybeCreateOtelContext(runTree, tracer) {
|
|
16
16
|
if (!runTree || getEnvironmentVariable("OTEL_ENABLED") !== "true") {
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
@@ -21,7 +21,7 @@ function maybeCreateOtelContext(runTree) {
|
|
|
21
21
|
try {
|
|
22
22
|
const spanContext = createOtelSpanContextFromRun(runTree);
|
|
23
23
|
return (fn) => {
|
|
24
|
-
const resolvedTracer = otel_trace.getTracer("langsmith", __version__);
|
|
24
|
+
const resolvedTracer = tracer ?? otel_trace.getTracer("langsmith", __version__);
|
|
25
25
|
return resolvedTracer.startActiveSpan(runTree.name, {
|
|
26
26
|
attributes: {
|
|
27
27
|
"langsmith.traceable": "true",
|
|
@@ -379,7 +379,7 @@ export function traceable(wrappedFunc, config) {
|
|
|
379
379
|
const currentRunTree = getTracingRunTree(firstArg === ROOT
|
|
380
380
|
? new RunTree(ensuredConfig)
|
|
381
381
|
: firstArg.createChild(ensuredConfig), restArgs, config?.getInvocationParams, processInputsFn, extractAttachmentsFn);
|
|
382
|
-
return [currentRunTree, restArgs];
|
|
382
|
+
return [currentRunTree, [currentRunTree, ...restArgs]];
|
|
383
383
|
}
|
|
384
384
|
// Node.JS uses AsyncLocalStorage (ALS) and AsyncResource
|
|
385
385
|
// to allow storing context
|
|
@@ -402,7 +402,7 @@ export function traceable(wrappedFunc, config) {
|
|
|
402
402
|
}
|
|
403
403
|
return [currentRunTree, processedArgs];
|
|
404
404
|
})();
|
|
405
|
-
const otelContextManager = maybeCreateOtelContext(currentRunTree);
|
|
405
|
+
const otelContextManager = maybeCreateOtelContext(currentRunTree, config?.tracer);
|
|
406
406
|
const otel_context = getOTELContext();
|
|
407
407
|
const runWithContext = () => {
|
|
408
408
|
const postRunPromise = currentRunTree?.postRun();
|