2025-09-09 14:37:32 +05:30
|
|
|
import base64
|
|
|
|
|
import os
|
|
|
|
|
|
2025-10-21 12:11:34 +05:30
|
|
|
from loguru import logger
|
2025-09-09 14:37:32 +05:30
|
|
|
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
|
|
|
|
|
2026-02-25 13:53:30 +05:30
|
|
|
from api.constants import (
|
|
|
|
|
ENABLE_TRACING,
|
|
|
|
|
LANGFUSE_HOST,
|
|
|
|
|
LANGFUSE_PUBLIC_KEY,
|
|
|
|
|
LANGFUSE_SECRET_KEY,
|
|
|
|
|
)
|
2025-09-09 14:37:32 +05:30
|
|
|
from pipecat.utils.tracing.setup import setup_tracing
|
|
|
|
|
|
2026-02-25 13:53:30 +05:30
|
|
|
_tracing_initialized = False
|
|
|
|
|
|
2025-09-09 14:37:32 +05:30
|
|
|
|
|
|
|
|
def is_tracing_enabled():
|
|
|
|
|
"""Check if tracing should be enabled based on ENABLE_TRACING flag."""
|
|
|
|
|
# Tracing is only enabled when ENABLE_TRACING is explicitly set to true
|
|
|
|
|
# This makes the system OSS-friendly by default (no external dependencies required)
|
|
|
|
|
return ENABLE_TRACING
|
|
|
|
|
|
|
|
|
|
|
2026-02-25 13:53:30 +05:30
|
|
|
def setup_tracing_exporter():
|
|
|
|
|
"""Setup the OTEL tracing exporter for Langfuse if enabled.
|
2025-09-09 14:37:32 +05:30
|
|
|
|
2026-02-25 13:53:30 +05:30
|
|
|
Idempotent — safe to call from both the pipeline process and the ARQ worker.
|
|
|
|
|
"""
|
|
|
|
|
global _tracing_initialized
|
|
|
|
|
if _tracing_initialized:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if is_tracing_enabled():
|
|
|
|
|
if not all([LANGFUSE_HOST, LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY]):
|
2025-10-21 12:11:34 +05:30
|
|
|
logger.warning(
|
2025-09-09 14:37:32 +05:30
|
|
|
"Warning: ENABLE_TRACING is true but Langfuse credentials are not configured. Tracing disabled."
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
2026-02-25 13:53:30 +05:30
|
|
|
langfuse_auth = base64.b64encode(
|
|
|
|
|
f"{LANGFUSE_PUBLIC_KEY}:{LANGFUSE_SECRET_KEY}".encode()
|
2025-09-09 14:37:32 +05:30
|
|
|
).decode()
|
|
|
|
|
|
2026-02-25 13:53:30 +05:30
|
|
|
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{LANGFUSE_HOST}/api/public/otel"
|
2025-09-09 14:37:32 +05:30
|
|
|
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = (
|
2026-02-25 13:53:30 +05:30
|
|
|
f"Authorization=Basic {langfuse_auth}"
|
2025-09-09 14:37:32 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
otlp_exporter = OTLPSpanExporter()
|
|
|
|
|
setup_tracing(service_name="dograh-pipeline", exporter=otlp_exporter)
|
2026-02-25 13:53:30 +05:30
|
|
|
_tracing_initialized = True
|