
    tni!                        d dl mZmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZmZ d dlmZ d dlmZmZ 	 d dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z* d dl+m,Z,m-Z-m.Z. d dl/m0Z0 d dl1m2Z2 n# e3$ r  ed          w xY wd dl4m5Z5 e5rd dl4m6Z6m7Z7m8Z8m9Z9 d$dZ:d%d&dZ;da<d'dZ= G d de0          Z>dd de?fd!Z@ G d" d#e          ZAdS )(    )
get_clientcapture_event)IntegrationDidNotEnable)%register_external_propagation_context)Dsnloggerevent_from_exceptioncapture_internal_exceptions)VERSIONEndpointType)Baggage)BAGGAGE_HEADER_NAMESENTRY_TRACE_HEADER_NAME)set_global_textmap)TracerProviderSpan)BatchSpanProcessor)OTLPSpanExporter)get_current_spanget_tracer_providerset_tracer_providerformat_trace_idformat_span_idSpanContextINVALID_SPAN_IDINVALID_TRACE_ID)Contextget_current	get_value)CarrierTSetterdefault_setter)SentryPropagator)SENTRY_BAGGAGE_KEYz+opentelemetry-distro[otlp] is not installed)TYPE_CHECKING)OptionalDictAnyTuplereturnOptional[Tuple[str, str]]c                      t                                                      } | j        t          k    s| j        t
          k    rdS t          | j                  t          | j                  fS )zC
    Get the (trace_id, span_id) from opentelemetry if exists.
    N)r   get_span_contexttrace_idr   span_idr   r   r   )ctxs    Y/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/sentry_sdk/integrations/otlp.pyotel_propagation_contextr3   9   sY     


-
-
/
/C
|'''3;/+I+ItCL))>#++F+FGG    NdsnOptional[str]c                    t                      }t          |t                    s1t          j        d           t                      }t          |           d }d }| rvt          |                               dt                     }|	                    t          j                  }d|                                i}t          j        d|            t          ||          }t          |          }|                    |           d S )Nz?[OTLP] No TracerProvider configured by user, creating a new onezsentry.python/zX-Sentry-Authz[OTLP] Sending traces to )endpointheaders)r   
isinstancer   r	   debugr   r   to_authr   get_api_urlr   OTLP_TRACES	to_headerr   r   add_span_processor)r5   tracer_providerr8   r9   authotlp_exporterspan_processors          r2   setup_otlp_traces_exporterrE   E   s    )++Oo~66 -VWWW(**O,,,HG
 =3xx : : :;;##L$<=="DNN$4$45;;;<<<$hHHHM'66N&&~66666r4   Fc                  n    t           j        t          rdS 	 	 	 	 	 	 	 	 	 	 dfd
} | t           _        dadS )ze
    Intercept otel's Span.record_exception to automatically capture those exceptions in Sentry.
    Nselfr   	exceptionBaseExceptionargsr)   kwargsr+   c                 T   t                                          t                    }|rq|j        rjt	                      5  t          |t                      j        t          j        dd          \  }}t          ||           d d d            n# 1 swxY w Y    | |g|R i | d S )NF)typehandled)client_options	mechanism)hint)	r   get_integrationOTLPIntegrationcapture_exceptionsr   r
   options
identifierr   )rG   rH   rJ   rK   otlp_integrationeventrQ   _original_record_exceptions          r2    _sentry_patched_record_exceptionzBsetup_capture_exceptions.<locals>._sentry_patched_record_exceptiong   s    &<<77HH 	0 0 C 	0,.. 0 02#-<<#7'6'AeTT  t
 e$////0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 	#"4DTDDDVDDDDDs   ABBBT)
rG   r   rH   rI   rJ   r)   rK   r)   r+   N)r   record_exception_sentry_patched_exception)rZ   rY   s    @r2   setup_capture_exceptionsr]   ]   s    
 "&!6  EE!0E9>EJOE	E E E E E E =D $r4   c                   .    e Zd ZdZdef	 	 	 	 	 	 	 dd
ZdS )SentryOTLPPropagatora  
    We need to override the inject of the older propagator since that
    is SpanProcessor based.

    !!! Note regarding baggage:
    We cannot meaningfully populate a new baggage as a head SDK
    when we are using OTLP since we don't have any sort of transaction semantic to
    track state across a group of spans.

    For incoming baggage, we just pass it on as is so that case is correctly handled.
    Ncarrierr!   contextOptional[Context]setterSetter[CarrierT]r+   c                    t                                          t                    }|d S |t                      }t	          |          }|                                }|j        sd S t          |          }|                    |t          |           t          t          |          }|It          |t                    r6|                                }	|	r"|                    |t          |	           d S d S d S d S N)r   rR   rS   r   r   r.   is_valid_to_traceparentsetr   r    r%   r:   r   	serializer   )
rG   r`   ra   rc   rW   current_spancurrent_span_contextsentry_tracebaggagebaggage_datas
             r2   injectzSentryOTLPPropagator.inject   s     &<<77HH#F?!mmG'00+<<>>#, 	F&';<<

74lCCC.88:gw#?#?",,..L G

7$7FFFFF G Gr4   )r`   r!   ra   rb   rc   rd   r+   N)__name__
__module____qualname____doc__r#   rp    r4   r2   r_   r_   z   sj        
 
 (,%3	GG %G #	G
 
G G G G G Gr4   r_   span_contextr   c                     t          | j                  }t          | j                  }| j        j        }| d| d|rdnd S )z<
    Helper method to generate the sentry-trace header.
    -10)r   r0   r   r/   trace_flagssampled)rv   r0   r/   r|   s       r2   rh   rh      sT     \122G|455H&.G<<<<'#:33s<<<r4   c            	       `    e Zd ZdZdZ	 	 	 ddedededd	fd
Zedd            Z	 d	 	 	 ddZ	d	S )rS   a  
    Automatically setup OTLP ingestion from the DSN.

    :param setup_otlp_traces_exporter: Automatically configure an Exporter to send OTLP traces from the DSN, defaults to True.
        Set to False if using a custom collector or to setup the TracerProvider manually.
    :param setup_propagator: Automatically configure the Sentry Propagator for Distributed Tracing, defaults to True.
        Set to False to configure propagators manually or to disable propagation.
    :param capture_exceptions: Intercept and capture exceptions on the OpenTelemetry Span in Sentry as well, defaults to False.
        Set to True to turn on capturing but be aware that since Sentry captures most exceptions, duplicate exceptions might be dropped by DedupeIntegration in many cases.
    otlpTFrE   setup_propagatorrT   r+   Nc                 0    || _         || _        || _        d S rf   )rE   r   rT   )rG   rE   r   rT   s       r2   __init__zOTLPIntegration.__init__   s#     +E' 0"4r4   c                  V    t          j        d           t          t                     d S )Nz.[OTLP] Setting up trace linking for all events)r	   r;   r   r3   ru   r4   r2   
setup_oncezOTLPIntegration.setup_once   s(    EFFF-.FGGGGGr4   rU   Optional[Dict[str, Any]]c                    | j         r<t          j        d           |r|                    d          nd }t          |           | j        r/t          j        d           t          t                                 t                       d S )Nz[OTLP] Setting up OTLP exporterr5   z4[OTLP] Setting up propagator for distributed tracing)rE   r	   r;   getr   r   r_   r]   )rG   rU   r5   s      r2   setup_once_with_optionsz'OTLPIntegration.setup_once_with_options   s     * 	,L:;;;9@#J7;;u#5#5#5dC&s+++  	7LOPPP355666 """""r4   )TTFr+   Nrf   )rU   r   r+   N)
rq   rr   rs   rt   rV   boolr   staticmethodr   r   ru   r4   r2   rS   rS      s        	 	 J ,0!%#(	5 5$(5 5 !	5
 
5 5 5 5 H H H \H
 59#1#	# # # # # #r4   rS   )r+   r,   rf   )r5   r6   r+   Nr   )B
sentry_sdkr   r   sentry_sdk.integrationsr   r   sentry_sdk.scoper   sentry_sdk.utilsr   r	   r
   r   sentry_sdk.constsr   r   sentry_sdk.tracing_utilsr   sentry_sdk.tracingr   r   opentelemetry.propagater   opentelemetry.sdk.tracer   r   opentelemetry.sdk.trace.exportr   5opentelemetry.exporter.otlp.proto.http.trace_exporterr   opentelemetry.tracer   r   r   r   r   r   r   r   opentelemetry.contextr   r   r    !opentelemetry.propagators.textmapr!   r"   r#   0sentry_sdk.integrations.opentelemetry.propagatorr$   ,sentry_sdk.integrations.opentelemetry.constsr%   ImportErrortypingr&   r'   r(   r)   r*   r3   rE   r\   r]   r_   strrh   rS   ru   r4   r2   <module>r      sd   0 0 0 0 0 0 0 0 = = = = = = = = B B B B B B            4 3 3 3 3 3 3 3 , , , , , ,       
 F::::::<<<<<<<<AAAAAAVVVVVV	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	                   RQQQQQOOOOOOO F F F
,D
E
EEF !           2111111111111	H 	H 	H 	H7 7 7 7 7* " % % % %:'G 'G 'G 'G 'G+ 'G 'G 'GT=- =C = = = =*# *# *# *# *#k *# *# *# *# *#s   AB	 	B