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).
This commit is contained in:
Dayuan Jiang
2025-12-24 10:47:34 +09:00
committed by GitHub
parent 82f4deb23a
commit 63398d9f34

View File

@@ -14,22 +14,14 @@ export function register() {
publicKey: process.env.LANGFUSE_PUBLIC_KEY, publicKey: process.env.LANGFUSE_PUBLIC_KEY,
secretKey: process.env.LANGFUSE_SECRET_KEY, secretKey: process.env.LANGFUSE_SECRET_KEY,
baseUrl: process.env.LANGFUSE_BASEURL, 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 }) => { shouldExportSpan: ({ otelSpan }) => {
const spanName = otelSpan.name const spanName = otelSpan.name
// Skip Next.js HTTP infrastructure spans // Only export AI SDK spans (ai.*) and our explicit "chat" wrapper
if ( if (spanName === "chat" || spanName.startsWith("ai.")) {
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
}
return true return true
}
return false
}, },
}) })