o
    0 iv#                     @   sr  d Z ddlZddlmZmZ ddlmZmZmZm	Z	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 e
e	e
e ef  ZG d	d
 d
eZG dd deZdd Zdeeef deeef fddZdeeef ddfddZdeeef ddfddZdeeef ddfddZdeeef defddZdeeef defddZ G dd dZ!G d d! d!eZ"dS )"ae  
This integration ingests tracing data from native extensions written in Rust.

Using it requires additional setup on the Rust side to accept a
`RustTracingLayer` Python object and register it with the `tracing-subscriber`
using an adapter from the `pyo3-python-tracing-subscriber` crate. For example:
```rust
#[pyfunction]
pub fn initialize_tracing(py_impl: Bound<'_, PyAny>) {
    tracing_subscriber::registry()
        .with(pyo3_python_tracing_subscriber::PythonCallbackLayerBridge::new(py_impl))
        .init();
}
```

Usage in Python would then look like:
```
sentry_sdk.init(
    dsn=sentry_dsn,
    integrations=[
        RustTracingIntegration(
            "demo_rust_extension",
            demo_rust_extension.initialize_tracing,
            event_type_mapping=event_type_mapping,
        )
    ],
)
```

Each native extension requires its own integration.
    N)Enumauto)AnyCallableDictTupleOptional)Integration)should_send_default_pii)Span)SENSITIVE_DATA_SUBSTITUTEc                   @   s    e Zd ZdZdZdZdZdZdS )RustTracingLevelTRACEDEBUGINFOWARNERRORN)__name__
__module____qualname__TraceDebugInfoWarnError r   r   p/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/integrations/rust_tracing.pyr   .   s    r   c                   @   s$   e Zd Ze Ze Ze Ze ZdS )EventTypeMappingN)r   r   r   r   IgnoreExc
BreadcrumbEventr   r   r   r   r   6   s
    
r   c                 C   sJ   t | } | t jt jfv rdS | t jkrdS | t jkrdS | t jkr#dS dS )Ndebuginfowarningerror)r   r   r   r   r   r   )levelr   r   r   tracing_level_to_sentry_level=   s   


r'   eventreturnc                 C   s   |  di }i }i }dD ]}||v r|| ||< qt|dkr#||d< i }| dg D ]	}|  |||< q+t|dkr?||d< |S )Nmetadata)module_pathfileliner   Zrust_tracing_locationfieldsZrust_tracing_fields)getlen)r(   r*   contextslocationfieldr.   r   r   r   extract_contextsM   s   r4   c                 C   sR   |  di }| d}t| d}|  d}t| }||||d}t| d S )Nr*   targetr&   message)loggerr&   r6   r1   )r/   r'   r4   
sentry_sdkZcapture_event)r(   r*   r7   r&   r6   r1   Zsentry_eventr   r   r   process_eventa   s   

r9   c                 C   s   t |  d S N)r9   )r(   r   r   r   process_exceptions   s   r;   c                 C   s2   t | di d}| d}tj||d d S )Nr*   r&   r6   )r&   r6   )r'   r/   r8   Zadd_breadcrumb)r(   r&   r6   r   r   r   process_breadcrumbw   s   
r<   r*   c                 C   s   t | dt jt jt jfv S Nr&   )r   r/   r   r   r   )r*   r   r   r   default_span_filter~   s
   r>   c                 C   sP   t | d}|t jkrtjS |t jt jfv rtjS |t jt j	fv r%tj
S tj
S r=   )r   r/   r   r   r   r   r   r    r   r   r   )r*   r&   r   r   r   default_event_type_mapping   s   
r?   c                
   @   s   e Zd Zeedfdedeeeef ge	f deeeef ge
f dee
 fddZde
fd	d
ZdededdfddZdededefddZdededdfddZdedededdfddZdS )RustTracingLayerNoriginevent_type_mappingspan_filterinclude_tracing_fieldsc                 C   s   || _ || _|| _|| _d S r:   )rA   rB   rC   rD   )selfrA   rB   rC   rD   r   r   r   __init__   s   	
zRustTracingLayer.__init__r)   c                 C   s   | j du rt S | j S )a9  
        By default, the values of tracing fields are not included in case they
        contain PII. A user may override that by passing `True` for the
        `include_tracing_fields` keyword argument of this integration or by
        setting `send_default_pii` to `True` in their Sentry client options.
        N)rD   r
   )rE   r   r   r   _include_tracing_fields   s
   
	z(RustTracingLayer._include_tracing_fieldsr(   _span_statec                 C   st   t |}|di }| |}|tjkrd S |tjkr"t| d S |tjkr-t	| d S |tj
kr8t| d S d S )Nr*   )jsonloadsr/   rB   r   r   r   r;   r    r<   r!   r9   )rE   r(   rH   Zdeserialized_eventr*   Z
event_typer   r   r   on_event   s   





zRustTracingLayer.on_eventattrsspan_idc                 C   s  t |}|di }| |sd S |d}|d}|d}|d ur(|}n|d ur8|d ur8| d| }n	|d ur?|}nd}d|| jd}t }	|	j}
|
rZ|
jd
i |}n|	j	d
i |}|d	g }|D ]}| 
 rz|||| qj||t qj||	_|
|fS )Nr*   r+   namer6   z::z	<unknown>function)oprN   rA   r.   r   )rI   rJ   r/   rC   rA   r8   get_current_scopespanZstart_childZ
start_spanrG   set_datar   )rE   rL   rM   r*   r+   rN   r6   Zsentry_span_namekwargsscopeparent_sentry_spansentry_spanr.   r3   r   r   r   on_new_span   s<   




zRustTracingLayer.on_new_span
span_statec                 C   s*   |d u rd S |\}}|   |t _d S r:   )finishr8   rQ   rR   )rE   rM   rY   rV   rW   r   r   r   on_close   s
   zRustTracingLayer.on_closevaluesc           	      C   sV   |d u rd S |\}}t |}| D ]\}}|  r"||| q||t qd S r:   )rI   rJ   itemsrG   rS   r   )	rE   rM   r\   rY   Z_parent_sentry_spanrW   Zdeserialized_valueskeyvaluer   r   r   	on_record   s   
zRustTracingLayer.on_record)r   r   r   r?   r>   strr   r   r   r   boolr   rF   rG   
TraceStaterK   rX   r[   r`   r   r   r   r   r@      s(    
+r@   c                   @   sv   e Zd ZdZeedfdedeegdf dee	ee
f gef dee	ee
f gef dee f
dd	ZedddZdS )RustTracingIntegrationa  
    Ingests tracing data from a Rust native extension's `tracing` instrumentation.

    If a project uses more than one Rust native extension, each one will need
    its own instance of `RustTracingIntegration` with an initializer function
    specific to that extension.

    Since all of the setup for this integration requires instance-specific state
    which is not available in `setup_once()`, setup instead happens in `__init__()`.
    N
identifierinitializerrB   rC   rD   c                 C   s.   || _ d| }t||||| _|| j d S )Nzauto.function.rust_tracing.)re   r@   Ztracing_layer)rE   re   rf   rB   rC   rD   rA   r   r   r   rF     s   

zRustTracingIntegration.__init__r)   c                   C   s   d S r:   r   r   r   r   r   
setup_once  s   z!RustTracingIntegration.setup_once)r)   N)r   r   r   __doc__r?   r>   ra   r   r@   r   r   r   rb   r   rF   staticmethodrg   r   r   r   r   rd      s(    
rd   )#rh   rI   enumr   r   typingr   r   r   r   r   r8   Zsentry_sdk.integrationsr	   Zsentry_sdk.scoper
   Zsentry_sdk.tracingr   Z
SentrySpanZsentry_sdk.utilsr   rc   r   r   r'   ra   r4   r9   r;   r<   rb   r>   r?   r@   rd   r   r   r   r   <module>   s*     "j