o
    Zh                     @   s   d Z ddl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 ddlmZ zdd	lmZ dd
lmZmZ ddlmZ ddlmZ W n eyU   edw er^ddlmZ dddZG dd deZdd Zdd Zdd Z dd Z!dS )z
IMPORTANT: The contents of this file are part of a proof of concept and as such
are experimental and not suitable for production use. They may be changed or
removed at any time without prior notice.
    N)import_module)DidNotEnableIntegration)SentrySpanProcessor)SentryPropagator)logger_get_installed_modules)TYPE_CHECKING)trace)_load_distro_load_instrumentors)set_global_textmap)TracerProviderzopentelemetry not installed)Dictzfastapi.FastAPIzflask.Flask)ZfastapiZflaskc                   @   s   e Zd ZdZedd ZdS )OpenTelemetryIntegrationopentelemetryc                  C   s   t d t } zt }|  t| W n ty#   t d Y nw zt|  W n ty8   t d Y nw t	  t 
d d S )NzT[OTel] Initializing highly experimental OpenTelemetry support. Use at your own risk.z.[OTel] Failed to auto-initialize OpenTelemetryz[OTel] Failed to post-patch instrumented classes. You might have to make sure sentry_sdk.init() is called before importing anything else.z4[OTel] Finished setting up OpenTelemetry integration)r   warning_record_unpatched_classesr   	configurer   	Exception	exception_patch_remaining_classes_setup_sentry_tracingdebug)original_classesdistro r   h/var/www/html/lang_env/lib/python3.10/site-packages/sentry_sdk/integrations/opentelemetry/integration.py
setup_once+   s(   z#OpenTelemetryIntegration.setup_onceN)__name__
__module____qualname__
identifierstaticmethodr   r   r   r   r   r   (   s    r   c               
   C   s`   t  } i }t D ]$\}}|| v r-zt|}W n ttfy(   td| Y q	w |||< q	|S )z
    Keep references to classes that are about to be instrumented.

    Used to search for unpatched classes after the instrumentation has run so
    that they can be patched manually.
    z[OTel] Failed to import %s)r   CLASSES_TO_INSTRUMENTitems_import_by_pathAttributeErrorImportErrorr   r   )Zinstalled_packagesr   package	orig_pathoriginal_clsr   r   r   r   I   s   r   c           
   
   C   s   i }t |  D ]/}t| }zt|}W n ttfy(   td| | |= Y qw |j	ds3| |= q|||< q|s<dS t
j  D ]8\}}|	dsQ|t
jv rRqC|  D ]$\}}t|  D ]\}}	|	|krytd|| t||||  qbqVqCdS )a  
    Best-effort attempt to patch any uninstrumented classes in sys.modules.

    This enables us to not care about the order of imports and sentry_sdk.init()
    in user code. If e.g. the Flask class had been imported before sentry_sdk
    was init()ed (and therefore before the OTel instrumentation ran), it would
    not be instrumented. This function goes over remaining uninstrumented
    occurrences of the class in sys.modules and replaces them with the
    instrumented class.

    Since this is looking for exact matches, it will not work in some scenarios
    (e.g. if someone is not using the specific class explicitly, but rather
    inheriting from it). In those cases it's still necessary to sentry_sdk.init()
    before importing anything that's supposed to be instrumented.
    z9[OTel] Failed to check if class has been instrumented: %szopentelemetry.NZ
sentry_sdkz'[OTel] Additionally patching %s from %s)listkeysr$   r&   r'   r(   r   r   r    
startswithsysmodulescopyr%   builtin_module_namesvarssetattr)
r   Zinstrumented_classesr)   Zoriginal_pathclsmodule_namemoduler+   var_namevarr   r   r   r   b   sJ   

r   c                 C   s$   | j ddd}tt|d |d S )N.   )maxsplitr   )rsplitgetattrr   )pathpartsr   r   r   r&      s   r&   c                  C   s*   t  } | t  t|  tt  d S )N)r   Zadd_span_processorr   r
   Zset_tracer_providerr   r   )providerr   r   r   r      s   
r   )"__doc__r/   	importlibr   Zsentry_sdk.integrationsr   r   Z4sentry_sdk.integrations.opentelemetry.span_processorr   Z0sentry_sdk.integrations.opentelemetry.propagatorr   Zsentry_sdk.utilsr   r   Zsentry_sdk._typesr	   r   r
   Z8opentelemetry.instrumentation.auto_instrumentation._loadr   r   Zopentelemetry.propagater   Zopentelemetry.sdk.tracer   r(   typingr   r$   r   r   r   r&   r   r   r   r   r   <module>   s4    	!>