
    qPh}%                       d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZmZmZ d dlmZmZ d dlmZmZmZm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%mZ& erd dl'mZmZ dZ(dZ)e&jT                  ejT                  e&jV                  ejV                  e&jX                  ejX                  e&jZ                  ejZ                  e&j\                  ej\                  e&j^                  ej\                  iZ0 G d d      Z1y# e!$ r d
Z Y w xY w)    )annotations)contextmanager)Token)OptionalDictSequencecastCallableIteratorTYPE_CHECKING)contexttrace)SpanSpanKindLink
StatusCode)get_current_span)extractinject)"_SUPPRESS_HTTP_INSTRUMENTATION_KEYsuppress_http_instrumentation   )VERSION   )
Attributesr   )r   r   z'https://opentelemetry.io/schemas/1.23.1z
azure-corec                  N   e Zd ZdZddddd	 	 	 	 	 	 	 	 	 ddZej                  ddd	 	 	 	 	 	 	 	 	 ddZeej                  dddd	 	 	 	 	 	 	 	 	 	 	 dd	       Z	e
edd
dd              Zeddd       ZddZe
dd       Ze
dd       Ze
dd       Ze
dd       Ze
dd       Zy)OpenTelemetryTracera7  A tracer that uses OpenTelemetry to trace operations.

    :keyword library_name: The name of the library to use in the tracer.
    :paramtype library_name: str
    :keyword library_version: The version of the library to use in the tracer.
    :paramtype library_version: str
    :keyword schema_url: Specifies the Schema URL of the emitted spans. Defaults to
        "https://opentelemetry.io/schemas/1.23.1".
    :paramtype schema_url: str
    :keyword attributes: Attributes to add to the emitted spans.
    :paramtype attributes: Mapping[str, AttributeValue]
    N)library_namelibrary_version
schema_url
attributesc               p    t        j                  |xs t        |xs t        |xs t        |      | _        y )N)instrumenting_module_nameinstrumenting_library_versionr    r!   )r   
get_tracer_DEFAULT_MODULE_NAMEr   _DEFAULT_SCHEMA_URL_tracer)selfr   r   r    r!   s        n/var/www/dev.core.comfenalco.cic-ware.com/crm/lib/python3.12/site-packages/azure/core/tracing/opentelemetry.py__init__zOpenTelemetryTracer.__init__>   s3     ''&2&J6J*9*DW!8%8!	
    kindr!   linksc                   t         j                  |t        j                        }| j	                  |      }| j
                  j                  ||||d      }|S )a@  Starts a span without setting it as the current span in the context.

        :param name: The name of the span
        :type name: str
        :keyword kind: The kind of the span. INTERNAL by default.
        :paramtype kind: ~azure.core.tracing.SpanKind
        :keyword attributes: Attributes to add to the span.
        :paramtype attributes: Mapping[str, AttributeValue]
        :keyword links: Links to add to the span.
        :paramtype links: list[~azure.core.tracing.Link]
        :return: The span that was started
        :rtype: ~opentelemetry.trace.Span
        F)r.   r!   r/   record_exception)_KIND_MAPPINGSgetOpenTelemetrySpanKindINTERNAL_parse_linksr(   
start_span)r)   namer.   r!   r/   	otel_kind
otel_links	otel_spans           r*   r7   zOpenTelemetryTracer.start_spanM   s]    * #&&t-B-K-KL	&&u-
LL++!" , 
	 r,   T)r.   r!   r/   end_on_exitc             #     K   | j                  ||||      }t        j                  |d|      5 }| ddd       y# 1 sw Y   yxY ww)a  Context manager that starts a span and sets it as the current span in the context.

        .. code:: python

            with tracer.start_as_current_span("span_name") as span:
                # Do something with the span
                span.set_attribute("key", "value")

        :param name: The name of the span
        :type name: str
        :keyword kind: The kind of the span. INTERNAL by default.
        :paramtype kind: ~azure.core.tracing.SpanKind
        :keyword attributes: Attributes to add to the span.
        :paramtype attributes: Optional[Attributes]
        :keyword links: Links to add to the span.
        :paramtype links: Optional[Sequence[Link]]
        :keyword end_on_exit: Whether to end the span when exiting the context manager. Defaults to True.
        :paramtype end_on_exit: bool
        :return: The span that was started
        :rtype: Iterator[~opentelemetry.trace.Span]
        r-   Fr1   r<   N)r7   r   use_span)r)   r8   r.   r!   r/   r<   spans          r*   start_as_current_spanz)OpenTelemetryTracer.start_as_current_spano   sM     > t$:US^^5k
 	J	 	 	s   .A
>	A
AA
)r<   c             #  n   K   t        j                  |d|      5 }| ddd       y# 1 sw Y   yxY ww)a  Context manager that takes a non-active span and activates it in the current context.

        :param span: The span to set as the current span
        :type span: ~opentelemetry.trace.Span
        :keyword end_on_exit: Whether to end the span when exiting the context manager. Defaults to True.
        :paramtype end_on_exit: bool
        :return: The span that was activated.
        :rtype: Iterator[~opentelemetry.trace.Span]
        Fr>   N)r   r?   )clsr@   r<   active_spans       r*   r?   zOpenTelemetryTracer.use_span   s8      ^^5k
 		 	 	s   5)	525c                F    | j                  t        j                  |       y)a  Set the status of a span to ERROR with the provided description, if any.

        :param span: The span to set the ERROR status on.
        :type span: ~opentelemetry.trace.Span
        :param description: An optional description of the error.
        :type description: str
        )descriptionN)
set_statusr   ERROR)r@   rF   s     r*   set_span_error_statusz)OpenTelemetryTracer.set_span_error_status   s     	
((kBr,   c                   |sy 	 g }|D ]U  }t        |j                        }t        |      j                         }|j	                  t        ||j                               W |S # t        $ r t        t        t
           |      cY S w xY wN)
r   headersget_current_span_otelget_span_contextappendOpenTelemetryLinkr!   AttributeErrorr	   r   )r)   r/   r:   linkctxspan_ctxs         r*   r6   z OpenTelemetryTracer._parse_links   s    
	<J Pdll+05FFH!!"3Hdoo"NOP  	< !23U;;	<s   AA# # BBc                    t               S )z~Returns the current span in the context.

        :return: The current span
        :rtype: ~opentelemetry.trace.Span
        )rM   rC   s    r*   r   z$OpenTelemetryTracer.get_current_span   s     %&&r,   c                >    t        j                         fd}|S )zPasses the current spans to the new context the function will be run in.

        :param func: The function that will be run in the new context
        :type func: callable
        :return: The wrapped function
        :rtype: callable
        c                     d }	 t        j                        } | i ||t        j                  |       S S # |t        j                  |       w w xY wrK   )otel_context_moduleattachdetach)argskwargstokencurrent_contextfuncs      r*   call_with_current_contextzKOpenTelemetryTracer.with_current_context.<locals>.call_with_current_context   s]    E6+22?CT,V,$'..u5 %5$'..u5 %s	   : A)rY   get_current)rC   r`   ra   r_   s    ` @r*   with_current_contextz(OpenTelemetryTracer.with_current_context   s      .99;	6 )(r,   c                     i }t        |       |S )a  Returns the Trace Context header values associated with the current span.

        These are generally the W3C Trace Context headers (i.e. "traceparent" and "tracestate").

        :return: A key value pair dictionary
        :rtype: dict[str, str]
        )r   )rC   trace_contexts     r*   get_trace_contextz%OpenTelemetryTracer.get_trace_context   s     )+}r,   c                \    t        j                  t        j                  t        d            S )a  Enabled automatic HTTP instrumentation suppression.

        Since azure-core already instruments HTTP calls, we need to suppress any automatic HTTP
        instrumentation provided by other libraries to prevent duplicate spans. This has no effect if no
        automatic HTTP instrumentation libraries are being used.

        :return: A token that can be used to detach the suppression key from the context
        :rtype: ~contextvars.Token
        T)rY   rZ   	set_valuer   rV   s    r*   #_suppress_auto_http_instrumentationz7OpenTelemetryTracer._suppress_auto_http_instrumentation   s%     #))*=*G*GHjlp*qrrr,   c                .    t        j                  |       y)z|Detach a token from the context.

        :param token: The token to detach
        :type token: ~contextvars.Token
        N)rY   r[   )rC   r^   s     r*   _detach_from_contextz(OpenTelemetryTracer._detach_from_context   s     	""5)r,   )
r   Optional[str]r   rl   r    rl   r!   Optional[Attributes]returnNone)
r8   strr.   r   r!   rm   r/   Optional[Sequence[Link]]rn   r   )r8   rp   r.   r   r!   rm   r/   rq   r<   boolrn   Iterator[Span])r@   r   r<   rr   rn   rs   rK   )r@   r   rF   rl   rn   ro   )r/   rq   rn   z%Optional[Sequence[OpenTelemetryLink]])rn   r   )r`   r
   rn   r
   )rn   zDict[str, str])rn   r   )r^   r   rn   ro   )__name__
__module____qualname____doc__r+   	_SpanKindr5   r7   r   rA   classmethodr?   staticmethodrI   r6   r   rc   rf   ri   rk    r,   r*   r   r   0   s     '+)-$(+/
 $
 '	

 "
 )
 

& #+++/*.   	 
 )  (  
 D 
 #+++/*. "" 	"
 )" (" " 
" "H 9=    C C<  ' ' ) )* 
 
 
s 
s * *r,   r   N)2
__future__r   
contextlibr   contextvarsr   typingr   r   r   r	   r
   r   r   opentelemetryr   rY   r   opentelemetry.tracer   r   r4   r   rP   r   opentelemetry.trace.propagationr   rM   opentelemetry.propagater   r   opentelemetry.contextr   ImportError_versionr   _modelsr   rx   azure.core.tracingr'   r&   CLIENTCONSUMERPRODUCERSERVERr5   UNSPECIFIEDr2   r   r{   r,   r*   <module>r      s   
 # %  T T T ?  V 3IH 
 1 @ #  +22-66-66+22-66099P* P*5  I)H&Is   C; ;DD