o
    ٰi                     @   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   b/home/ubuntu/.local/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eddfdd	Zedefd
dZedddZedefddZedddZedefddZedddZedddZdS )CloudResourceContextIntegrationz8
    Adds cloud resource context to the Senty scope
    cloudresourcecontext )timeoutNcloud_providerreturnc                 C   s
   |t _d S )N)r    r$   )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_awsG   s(   
z'CloudResourceContextIntegration._is_awsDict[str, str]c              
   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_URLr4   r1   jsonloadsr2   r3   r:   r5   r6   r7   r   r8   r9   r;   )r=   ctxr>   r2   r?   r   r   r   _get_aws_context_   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 )NrC   Metadata-FlavorGoogler*   r,   FrD   T/GCP metadata service timed out after %s secondsz'Error checking GCP metadata service: %s)r.   r/   GCP_METADATA_URLr1   rO   rP   r2   r3   gcp_metadatar5   r6   r7   r   r8   r9   r:   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 )NrB   rC   rS   rT   r*   r,   rD   project	projectIdrF   instancezone/rH   rI   rJ   idrL   rU   zError fetching GCP metadata: %s)r   r   r   r   rW   r.   r/   rV   r1   rO   rP   r2   r3   r:   splitr5   r6   r7   r   r8   r9   r;   )r=   rQ   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   rX   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 rb   )r$   r    rc   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$   rd   re   r   warninglistrf   r   CONTEXT_TYPE)r$   unsupported_cloud_providercontextr   r   r   
setup_once   s   
z*CloudResourceContextIntegration.setup_once)r"   )r%   rA   )r%   N)r   r   r   r   
identifierr$   r4   r5   PoolManagerr9   r.   rW   r;   r'   classmethodboolr@   rR   rX   ra   rc   rf   staticmethodrl   r   r   r   r   r    6   s,    45	r    )rO   r5   sentry_sdk.integrationsr   sentry_sdk.apir   sentry_sdk.utilsr   typingr   r   ri   r9   AWS_METADATA_HOSTformatr0   rN   GCP_METADATA_HOSTrV   r   r   r    r   rR   r   ra   rd   r   r   r   r   <module>   s4    

 Y