Skip to content

Commit

Permalink
feat(langfuse-core): add traces forwarding for Defer integration (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypoly authored Nov 10, 2023
1 parent 0e6c1c7 commit c12f969
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
16 changes: 16 additions & 0 deletions langfuse-core/src/globalThis.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export {};

declare interface DeferLangFuseTrace {
id: string;
name: string;
url: string;
}

declare global {
namespace globalThis {
// eslint-disable-next-line no-var
var __deferRuntime: {
langfuseTraces: (traces: DeferLangFuseTrace[]) => void;
};
}
}
16 changes: 15 additions & 1 deletion langfuse-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,21 @@ export abstract class LangfuseCore extends LangfuseCoreStateless {

trace(body?: CreateLangfuseTraceBody): LangfuseTraceClient {
const id = this.traceStateless(body ?? {});
return new LangfuseTraceClient(this, id);
const t = new LangfuseTraceClient(this, id);
if (process.env.DEFER && body) {
try {
if (globalThis.__deferRuntime) {
__deferRuntime.langfuseTraces([
{
id: id,
name: body.name || "",
url: t.getTraceUrl(),
},
]);
}
} catch {}
}
return t;
}

span(body: CreateLangfuseSpanBody): LangfuseSpanClient {
Expand Down

0 comments on commit c12f969

Please sign in to comment.