U 췀g#@sfdZddlZddlmZmZddlmZmZmZm Z m Z ddl Z ddl m Z ddlmZddlmZddlmZe e e eefZGd d d eZGd d d eZd dZeeefeeefdddZeeefddddZeeefddddZeeefddddZeeefedddZeeefedddZ GdddZ!Gdd d e Z"dS)!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)Enumauto)AnyCallableDictTupleOptional) Integration)should_send_default_pii)Span)SENSITIVE_DATA_SUBSTITUTEc@sJeZdZUdZeed<dZeed<dZeed<dZeed<d Z eed <d S) RustTracingLevelTRACETraceDEBUGDebugINFOInfoWARNWarnERRORErrorN) __name__ __module__ __qualname__rstr__annotations__rrrrrrR/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/integrations/rust_tracing.pyr .s     r c@s$eZdZeZeZeZeZdS)EventTypeMappingN)rrrrIgnoreExc BreadcrumbEventrrrrr6srcCsNt|}|tjtjfkrdS|tjkr*dS|tjkr8dS|tjkrFdSdSdS)Ndebuginfowarningerror)r rrrrr)levelrrrtracing_level_to_sentry_level=s   r))eventreturncCs|di}i}i}dD]}||kr||||<qt|dkrF||d<i}|dgD]}||||<qVt|dkr~||d<|S)Nmetadata) module_pathfilelinerZrust_tracing_locationfieldsZrust_tracing_fields)getlen)r*r,contextslocationfieldr0rrrextract_contextsMs   r6cCsR|di}|d}t|d}|d}t|}||||d}t|dS)Nr,targetr(message)loggerr(r8r3)r1r)r6 sentry_sdkZ capture_event)r*r,r9r(r8r3Z sentry_eventrrr process_eventas   r;cCs t|dSN)r;)r*rrrprocess_exceptionssr=cCs2t|did}|d}tj||ddS)Nr,r(r8)r(r8)r)r1r:Zadd_breadcrumb)r*r(r8rrrprocess_breadcrumbws r>)r,r+cCst|dtjtjtjfkSNr()r r1rrr)r,rrrdefault_span_filter~s  r@cCsTt|d}|tjkrtjS|tjtjfkr4tjS|tjtj fkrJtj Stj SdSr?) r r1rrr!rrr"rrr )r,r(rrrdefault_event_type_mappings rAc@seZdZeedfeeeeefge feeeefge fe e dddZ e dddZ eeddd d Zeeed d d ZeeddddZeeeddddZdS)RustTracingLayerNoriginevent_type_mapping span_filterinclude_tracing_fieldscCs||_||_||_||_dSr<rC)selfrDrErFrGrrr__init__s zRustTracingLayer.__init__r+cCs|jdkrtS|jS)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)rGr )rHrrr_include_tracing_fieldss z(RustTracingLayer._include_tracing_fields)r* _span_stater+cCslt|}|di}||}|tjkr.dS|tjkrBt|n&|tjkrVt |n|tj krht |dS)Nr,) jsonloadsr1rErr r!r=r"r>r#r;)rHr*rLZdeserialized_eventr,Z event_typerrron_events         zRustTracingLayer.on_event)attrsspan_idr+cCst|}|di}||s$dS|d}|d}|d}|dk rP|}n2|dk rp|dk rp|d|}n|dk r~|}nd}d||jd}t} | j} | r| jf|} n | j f|} |d g} | D],} | r| | || q| | t q| | _| | fS) Nr,r-namer8z::z function)oprRrDr0) rMrNr1rFrDr:get_current_scopespanZ start_childZ start_spanrKset_datar )rHrPrQr,r-rRr8Zsentry_span_namekwargsscopeparent_sentry_span sentry_spanr0r5rrr on_new_spans<        zRustTracingLayer.on_new_span)rQ span_stater+cCs*|dkr dS|\}}||t_dSr<)finishr:rUrV)rHrQr]rZr[rrron_closes zRustTracingLayer.on_close)rQvaluesr]r+c CsV|dkr dS|\}}t|}|D]*\}}|rD|||q&||tq&dSr<)rMrNitemsrKrWr ) rHrQr`r]Z_parent_sentry_spanr[Zdeserialized_valueskeyvaluerrr on_records zRustTracingLayer.on_record)rrrrAr@rrrrrboolrrIrK TraceStaterOr\r_rdrrrrrBs   +rBc@sreZdZdZeedfeeegdfee ee fge fee ee fge fe e dddZeddddZdS) 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 initializerrErFrGcCs.||_d|}t|||||_||jdS)Nzauto.function.rust_tracing.)rhrBZ tracing_layer)rHrhrirErFrGrDrrrrIs  zRustTracingIntegration.__init__rJcCsdSr<rrrrr setup_oncesz!RustTracingIntegration.setup_once)rrr__doc__rAr@rrrBrrrrerrI staticmethodrjrrrrrgs  rg)#rkrMenumrrtypingrrrrrr:Zsentry_sdk.integrationsr Zsentry_sdk.scoper Zsentry_sdk.tracingr Z SentrySpanZsentry_sdk.utilsr rfr rr)rr6r;r=r>rer@rArBrgrrrrs(       j