o
    Á¿iÆ  ã                   @   sV   d dl mZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ e
eƒZG dd„ dƒZdS )	é    ©ÚContext)ÚBaseContextProvider©ÚSpan)Ú
get_logger)Útracerc                   @   s4   e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZd
S )ÚDDRuntimeContextzž
    Responsible for converting between OpenTelemetry and Datadog spans. This class is loaded by entrypoint
    when `opentelemetry.context` is imported.
    c                 C   sÄ   ddl m} ddlm} ddlm} ddlm} ddlm} ||ƒ}t	||ƒr3| j
 |j¡ |jj}n||urB||ƒ}| j
 |¡ ntƒ S ||ƒ}	|r_| ¡  |	r_|	 ¡ D ]	\}
}||j|
< qUtƒ S )z§
        Converts an OpenTelemetry Span to a Datadog Span/Context then stores the
        Datadog representation in the Global DDtrace Trace Context Provider.
        r   )Úget_all)ÚINVALID_SPAN)Úget_current_spané   r   )Ú_otel_to_dd_span_context)Úopentelemetry.baggager
   Úopentelemetry.tracer   r   Úspanr   Útracer   Ú
isinstanceÚ_ddcontext_providerÚactivateÚ_ddspanÚcontextÚobjectÚremove_all_baggage_itemsÚitemsÚ_baggage)ÚselfÚotel_contextr
   r   r   r   r   Ú	otel_spanÚ	ddcontextÚotel_baggageÚkeyÚvalue© r#   úZ/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/opentelemetry/context.pyÚattach   s(   

zDDRuntimeContext.attachc                 C   s,  ddl m} ddlm} ddlm} ddlm} ddlm} ddlm	} dd	l
m} dd
l
m} | j ¡ }	|ƒ }
t|	tƒrG||	ƒ}|||
ƒ}
n)t|	tƒrp| |	jg¡}||	jƒ}||	jp]d|	jpad|	j||ƒ}||ƒ}|||
ƒ}
t|	tƒrz|	jj}nt|	tƒrƒ|	j}ni }| ¡ D ]
\}}||||
ƒ}
q‰|
S )zŸ
        Converts the active datadog span to an Opentelemetry Span and then stores it
        in a format that can be parsed by the OpenTelemetry API.
        r   )Úset_baggager   )ÚNonRecordingSpan)ÚSpanContext)Úset_span_in_context)Ú
TraceStater   r   )Ú_get_trace_flags)r   r&   Úopentelemetry.context.contextr   r   r'   r(   r)   Úopentelemetry.trace.spanr*   r   r   r+   r   Úactiver   ÚDDSpanÚ	DDContextÚfrom_headerÚ_tracestateÚsampling_priorityÚtrace_idÚspan_idÚ
_is_remoter   r   r   )r   r&   ÚOtelContextÚOtelNonRecordingSpanÚOtelSpanContextr)   r*   r   r+   Úddactiver   r   ÚtsÚtfÚscÚ
dd_baggager!   r"   r#   r#   r$   Úget_current5   s6   







zDDRuntimeContext.get_currentc                 C   s   dS )z•
        NOP, The otel api uses this method to deactivate spans but this operation is not supported by
        the datadog context provider.
        Nr#   )r   Útokenr#   r#   r$   Údetach]   s   zDDRuntimeContext.detachc                 C   s   t jS )zœ
        Get the ddtrace context provider from the global Datadog tracer.
        This can reterive a default, gevent, or asyncio context provider.
        )ÚddtracerÚcontext_provider)r   r#   r#   r$   r   d   s   z$DDRuntimeContext._ddcontext_providerN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r?   rA   Úpropertyr   r#   r#   r#   r$   r	      s    $(r	   N)Úddtrace._trace.contextr   r0   Úddtrace._trace.providerr   ÚDDBaseContextProviderÚddtrace._trace.spanr   r/   Úddtrace.internal.loggerr   Úddtrace.tracer   rB   rD   Úlogr	   r#   r#   r#   r$   Ú<module>   s    