From 63398d9f34639dc81aaaf05b2625508945a3b20d Mon Sep 17 00:00:00 2001 From: Dayuan Jiang <34411969+DayuanJiang@users.noreply.github.com> Date: Wed, 24 Dec 2025 10:47:34 +0900 Subject: [PATCH] fix: filter Langfuse traces to only export chat and AI SDK spans (#392) Switch from blocklist to whitelist approach - only export spans named 'chat' or starting with 'ai.' to filter out Next.js infrastructure noise (HEAD, fetch, POST requests). --- instrumentation.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/instrumentation.ts b/instrumentation.ts index 7fe59fb..d6d4506 100644 --- a/instrumentation.ts +++ b/instrumentation.ts @@ -14,22 +14,14 @@ export function register() { publicKey: process.env.LANGFUSE_PUBLIC_KEY, secretKey: process.env.LANGFUSE_SECRET_KEY, baseUrl: process.env.LANGFUSE_BASEURL, - // Filter out Next.js HTTP request spans so AI SDK spans become root traces + // Whitelist approach: only export AI-related spans shouldExportSpan: ({ otelSpan }) => { const spanName = otelSpan.name - // Skip Next.js HTTP infrastructure spans - if ( - spanName.startsWith("POST") || - spanName.startsWith("GET") || - spanName.startsWith("RSC") || - spanName.includes("BaseServer") || - spanName.includes("handleRequest") || - spanName.includes("resolve page") || - spanName.includes("start response") - ) { - return false + // Only export AI SDK spans (ai.*) and our explicit "chat" wrapper + if (spanName === "chat" || spanName.startsWith("ai.")) { + return true } - return true + return false }, })