feat: custom telemetry configuration

This commit is contained in:
Abhishek Kumar 2026-03-23 11:36:39 +05:30
parent 1967a71935
commit affb39e57f
23 changed files with 927 additions and 139 deletions

View file

@ -25,6 +25,7 @@ if TYPE_CHECKING:
from pipecat.services.anthropic.llm import AnthropicLLMService
from pipecat.services.google.llm import GoogleLLMService
from pipecat.services.openai.llm import OpenAILLMService
from pipecat.utils.tracing.tracing_context import TracingContext
LLMService = Union[OpenAILLMService, AnthropicLLMService, GoogleLLMService]
@ -135,7 +136,9 @@ class PipecatEngine:
Returns the turn-level context if available, otherwise the
conversation-level context, or None.
"""
tracing_ctx = getattr(self.task, "_tracing_context", None)
tracing_ctx: TracingContext | None = getattr(
self.task, "_tracing_context", None
)
if not tracing_ctx:
return None
return tracing_ctx.get_turn_context() or tracing_ctx.get_conversation_context()
@ -439,6 +442,10 @@ class PipecatEngine:
)
)
self._gathered_context.update(extracted_data)
extracted_variables = self._gathered_context.setdefault(
"extracted_variables", {}
)
extracted_variables.update(extracted_data)
logger.debug(
f"Variable extraction completed. Extracted: {extracted_data}"
)

View file

@ -7,7 +7,7 @@ from loguru import logger
from opentelemetry import trace
from api.services.gen_ai.json_parser import parse_llm_json
from api.services.pipecat.tracing_config import is_tracing_enabled
from api.services.pipecat.tracing_config import ensure_tracing
from api.services.workflow.dto import ExtractionVariableDTO
from pipecat.processors.aggregators.llm_context import LLMContext
from pipecat.utils.tracing.service_attributes import add_llm_span_attributes
@ -206,7 +206,7 @@ class VariableExtractionManager:
# Get model name for tracing
model_name = getattr(self._engine.llm, "model_name", "unknown")
if is_tracing_enabled():
if ensure_tracing():
tracer = trace.get_tracer("pipecat")
with tracer.start_as_current_span(
"llm-variable-extraction", context=parent_ctx

View file

@ -5,18 +5,21 @@ import re
from loguru import logger
from api.db.models import WorkflowRunModel
from api.services.pipecat.tracing_config import get_trace_url
def extract_trace_id(gathered_context: dict) -> str | None:
"""Extract Langfuse trace_id from gathered_context trace_url.
URL format: https://langfuse.dograh.com/project/<project_id>/traces/<trace_id>
Supports both URL formats:
- New: https://langfuse.dograh.com/trace/<trace_id>
- Legacy: https://langfuse.dograh.com/project/<project_id>/traces/<trace_id>
"""
trace_url = gathered_context.get("trace_url")
if not trace_url:
return None
try:
match = re.search(r"/traces/([a-fA-F0-9]+)$", trace_url)
match = re.search(r"/traces?/([a-fA-F0-9]+)$", trace_url)
if match:
return match.group(1)
except Exception:
@ -37,16 +40,11 @@ def setup_langfuse_parent_context(workflow_run: WorkflowRunModel):
set_span_in_context,
)
from api.services.pipecat.tracing_config import (
is_tracing_enabled,
setup_tracing_exporter,
)
from api.services.pipecat.tracing_config import ensure_tracing
if not is_tracing_enabled():
if not ensure_tracing():
return None
setup_tracing_exporter()
gathered_context = workflow_run.gathered_context or {}
trace_id = extract_trace_id(gathered_context)
if not trace_id:
@ -114,17 +112,12 @@ def create_node_summary_trace(
from opentelemetry import trace as otel_trace
from opentelemetry.context import Context
from api.services.pipecat.tracing_config import (
is_tracing_enabled,
setup_tracing_exporter,
)
from api.services.pipecat.tracing_config import ensure_tracing
from pipecat.utils.tracing.service_attributes import add_llm_span_attributes
if not is_tracing_enabled():
if not ensure_tracing():
return None
setup_tracing_exporter()
tracer = otel_trace.get_tracer("pipecat")
# Create a root span (new trace) for this node summary generation
@ -144,10 +137,7 @@ def create_node_summary_trace(
)
trace_id = format(span.get_span_context().trace_id, "032x")
from langfuse import get_client
langfuse = get_client()
return langfuse.get_trace_url(trace_id=trace_id)
return get_trace_url(trace_id)
except Exception as e:
logger.warning(f"Failed to create node summary trace for '{node_name}': {e}")

View file

@ -14,7 +14,7 @@ from opentelemetry import trace
from api.db import db_client
from api.services.gen_ai import OpenAIEmbeddingService
from api.services.pipecat.tracing_config import is_tracing_enabled
from api.services.pipecat.tracing_config import ensure_tracing
async def retrieve_from_knowledge_base(
@ -51,7 +51,7 @@ async def retrieve_from_knowledge_base(
- total_results: Number of results returned
"""
# Create span for retrieval operation if tracing is enabled
if is_tracing_enabled():
if ensure_tracing():
try:
parent_context = tracing_context