2025-12-06 12:46:40 +09:00
|
|
|
import { LangfuseSpanProcessor } from "@langfuse/otel"
|
|
|
|
|
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"
|
2025-12-05 21:15:02 +09:00
|
|
|
|
|
|
|
|
export function register() {
|
2025-12-06 12:46:40 +09:00
|
|
|
// Skip telemetry if Langfuse env vars are not configured
|
|
|
|
|
if (!process.env.LANGFUSE_PUBLIC_KEY || !process.env.LANGFUSE_SECRET_KEY) {
|
|
|
|
|
console.warn(
|
|
|
|
|
"[Langfuse] Environment variables not configured - telemetry disabled",
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
const langfuseSpanProcessor = new LangfuseSpanProcessor({
|
|
|
|
|
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
|
|
|
|
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
|
|
|
|
baseUrl: process.env.LANGFUSE_BASEURL,
|
2025-12-24 10:47:34 +09:00
|
|
|
// Whitelist approach: only export AI-related spans
|
2025-12-06 12:46:40 +09:00
|
|
|
shouldExportSpan: ({ otelSpan }) => {
|
|
|
|
|
const spanName = otelSpan.name
|
2025-12-24 10:47:34 +09:00
|
|
|
// Only export AI SDK spans (ai.*) and our explicit "chat" wrapper
|
|
|
|
|
if (spanName === "chat" || spanName.startsWith("ai.")) {
|
|
|
|
|
return true
|
2025-12-06 12:46:40 +09:00
|
|
|
}
|
2025-12-24 10:47:34 +09:00
|
|
|
return false
|
2025-12-06 12:46:40 +09:00
|
|
|
},
|
|
|
|
|
})
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
const tracerProvider = new NodeTracerProvider({
|
|
|
|
|
spanProcessors: [langfuseSpanProcessor],
|
|
|
|
|
})
|
2025-12-05 21:15:02 +09:00
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
// Register globally so AI SDK's telemetry also uses this processor
|
|
|
|
|
tracerProvider.register()
|
2025-12-23 09:47:23 +09:00
|
|
|
console.log("[Langfuse] Instrumentation initialized successfully")
|
2025-12-05 21:15:02 +09:00
|
|
|
}
|