
    uYfz                        d dl Z 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 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 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 ddlmZ ddlmZ erd dlmZ eee f   Z! G d de
      Z	y)    N)TYPE_CHECKING)Any)Dict)Optional)Text)Union)Span)tags)Context)	ERROR_MSG)ERROR_STACK)
ERROR_TYPE)NumericType)SPAN_API_OPENTRACING   )SpanContext)Tags)Tracerc                   x     e Zd ZdZ fdZddZd Zd Zd ZddZ	d Z
d	 Zd
 Zd Zd Zd Zed        Z xZS )r	   z3Datadog implementation of :class:`opentracing.Span`c                    |"t        |j                  |j                        }n
t               }t        t        |   ||       d| _        t        j                         | _	        t        ||j                  t              | _        y )N)	ddcontextbaggageF)contextspan_api)r   _dd_contextr   superr	   __init__finished	threadingLock_lockDatadogSpanr   _dd_span)selftracerr   operation_name	__class__s       W/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/ddtrace/opentracer/span.pyr   zSpan.__init__"   sc    !G,?,?YG!mGdD"673^^%
#NG<O<OZno    c                 b    | j                   ry| j                  j                  |       d| _         y)zFinish the span.

        This calls finish on the ddspan.

        :param finish_time: specify a custom finish time with a unix timestamp
            per time.time()
        :type timestamp: float
        NT)r   r#   finish)r$   finish_times     r(   r+   zSpan.finish0   s)     == 	[)r)   c                     | j                   j                  ||      }| j                  5  || _        ddd       | S # 1 sw Y   | S xY w)aO  Sets a baggage item in the span context of this span.

        Baggage is used to propagate state between spans.

        :param key: baggage item key
        :type key: str

        :param value: baggage item value
        :type value: a type that can be str'd

        :rtype: Span
        :return: itself for chaining calls
        N)r   with_baggage_itemr!   _context)r$   keyvaluenew_ctxs       r(   set_baggage_itemzSpan.set_baggage_itemA   sB     ,,00e<ZZ 	$#DM	$	$s	   ;Ac                 8    | j                   j                  |      S )zGets a baggage item from the span context of this span.

        :param key: baggage item key
        :type key: str

        :rtype: str
        :return: the baggage value for the given key or ``None``.
        )r   get_baggage_itemr$   r0   s     r(   r5   zSpan.get_baggage_itemU   s     ||,,S11r)   c                 (    || j                   _        | S )zSet the operation name.)r#   name)r$   r&   s     r(   set_operation_namezSpan.set_operation_namea   s     ,r)   c                 D   |j                         D ]  \  }}|dk(  r)|dk(  r$d| j                  _        | j                  dd       4|dk(  s|dk(  r| j                  t        |       U|dk(  r| j                  t
        |       q|dk(  r| j                  t        |        | S )a~  Add a log record to this span.

        Passes on relevant opentracing key values onto the datadog span.

        :param key_values: a dict of string keys and values of any type
        :type key_values: dict

        :param timestamp: a unix timestamp per time.time()
        :type timestamp: float

        :return: the span itself, for call chaining
        :rtype: Span
        eventerrorr   zerror.objectmessagestack)itemsr#   r<   set_tagr   r   r   )r$   
key_values	timestampr0   vals        r(   log_kvzSpan.log_kvg   s    $ #((* 	HCg~#.&'#Wa(3.#8Z-	!Y,[#.	 r)   c                    |t         j                  k(  r|| j                  _        | S |t         j                  k(  r|| j                  _        | S |t         j                  k(  s|t        j                  k(  r|| j                  _	        | S |t        j                  k(  r,| j                  j                  t         j                  |       | S |t        j                  k(  r,| j                  j                  t         j                  |       | S |t         j                   k(  r|| j                  j"                  _        | S | j                  j                  ||       | S )zZSet a tag on the span.

        This sets the tag on the underlying datadog span.
        )r   	SPAN_TYPEr#   	span_typeSERVICE_NAMEserviceRESOURCE_NAMEOTTagsDATABASE_STATEMENTresourcePEER_HOSTNAMEset_tag_strTARGET_HOST	PEER_PORTr@   TARGET_PORTSAMPLING_PRIORITYr   sampling_priority)r$   r0   r1   s      r(   r@   zSpan.set_tag   s    $.. &+DMM#  D%%%$)DMM!  D&&&#1J1J*J%*DMM"  F(((MM%%d&6&6>  F$$$MM!!$"2"2E:
 	 D***6;DMM!!3  MM!!#u-r)   c                 8    | j                   j                  |      S )zkGets a tag from the span.

        This method retrieves the tag from the underlying datadog span.
        )r#   get_tagr6   s     r(   _get_tagzSpan._get_tag   s     }}$$S))r)   c                 8    | j                   j                  |      S )zqGets a metric from the span.

        This method retrieves the metric from the underlying datadog span.
        )r#   
get_metricr6   s     r(   _get_metriczSpan._get_metric   s     }}'',,r)   c                     | S N r$   s    r(   	__enter__zSpan.__enter__   s    r)   c                     |r| j                   j                  |||       | j                   j                  |||       | j                          y r\   )r#   set_exc_info__exit__r+   )r$   exc_typeexc_valexc_tbs       r(   rb   zSpan.__exit__   s<    MM&&x&A 	x&9r)   c                 H    || _         |j                  | j                  _        y)z$Associates a DD span with this span.N)r#   r   r   )r$   ddspans     r(   _associate_dd_spanzSpan._associate_dd_span   s     #)>> r)   c                 .    | j                   j                  S r\   )r#   r   r^   s    r(   r   zSpan._dd_context   s     }}$$$r)   r\   )__name__
__module____qualname____doc__r   r+   r3   r5   r9   rD   r@   rW   rZ   r_   rb   rh   propertyr   __classcell__)r'   s   @r(   r	   r	      sX    =p"(
2 D,*-2 % %r)   r	   )"r   typingr   r   r   r   r   r   opentracingr	   OpenTracingSpanopentracing.extr
   rK   ddtrace._trace.contextr   DatadogContextddtrace._trace.spanr"   ddtrace.constantsr   r   r   ddtrace.internal.compatr   ddtrace.internal.constantsr   span_contextr   r   ddtrace._trace.tracerr   bytes_TagNameTyper]   r)   r(   <module>r~      sa            / * < 3 ' ) ( / ; %  , T5[!f%? f%r)   