o
    hd                     @   s   d dl 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	r(d dlm
Z
 dZdZd	Zd
eZdeZdZdeZG dd dZG dd dZG dd deZejejejejiZdS )    N)Integration)set_context)logger)TYPE_CHECKING)Dictcloud_resourceg       @z169.254.169.254zhttp://{}/latest/api/tokenz3http://{}/latest/dynamic/instance-identity/documentzmetadata.google.internalz,http://{}/computeMetadata/v1/?recursive=truec                   @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	CLOUD_PROVIDERz
    Name of the cloud provider.
    see https://opentelemetry.io/docs/reference/specification/resource/semantic_conventions/cloud/
    alibaba_cloudawsazuregcp	ibm_cloudtencent_cloudN)
__name__
__module____qualname____doc__ALIBABAAWSAZUREGCPIBMTENCENT r   r   p/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/cloud_resource_context.pyr      s    r   c                   @   s   e Zd ZdZdZdZdS )CLOUD_PLATFORMz
    The cloud platform.
    see https://opentelemetry.io/docs/reference/specification/resource/semantic_conventions/cloud/
    aws_ec2gcp_compute_engineN)r   r   r   r   AWS_EC2GCP_COMPUTE_ENGINEr   r   r   r   r   ,   s    r   c                   @   s   e Zd ZdZdZdZdZeje	dZ
dZdddZedd	 Zed
d Zedd Zedd Zedd Zedd Zedd ZdS )CloudResourceContextIntegrationz8
    Adds cloud resource context to the Senty scope
    cloudresourcecontext )timeoutNc                 C   s
   |t _d S )N)r    cloud_provider)selfr$   r   r   r   __init__D   s   
z(CloudResourceContextIntegration.__init__c              
   C   s   z| j jdtddid}|jdkrW dS |j | _W dS  tjj	y.   t
dt Y dS  tyH } zt
d	t| W Y d }~dS d }~ww )
NPUTz$X-aws-ec2-metadata-token-ttl-seconds60headers   FT/AWS metadata service timed out after %s secondsz'Error checking AWS metadata service: %s)httprequestAWS_TOKEN_URLstatusdatadecode	aws_tokenurllib3
exceptionsTimeoutErrorr   debugHTTP_TIMEOUT	Exceptionstrclsrer   r   r   _is_awsH   s(   
z'CloudResourceContextIntegration._is_awsc              
   C   s^  t jtjd}z{| jjdtd| jid}|jdkr|W S t	
|jd}z|d |d< W n	 ty6   Y nw z|d	 |d
< W n	 tyH   Y nw z|d |d< W n	 tyZ   Y nw z|d |d< W n	 tyl   Y nw z
|d |d< W W |S  ty   Y W |S w  tjjy   tdt Y |S  ty } ztdt| W Y d }~|S d }~ww )Nzcloud.providerzcloud.platformGETzX-aws-ec2-metadata-tokenr)   r+   utf-8	accountIdcloud.account.idavailabilityZonecloud.availability_zoneregioncloud.region
instanceIdhost.idinstanceTypez	host.typer,   zError fetching AWS metadata: %s)r   r   r   r   r-   r.   AWS_METADATA_URLr3   r0   jsonloadsr1   r2   r9   r4   r5   r6   r   r7   r8   r:   )r<   ctxr=   r1   r>   r   r   r   _get_aws_contexta   sb   
	z0CloudResourceContextIntegration._get_aws_contextc              
   C   s   z | j jdtddid}|jdkrW dS t|jd| _W dS  t	j
jy2   td	t Y dS  tyL } ztd
t| W Y d }~dS d }~ww )NrA   Metadata-FlavorGoogler)   r+   FrB   T/GCP metadata service timed out after %s secondsz'Error checking GCP metadata service: %s)r-   r.   GCP_METADATA_URLr0   rM   rN   r1   r2   gcp_metadatar4   r5   r6   r   r7   r8   r9   r:   r;   r   r   r   _is_gcp   s(   
z'CloudResourceContextIntegration._is_gcpc              
   C   sp  t jtjd}z| jd u r*| jjdtddid}|jdkr |W S t	
|jd| _z| jd d	 |d
< W n	 ty>   Y nw z| jd d dd |d< W n	 tyX   Y nw z| jd d dd |d< W n	 tyr   Y nw z| jd d |d< W W |S  ty   Y W |S w  tjjy   tdt Y |S  ty } ztdt| W Y d }~|S d }~ww )Nr@   rA   rQ   rR   r)   r+   rB   project	projectIdrD   instancezone/rF   rG   rH   idrJ   rS   zError fetching GCP metadata: %s)r   r   r   r   rU   r-   r.   rT   r0   rM   rN   r1   r2   r9   splitr4   r5   r6   r   r7   r8   r:   )r<   rO   r=   r>   r   r   r   _get_gcp_context   sf   

	z0CloudResourceContextIntegration._get_gcp_contextc                 C   s    |   rtjS |  rtjS dS Nr"   )r?   r   r   rV   r   )r<   r   r   r   _get_cloud_provider   s
   z3CloudResourceContextIntegration._get_cloud_providerc                 C   s2   | j dkr| j nt }|t v rt|  S i S r`   )r$   r    ra   context_getterskeys)r<   r$   r   r   r   _get_cloud_resource_context   s   

z;CloudResourceContextIntegration._get_cloud_resource_contextc                  C   sX   t j} | dko| t v}|rtdt jtt  t  }|i kr*tt	| d S d S )Nr"   zVInvalid value for cloud_provider: %s (must be in %s). Falling back to autodetection...)
r    r$   rb   rc   r   warninglistrd   r   CONTEXT_TYPE)r$   unsupported_cloud_providercontextr   r   r   
setup_once   s   
z*CloudResourceContextIntegration.setup_once)r"   )r   r   r   r   
identifierr$   r3   r4   PoolManagerr8   r-   rU   r&   classmethodr?   rP   rV   r_   ra   rd   staticmethodrj   r   r   r   r   r    6   s,    


5

6


r    )rM   r4   sentry_sdk.integrationsr   sentry_sdk.apir   sentry_sdk.utilsr   typingr   r   rg   r8   AWS_METADATA_HOSTformatr/   rL   GCP_METADATA_HOSTrT   r   r   r    r   rP   r   r_   rb   r   r   r   r   <module>   s4    

 a