
    uYf7?                     N   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Zd dlmZ d dlmZ d d	lm	Z	 d d
l
mZ 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'  ee(      Z)e!jT                  de!jV                  de!jX                  de!jZ                  de!j\                  de!j^                  i e!j`                  de!jb                  de!jd                  de!jf                  dg ii
Z4 G d dej                        Zy)    )Any)Dict)List)Optional)UnionN)Format)Scope)ScopeManager)ThreadLocalScopeManager)Tracer)Context)Span)SPAN_API_OPENTRACING)get_application_name)ConfigException   )
get_logger   )HTTPPropagator)
ConfigKeys)config_invalid_keys)SpanContext)&get_context_provider_for_scope_managerFFILTERSc                   z    e Zd ZdZ	 	 	 	 ddZed        Z	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZed        Z	d Z
d	 Zd
 Zy)r   z>A wrapper providing an OpenTracing API for the Datadog tracer.Nc                    t         j                         | _        || j                  j                  |       |xs
 t	               | _        | j                  j                  t        j                        | _	        | j                  rBt        | j                        }|r+dj                  |      }t        dj                  |            | j
                  st        d      |xs
 t               | _        t!        | j                        }|xs t"        j$                  xs
 t'               | _        | j(                  j+                  | j                  j                  t        j,                               | j(                  j/                  | j                  j                  t        j0                        | j                  j                  t        j2                        | j                  j                  t        j4                        | j                  j                  t        j6                        | j                  j                  t        j8                        | j                  j                  t        j:                        | j                  j                  t        j<                        | j                  j                  t        j>                        |	       t@        jB                  tD        t@        jF                  tD        i| _$        y)a:  Initialize a new Datadog opentracer.

        :param service_name: (optional) the name of the service that this
            tracer will be used with. Note if not provided, a service name will
            try to be determined based off of ``sys.argv``. If this fails a
            :class:`ddtrace.settings.ConfigException` will be raised.
        :param config: (optional) a configuration object to specify additional
            options. See the documentation for further information.
        :param scope_manager: (optional) the scope manager for this tracer to
            use. The available managers are listed in the Python OpenTracing repo
            here: https://github.com/opentracing/opentracing-python#scope-managers.
            If ``None`` is provided, defaults to
            :class:`opentracing.scope_managers.ThreadLocalScopeManager`.
        :param dd_tracer: (optional) the Datadog tracer for this tracer to use. This
            should only be passed if a custom Datadog tracer is being used. Defaults
            to the global ``ddtrace.tracer`` tracer.
        N,zinvalid key(s) given ({})z Cannot detect the 'service_name'.
                                      Please set the 'service_name='
                                      keyword argument.
                                  )	enabledhostnamehttpsportsamplersettingspriority_samplinguds_pathcontext_provider)%DEFAULT_CONFIGcopy_configupdater   _service_namegetkeysDEBUG_debugr   joinr   formatr   _scope_managerr   ddtracetracerDatadogTracer
_dd_tracerset_tagsGLOBAL_TAGS	configureENABLEDAGENT_HOSTNAMEAGENT_HTTPS
AGENT_PORTSAMPLERSETTINGSPRIORITY_SAMPLINGUDS_PATHr   HTTP_HEADERSr   TEXT_MAP_propagators)selfservice_nameconfigscope_manager	dd_tracerinvalid_keysstr_invalid_keysdd_context_providers           Y/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/ddtrace/opentracer/tracer.py__init__zTracer.__init__3   s   4 &**,LL')C-A-Cll&&tzz2;;.t||<L#&88L#9 %&A&H&HIY&Z[[!!!&  ,H/F/HDTEXEXY#Hw~~H  !1!1$2B2B!CD!!LL$$T\\2\\%%d&9&9:,,""4#3#34!!$//2LL$$T\\2\\%%dmm4"ll..t/E/EF\\%%dmm40 	" 
	
 OO^
    c                     | j                   S )z4Returns the scope manager being used by this tracer.)r2   rE   s    rM   rH   zTracer.scope_managerx   s     """rO   c                     | j                  ||||||      }| j                  j                  ||      }	| j                  j                  j                  |j
                         |	S )a  Returns a newly started and activated `Scope`.
        The returned `Scope` supports with-statement contexts. For example::

            with tracer.start_active_span('...') as scope:
                scope.span.set_tag('http.method', 'GET')
                do_some_work()
            # Span.finish() is called as part of Scope deactivation through
            # the with statement.

        It's also possible to not finish the `Span` when the `Scope` context
        expires::

            with tracer.start_active_span('...',
                                          finish_on_close=False) as scope:
                scope.span.set_tag('http.method', 'GET')
                do_some_work()
            # Span.finish() is not called as part of Scope deactivation as
            # `finish_on_close` is `False`.

        :param operation_name: name of the operation represented by the new
            span from the perspective of the current service.
        :param child_of: (optional) a Span or SpanContext instance representing
            the parent in a REFERENCE_CHILD_OF Reference. If specified, the
            `references` parameter must be omitted.
        :param references: (optional) a list of Reference objects that identify
            one or more parent SpanContexts. (See the Reference documentation
            for detail).
        :param tags: an optional dictionary of Span Tags. The caller gives up
            ownership of that dictionary, because the Tracer may use it as-is
            to avoid extra data copying.
        :param start_time: an explicit Span start time as a unix timestamp per
            time.time().
        :param ignore_active_span: (optional) an explicit flag that ignores
            the current active `Scope` and creates a root `Span`.
        :param finish_on_close: whether span should automatically be finished
            when `Scope.close()` is called.
        :return: a `Scope`, already registered via the `ScopeManager`.
        )operation_namechild_of
referencestags
start_timeignore_active_span)
start_spanr2   activater6   r&   _dd_span)
rE   rS   rT   rU   rV   rW   rX   finish_on_closeotspanscopes
             rM   start_active_spanzTracer.start_active_span~   se    b )!!1 ! 
 ##,,V_E((11&//BrO   c                 <   d}d}d}	||}n!|rt        |t              r|d   j                  }||s| j                  j                  }
t        |
dd      }t        |dd      }| j                  j                  j	                         }|r;t        |t              r+|j                  }|j                  |j                  k\  r|}	n^|}	n[|}	nX|)t        |t              r|j                  }|j                  }	n-|t        |t              r|j                  }	n|nt        d      | j                  j!                  ||	| j"                  dt$              }|xs |j&                  |_        t        | ||      }|j)                  |       ||D ]  }|j+                  |||           |S )a  Starts and returns a new Span representing a unit of work.

        Starting a root Span (a Span with no causal references)::

            tracer.start_span('...')

        Starting a child Span (see also start_child_span())::

            tracer.start_span(
                '...',
                child_of=parent_span)

        Starting a child Span in a more verbose way::

            tracer.start_span(
                '...',
                references=[opentracing.child_of(parent_span)])

        Note: the precedence when defining a relationship is the following, from highest to lowest:
        1. *child_of*
        2. *references*
        3. `scope_manager.active` (unless *ignore_active_span* is True)
        4. None

        Currently Datadog only supports `child_of` references.

        :param operation_name: name of the operation represented by the new
            span from the perspective of the current service.
        :param child_of: (optional) a Span or SpanContext instance representing
            the parent in a REFERENCE_CHILD_OF Reference. If specified, the
            `references` parameter must be omitted.
        :param references: (optional) a list of Reference objects that identify
            one or more parent SpanContexts. (See the Reference documentation
            for detail)
        :param tags: an optional dictionary of Span Tags. The caller gives up
            ownership of that dictionary, because the Tracer may use it as-is
            to avoid extra data copying.
        :param start_time: an explicit Span start time as a unix timestamp per
            time.time()
        :param ignore_active_span: an explicit flag that ignores the current
            active `Scope` and creates a root `Span`.
        :return: an already-started Span instance.
        Nr   spancontextz invalid span configuration givenF)namerT   servicerZ   span_api)
isinstancelistreferenced_contextr2   activegetattrr6   r&   DatadogSpanr[   start_nsr   rb   r   _dd_context	TypeErrorrY   r+   r   start_associate_dd_spanset_tag)rE   rS   rT   rU   rV   rW   rX   	ot_parentot_parent_context	dd_parentr^   parent_spanactive_dd_parentdd_parent_spanddspanr]   ks                    rM   rY   zTracer.start_span   s   j 	 	 IJz48"188I %7''..E!%6K 'Y E  $??FFHz*:KH!,!5!5#,,0G0GG 0I .I,	"z)T'B ) 1 1!**I"z)['I!--I >?? ++&&) , 
 "1V\\d-~>!!&) + q$q'*+
 rO   c                     | j                   j                  }|r|j                  S | j                  j	                         }d}|r(t        | d|j                        }|j                  |       |S )zRetrieves the active span from the opentracing scope manager

        Falls back to using the datadog active span if one is not found. This
        allows opentracing users to use datadog instrumentation.
        N)r2   ri   ra   r6   current_spanr   rc   rp   )rE   r^   dd_spanot_spans       rM   active_spanzTracer.active_span<  sa     ##**::oo224GGtT7<<8**73NrO   c                     | j                   j                  |d      }|t        j                  |j	                  ||       y)zInjects a span context into a carrier.

        :param span_context: span context to inject.
        :param format: format to encode the span context with.
        :param carrier: the carrier of the encoded span context.
        N)rD   r,   opentracingUnsupportedFormatExceptioninject)rE   span_contextr1   carrier
propagators        rM   r   zTracer.injectO  s>     &&**648
888,0rO   c                     | j                   j                  |d      }|t        j                  |j	                  |      }|j
                  }| j                  j                  j                  |       |S )zExtracts a span context from a carrier.

        :param format: format that the carrier is encoded with.
        :param carrier: the carrier to extract from.
        N)	rD   r,   r   r   extractrm   r6   r&   rZ   )rE   r1   r   r   ot_span_ctxdd_span_ctxs         rM   r   zTracer.extract^  si     &&**648
888 !((1!--((11+>rO   c                 6    | j                   j                         S )a  Retrieves the data used to correlate a log with the current active trace.
        Generates a dictionary for custom logging instrumentation including the trace id and
        span id of the current active span, as well as the configured service, version, and environment names.
        If there is no active span, a dictionary with an empty string for each value will be returned.
        )r6   get_log_correlation_contextrQ   s    rM   r   z"Tracer.get_log_correlation_contextq  s     ::<<rO   )NNNN)NNNNFT)NNNNNF)__name__
__module____qualname____doc__rN   propertyrH   r_   rY   r~   r   r   r    rO   rM   r   r   0   s    H C
J # #  =B  }~  $1&=rO   r   )5typingr   r   r   r   r   r   r   r	   r
   opentracing.scope_managersr   r3   r   r5   ddtrace._trace.contextr   DatadogContextddtrace._trace.spanr   rk   ddtrace.internal.constantsr   ddtrace.internal.utils.configr   ddtrace.settingsr   internal.loggerr   propagationr   r#   r   r-   r   ra   r   r   utilsr   r   logr;   r<   r=   r.   r:   r8   r>   r@   rA   r?   r'   r   rO   rM   <module>r      s            $ >  + < 3 ; > , ( ' ( )  % 9  	dOOTJJLL$bLL$DMM4MM2 H=[ H=rO   