o
    ail[                     @   s  d Z ddlZddlZddlmZmZ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mZmZ ddlmZ dd	lmZmZmZmZmZmZ e	rtd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$ edZ%edZ&dZ'dZ(G dd de)eZ*G dd de)eZ+e+j,gZ-e+j.e+j.e+j.e+j/e+j,e+j.e+j/e+j/e+j/d	Z0e1 Z2de2dfde)ddddde3ddf
dd Z4d2d#d$Z5d3d%d&Z6G d'd dZ7G d(d) d)e7Z8	d4dddd*d+d,dd-dd.de3dd/f
d0d1Z9dS )5z
EXPERIMENTAL. Do not use in production.

The API in this file is only meant to be used in span streaming mode.

You can enable span streaming mode via
sentry_sdk.init(_experiments={"trace_lifecycle": "stream"}).
    N)datetime	timedeltatimezone)Enum)TYPE_CHECKING)SPANDATA)get_profiler_id!try_autostart_continuous_profiler!try_profile_lifecycle_trace_start)Baggage)capture_internal_exceptionsformat_attributeget_current_thread_metaloggernanosecond_timeshould_be_treated_as_error)AnyCallableIteratorOptional	ParamSpecTypeVarUnion)
AttributesAttributeValue)ContinuousProfilePRbaggagezsentry-tracec                   @   s"   e Zd ZdZdZdefddZdS )
SpanStatusokerrorreturnc                 C      | j S Nvalueself r)   O/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/sentry_sdk/traces.py__str__2      zSpanStatus.__str__N)__name__
__module____qualname__OKERRORstrr+   r)   r)   r)   r*   r   .   s    r   c                   @   s2   e Zd ZdZdZdZdZdZdZde	fdd	Z
d
S )SegmentSource	componentcustomroutetaskurlviewr"   c                 C   r#   r$   r%   r'   r)   r)   r*   r+   @   r,   zSegmentSource.__str__N)r-   r.   r/   	COMPONENTCUSTOMROUTETASKURLVIEWr2   r+   r)   r)   r)   r*   r3   8   s    r3   )	endpointfunction_namehandler_namemethod_and_path_patternpath
route_nameroute_patternuri_templater8   Tname
attributesOptional[Attributes]parent_spanOptional[StreamedSpan]activer"   StreamedSpanc                 C   sB   ddl m} |t jstjddd t S t 	| |||S )a  
    Start a span.

    EXPERIMENTAL. Use sentry_sdk.start_transaction() and sentry_sdk.start_span()
    instead.

    The span's parent, unless provided explicitly via the `parent_span` argument,
    will be the current active span, if any. If there is none, this span will
    become the root of a new span tree. If you explicitly want this span to be
    top-level without a parent, set `parent_span=None`.

    `start_span()` can either be used as context manager or you can use the span
    object it returns and explicitly end it via `span.end()`. The following is
    equivalent:

    ```python
    import sentry_sdk

    with sentry_sdk.traces.start_span(name="My Span"):
        # do something

    # The span automatically finishes once the `with` block is exited
    ```

    ```python
    import sentry_sdk

    span = sentry_sdk.traces.start_span(name="My Span")
    # do something
    span.end()
    ```

    To continue a trace from another service, call
    `sentry_sdk.traces.continue_trace()` prior to creating a top-level span.

    :param name: The name to identify this span by.
    :type name: str

    :param attributes: Key-value attributes to set on the span from the start.
        These will also be accessible in the traces sampler.
    :type attributes: "Optional[Attributes]"

    :param parent_span: A span instance that the new span should consider its
        parent. If not provided, the parent will be set to the currently active
        span, if any. If set to `None`, this span will become a new root-level
        span.
    :type parent_span: "Optional[StreamedSpan]"

    :param active: Controls whether spans started while this span is running
        will automatically become its children. That's the default behavior. If
        you want to create a span that shouldn't have any children (unless
        provided explicitly via the `parent_span` argument), set this to `False`.
    :type active: bool

    :return: The span that has been started.
    :rtype: StreamedSpan
    r   )has_span_streaming_enabledz|Using span streaming API in non-span-streaming mode. Use sentry_sdk.start_transaction() and sentry_sdk.start_span() instead.   )
stacklevel)
sentry_sdk.tracing_utilsrO   
sentry_sdk
get_clientoptionswarningswarnNoOpStreamedSpanget_current_scopestart_streamed_span)rH   rI   rK   rM   rO   r)   r)   r*   
start_span\   s   ?r[   incomingdict[str, Any]c                 C   s    t  |  t  |  dS )a  
    Continue a trace from headers or environment variables.

    EXPERIMENTAL. Use sentry_sdk.continue_trace() instead.

    This function sets the propagation context on the scope. Any span started
    in the updated scope will belong under the trace extracted from the
    provided propagation headers or environment variables.

    continue_trace() doesn't start any spans on its own. Use the start_span()
    API for that.
    N)rS   get_isolation_scopegenerate_propagation_contextrY   )r\   r)   r)   r*   continue_trace   s   r`   c                   C   s   t    t    dS )a/  
    Resets the propagation context, forcing a new trace.

    EXPERIMENTAL.

    This function sets the propagation context on the scope. Any span started
    in the updated scope will start its own trace.

    new_trace() doesn't start any spans on its own. Use the start_span() API
    for that.
    N)rS   r^   set_new_propagation_contextrY   r)   r)   r)   r*   	new_trace   s   rb   c                   @   s  e Zd ZdZdZdddddddddd	deddd	ed
dddddddddddddddfddZdefddZdiddZ			 	!	 	"	 		djd#d$Z
dkdld'd(Zdkdld)d*Zdmd+d,Zdkdld-d.Zdnd0d1Zd2ed!d3ddfd4d5Zdod6d7Zd2eddfd8d9Zedpd;d<Zejdqd?d<Zedefd@dAZejdeddfdBdAZedefdCdDZedefdEdFZedefdGdHZedrdIdJZedsdLdMZedsdNdOZdefdPdQZdmdRdSZdtdUdVZdefdWdXZ dudYdZZ!dvd\d]Z"dwd_d`Z#dxdbdcZ$dydedfZ%dmdgdhZ&dS )zrN   a  
    A span holds timing information of a block of code.

    Spans can have multiple child spans, thus forming a span tree.

    This is the Span First span implementation that streams spans. The original
    transaction-based span implementation lives in tracing.Span.
    )_name_attributes_active_span_id	_trace_id_parent_span_id_segment_parent_sampled_start_timestamp_start_timestamp_monotonic_ns
_timestamp_status_scope_previous_span_on_scope_baggage_sample_rand_sample_rate_continuous_profileNT)	rI   rM   segmenttrace_idparent_span_idparent_sampledr   sample_ratesample_randrH   rI   rJ   rM   scopezsentry_sdk.Scoperu   rL   rv   Optional[str]rw   rx   Optional[bool]r   Optional[Baggage]ry   zOptional[float]rz   c                C   s   || _ || _i | _|r| D ]
\}}| || q|| _|p | | _|| _|| _|| _	|	| _
|| _|
| _ttj| _d | _zt | _W n	 tyM   Y nw d | _tjj| _| dtjj |   d | _|   |  t!  | "  d S )Nzsentry.span.source)#rc   re   rd   itemsset_attributero   ri   rg   rh   rj   rq   rr   rs   r   nowr   utcrk   rm   r   rl   AttributeErrorrf   r   r0   r&   rn   r3   r;   _update_active_threadrt   _start_profile_set_profile_idr   _start)r(   rH   rI   rM   r{   ru   rv   rw   rx   r   ry   rz   	attributer&   r)   r)   r*   __init__   s:   

zStreamedSpan.__init__r"   c                 C   s8   d| j j d| j d| j d| j d| j d| j dS )N<z(name=z, trace_id=z
, span_id=z, parent_span_id=z	, active=)>)	__class__r-   rc   rv   span_idrh   re   r'   r)   r)   r*   __repr__,  s   zStreamedSpan.__repr__c                 C      | S r$   r)   r'   r)   r)   r*   	__enter__6     zStreamedSpan.__enter__tyOptional[Any]r&   tbc                 C   s6   | j d urd S |d urt||rtjj| _|   d S r$   )rm   r   r   r1   r&   status_endr(   r   r&   r   r)   r)   r*   __exit__9  s
   

zStreamedSpan.__exit__end_timestamp Optional[Union[float, datetime]]c                 C   s   |  | dS )z
        Finish this span and queue it for sending.

        :param end_timestamp: End timestamp to use instead of current time.
        :type end_timestamp: "Optional[Union[float, datetime]]"
        Nr   r(   r   r)   r)   r*   endE  s   zStreamedSpan.endc                 C   s   t jddtd | | d S Nz4span.finish() is deprecated. Use span.end() instead.rP   )rQ   category)rV   rW   DeprecationWarningr   r   r)   r)   r*   finishN  s   zStreamedSpan.finishc                 C   s$   | j r| jj}| | j_|| _d S d S r$   )re   ro   spanrp   r(   old_spanr)   r)   r*   r   W  s
   
zStreamedSpan._startc                 C   s|  | j d urd S |  r(| jd ur(t  | j  W d    n1 s#w   Y  | jrGt  | j}| `|| j_W d    n1 sBw   Y  | 	d| j
j | 	d| j
j |d urt|ttfruz	t|tj}W n	 tyt   Y nw t|tr~|| _ ntd | j d u rzt | j }| jt|d d | _ W n ty   ttj| _ Y nw t }| sd S | j |  d S )Nzsentry.segment.idzsentry.segment.namezB[Tracing] Failed to set end_timestamp. Using current time instead.i  )microseconds)!rm   _is_segmentrt   r   stopre   rp   ro   r   r   ri   r   rH   
isinstancefloatintr   fromtimestampr   r   	Exceptionr   debugr   rl   rk   r   r   r   rS   rT   	is_active_capture_span)r(   r   r   elapsedclientr)   r)   r*   r   ]  sN   



zStreamedSpan._endr   c                 C   r#   r$   )rd   r'   r)   r)   r*   get_attributes  r,   zStreamedSpan.get_attributeskeyr   c                 C   s   t || j|< d S r$   )r   rd   r(   r   r&   r)   r)   r*   r     s   zStreamedSpan.set_attributec                 C   s"   |  D ]
\}}| || qd S r$   )r   r   )r(   rI   r   r&   r)   r)   r*   set_attributes  s   zStreamedSpan.set_attributesc                 C   s$   z| j |= W d S  ty   Y d S w r$   )rd   KeyErrorr(   r   r)   r)   r*   remove_attribute  s
   zStreamedSpan.remove_attributer2   c                 C   r#   r$   )rn   r'   r)   r)   r*   r        zStreamedSpan.statusr   Union[SpanStatus, str]c                 C   sB   t |tr|j}|dd tD vrtd| d d S || _d S )Nc                 S   s   h | ]}|j qS r)   r%   ).0er)   r)   r*   	<setcomp>  s    z&StreamedSpan.status.<locals>.<setcomp>z"[Tracing] Unsupported span status z . Expected one of: "ok", "error")r   r   r&   r   r   r   rn   r(   r   r)   r)   r*   r     s   


c                 C   r#   r$   rc   r'   r)   r)   r*   rH     r   zStreamedSpan.namec                 C   s
   || _ d S r$   r   )r(   rH   r)   r)   r*   rH     s   
c                 C   r#   r$   )re   r'   r)   r)   r*   rM     r   zStreamedSpan.activec                 C   s    | j st jdd  | _ | j S )N   )rf   uuiduuid4hexr'   r)   r)   r*   r     s   zStreamedSpan.span_idc                 C   s   | j s	t j| _ | j S r$   )rg   r   r   r   r'   r)   r)   r*   rv     s   zStreamedSpan.trace_idc                 C      dS NTr)   r'   r)   r)   r*   sampled     zStreamedSpan.sampledOptional[datetime]c                 C   r#   r$   )rk   r'   r)   r)   r*   start_timestamp  r   zStreamedSpan.start_timestampc                 C   r#   r$   )rm   r'   r)   r)   r*   	timestamp  r   zStreamedSpan.timestampc                 C   s
   | j | u S r$   )ri   r'   r)   r)   r*   r        
zStreamedSpan._is_segmentc                 C   sF   t  \}}|d ur| tjt| |d ur!| tj| d S d S d S r$   )r   r   r   	THREAD_IDr2   THREAD_NAME)r(   	thread_idthread_namer)   r)   r*   r     s   
z"StreamedSpan._update_active_threaddict[str, str]c                 C   s   | j   S r$   )ri   _get_baggagedynamic_sampling_contextr'   r)   r)   r*   _dynamic_sampling_context  s   z&StreamedSpan._dynamic_sampling_contextc                 C   sN   | j du rd}n
| j du rd}nd }d| j| jf }|d ur%|d|f 7 }|S )NT1F0z%s-%sz-%s)r   rv   r   )r(   r   traceparentr)   r)   r*   _to_traceparent  s   

zStreamedSpan._to_traceparentc                 C   s   | j r| j  S d S r$   )ri   r   r'   r)   r)   r*   _to_baggage  s   
zStreamedSpan._to_baggager   c                 C   s    | j r| j jrt| | _ | j S )z
        Return the :py:class:`~sentry_sdk.tracing_utils.Baggage` associated with
        the segment.

        The first time a new baggage with Sentry items is made, it will be frozen.
        )rq   mutabler   populate_from_segmentr'   r)   r)   r*   r     s   zStreamedSpan._get_baggageIterator[tuple[str, str]]c                 c   s>    | j sd S t|  fV  | j   }|rt|fV  d S d S r$   )ri   SENTRY_TRACE_HEADER_NAMEr   r   	serializeBAGGAGE_HEADER_NAME)r(   r   r)   r)   r*   _iter_headers  s   zStreamedSpan._iter_headersr]   c                 C   sL   | j | j| j|  d}d| jv r| jd |d< d| jv r$| jd |d< |S )N)rv   r   rw   r   z	sentry.opopzsentry.originorigin)rv   r   rh   r   rd   )r(   contextr)   r)   r*   _get_trace_context  s   

zStreamedSpan._get_trace_contextprofiler_idc                 C   s   |d ur|  d| d S d S )Nzsentry.profiler_id)r   )r(   r   r)   r)   r*   r   #  s   zStreamedSpan._set_profile_idc                 C   s   |   sd S t  t | _d S r$   )r   r	   r
   rt   r'   r)   r)   r*   r   '  s   zStreamedSpan._start_profile)r"   rN   r   r   r&   r   r   r   r"   Nr$   r   r   r"   Nr"   Nr"   r   rI   r   r"   Nr"   r2   r   r   r"   Nr"   r}   r"   r   )r"   r   )r"   r~   )r"   r   )r"   r   )r"   r]   )r   r|   r"   N)'r-   r.   r/   __doc__	__slots__r2   boolr   r   r   r   r   r   r   r   r   r   r   r   propertyr   setterrH   rM   r   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   rN      s    		

8


	
	
6


	





c                   @   s|  e Zd ZdZ		dC					dDdd	Zdefd
dZdEddZ								dFddZdGddZ	dHdIddZ
dHdIddZdHdIddZdJdd Zd!edd"ddfd#d$ZdKd&d'Zd!eddfd(d)Zdefd*d+ZedLd-d.ZejdMd1d.Zedefd2d3Zejdeddfd4d3Zedefd5d6Zedefd7d8Zedefd9d:ZedNd<d=ZedOd?d@ZedOdAdBZdS )PrX   )	_finished_unsampled_reasonNunsampled_reasonr|   r{   Optional[sentry_sdk.Scope]r"   c                 C   s   || _ || _d| _|   d S NF)ro   r   r   r   )r(   r   r{   r)   r)   r*   r   6  s   zNoOpStreamedSpan.__init__c                 C   s   d| j j d| j dS )Nr   z	(sampled=r   )r   r-   r   r'   r)   r)   r*   r   B  s   zNoOpStreamedSpan.__repr__c                 C   r   r$   r)   r'   r)   r)   r*   r   E  r   zNoOpStreamedSpan.__enter__r   r   r&   r   c                 C      |    d S r$   r   r   r)   r)   r*   r   H  s   zNoOpStreamedSpan.__exit__c                 C   s(   | j d u rd S | j j}| | j _|| _d S r$   )ro   r   rp   r   r)   r)   r*   r   M  s
   

zNoOpStreamedSpan._startr   r   c                 C   s   | j rd S | jd ur)t }| r)|jr)td| j d |jj| jddd | j	rMt
| drMt  | j}| `|| j	_W d    n1 sHw   Y  d| _ d S )Nz9[Tracing] Discarding span because sampled=False (reason: )r      )reasondata_categoryquantityrp   T)r   r   rS   rT   r   	transportr   r   record_lost_eventro   hasattrr   rp   r   )r(   r   r   r   r)   r)   r*   r   U  s(   


zNoOpStreamedSpan._endc                 C   r   r$   r   r   r)   r)   r*   r   m  s   zNoOpStreamedSpan.endc                 C   s   t jddtd |   d S r   )rV   rW   r   r   r   r)   r)   r*   r   p  s   zNoOpStreamedSpan.finishr   c                 C   s   i S r$   r)   r'   r)   r)   r*   r   y  r   zNoOpStreamedSpan.get_attributesr   r   c                 C      d S r$   r)   r   r)   r)   r*   r   |  r   zNoOpStreamedSpan.set_attributerI   c                 C   r  r$   r)   )r(   rI   r)   r)   r*   r     r   zNoOpStreamedSpan.set_attributesc                 C   r  r$   r)   r   r)   r)   r*   r     r   z!NoOpStreamedSpan.remove_attributec                 C   s
   | j d uS r$   )ro   r'   r)   r)   r*   r     r   zNoOpStreamedSpan._is_segmentr2   c                 C   s   t jjS r$   )r   r0   r&   r'   r)   r)   r*   r     s   zNoOpStreamedSpan.statusr   r   c                 C   r  r$   r)   r   r)   r)   r*   r     r   c                 C   r   )N r)   r'   r)   r)   r*   rH     r   zNoOpStreamedSpan.namec                 C   r  r$   r)   )r(   r&   r)   r)   r*   rH     r   c                 C   r   r   r)   r'   r)   r)   r*   rM     r   zNoOpStreamedSpan.activec                 C   r   )N0000000000000000r)   r'   r)   r)   r*   r     r   zNoOpStreamedSpan.span_idc                 C   r   )N 00000000000000000000000000000000r)   r'   r)   r)   r*   rv     r   zNoOpStreamedSpan.trace_idr}   c                 C   r   r   r)   r'   r)   r)   r*   r     r   zNoOpStreamedSpan.sampledr   c                 C   r  r$   r)   r'   r)   r)   r*   r     r   z NoOpStreamedSpan.start_timestampc                 C   r  r$   r)   r'   r)   r)   r*   r     r   zNoOpStreamedSpan.timestamp)NN)r   r|   r{   r   r"   N)r"   rX   r   r   r$   r   r   r   r   r   r   r   )r-   r.   r/   r   r   r2   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rH   rM   r   rv   r   r   r   r)   r)   r)   r*   rX   0  sf    




	
rX   rH   rI   rM   funczOptional[Callable[P, R]]r|   zOptional[dict[str, Any]]zAUnion[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]c                C   s*   ddl m} ||||d}| r|| S |S )a  
    Decorator to start a span around a function call.

    EXPERIMENTAL. Use @sentry_sdk.trace instead.

    This decorator automatically creates a new span when the decorated function
    is called, and finishes the span when the function returns or raises an exception.

    :param func: The function to trace. When used as a decorator without parentheses,
        this is the function being decorated. When used with parameters (e.g.,
        ``@trace(op="custom")``, this should be None.
    :type func: Callable or None

    :param name: The human-readable name/description for the span. If not provided,
        defaults to the function name. This provides more specific details about
        what the span represents (e.g., "GET /api/users", "process_user_data").
    :type name: str or None

    :param attributes: A dictionary of key-value pairs to add as attributes to the span.
        Attribute values must be strings, integers, floats, or booleans. These
        attributes provide additional context about the span's execution.
    :type attributes: dict[str, Any] or None

    :param active: Controls whether spans started while this span is running
        will automatically become its children. That's the default behavior. If
        you want to create a span that shouldn't have any children (unless
        provided explicitly via the `parent_span` argument), set this to False.
    :type active: bool

    :returns: When used as ``@trace``, returns the decorated function. When used as
        ``@trace(...)`` with parameters, returns a decorator function.
    :rtype: Callable or decorator function

    Example::

        import sentry_sdk

        # Simple usage with default values
        @sentry_sdk.trace
        def process_data():
            # Function implementation
            pass

        # With custom parameters
        @sentry_sdk.trace(
            name="Get user data",
            attributes={"postgres": True}
        )
        def make_db_query(sql):
            # Function implementation
            pass
    r   )create_streaming_span_decoratorr  )rR   r  )r  rH   rI   rM   r  	decoratorr)   r)   r*   trace  s   ;r	  )r\   r]   r"   Nr   r$   ):r   r   rV   r   r   r   enumr   typingr   rS   sentry_sdk.constsr   'sentry_sdk.profiler.continuous_profilerr   r	   r
   rR   r   sentry_sdk.utilsr   r   r   r   r   r   r   r   r   r   r   r   r   sentry_sdk._typesr   r   r   r   r   r   r   r2   r   r3   r>   LOW_QUALITY_SEGMENT_SOURCESr:   r<   SOURCE_FOR_STYLEobject_DEFAULT_PARENT_SPANr   r[   r`   rb   rN   rX   r	  r)   r)   r)   r*   <module>   s    	 	$


O
  ] 