mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
feat: add trace-level input/output to Langfuse observability (#69)
* feat: add trace-level input/output to Langfuse observability - Add @langfuse/client and @langfuse/tracing dependencies - Wrap POST handler with observe() for proper tracing - Use updateActiveTrace() to set trace input, output, sessionId, userId - Filter Next.js HTTP spans in shouldExportSpan so AI SDK spans become root traces - Enable recordInputs/recordOutputs in experimental_telemetry * refactor: extract Langfuse logic to separate lib/langfuse.ts module
This commit is contained in:
63
lib/langfuse.ts
Normal file
63
lib/langfuse.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { observe, updateActiveTrace } from '@langfuse/tracing';
|
||||
import * as api from '@opentelemetry/api';
|
||||
|
||||
// Check if Langfuse is configured
|
||||
export function isLangfuseEnabled(): boolean {
|
||||
return !!process.env.LANGFUSE_PUBLIC_KEY;
|
||||
}
|
||||
|
||||
// Update trace with input data at the start of request
|
||||
export function setTraceInput(params: {
|
||||
input: string;
|
||||
sessionId?: string;
|
||||
userId?: string;
|
||||
}) {
|
||||
if (!isLangfuseEnabled()) return;
|
||||
|
||||
updateActiveTrace({
|
||||
name: 'chat',
|
||||
input: params.input,
|
||||
sessionId: params.sessionId,
|
||||
userId: params.userId,
|
||||
});
|
||||
}
|
||||
|
||||
// Update trace with output and end the span
|
||||
export function setTraceOutput(output: string) {
|
||||
if (!isLangfuseEnabled()) return;
|
||||
|
||||
updateActiveTrace({ output });
|
||||
const activeSpan = api.trace.getActiveSpan();
|
||||
if (activeSpan) {
|
||||
activeSpan.end();
|
||||
}
|
||||
}
|
||||
|
||||
// Get telemetry config for streamText
|
||||
export function getTelemetryConfig(params: {
|
||||
sessionId?: string;
|
||||
userId?: string;
|
||||
}) {
|
||||
if (!isLangfuseEnabled()) return undefined;
|
||||
|
||||
return {
|
||||
isEnabled: true,
|
||||
recordInputs: true,
|
||||
recordOutputs: true,
|
||||
metadata: {
|
||||
sessionId: params.sessionId,
|
||||
userId: params.userId,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Wrap a handler with Langfuse observe
|
||||
export function wrapWithObserve<T>(
|
||||
handler: (req: Request) => Promise<T>
|
||||
): (req: Request) => Promise<T> {
|
||||
if (!isLangfuseEnabled()) {
|
||||
return handler;
|
||||
}
|
||||
|
||||
return observe(handler, { name: 'chat', endOnExit: false });
|
||||
}
|
||||
Reference in New Issue
Block a user