Hey community!!
I have integrated langfuse with livekit(using python), but an issue which i am facing is that after the agent terminal is run for the first time, the telemetry data of the very first voice call conducted is being shown correctly as the new session in the langfuse dashboard but any subsequent calls conducted are not shown in the dashboard.
Below given is the entrypoint code related to langfuse:
async def entrypoint(ctx: agents.JobContext):
# Setup Langfuse locally per-job to properly bind LiveKit's internal telemetry
try:
trace_provider = setup_langfuse(metadata={"langfuse.session.id": ctx.room.name})
logger.info(f"Langfuse tracing bound to session: {ctx.room.name}")
except Exception as e:
logger.error(f"Failed to setup Langfuse tracing: {e}")
trace_provider = None
if trace_provider:
tracer = trace.get_tracer(__name__, tracer_provider=trace_provider)
else:
tracer = trace.get_tracer(__name__)
# Create root span
span = tracer.start_span("voice_agent_session")
span.set_attribute("langfuse.trace.name", "voice_call")
# Also set the session id on the root span explicitly for safety
span.set_attribute("langfuse.session.id", ctx.room.name)
span_ctx = trace.use_span(span, end_on_exit=False)
span_ctx.__enter__()
async def _cleanup():
logger.info(f"🔄 [LANGFUSE] _cleanup called for {ctx.room.name}, ending root span...")
# Exit the span context first to detach from OpenTelemetry context
try:
span_ctx.__exit__(None, None, None)
except Exception:
pass
span.end()
if trace_provider:
try:
logger.info("🔄 [LANGFUSE] Forcing flush of Langfuse traces...")
# Small delay to let BatchSpanProcessor pick up the final span
await asyncio.sleep(0.2)
trace_provider.force_flush()
logger.info("âś… [LANGFUSE] Trace flush successful!")
except Exception as e:
logger.error(f"❌ [LANGFUSE] Trace flush failed: {e}")
else:
logger.warning("⚠️ [LANGFUSE] No trace_provider found during cleanup!")
ctx.add_shutdown_call
back(_cleanup)
logger.info(f"Connecting to room: {ctx.room.name}")
await ctx.connect()
and below, i have attached the .py file responsible for initializing the setup_langfuse function , TracerProvider :
langfuse_tracing.py (2.4 KB)
please help me understand if there’s anything wrong.
Thank you