Skip to content

Commit

Permalink
feat(vercel): add debug flag (#1166)
Browse files Browse the repository at this point in the history
- **feat(vercel): add debug flag**
- **Bump to 0.2.4-dev.0**
- **Prevent unnecessary nesting**
  • Loading branch information
dqbd authored Nov 4, 2024
1 parent 13a5f70 commit 9e17e05
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langsmith",
"version": "0.2.3",
"version": "0.2.4-dev.0",
"description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
"packageManager": "yarn@1.22.19",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export { RunTree, type RunTreeConfig } from "./run_trees.js";
export { overrideFetchImplementation } from "./singletons/fetch.js";

// Update using yarn bump-version
export const __version__ = "0.2.3";
export const __version__ = "0.2.4-dev.0";
27 changes: 24 additions & 3 deletions js/src/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { Client, RunTree } from "./index.js";
import { KVMap, RunCreate } from "./schemas.js";
import { v5 as uuid5, v4 as uuid4 } from "uuid";
import { getCurrentRunTree } from "./singletons/traceable.js";
import { getLangSmithEnvironmentVariable } from "./utils/env.js";
import {
getLangSmithEnvironmentVariable,
getEnvironmentVariable,
} from "./utils/env.js";

// eslint-disable-next-line @typescript-eslint/ban-types
type AnyString = string & {};
Expand Down Expand Up @@ -310,8 +313,12 @@ export class AISDKExporter {
}
> = {};

constructor(args?: { client?: Client }) {
private debug: boolean;

constructor(args?: { client?: Client; debug?: boolean }) {
this.client = args?.client ?? new Client();
this.debug =
args?.debug ?? getEnvironmentVariable("OTEL_LOG_LEVEL") === "DEBUG";
}

static getSettings(settings?: TelemetrySettings) {
Expand Down Expand Up @@ -706,6 +713,8 @@ export class AISDKExporter {
spans: unknown[],
resultCallback: (result: { code: 0 | 1; error?: Error }) => void
): void {
this.logDebug("exporting spans", spans);

const typedSpans = (spans as AISDKSpan[])
.slice()
.sort((a, b) => sortByHr(a.startTime, b.startTime));
Expand All @@ -730,7 +739,10 @@ export class AISDKExporter {
const traceMap = this.traceByMap[traceId];

const run = this.getRunCreate(span);
if (!run) continue;
if (!run) {
this.logDebug("skipping span", span);
continue;
}

traceMap.relativeExecutionOrder[parentRunId ?? ROOT] ??= -1;
traceMap.relativeExecutionOrder[parentRunId ?? ROOT] += 1;
Expand All @@ -744,6 +756,7 @@ export class AISDKExporter {
executionOrder: traceMap.relativeExecutionOrder[parentRunId ?? ROOT],
};

if (this.debug) console.log(`[${span.name}] ${runId}`, run);
traceMap.childMap[parentRunId ?? ROOT] ??= [];
traceMap.childMap[parentRunId ?? ROOT].push(traceMap.nodeMap[runId]);
traceMap.interop = this.parseInteropFromMetadata(span);
Expand Down Expand Up @@ -854,6 +867,7 @@ export class AISDKExporter {
}
}

this.logDebug(`sampled runs to be sent to LangSmith`, sampled);
Promise.all(
sampled.map(([override, value]) =>
this.client.createRun({ ...value, ...override })
Expand All @@ -869,6 +883,7 @@ export class AISDKExporter {
const incompleteNodes = Object.values(this.traceByMap).flatMap((trace) =>
Object.values(trace.nodeMap).filter((i) => !i.sent)
);
this.logDebug("shutting down", { incompleteNodes: incompleteNodes.length });

if (incompleteNodes.length > 0) {
console.warn(
Expand All @@ -878,7 +893,13 @@ export class AISDKExporter {

await this.client?.awaitPendingTraceBatches();
}

async forceFlush?(): Promise<void> {
await this.client?.awaitPendingTraceBatches();
}

protected logDebug(...args: Parameters<typeof console.debug>): void {
if (!this.debug) return;
console.debug(`[${new Date().toISOString()}] [LangSmith]`, ...args);
}
}

0 comments on commit 9e17e05

Please sign in to comment.