
    \j!                        d Z ddlZddlmZmZ ddl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  G d	 d
e      Z G d de      ZdeddfdZddZddZddZddZdddefdZddZ G d d      Z G d de      Z y) 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Optional)Integration)should_send_default_pii)Span)SENSITIVE_DATA_SUBSTITUTEc                        e Zd ZdZdZdZdZdZy)RustTracingLevelTRACEDEBUGINFOWARNERRORN)__name__
__module____qualname__TraceDebugInfoWarnError     N/root/env/lib/python3.12/site-packages/sentry_sdk/integrations/rust_tracing.pyr   r   ,   s    EEDDEr   r   c                   D    e Zd Z e       Z e       Z e       Z e       Zy)EventTypeMappingN)r   r   r   r   IgnoreExc
BreadcrumbEventr   r   r   r    r    4   s    VF
&CJFEr   r    levelreturnzsentry_sdk._types.LogLevelStrc                     t        |       } | t         j                  t         j                  fv ry| t         j                  k(  ry| t         j                  k(  ry| t         j
                  k(  ryy)Ndebuginfowarningerror)r   r   r   r   r   r   )r%   s    r   tracing_level_to_sentry_levelr,   ;   se    U#E!'')9)?)?@@	"''	'	"''	'	"((	( r   Dict[str, Any]c                     | j                  di       }i }i }dD ]  }||v s||   ||<    t        |      dkD  r||d<   i }|j                  dg       D ]  }| j                  |      ||<    t        |      dkD  r||d<   |S )Nmetadata)module_pathfileliner   rust_tracing_locationfieldsrust_tracing_fields)getlen)eventr/   contextslocationfieldr4   s         r   extract_contextsr<   J   s    yyR(HHH0 .H&uoHUO. 8}q,4()Fh+ )		%(u)
6{Q*0&'Or   c                     | j                  di       }|j                  d      }t        |j                  d            }| j                  d      }t        |       }||||d}t        j                  |       y )Nr/   targetr%   message)loggerr%   r?   r9   )r6   r,   r<   
sentry_sdkcapture_event)r8   r/   r@   r%   r?   r9   sentry_events          r   process_eventrD   ^   sp    yyR(H\\(#F)(,,w*?@E',yy';G&H 	/L \*r   c                     t        |        y N)rD   )r8   s    r   process_exceptionrG   p   s
    %r   c                     t        | j                  di       j                  d            }| j                  d      }t        j                  ||       y )Nr/   r%   r?   )r%   r?   )r,   r6   rA   add_breadcrumb)r8   r%   r?   s      r   process_breadcrumbrJ   t   s@    )%))J*C*G*G*PQEii	"GE7;r   r/   c                     t        | j                  d            t         j                  t         j                  t         j                  fv S Nr%   )r   r6   r   r   r   )r/   s    r   default_span_filterrM   {   s=    HLL127  r   c                 d   t        | j                  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 rL   )r   r6   r   r    r"   r   r   r#   r   r   r!   )r/   r%   s     r   default_event_type_mappingrO      s    X\\'23E &&&###	#((*:*?*?@	@***	#))+;+A+AB	B&&&&&&r   c            	           e Zd Zeedfdeddddddfd	Zd
efdZdeddd
dfdZ	deded
dfdZ
deddd
dfdZdededdd
dfdZy)RustTracingLayerNoriginevent_type_mappingBCallable[
            [Dict[str, Any]], EventTypeMapping
        ]span_filter Callable[[Dict[str, Any]], bool]include_tracing_fieldsOptional[bool]c                 <    || _         || _        || _        || _        y rF   )rR   rS   rU   rW   )selfrR   rS   rU   rW   s        r   __init__zRustTracingLayer.__init__   s#     "4&&<#r   r&   c                 F    | j                   
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.
        )rW   r
   )rZ   s    r   _include_tracing_fieldsz(RustTracingLayer._include_tracing_fields   s-     **2 $%	
 ,,	
r   r8   sentry_span
SentrySpanc                 V   t        j                  |      }|j                  di       }| j                  |      }|t        j
                  k(  ry |t        j                  k(  rt        |       y |t        j                  k(  rt        |       y |t        j                  k(  rt        |       y y )Nr/   )jsonloadsr6   rS   r    r!   r"   rG   r#   rJ   r$   rD   )rZ   r8   r^   deserialized_eventr/   
event_types         r   on_eventzRustTracingLayer.on_event   s    !ZZ.%))*b9,,X6
)000+///01+66612+111,- 2r   attrsspan_idzOptional[SentrySpan]c                 :   t        j                  |      }|j                  di       }| j                  |      sy |j                  d      }|j                  d      }|j                  d      }||}n|
|| d| }n||}nd}d|| j                  d}t        j                  d
i |}	|j                  d	g       }
|
D ]J  }| j                         r"|	j                  ||j                  |             5|	j                  |t               L |	j                          |	S )Nr/   r0   namer?   z::z	<unknown>function)opri   rR   r4   r   )ra   rb   r6   rU   rR   rA   
start_spanr]   set_datar   	__enter__)rZ   rf   rg   r/   r0   ri   r?   sentry_span_namekwargsr^   r4   r;   s               r   on_new_spanzRustTracingLayer.on_new_span   s+   

5!99Z,)ll=1||F#))I&&$)9"-b7#* $kk
 !++5f5h+ 	GE++-$$UEIIe,<=$$U,EF		G 	r   c                 0    |y |j                  d d d        y rF   )__exit__)rZ   rg   r^   s      r   on_closezRustTracingLayer.on_close   s    T4.r   valuesc                     |y t        j                  |      }|j                         D ]>  \  }}| j                         r|j	                  ||       )|j	                  |t
               @ y rF   )ra   rb   itemsr]   rm   r   )rZ   rg   ru   r^   deserialized_valueskeyvalues          r   	on_recordzRustTracingLayer.on_record   se    "jj0-335 	EJC++-$$S%0$$S*CD		Er   )r   r   r   rO   rM   strr[   boolr]   re   rq   rt   r{   r   r   r   rQ   rQ      s     *:M37=== 8= !1=
 
.c . . .# #s #7M #J/ /, /4 /	E 	Ec 	E 	EQU 	Er   rQ   c                   F    e Zd ZdZeedfdedddddd	d
df
dZedd       Z	y)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initializerz"Callable[[RustTracingLayer], None]rS   rT   rU   rV   rW   rX   c                 f    || _         d| }t        ||||      | _         || j                         y )Nzauto.function.rust_tracing.)r   rQ   tracing_layer)rZ   r   r   rS   rU   rW   rR   s          r   r[   zRustTracingIntegration.__init__   s?     %.zl;-&5K
 	D&&'r   c                       y rF   r   r   r   r   
setup_oncez!RustTracingIntegration.setup_once  s    r   )r&   N)
r   r   r   __doc__rO   rM   r|   r[   staticmethodr   r   r   r   r   r      s[    	" *:M37(( :(	( 8( !1($  r   r   )r8   r-   r&   r-   )r8   r-   r&   N)r/   r-   r&   r    )!r   ra   enumr   r   typingr   r   r   r   rA   sentry_sdk.integrationsr	   sentry_sdk.scoper
   sentry_sdk.tracingr   r_   sentry_sdk.utilsr   r   r    r|   r,   r<   rD   rG   rJ   r}   rM   rO   rQ   r   r   r   r   <module>r      s   @   0 0  / 4 1 6t t  1P (+$<"2 t 	'^E ^EB [  r   