mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
feat: integrate Langfuse for LLM observability (#66)
* feat: integrate Langfuse for LLM observability - Add instrumentation.ts with Langfuse OpenTelemetry exporter - Enable experimental telemetry on streamText calls - Add instrumentationHook to Next.js config - Install required dependencies (@vercel/otel, langfuse-vercel, etc.) * feat: add optional Langfuse observability integration - Add session tracking with unique sessionId per conversation - Add user tracking via IP address (x-forwarded-for header) - Make telemetry conditional - only enabled if LANGFUSE_PUBLIC_KEY is set - Add environment variable validation in instrumentation.ts - Add sessionId validation (type check + 200 char limit) - Update env.example with Langfuse configuration docs - Remove unused langfuse-vercel and @vercel/otel packages * fix: remove deprecated instrumentationHook (enabled by default in Next.js 15)
This commit is contained in:
@@ -30,7 +30,16 @@ function createCachedStreamResponse(xml: string): Response {
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const { messages, xml } = await req.json();
|
||||
const { messages, xml, sessionId } = await req.json();
|
||||
|
||||
// Get user IP for Langfuse tracking
|
||||
const forwardedFor = req.headers.get('x-forwarded-for');
|
||||
const userId = forwardedFor?.split(',')[0]?.trim() || 'anonymous';
|
||||
|
||||
// Validate sessionId for Langfuse (must be string, max 200 chars)
|
||||
const validSessionId = sessionId && typeof sessionId === 'string' && sessionId.length <= 200
|
||||
? sessionId
|
||||
: undefined;
|
||||
|
||||
// === CACHE CHECK START ===
|
||||
const isFirstMessage = messages.length === 1;
|
||||
@@ -257,6 +266,16 @@ ${lastMessageText}
|
||||
messages: [systemMessageWithCache, ...enhancedMessages],
|
||||
...(providerOptions && { providerOptions }),
|
||||
...(headers && { headers }),
|
||||
// Only enable telemetry if Langfuse is configured
|
||||
...(process.env.LANGFUSE_PUBLIC_KEY && {
|
||||
experimental_telemetry: {
|
||||
isEnabled: true,
|
||||
metadata: {
|
||||
sessionId: validSessionId,
|
||||
userId: userId,
|
||||
},
|
||||
},
|
||||
}),
|
||||
onFinish: ({ usage, providerMetadata }) => {
|
||||
console.log('[Cache] Usage:', JSON.stringify({
|
||||
inputTokens: usage?.inputTokens,
|
||||
|
||||
Reference in New Issue
Block a user