HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //proc/722141/root/lib/python3.6/site-packages/botocore/__pycache__/monitoring.cpython-36.pyc
3

�T_jP�@s�ddlZddlZddlZddlZddlmZmZmZddlm	Z
eje�Z
Gdd�de�ZGdd�de�ZGdd	�d	e�ZGd
d�de�ZGdd
�d
e�ZGdd�de�ZGdd�de�ZdS)�N)�ensure_unicode�ensure_bytes�urlparse)�
EXCEPTION_MAPc@s2eZdZdddddgZdd�Zdd	�Zd
d�ZdS)
�Monitorzbefore-parameter-buildzrequest-createdzresponse-receivedz
after-callzafter-call-errorcCs||_||_dS)z�Abstraction for monitoring clients API calls

        :param adapter: An adapter that takes event emitter events
            and produces monitor events

        :param publisher: A publisher for generated monitor events
        N)�_adapter�
_publisher)�selfZadapterZ	publisher�r
� /usr/lib/python3.6/monitoring.py�__init__"szMonitor.__init__cCs"x|jD]}|j||j�qWdS)z(Register an event emitter to the monitorN)�_EVENTS_TO_REGISTERZ
register_last�capture)r	Z
event_emitterZevent_to_registerr
r
r�register-szMonitor.registercKs\y"|jj||�}|r |jj|�Wn4tk
rV}ztjd||dd�WYdd}~XnXdS)z�Captures an incoming event from the event emitter

        It will feed an event emitter event to the monitor's adaptor to create
        a monitor event and then publish that event to the monitor's publisher.
        z:Exception %s raised by client monitor in handling event %sT)�exc_infoN)r�feedr�publish�	Exception�logger�debug)r	�
event_nameZpayloadZ
monitor_event�er
r
rr2szMonitor.captureN)�__name__�
__module__�__qualname__r
rrrr
r
r
rrsrc@sreZdZejfdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�MonitorEventAdaptercCs
||_dS)z�Adapts event emitter events to produce monitor events

        :type time: callable
        :param time: A callable that produces the current time
        N)�_time)r	�timer
r
rrCszMonitorEventAdapter.__init__cCs|j|�f|�S)a�Feed an event emitter event to generate a monitor event

        :type emitter_event_name: str
        :param emitter_event_name: The name of the event emitted

        :type emitter_payload: dict
        :param emitter_payload: The payload to associated to the event
            emitted

        :rtype: BaseMonitorEvent
        :returns: A monitor event based on the event emitter events
            fired
        )�_get_handler)r	Zemitter_event_nameZemitter_payloadr
r
rrKszMonitorEventAdapter.feedcCs t|d|jd�djdd��S)NZ_handle_�.r�-�_)�getattr�split�replace)r	rr
r
rr[sz MonitorEventAdapter._get_handlercKs t|jj|j|j�d�|d<dS)N)�service�	operation�	timestamp�current_api_call_event)�APICallEventZ
service_modelZ
service_idZ	wire_name�_get_current_time)r	Zmodel�context�kwargsr
r
r�_handle_before_parameter_build`sz2MonitorEventAdapter._handle_before_parameter_buildcKs6|j}|dj|j�d�}|j|_|j|_||d<dS)Nr()r'�current_api_call_attempt_event)r+�new_api_call_attemptr*Zheaders�request_headers�url)r	Zrequestr,r+Znew_attempt_eventr
r
r�_handle_request_createdgsz+MonitorEventAdapter._handle_request_createdcKsR|jd�}|j|�|_|dk	rH|dd|_|dd|_|jd�|_n||_|S)Nr.�ResponseMetadataZHTTPStatusCodeZHTTPHeaders�Error)�pop�_get_latency�latency�http_status_code�response_headers�get�parsed_error�wire_exception)r	Zparsed_responser+�	exceptionr,�
attempt_eventr
r
r�_handle_response_receivedps
z-MonitorEventAdapter._handle_response_receivedcKs |djdd�|d_|j|�S)Nr3ZMaxAttemptsReachedFr()r:�retries_exceeded�_complete_api_call)r	r+Zparsedr,r
r
r�_handle_after_call~sz&MonitorEventAdapter._handle_after_callcKs|j|�|d_|j|�S)Nr()�_is_retryable_exceptionr@rA)r	r+r=r,r
r
r�_handle_after_call_error�sz,MonitorEventAdapter._handle_after_call_errorcCst|ttd��S)NZGENERAL_CONNECTION_ERROR)�
isinstance�tuple�RETRYABLE_EXCEPTIONS)r	r=r
r
rrC�sz+MonitorEventAdapter._is_retryable_exceptioncCs|jd�}|j|�|_|S)Nr()r5r6r7)r	r+Z
call_eventr
r
rrA�s
z&MonitorEventAdapter._complete_api_callcCs|j�|jS)N)r*r')r	�eventr
r
rr6�sz MonitorEventAdapter._get_latencycCst|j�d�S)Ni�)�intr)r	r
r
rr*�sz%MonitorEventAdapter._get_current_timeN)rrrrrrrr-r2r?rBrDrCrAr6r*r
r
r
rrBs		rc@s$eZdZdd�Zdd�Zdd�ZdS)�BaseMonitorEventcCs||_||_||_dS)a�Base monitor event

        :type service: str
        :param service: A string identifying the service associated to
            the event

        :type operation: str
        :param operation: A string identifying the operation of service
            associated to the event

        :type timestamp: int
        :param timestamp: Epoch time in milliseconds from when the event began
        N)r%r&r')r	r%r&r'r
r
rr�szBaseMonitorEvent.__init__cCsd|jj|jfS)Nz%s(%r))�	__class__r�__dict__)r	r
r
r�__repr__�szBaseMonitorEvent.__repr__cCst||j�r|j|jkSdS)NF)rErKrL)r	�otherr
r
r�__eq__�szBaseMonitorEvent.__eq__N)rrrrrMrOr
r
r
rrJ�srJcs&eZdZd�fdd�	Zdd�Z�ZS)r)NFcs:tt|�j|||d�||_||_|dkr0g|_||_dS)a�Monitor event for a single API call

        This event corresponds to a single client method call, which includes
        every HTTP requests attempt made in order to complete the client call

        :type service: str
        :param service: A string identifying the service associated to
            the event

        :type operation: str
        :param operation: A string identifying the operation of service
            associated to the event

        :type timestamp: int
        :param timestamp: Epoch time in milliseconds from when the event began

        :type latency: int
        :param latency: The time in milliseconds to complete the client call

        :type attempts: list
        :param attempts: The list of APICallAttempts associated to the
            APICall

        :type retries_exceeded: bool
        :param retries_exceeded: True if API call exceeded retries. False
            otherwise
        )r%r&r'N)�superr)rr7�attemptsr@)r	r%r&r'r7rQr@)rKr
rr�s
zAPICallEvent.__init__cCs"t|j|j|d�}|jj|�|S)z�Instantiates APICallAttemptEvent associated to the APICallEvent

        :type timestamp: int
        :param timestamp: Epoch time in milliseconds to associate to the
            APICallAttemptEvent
        )r%r&r')�APICallAttemptEventr%r&rQ�append)r	r'r>r
r
rr/�sz!APICallEvent.new_api_call_attempt)NNF)rrrrr/�
__classcell__r
r
)rKrr)�s$r)cseZdZd�fdd�	Z�ZS)rRNcsDtt|�j|||d�||_||_||_||_||_|	|_|
|_	dS)a�Monitor event for a single API call attempt

        This event corresponds to a single HTTP request attempt in completing
        the entire client method call.

        :type service: str
        :param service: A string identifying the service associated to
            the event

        :type operation: str
        :param operation: A string identifying the operation of service
            associated to the event

        :type timestamp: int
        :param timestamp: Epoch time in milliseconds from when the HTTP request
            started

        :type latency: int
        :param latency: The time in milliseconds to complete the HTTP request
            whether it succeeded or failed

        :type url: str
        :param url: The URL the attempt was sent to

        :type http_status_code: int
        :param http_status_code: The HTTP status code of the HTTP response
            if there was a response

        :type request_headers: dict
        :param request_headers: The HTTP headers sent in making the HTTP
            request

        :type response_headers: dict
        :param response_headers: The HTTP headers returned in the HTTP response
            if there was a response

        :type parsed_error: dict
        :param parsed_error: The error parsed if the service returned an
            error back

        :type wire_exception: Exception
        :param wire_exception: The exception raised in sending the HTTP
            request (i.e. ConnectionError)
        )r%r&r'N)
rPrRrr7r1r8r0r9r;r<)r	r%r&r'r7r1r8r0r9r;r<)rKr
rr�s0
zAPICallAttemptEvent.__init__)NNNNNNN)rrrrrTr
r
)rKrrR�srRc@s&eZdZdZdZdZdZdZdddd�Ze	j
d	�e	j
d
�d�Zdd
ddddddddddgZdd�Z
dd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Zd<d=�Zd>d?�Z d@dA�Z!dBdC�Z"dDdE�Z#dFdG�Z$dHdI�Z%dJS)K�
CSMSerializer���iZXAmznRequestIdZ
XAmzRequestIdZXAmzId2)zx-amzn-requestidzx-amz-request-idz
x-amz-id-2zSAWS4-HMAC-SHA256 Credential=(?P<access_key>\w+)/\d+/(?P<signing_region>[a-z0-9-]+)/zAWS (?P<access_key>\w+):)�v4Zs3r%r&r'rQr7r@r1r0r8r9r;r<cCs|j|�||_dS)z�Serializes monitor events to CSM (Client Side Monitoring) format

        :type csm_client_id: str
        :param csm_client_id: The application identifier to associate
            to the serialized events
        N)�_validate_client_id�
csm_client_id)r	r[r
r
rrOs
zCSMSerializer.__init__cCs$t|�|jkr td||jf��dS)NzTThe value provided for csm_client_id: %s exceeds the maximum length of %s characters)�len�_MAX_CLIENT_ID_LENGTH�
ValueError)r	r[r
r
rrZYsz!CSMSerializer._validate_client_idcCsj|j|�}|j|�}||d<x:|jD]0}t||d�}|dk	r$t|d|�|||d�q$Wttj|dd��S)	z�Serializes a monitor event to the CSM format

        :type event: BaseMonitorEvent
        :param event: The event to serialize to bytes

        :rtype: bytes
        :returns: The CSM serialized form of the event
        ZTypeNZ_serialize_)�
event_type�,�:)Z
separators)r`ra)�_get_base_event_dict�_get_event_type�_SERIALIZEABLE_EVENT_PROPERTIESr"r�json�dumps)r	rH�
event_dictr_�attr�valuer
r
r�	serializeas	

zCSMSerializer.serializecCsd|jd�S)N�)ZVersionZClientId)r[)r	rHr
r
rrbusz"CSMSerializer._get_base_event_dictcKs||d<dS)NZServicer
)r	r%rgr,r
r
r�_serialize_service{sz CSMSerializer._serialize_servicecKs||d<dS)NZApir
)r	r&rgr,r
r
r�_serialize_operation~sz"CSMSerializer._serialize_operationcKs||d<dS)NZ	Timestampr
)r	r'rgr,r
r
r�_serialize_timestamp�sz"CSMSerializer._serialize_timestampcKs$t|�|d<|r |j||d�dS)NZAttemptCountrk���)r\�_add_fields_from_last_attempt)r	rQrgr,r
r
r�_serialize_attempts�sz!CSMSerializer._serialize_attemptscCs~|jr2|j|j�}|dk	r"||d<|j|j�|d<|jdk	rF|j|d<|jdk	r`|j|j|d�|jdk	rz|j|j|d�dS)N�Region�	UserAgentZFinalHttpStatusCode�ApiCall)r0�_get_region�_get_user_agentr8r;�_serialize_parsed_errorr<�_serialize_wire_exception)r	rgZlast_attempt�regionr
r
rrp�s



z+CSMSerializer._add_fields_from_last_attemptcCs&|dkr||d<n|dkr"||d<dS)NrtZLatency�ApiCallAttemptZAttemptLatencyr
)r	r7rgr_r
r
r�_serialize_latency�s
z CSMSerializer._serialize_latencycKs|rdnd|d<dS)NrkrZMaxRetriesExceededr
)r	r@rgr,r
r
r�_serialize_retries_exceeded�sz)CSMSerializer._serialize_retries_exceededcKst|�j|d<dS)NZFqdn)rZnetloc)r	r1rgr,r
r
r�_serialize_url�szCSMSerializer._serialize_urlcKsX|j|�|d<|j|�r&|j|�|d<|j|�}|dk	r@||d<d|krT|d|d<dS)NrsZ	AccessKeyrrzX-Amz-Security-TokenZSessionToken)rv�
_is_signed�_get_access_keyru)r	r0rgr,ryr
r
r�_serialize_request_headers�s

z(CSMSerializer._serialize_request_headerscKs||d<dS)NZHttpStatusCoder
)r	r8rgr,r
r
r�_serialize_http_status_code�sz)CSMSerializer._serialize_http_status_codecKs0x*|jj�D]\}}||kr||||<qWdS)N)�"_RESPONSE_HEADERS_TO_EVENT_ENTRIES�items)r	r9rgr,�header�entryr
r
r�_serialize_response_headers�sz)CSMSerializer._serialize_response_headerscKsH|dkrdnd}|j|d|j�||d<|j|d|j�||d<dS)Nrt�Final�ZCodeZAwsExceptionZMessageZAwsExceptionMessage)�	_truncate�_MAX_ERROR_CODE_LENGTH�_MAX_MESSAGE_LENGTH)r	r;rgr_r,�field_prefixr
r
rrw�s
z%CSMSerializer._serialize_parsed_errorcKsH|dkrdnd}|j|jj|j�||d<|jt|�|j�||d<dS)Nrtr�r�ZSdkExceptionZSdkExceptionMessage)r�rKr�_MAX_EXCEPTION_CLASS_LENGTH�strr�)r	r<rgr_r,r�r
r
rrx�sz'CSMSerializer._serialize_wire_exceptioncCs t|t�rdSt|t�rdSdS)Nrtrz)rEr)rR)r	rHr
r
rrc�s

zCSMSerializer._get_event_typecCs"|j|�}|j|�\}}|jd�S)NZ
access_key)�_get_auth_value�_get_auth_match�group)r	r0�auth_valr!�
auth_matchr
r
rr�s
zCSMSerializer._get_access_keycCs<|j|�sdS|j|�}|j|�\}}|dkr2dS|jd�S)NrYZsigning_region)r~r�r�r�)r	r0r��signature_versionr�r
r
rru�s

zCSMSerializer._get_regioncCs|jt|jdd��|j�S)Nz
User-Agentr�)r�rr:�_MAX_USER_AGENT_LENGTH)r	r0r
r
rrv�szCSMSerializer._get_user_agentcCsd|kS)N�
Authorizationr
)r	r0r
r
rr~�szCSMSerializer._is_signedcCst|d�S)Nr�)r)r	r0r
r
rr��szCSMSerializer._get_auth_valuecCs2x,|jj�D]\}}|j|�}|r||fSqWdS)N)NN)�_AUTH_REGEXSr��match)r	r�r�Zregexr�r
r
rr��s

zCSMSerializer._get_auth_matchcCs*t|�|kr&tjd||�|d|�S|S)Nz6Truncating following value to maximum length of %s: %s)r\rr)r	�textZ
max_lengthr
r
rr��szCSMSerializer._truncateN)&rrrr]r�r�r�r�r��re�compiler�rdrrZrjrbrlrmrnrqrpr{r|r}r�r�r�rwrxrcrrurvr~r�r�r�r
r
r
rrU+sb

		rUc@s eZdZdZdd�Zdd�ZdS)	�SocketPublisher�icCs||_||f|_||_dS)a)Publishes monitor events to a socket

        :type socket: socket.socket
        :param socket: The socket object to use to publish events

        :type host: string
        :param host: The host to send events to

        :type port: integer
        :param port: The port on the host to send events to

        :param serializer: The serializer to use to serialize the event
            to a form that can be published to the socket. This must
            have a `serialize()` method that accepts a monitor event
            and return bytes
        N)�_socket�_address�_serializer)r	Zsocket�hostZportZ
serializerr
r
rrs
zSocketPublisher.__init__cCsF|jj|�}t|�|jkr2tjdt|�|j�dS|jj||j�dS)z�Publishes a specified monitor event

        :type event: BaseMonitorEvent
        :param event: The monitor event to be sent
            over the publisher's socket to the desired address.
        z`Serialized event of size %s exceeds the maximum length allowed: %s. Not sending event to socket.N)	r�rjr\�_MAX_MONITOR_EVENT_LENGTHrrr�Zsendtor�)r	rHZserialized_eventr
r
rrszSocketPublisher.publishNi )rrrr�rrr
r
r
rr��sr�)reZloggingr�rZbotocore.compatrrrZbotocore.retryhandlerrrGZ	getLoggerrr�objectrrrJr)rRrUr�r
r
r
r�<module>
s
)Z6=U