
    qi                         d Z ddlZddlmZmZ erddlmZ ddlmZ ddl	m
Z
  e
       rddlmZ ddlmZmZmZ  G d d	      Zy)
a)  Pipeline-scoped tracing context for OpenTelemetry tracing in Pipecat.

This module provides a per-pipeline tracing context that holds the current
conversation and turn span contexts. Each PipelineTask creates its own
TracingContext, ensuring concurrent pipelines do not interfere with each other.
    N)TYPE_CHECKINGOptional)Context)SpanContext)is_tracing_available)NonRecordingSpanr   set_span_in_contextc                       e Zd ZdZd Z	 dded   dee   fdZded	   fd
Zded   fdZ	ded	   fdZ
edee   fd       Zedefd       Zy)TracingContextzPipeline-scoped tracing context.

    Holds the current conversation and turn span contexts for a single pipeline.
    Created by PipelineTask, passed to TurnTraceObserver (writer) and services
    (readers) via StartFrame.
    c                 .    d| _         d| _        d| _        y)z0Initialize the tracing context with empty state.N)_conversation_context_turn_context_conversation_idselfs    W/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/utils/tracing/tracing_context.py__init__zTracingContext.__init__$   s    :>"26/3    Nspan_contextr   conversation_idc                 r    t               sy|| _        |rt        |      }t        |      | _        yd| _        y)zSet the current conversation context.

        Args:
            span_context: The span context for the current conversation or None to clear it.
            conversation_id: Optional ID for the conversation.
        N)r   r   r   r	   r   )r   r   r   non_recording_spans       r   set_conversation_contextz'TracingContext.set_conversation_context*   s9     $% /!1,!?)<=O)PD&)-D&r   returnr   c                     | j                   S )zGet the OpenTelemetry context for the current conversation.

        Returns:
            The current conversation context or None if not available.
        )r   r   s    r   get_conversation_contextz'TracingContext.get_conversation_context>   s     )))r   c                 d    t               sy|rt        |      }t        |      | _        yd| _        y)zSet the current turn context.

        Args:
            span_context: The span context for the current turn or None to clear it.
        N)r   r   r	   r   )r   r   r   s      r   set_turn_contextzTracingContext.set_turn_contextF   s1     $%!1,!?!45G!HD!%Dr   c                     | j                   S )zGet the OpenTelemetry context for the current turn.

        Returns:
            The current turn context or None if not available.
        )r   r   s    r   get_turn_contextzTracingContext.get_turn_contextU   s     !!!r   c                     | j                   S )zGet the ID for the current conversation.

        Returns:
            The current conversation ID or None if not available.
        )r   r   s    r   r   zTracingContext.conversation_id]   s     $$$r   c                  <    t        t        j                               S )zlGenerate a new conversation ID.

        Returns:
            A new randomly generated UUID string.
        )struuiduuid4 r   r   generate_conversation_idz'TracingContext.generate_conversation_idf   s     4::<  r   )N)__name__
__module____qualname____doc__r   r   r#   r   r   r   r    propertyr   staticmethodr'   r&   r   r   r   r      s    4 W[.$]3.FNsm.(*(9*= *&Xm-D &"(9"5 " %# % % !c ! !r   r   )r+   r$   typingr   r   opentelemetry.contextr   opentelemetry.tracer   pipecat.utils.tracing.setupr   r   r	   r   r&   r   r   <module>r2      s9     *-/ <-VVQ! Q!r   