langsmith 0.3.47 → 0.3.49-rc.0
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 +10 -9
- package/dist/client.d.ts +1 -1
- package/dist/client.js +10 -9
- package/dist/experimental/otel/constants.cjs +2 -1
- package/dist/experimental/otel/constants.d.ts +1 -0
- package/dist/experimental/otel/constants.js +1 -0
- package/dist/experimental/otel/exporter.cjs +18 -3
- package/dist/experimental/otel/exporter.d.ts +11 -0
- package/dist/experimental/otel/exporter.js +18 -3
- package/dist/experimental/otel/setup.cjs +14 -5
- package/dist/experimental/otel/setup.d.ts +6 -0
- package/dist/experimental/otel/setup.js +14 -5
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/singletons/otel.cjs +2 -2
- package/dist/singletons/otel.js +3 -3
- package/dist/traceable.cjs +4 -2
- package/dist/traceable.js +6 -4
- package/dist/utils/env.cjs +5 -0
- package/dist/utils/env.d.ts +1 -0
- package/dist/utils/env.js +4 -0
- package/package.json +1 -1
package/dist/client.cjs
CHANGED
|
@@ -34,7 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.Client = exports.DEFAULT_BATCH_SIZE_LIMIT_BYTES = exports.AutoBatchQueue = void 0;
|
|
37
|
-
exports.
|
|
37
|
+
exports.mergeRuntimeEnvIntoRun = mergeRuntimeEnvIntoRun;
|
|
38
38
|
const uuid = __importStar(require("uuid"));
|
|
39
39
|
const translator_js_1 = require("./experimental/otel/translator.cjs");
|
|
40
40
|
const otel_js_1 = require("./singletons/otel.cjs");
|
|
@@ -48,7 +48,7 @@ const prompts_js_1 = require("./utils/prompts.cjs");
|
|
|
48
48
|
const error_js_1 = require("./utils/error.cjs");
|
|
49
49
|
const fetch_js_1 = require("./singletons/fetch.cjs");
|
|
50
50
|
const index_js_2 = require("./utils/fast-safe-stringify/index.cjs");
|
|
51
|
-
function
|
|
51
|
+
function mergeRuntimeEnvIntoRun(run) {
|
|
52
52
|
const runtimeEnv = (0, env_js_1.getRuntimeEnvironment)();
|
|
53
53
|
const envVars = (0, env_js_1.getLangChainEnvVarsMetadata)();
|
|
54
54
|
const extra = run.extra ?? {};
|
|
@@ -61,8 +61,11 @@ function mergeRuntimeEnvIntoRunCreate(run) {
|
|
|
61
61
|
},
|
|
62
62
|
metadata: {
|
|
63
63
|
...envVars,
|
|
64
|
-
...(envVars.revision_id || run.revision_id
|
|
65
|
-
? {
|
|
64
|
+
...(envVars.revision_id || ("revision_id" in run && run.revision_id)
|
|
65
|
+
? {
|
|
66
|
+
revision_id: ("revision_id" in run ? run.revision_id : undefined) ??
|
|
67
|
+
envVars.revision_id,
|
|
68
|
+
}
|
|
66
69
|
: {}),
|
|
67
70
|
...metadata,
|
|
68
71
|
},
|
|
@@ -401,7 +404,7 @@ class Client {
|
|
|
401
404
|
this.batchSizeBytesLimit = config.batchSizeBytesLimit;
|
|
402
405
|
this.fetchOptions = config.fetchOptions || {};
|
|
403
406
|
this.manualFlushMode = config.manualFlushMode ?? this.manualFlushMode;
|
|
404
|
-
if ((0, env_js_1.
|
|
407
|
+
if ((0, env_js_1.getOtelEnabled)()) {
|
|
405
408
|
this.langSmithToOTELTranslator = new translator_js_1.LangSmithToOTELTranslator();
|
|
406
409
|
}
|
|
407
410
|
}
|
|
@@ -724,9 +727,7 @@ class Client {
|
|
|
724
727
|
async processRunOperation(item) {
|
|
725
728
|
clearTimeout(this.autoBatchTimeout);
|
|
726
729
|
this.autoBatchTimeout = undefined;
|
|
727
|
-
|
|
728
|
-
item.item = mergeRuntimeEnvIntoRunCreate(item.item);
|
|
729
|
-
}
|
|
730
|
+
item.item = mergeRuntimeEnvIntoRun(item.item);
|
|
730
731
|
const itemPromise = this.autoBatchQueue.push(item);
|
|
731
732
|
if (this.manualFlushMode) {
|
|
732
733
|
// Rely on manual flushing in serverless environments
|
|
@@ -833,7 +834,7 @@ class Client {
|
|
|
833
834
|
}).catch(console.error);
|
|
834
835
|
return;
|
|
835
836
|
}
|
|
836
|
-
const mergedRunCreateParam =
|
|
837
|
+
const mergedRunCreateParam = mergeRuntimeEnvIntoRun(runCreate);
|
|
837
838
|
if (options?.apiKey !== undefined) {
|
|
838
839
|
headers["x-api-key"] = options.apiKey;
|
|
839
840
|
}
|
package/dist/client.d.ts
CHANGED
|
@@ -260,7 +260,7 @@ type Thread = {
|
|
|
260
260
|
last_outputs: string;
|
|
261
261
|
last_error: string | null;
|
|
262
262
|
};
|
|
263
|
-
export declare function
|
|
263
|
+
export declare function mergeRuntimeEnvIntoRun<T extends RunCreate | RunUpdate>(run: T): T;
|
|
264
264
|
export declare class AutoBatchQueue {
|
|
265
265
|
items: {
|
|
266
266
|
action: "create" | "update";
|
package/dist/client.js
CHANGED
|
@@ -3,7 +3,7 @@ import { LangSmithToOTELTranslator, } from "./experimental/otel/translator.js";
|
|
|
3
3
|
import { getDefaultOTLPTracerComponents, getOTELTrace, getOTELContext, } from "./singletons/otel.js";
|
|
4
4
|
import { AsyncCaller } from "./utils/async_caller.js";
|
|
5
5
|
import { convertLangChainMessageToExample, isLangChainMessage, } from "./utils/messages.js";
|
|
6
|
-
import { getEnvironmentVariable, getLangChainEnvVarsMetadata, getLangSmithEnvironmentVariable, getRuntimeEnvironment, } from "./utils/env.js";
|
|
6
|
+
import { getEnvironmentVariable, getLangChainEnvVarsMetadata, getLangSmithEnvironmentVariable, getRuntimeEnvironment, getOtelEnabled, } from "./utils/env.js";
|
|
7
7
|
import { __version__ } from "./index.js";
|
|
8
8
|
import { assertUuid } from "./utils/_uuid.js";
|
|
9
9
|
import { warnOnce } from "./utils/warn.js";
|
|
@@ -11,7 +11,7 @@ import { parsePromptIdentifier } from "./utils/prompts.js";
|
|
|
11
11
|
import { raiseForStatus } from "./utils/error.js";
|
|
12
12
|
import { _globalFetchImplementationIsNodeFetch, _getFetchImplementation, } from "./singletons/fetch.js";
|
|
13
13
|
import { serialize as serializePayloadForTracing } from "./utils/fast-safe-stringify/index.js";
|
|
14
|
-
export function
|
|
14
|
+
export function mergeRuntimeEnvIntoRun(run) {
|
|
15
15
|
const runtimeEnv = getRuntimeEnvironment();
|
|
16
16
|
const envVars = getLangChainEnvVarsMetadata();
|
|
17
17
|
const extra = run.extra ?? {};
|
|
@@ -24,8 +24,11 @@ export function mergeRuntimeEnvIntoRunCreate(run) {
|
|
|
24
24
|
},
|
|
25
25
|
metadata: {
|
|
26
26
|
...envVars,
|
|
27
|
-
...(envVars.revision_id || run.revision_id
|
|
28
|
-
? {
|
|
27
|
+
...(envVars.revision_id || ("revision_id" in run && run.revision_id)
|
|
28
|
+
? {
|
|
29
|
+
revision_id: ("revision_id" in run ? run.revision_id : undefined) ??
|
|
30
|
+
envVars.revision_id,
|
|
31
|
+
}
|
|
29
32
|
: {}),
|
|
30
33
|
...metadata,
|
|
31
34
|
},
|
|
@@ -363,7 +366,7 @@ export class Client {
|
|
|
363
366
|
this.batchSizeBytesLimit = config.batchSizeBytesLimit;
|
|
364
367
|
this.fetchOptions = config.fetchOptions || {};
|
|
365
368
|
this.manualFlushMode = config.manualFlushMode ?? this.manualFlushMode;
|
|
366
|
-
if (
|
|
369
|
+
if (getOtelEnabled()) {
|
|
367
370
|
this.langSmithToOTELTranslator = new LangSmithToOTELTranslator();
|
|
368
371
|
}
|
|
369
372
|
}
|
|
@@ -686,9 +689,7 @@ export class Client {
|
|
|
686
689
|
async processRunOperation(item) {
|
|
687
690
|
clearTimeout(this.autoBatchTimeout);
|
|
688
691
|
this.autoBatchTimeout = undefined;
|
|
689
|
-
|
|
690
|
-
item.item = mergeRuntimeEnvIntoRunCreate(item.item);
|
|
691
|
-
}
|
|
692
|
+
item.item = mergeRuntimeEnvIntoRun(item.item);
|
|
692
693
|
const itemPromise = this.autoBatchQueue.push(item);
|
|
693
694
|
if (this.manualFlushMode) {
|
|
694
695
|
// Rely on manual flushing in serverless environments
|
|
@@ -795,7 +796,7 @@ export class Client {
|
|
|
795
796
|
}).catch(console.error);
|
|
796
797
|
return;
|
|
797
798
|
}
|
|
798
|
-
const mergedRunCreateParam =
|
|
799
|
+
const mergedRunCreateParam = mergeRuntimeEnvIntoRun(runCreate);
|
|
799
800
|
if (options?.apiKey !== undefined) {
|
|
800
801
|
headers["x-api-key"] = options.apiKey;
|
|
801
802
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AI_SDK_TOOL_OPERATIONS = exports.AI_SDK_LLM_OPERATIONS = exports.GEN_AI_CHOICE = exports.GEN_AI_ASSISTANT_MESSAGE = exports.GEN_AI_USER_MESSAGE = exports.GEN_AI_SYSTEM_MESSAGE = exports.LANGSMITH_REFERENCE_EXAMPLE_ID = exports.LANGSMITH_USAGE_METADATA = exports.LANGSMITH_PARENT_RUN_ID = exports.LANGSMITH_DOTTED_ORDER = exports.LANGSMITH_TRACE_ID = exports.LANGSMITH_RUN_ID = exports.LANGSMITH_REQUEST_HEADERS = exports.LANGSMITH_REQUEST_STREAMING = exports.LANGSMITH_RUNTIME = exports.LANGSMITH_TAGS = exports.LANGSMITH_METADATA = exports.LANGSMITH_NAME = exports.LANGSMITH_RUN_TYPE = exports.LANGSMITH_SESSION_NAME = exports.LANGSMITH_SESSION_ID = exports.GEN_AI_USAGE_OUTPUT_TOKEN_DETAILS = exports.GEN_AI_USAGE_INPUT_TOKEN_DETAILS = exports.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT = exports.GEN_AI_RESPONSE_SERVICE_TIER = exports.GEN_AI_RESPONSE_ID = exports.GEN_AI_SERIALIZED_DOC = exports.GEN_AI_SERIALIZED_SIGNATURE = exports.GEN_AI_SERIALIZED_NAME = exports.GEN_AI_REQUEST_EXTRA_BODY = exports.GEN_AI_REQUEST_EXTRA_QUERY = exports.GENAI_COMPLETION = exports.GENAI_PROMPT = exports.GEN_AI_RESPONSE_FINISH_REASONS = exports.GEN_AI_REQUEST_PRESENCE_PENALTY = exports.GEN_AI_REQUEST_FREQUENCY_PENALTY = exports.GEN_AI_REQUEST_TOP_P = exports.GEN_AI_REQUEST_TEMPERATURE = exports.GEN_AI_REQUEST_MAX_TOKENS = exports.GEN_AI_USAGE_TOTAL_TOKENS = exports.GEN_AI_USAGE_OUTPUT_TOKENS = exports.GEN_AI_USAGE_INPUT_TOKENS = exports.GEN_AI_RESPONSE_MODEL = exports.GEN_AI_REQUEST_MODEL = exports.GEN_AI_SYSTEM = exports.GEN_AI_OPERATION_NAME = void 0;
|
|
3
|
+
exports.AI_SDK_TOOL_OPERATIONS = exports.AI_SDK_LLM_OPERATIONS = exports.GEN_AI_CHOICE = exports.GEN_AI_ASSISTANT_MESSAGE = exports.GEN_AI_USER_MESSAGE = exports.GEN_AI_SYSTEM_MESSAGE = exports.LANGSMITH_TRACEABLE = exports.LANGSMITH_REFERENCE_EXAMPLE_ID = exports.LANGSMITH_USAGE_METADATA = exports.LANGSMITH_PARENT_RUN_ID = exports.LANGSMITH_DOTTED_ORDER = exports.LANGSMITH_TRACE_ID = exports.LANGSMITH_RUN_ID = exports.LANGSMITH_REQUEST_HEADERS = exports.LANGSMITH_REQUEST_STREAMING = exports.LANGSMITH_RUNTIME = exports.LANGSMITH_TAGS = exports.LANGSMITH_METADATA = exports.LANGSMITH_NAME = exports.LANGSMITH_RUN_TYPE = exports.LANGSMITH_SESSION_NAME = exports.LANGSMITH_SESSION_ID = exports.GEN_AI_USAGE_OUTPUT_TOKEN_DETAILS = exports.GEN_AI_USAGE_INPUT_TOKEN_DETAILS = exports.GEN_AI_RESPONSE_SYSTEM_FINGERPRINT = exports.GEN_AI_RESPONSE_SERVICE_TIER = exports.GEN_AI_RESPONSE_ID = exports.GEN_AI_SERIALIZED_DOC = exports.GEN_AI_SERIALIZED_SIGNATURE = exports.GEN_AI_SERIALIZED_NAME = exports.GEN_AI_REQUEST_EXTRA_BODY = exports.GEN_AI_REQUEST_EXTRA_QUERY = exports.GENAI_COMPLETION = exports.GENAI_PROMPT = exports.GEN_AI_RESPONSE_FINISH_REASONS = exports.GEN_AI_REQUEST_PRESENCE_PENALTY = exports.GEN_AI_REQUEST_FREQUENCY_PENALTY = exports.GEN_AI_REQUEST_TOP_P = exports.GEN_AI_REQUEST_TEMPERATURE = exports.GEN_AI_REQUEST_MAX_TOKENS = exports.GEN_AI_USAGE_TOTAL_TOKENS = exports.GEN_AI_USAGE_OUTPUT_TOKENS = exports.GEN_AI_USAGE_INPUT_TOKENS = exports.GEN_AI_RESPONSE_MODEL = exports.GEN_AI_REQUEST_MODEL = exports.GEN_AI_SYSTEM = exports.GEN_AI_OPERATION_NAME = void 0;
|
|
4
4
|
// OpenTelemetry GenAI semantic convention attribute names
|
|
5
5
|
exports.GEN_AI_OPERATION_NAME = "gen_ai.operation.name";
|
|
6
6
|
exports.GEN_AI_SYSTEM = "gen_ai.system";
|
|
@@ -43,6 +43,7 @@ exports.LANGSMITH_DOTTED_ORDER = "langsmith.span.dotted_order";
|
|
|
43
43
|
exports.LANGSMITH_PARENT_RUN_ID = "langsmith.span.parent_id";
|
|
44
44
|
exports.LANGSMITH_USAGE_METADATA = "langsmith.usage_metadata";
|
|
45
45
|
exports.LANGSMITH_REFERENCE_EXAMPLE_ID = "langsmith.reference_example_id";
|
|
46
|
+
exports.LANGSMITH_TRACEABLE = "langsmith.traceable";
|
|
46
47
|
// GenAI event names
|
|
47
48
|
exports.GEN_AI_SYSTEM_MESSAGE = "gen_ai.system.message";
|
|
48
49
|
exports.GEN_AI_USER_MESSAGE = "gen_ai.user.message";
|
|
@@ -38,6 +38,7 @@ export declare const LANGSMITH_DOTTED_ORDER = "langsmith.span.dotted_order";
|
|
|
38
38
|
export declare const LANGSMITH_PARENT_RUN_ID = "langsmith.span.parent_id";
|
|
39
39
|
export declare const LANGSMITH_USAGE_METADATA = "langsmith.usage_metadata";
|
|
40
40
|
export declare const LANGSMITH_REFERENCE_EXAMPLE_ID = "langsmith.reference_example_id";
|
|
41
|
+
export declare const LANGSMITH_TRACEABLE = "langsmith.traceable";
|
|
41
42
|
export declare const GEN_AI_SYSTEM_MESSAGE = "gen_ai.system.message";
|
|
42
43
|
export declare const GEN_AI_USER_MESSAGE = "gen_ai.user.message";
|
|
43
44
|
export declare const GEN_AI_ASSISTANT_MESSAGE = "gen_ai.assistant.message";
|
|
@@ -40,6 +40,7 @@ export const LANGSMITH_DOTTED_ORDER = "langsmith.span.dotted_order";
|
|
|
40
40
|
export const LANGSMITH_PARENT_RUN_ID = "langsmith.span.parent_id";
|
|
41
41
|
export const LANGSMITH_USAGE_METADATA = "langsmith.usage_metadata";
|
|
42
42
|
export const LANGSMITH_REFERENCE_EXAMPLE_ID = "langsmith.reference_example_id";
|
|
43
|
+
export const LANGSMITH_TRACEABLE = "langsmith.traceable";
|
|
43
44
|
// GenAI event names
|
|
44
45
|
export const GEN_AI_SYSTEM_MESSAGE = "gen_ai.system.message";
|
|
45
46
|
export const GEN_AI_USER_MESSAGE = "gen_ai.user.message";
|
|
@@ -39,6 +39,7 @@ const constants = __importStar(require("./constants.cjs"));
|
|
|
39
39
|
const env_js_1 = require("../../env.cjs");
|
|
40
40
|
const env_js_2 = require("../../utils/env.cjs");
|
|
41
41
|
const vercel_js_1 = require("../../utils/vercel.cjs");
|
|
42
|
+
const constants_js_1 = require("./constants.cjs");
|
|
42
43
|
/**
|
|
43
44
|
* Convert headers string in format "name=value,name2=value2" to object
|
|
44
45
|
*/
|
|
@@ -54,6 +55,10 @@ function parseHeadersString(headersStr) {
|
|
|
54
55
|
});
|
|
55
56
|
return headers;
|
|
56
57
|
}
|
|
58
|
+
function isTraceableSpan(span) {
|
|
59
|
+
return (span.attributes[constants_js_1.LANGSMITH_TRACEABLE] === "true" ||
|
|
60
|
+
typeof span.attributes["ai.operationId"] === "string");
|
|
61
|
+
}
|
|
57
62
|
/**
|
|
58
63
|
* LangSmith OpenTelemetry trace exporter that extends the standard OTLP trace exporter
|
|
59
64
|
* with LangSmith-specific configuration and span attribute transformations.
|
|
@@ -71,11 +76,11 @@ function parseHeadersString(headersStr) {
|
|
|
71
76
|
*/
|
|
72
77
|
class LangSmithOTLPTraceExporter extends exporter_trace_otlp_proto_1.OTLPTraceExporter {
|
|
73
78
|
constructor(config) {
|
|
74
|
-
const defaultLsEndpoint = (0, env_js_2.
|
|
75
|
-
(0, env_js_2.getLangSmithEnvironmentVariable)("ENDPOINT") ||
|
|
79
|
+
const defaultLsEndpoint = (0, env_js_2.getLangSmithEnvironmentVariable)("ENDPOINT") ||
|
|
76
80
|
"https://api.smith.langchain.com";
|
|
77
81
|
const defaultBaseUrl = defaultLsEndpoint.replace(/\/$/, "");
|
|
78
|
-
const defaultUrl =
|
|
82
|
+
const defaultUrl = (0, env_js_2.getEnvironmentVariable)("OTEL_EXPORTER_OTLP_ENDPOINT") ??
|
|
83
|
+
`${defaultBaseUrl}/otel/v1/traces`;
|
|
79
84
|
// Configure headers with API key and project if available
|
|
80
85
|
let headers = config?.headers;
|
|
81
86
|
if (headers === undefined) {
|
|
@@ -105,9 +110,16 @@ class LangSmithOTLPTraceExporter extends exporter_trace_otlp_proto_1.OTLPTraceEx
|
|
|
105
110
|
writable: true,
|
|
106
111
|
value: void 0
|
|
107
112
|
});
|
|
113
|
+
Object.defineProperty(this, "exportAllSpans", {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
configurable: true,
|
|
116
|
+
writable: true,
|
|
117
|
+
value: void 0
|
|
118
|
+
});
|
|
108
119
|
this.transformExportedSpan = config?.transformExportedSpan;
|
|
109
120
|
this.projectName =
|
|
110
121
|
config?.projectName ?? (0, env_js_2.getLangSmithEnvironmentVariable)("PROJECT");
|
|
122
|
+
this.exportAllSpans = config?.exportAllSpans;
|
|
111
123
|
}
|
|
112
124
|
export(spans, resultCallback) {
|
|
113
125
|
if (!(0, env_js_1.isTracingEnabled)()) {
|
|
@@ -115,6 +127,9 @@ class LangSmithOTLPTraceExporter extends exporter_trace_otlp_proto_1.OTLPTraceEx
|
|
|
115
127
|
}
|
|
116
128
|
const runExport = async () => {
|
|
117
129
|
for (let span of spans) {
|
|
130
|
+
if (!this.exportAllSpans && !isTraceableSpan(span)) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
118
133
|
if (this.transformExportedSpan) {
|
|
119
134
|
span = await this.transformExportedSpan(span);
|
|
120
135
|
}
|
|
@@ -36,6 +36,16 @@ export type LangSmithOTLPTraceExporterConfig = ConstructorParameters<typeof OTLP
|
|
|
36
36
|
* Default headers to add to exporter requests.
|
|
37
37
|
*/
|
|
38
38
|
headers?: Record<string, string>;
|
|
39
|
+
/**
|
|
40
|
+
* Trace all spans.
|
|
41
|
+
*
|
|
42
|
+
* If true, all spans will be traced, regardless of whether they are
|
|
43
|
+
* normally interesting to LangSmith.
|
|
44
|
+
* If false, only selected spans will be traced.
|
|
45
|
+
*
|
|
46
|
+
* @default false
|
|
47
|
+
*/
|
|
48
|
+
exportAllSpans?: boolean;
|
|
39
49
|
};
|
|
40
50
|
/**
|
|
41
51
|
* LangSmith OpenTelemetry trace exporter that extends the standard OTLP trace exporter
|
|
@@ -55,6 +65,7 @@ export type LangSmithOTLPTraceExporterConfig = ConstructorParameters<typeof OTLP
|
|
|
55
65
|
export declare class LangSmithOTLPTraceExporter extends OTLPTraceExporter {
|
|
56
66
|
private transformExportedSpan?;
|
|
57
67
|
private projectName?;
|
|
68
|
+
private exportAllSpans?;
|
|
58
69
|
constructor(config?: LangSmithOTLPTraceExporterConfig);
|
|
59
70
|
export(spans: ReadableSpan[], resultCallback: Parameters<OTLPTraceExporter["export"]>[1]): void;
|
|
60
71
|
}
|
|
@@ -3,6 +3,7 @@ import * as constants from "./constants.js";
|
|
|
3
3
|
import { isTracingEnabled } from "../../env.js";
|
|
4
4
|
import { getEnvironmentVariable, getLangSmithEnvironmentVariable, } from "../../utils/env.js";
|
|
5
5
|
import { extractUsageMetadata } from "../../utils/vercel.js";
|
|
6
|
+
import { LANGSMITH_TRACEABLE } from "./constants.js";
|
|
6
7
|
/**
|
|
7
8
|
* Convert headers string in format "name=value,name2=value2" to object
|
|
8
9
|
*/
|
|
@@ -18,6 +19,10 @@ function parseHeadersString(headersStr) {
|
|
|
18
19
|
});
|
|
19
20
|
return headers;
|
|
20
21
|
}
|
|
22
|
+
function isTraceableSpan(span) {
|
|
23
|
+
return (span.attributes[LANGSMITH_TRACEABLE] === "true" ||
|
|
24
|
+
typeof span.attributes["ai.operationId"] === "string");
|
|
25
|
+
}
|
|
21
26
|
/**
|
|
22
27
|
* LangSmith OpenTelemetry trace exporter that extends the standard OTLP trace exporter
|
|
23
28
|
* with LangSmith-specific configuration and span attribute transformations.
|
|
@@ -35,11 +40,11 @@ function parseHeadersString(headersStr) {
|
|
|
35
40
|
*/
|
|
36
41
|
export class LangSmithOTLPTraceExporter extends OTLPTraceExporter {
|
|
37
42
|
constructor(config) {
|
|
38
|
-
const defaultLsEndpoint =
|
|
39
|
-
getLangSmithEnvironmentVariable("ENDPOINT") ||
|
|
43
|
+
const defaultLsEndpoint = getLangSmithEnvironmentVariable("ENDPOINT") ||
|
|
40
44
|
"https://api.smith.langchain.com";
|
|
41
45
|
const defaultBaseUrl = defaultLsEndpoint.replace(/\/$/, "");
|
|
42
|
-
const defaultUrl =
|
|
46
|
+
const defaultUrl = getEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT") ??
|
|
47
|
+
`${defaultBaseUrl}/otel/v1/traces`;
|
|
43
48
|
// Configure headers with API key and project if available
|
|
44
49
|
let headers = config?.headers;
|
|
45
50
|
if (headers === undefined) {
|
|
@@ -69,9 +74,16 @@ export class LangSmithOTLPTraceExporter extends OTLPTraceExporter {
|
|
|
69
74
|
writable: true,
|
|
70
75
|
value: void 0
|
|
71
76
|
});
|
|
77
|
+
Object.defineProperty(this, "exportAllSpans", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: void 0
|
|
82
|
+
});
|
|
72
83
|
this.transformExportedSpan = config?.transformExportedSpan;
|
|
73
84
|
this.projectName =
|
|
74
85
|
config?.projectName ?? getLangSmithEnvironmentVariable("PROJECT");
|
|
86
|
+
this.exportAllSpans = config?.exportAllSpans;
|
|
75
87
|
}
|
|
76
88
|
export(spans, resultCallback) {
|
|
77
89
|
if (!isTracingEnabled()) {
|
|
@@ -79,6 +91,9 @@ export class LangSmithOTLPTraceExporter extends OTLPTraceExporter {
|
|
|
79
91
|
}
|
|
80
92
|
const runExport = async () => {
|
|
81
93
|
for (let span of spans) {
|
|
94
|
+
if (!this.exportAllSpans && !isTraceableSpan(span)) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
82
97
|
if (this.transformExportedSpan) {
|
|
83
98
|
span = await this.transformExportedSpan(span);
|
|
84
99
|
}
|
|
@@ -41,16 +41,25 @@ const otel_js_1 = require("../../singletons/otel.cjs");
|
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
43
|
const initializeOTEL = (config = {}) => {
|
|
44
|
-
const { globalTracerProvider, globalContextManager, exporterConfig } = config;
|
|
44
|
+
const { globalTracerProvider, globalContextManager, skipGlobalContextManagerSetup, exporterConfig, } = config;
|
|
45
45
|
const otel = {
|
|
46
46
|
trace: api_1.trace,
|
|
47
47
|
context: api_1.context,
|
|
48
48
|
};
|
|
49
49
|
(0, otel_js_1.setOTELInstances)(otel);
|
|
50
|
-
if (!globalContextManager) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
if (!globalContextManager && !skipGlobalContextManagerSetup) {
|
|
51
|
+
try {
|
|
52
|
+
const contextManager = new context_async_hooks_1.AsyncHooksContextManager();
|
|
53
|
+
contextManager.enable();
|
|
54
|
+
api_1.context.setGlobalContextManager(contextManager);
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
console.log([
|
|
58
|
+
`Could not automatically set up an OTEL context manager.`,
|
|
59
|
+
`This may be expected if you have (or another imported library has) already set a global context manager.`,
|
|
60
|
+
`If expected, you can skip this warning by passing "skipGlobalContextManagerSetup: true" into your initializeOTEL call.`,
|
|
61
|
+
].join("\n"));
|
|
62
|
+
}
|
|
54
63
|
}
|
|
55
64
|
const DEFAULT_LANGSMITH_SPAN_EXPORTER = new exporter_js_1.LangSmithOTLPTraceExporter(exporterConfig);
|
|
56
65
|
const DEFAULT_LANGSMITH_SPAN_PROCESSOR = new sdk_trace_base_1.BatchSpanProcessor(DEFAULT_LANGSMITH_SPAN_EXPORTER);
|
|
@@ -15,6 +15,12 @@ export type InitializeOTELConfig = {
|
|
|
15
15
|
* creating and globally setting a new one with AsyncHooksContextManager.
|
|
16
16
|
*/
|
|
17
17
|
globalContextManager?: ContextManager;
|
|
18
|
+
/**
|
|
19
|
+
* Skip automatic initialization of a global context manager.
|
|
20
|
+
*
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
23
|
+
skipGlobalContextManagerSetup?: boolean;
|
|
18
24
|
/**
|
|
19
25
|
* Optional configuration passed to the default LangSmith OTLP trace exporter.
|
|
20
26
|
*/
|
|
@@ -38,16 +38,25 @@ import { setDefaultOTLPTracerComponents, setOTELInstances, } from "../../singlet
|
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
40
|
export const initializeOTEL = (config = {}) => {
|
|
41
|
-
const { globalTracerProvider, globalContextManager, exporterConfig } = config;
|
|
41
|
+
const { globalTracerProvider, globalContextManager, skipGlobalContextManagerSetup, exporterConfig, } = config;
|
|
42
42
|
const otel = {
|
|
43
43
|
trace: otel_trace,
|
|
44
44
|
context: otel_context,
|
|
45
45
|
};
|
|
46
46
|
setOTELInstances(otel);
|
|
47
|
-
if (!globalContextManager) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
if (!globalContextManager && !skipGlobalContextManagerSetup) {
|
|
48
|
+
try {
|
|
49
|
+
const contextManager = new AsyncHooksContextManager();
|
|
50
|
+
contextManager.enable();
|
|
51
|
+
otel_context.setGlobalContextManager(contextManager);
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
console.log([
|
|
55
|
+
`Could not automatically set up an OTEL context manager.`,
|
|
56
|
+
`This may be expected if you have (or another imported library has) already set a global context manager.`,
|
|
57
|
+
`If expected, you can skip this warning by passing "skipGlobalContextManagerSetup: true" into your initializeOTEL call.`,
|
|
58
|
+
].join("\n"));
|
|
59
|
+
}
|
|
51
60
|
}
|
|
52
61
|
const DEFAULT_LANGSMITH_SPAN_EXPORTER = new LangSmithOTLPTraceExporter(exporterConfig);
|
|
53
62
|
const DEFAULT_LANGSMITH_SPAN_PROCESSOR = new BatchSpanProcessor(DEFAULT_LANGSMITH_SPAN_EXPORTER);
|
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.
|
|
13
|
+
exports.__version__ = "0.3.49-rc.0";
|
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.
|
|
6
|
+
export declare const __version__ = "0.3.49-rc.0";
|
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.
|
|
6
|
+
export const __version__ = "0.3.49-rc.0";
|
package/dist/singletons/otel.cjs
CHANGED
|
@@ -18,8 +18,8 @@ class MockTracer {
|
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
startActiveSpan(_name, ...args) {
|
|
21
|
-
if (!this.hasWarned && (0, env_js_1.
|
|
22
|
-
console.warn("You have enabled OTEL export via the `OTEL_ENABLED` environment variable, but have not initialized the required OTEL instances. " +
|
|
21
|
+
if (!this.hasWarned && (0, env_js_1.getOtelEnabled)()) {
|
|
22
|
+
console.warn("You have enabled OTEL export via the `OTEL_ENABLED` or `LANGSMITH_OTEL_ENABLED` environment variable, but have not initialized the required OTEL instances. " +
|
|
23
23
|
'Please add:\n```\nimport { initializeOTEL } from "langsmith/experimental/otel/setup";\ninitializeOTEL();\n```\nat the beginning of your code.');
|
|
24
24
|
this.hasWarned = true;
|
|
25
25
|
}
|
package/dist/singletons/otel.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Should not import any OTEL packages to avoid pulling in optional deps.
|
|
2
|
-
import {
|
|
2
|
+
import { getOtelEnabled } from "../utils/env.js";
|
|
3
3
|
class MockTracer {
|
|
4
4
|
constructor() {
|
|
5
5
|
Object.defineProperty(this, "hasWarned", {
|
|
@@ -10,8 +10,8 @@ class MockTracer {
|
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
12
|
startActiveSpan(_name, ...args) {
|
|
13
|
-
if (!this.hasWarned &&
|
|
14
|
-
console.warn("You have enabled OTEL export via the `OTEL_ENABLED` environment variable, but have not initialized the required OTEL instances. " +
|
|
13
|
+
if (!this.hasWarned && getOtelEnabled()) {
|
|
14
|
+
console.warn("You have enabled OTEL export via the `OTEL_ENABLED` or `LANGSMITH_OTEL_ENABLED` environment variable, but have not initialized the required OTEL instances. " +
|
|
15
15
|
'Please add:\n```\nimport { initializeOTEL } from "langsmith/experimental/otel/setup";\ninitializeOTEL();\n```\nat the beginning of your code.');
|
|
16
16
|
this.hasWarned = true;
|
|
17
17
|
}
|
package/dist/traceable.cjs
CHANGED
|
@@ -20,7 +20,7 @@ traceable_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new n
|
|
|
20
20
|
function maybeCreateOtelContext(runTree, projectName, tracer
|
|
21
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
22
|
) {
|
|
23
|
-
if (!runTree || (0, env_js_2.
|
|
23
|
+
if (!runTree || !(0, env_js_2.getOtelEnabled)()) {
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
26
|
const otel_trace = (0, otel_js_1.getOTELTrace)();
|
|
@@ -29,7 +29,9 @@ function maybeCreateOtelContext(runTree, projectName, tracer
|
|
|
29
29
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
30
|
return (fn) => {
|
|
31
31
|
const resolvedTracer = tracer ?? otel_trace.getTracer("langsmith", index_js_1.__version__);
|
|
32
|
-
const attributes = {
|
|
32
|
+
const attributes = {
|
|
33
|
+
[constants_js_2.LANGSMITH_TRACEABLE]: "true",
|
|
34
|
+
};
|
|
33
35
|
if (runTree.reference_example_id) {
|
|
34
36
|
attributes[constants_js_2.LANGSMITH_REFERENCE_EXAMPLE_ID] =
|
|
35
37
|
runTree.reference_example_id;
|
package/dist/traceable.js
CHANGED
|
@@ -4,11 +4,11 @@ import { isTracingEnabled } from "./env.js";
|
|
|
4
4
|
import { ROOT, AsyncLocalStorageProviderSingleton, getCurrentRunTree, } from "./singletons/traceable.js";
|
|
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
|
-
import {
|
|
7
|
+
import { getOtelEnabled } from "./utils/env.js";
|
|
8
8
|
import { __version__ } from "./index.js";
|
|
9
9
|
import { getOTELTrace, getOTELContext } from "./singletons/otel.js";
|
|
10
10
|
import { getUuidFromOtelSpanId } from "./experimental/otel/utils.js";
|
|
11
|
-
import { LANGSMITH_REFERENCE_EXAMPLE_ID, LANGSMITH_SESSION_NAME, } from "./experimental/otel/constants.js";
|
|
11
|
+
import { LANGSMITH_REFERENCE_EXAMPLE_ID, LANGSMITH_SESSION_NAME, LANGSMITH_TRACEABLE, } from "./experimental/otel/constants.js";
|
|
12
12
|
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
13
13
|
/**
|
|
14
14
|
* Create OpenTelemetry context manager from RunTree if OTEL is enabled.
|
|
@@ -16,7 +16,7 @@ AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorag
|
|
|
16
16
|
function maybeCreateOtelContext(runTree, projectName, tracer
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
18
|
) {
|
|
19
|
-
if (!runTree ||
|
|
19
|
+
if (!runTree || !getOtelEnabled()) {
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
22
|
const otel_trace = getOTELTrace();
|
|
@@ -25,7 +25,9 @@ function maybeCreateOtelContext(runTree, projectName, tracer
|
|
|
25
25
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
26
|
return (fn) => {
|
|
27
27
|
const resolvedTracer = tracer ?? otel_trace.getTracer("langsmith", __version__);
|
|
28
|
-
const attributes = {
|
|
28
|
+
const attributes = {
|
|
29
|
+
[LANGSMITH_TRACEABLE]: "true",
|
|
30
|
+
};
|
|
29
31
|
if (runTree.reference_example_id) {
|
|
30
32
|
attributes[LANGSMITH_REFERENCE_EXAMPLE_ID] =
|
|
31
33
|
runTree.reference_example_id;
|
package/dist/utils/env.cjs
CHANGED
|
@@ -9,6 +9,7 @@ exports.getEnvironmentVariable = getEnvironmentVariable;
|
|
|
9
9
|
exports.getLangSmithEnvironmentVariable = getLangSmithEnvironmentVariable;
|
|
10
10
|
exports.setEnvironmentVariable = setEnvironmentVariable;
|
|
11
11
|
exports.getShas = getShas;
|
|
12
|
+
exports.getOtelEnabled = getOtelEnabled;
|
|
12
13
|
// Inlined from https://github.com/flexdinesh/browser-or-node
|
|
13
14
|
const index_js_1 = require("../index.cjs");
|
|
14
15
|
let globalEnv;
|
|
@@ -230,3 +231,7 @@ function getShas() {
|
|
|
230
231
|
cachedCommitSHAs = shas;
|
|
231
232
|
return shas;
|
|
232
233
|
}
|
|
234
|
+
function getOtelEnabled() {
|
|
235
|
+
return (getEnvironmentVariable("OTEL_ENABLED") === "true" ||
|
|
236
|
+
getLangSmithEnvironmentVariable("OTEL_ENABLED") === "true");
|
|
237
|
+
}
|
package/dist/utils/env.d.ts
CHANGED
package/dist/utils/env.js
CHANGED