o
    i                     @   s|   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
 r5ddlmZ ddlmZmZmZ G dd	 d	ZdS )
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                   @   s   e Zd ZdZdd Z	dded dee fdd	Zd
ed fddZded fddZ	d
ed fddZ
ed
ee fddZed
efddZdS )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                 C   s   d| _ d| _d| _dS )z0Initialize the tracing context with empty state.N)_conversation_context_turn_context_conversation_idself r   Y/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/utils/tracing/tracing_context.py__init__$   s   
zTracingContext.__init__Nspan_contextr   conversation_idc                 C   s4   t  sdS || _|rt|}t|| _dS d| _dS )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_spanr   r   r   set_conversation_context*   s   	
z'TracingContext.set_conversation_contextreturnr   c                 C      | j S )zGet the OpenTelemetry context for the current conversation.

        Returns:
            The current conversation context or None if not available.
        )r
   r   r   r   r   get_conversation_context>      z'TracingContext.get_conversation_contextc                 C   s.   t  sdS |rt|}t|| _dS d| _dS )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   r   r   r   set_turn_contextF   s   
zTracingContext.set_turn_contextc                 C   r   )zGet the OpenTelemetry context for the current turn.

        Returns:
            The current turn context or None if not available.
        )r   r   r   r   r   get_turn_contextU   r   zTracingContext.get_turn_contextc                 C   r   )zGet the ID for the current conversation.

        Returns:
            The current conversation ID or None if not available.
        )r   r   r   r   r   r   ]   s   zTracingContext.conversation_idc                   C   s   t t S )zlGenerate a new conversation ID.

        Returns:
            A new randomly generated UUID string.
        )struuiduuid4r   r   r   r   generate_conversation_idf   s   z'TracingContext.generate_conversation_id)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   propertyr   staticmethodr   r   r   r   r   r	      s     
r	   )r#   r   typingr   r   opentelemetry.contextr   opentelemetry.tracer   pipecat.utils.tracing.setupr   r   r   r	   r   r   r   r   <module>   s   