
    jn                         d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZ ded	efd
Z G d dej                  Zde	eeef         d	efdZded	efdZdS )zUtility functions for Langfuse OpenTelemetry integration.

This module provides utility functions for working with OpenTelemetry spans,
including formatting and serialization of span data, and async execution helpers.
    N)sha256)Any	Coroutine)trace)util)ReadableSpanspanreturnc                    | j         rt          j        | j         j                  nd }| j        rt          j        | j                  nd }| j        rt          j        | j                  nd }dt          | j	        j
        j                  i}| j	        j        r| j	        j        |d<   | j        rUt          j        | j        j                  t          j        | j        j                  t!          | j        j                  dnd }t%          j        | j        r| j                                        nd          }t%          j        | j        |t          | j                  |||||                     | j                  |                     | j                  |                     | j                  t%          j        | j                                                  |dd          dz   S )	Nstatus_codedescription)trace_idspan_idtrace_statez{})namecontextkind	parent_id
start_timeend_timestatus
attributeseventslinksresourceinstrumentationScope   )indent
) parentotel_trace_apiformat_span_idr   _start_timer   ns_to_iso_str	_end_timestr_statusr   r   r   _contextformat_trace_idr   reprr   jsonloads_instrumentation_scopeto_jsondumps_namer   _format_attributes_attributes_format_events_events_format_links_linksr   )r	   r   r   r   r   r   r   s          f/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/langfuse/_client/utils.pyspan_formatterr8      s   >BkS%dk&9:::t  :>9IS#D$4555tJ59^Mt!$.111Hs4<3899F | 9 $ 8} =	&6t}7MNN%4T]5JKK 9::	
 	
 	
    :151LV#++---RV 
 	

"DI&($ "55d6FGG--dl;;++DK88 Jt}'<'<'>'>??(<  	
 	
 	
" #	    c                   F     e Zd ZdZdeeeef         ddf fdZddZ xZS )_RunAsyncThreadzFHelper thread class for running async coroutines in a separate thread.coror
   Nc                     || _         t          j                    | _        d | _        d | _        t                                                       d S N)r<   contextvarscopy_contextr   result	exceptionsuper__init__)selfr<   	__class__s     r7   rD   z_RunAsyncThread.__init__G   sD    	"/11+/r9   c                     	 | j                             t          j        | j                  | _        d S # t
          $ r}|| _        Y d }~d S d }~ww xY wr>   )r   runasyncior<   rA   	ExceptionrB   )rE   es     r7   rH   z_RunAsyncThread.runN   sZ    	,**7;	BBDKKK 	 	 	DNNNNNNN	s   /3 
AA

A)r
   N)	__name__
__module____qualname____doc__r   r   rD   rH   __classcell__)rF   s   @r7   r;   r;   D   sp        PPYsC}5 $             r9   r;   r<   c                 `   	 t          j                    }n$# t          $ r t          j        |           cY S w xY w|r`|                                rLt          |           }|                                 |                                 |j        r|j        |j	        S t          j        |           S )a  Safely run an async coroutine, handling existing event loops.

    This function detects if there's already a running event loop and uses
    a separate thread if needed to avoid the "asyncio.run() cannot be called
    from a running event loop" error. This is particularly useful in environments
    like Jupyter notebooks, FastAPI applications, or other async frameworks.

    Args:
        coro: The coroutine to run

    Returns:
        The result of the coroutine

    Raises:
        Any exception raised by the coroutine

    Example:
        ```python
        # Works in both sync and async contexts
        async def my_async_function():
            await asyncio.sleep(1)
            return "done"

        result = run_async_safely(my_async_function())
        ```
    )
rI   get_running_loopRuntimeErrorrH   
is_runningr;   startjoinrB   rA   )r<   loopthreads      r7   run_async_safelyrY   U   s    6!')) ! ! !{4     !  !!! ! && 	#""} {4   s    77valuec                     t          |                     d                                                                                    S )Nzutf-8)r   encodedigesthex)rZ   s    r7   get_sha256_hash_hexr_      s4    %,,w''((//1155777r9   )rO   rI   r?   r+   	threadinghashlibr   typingr   r   opentelemetryr   r!   opentelemetry.sdkr   opentelemetry.sdk.tracer   r&   r8   Threadr;   rY   r_    r9   r7   <module>rh      sE                    ! ! ! ! ! ! ! ! 1 1 1 1 1 1 " " " " " " 0 0 0 0 0 0. .# . . . .b    i&   "-!9S#s]3 -! -! -! -! -!`8s 8s 8 8 8 8 8 8r9   