o ?cuP@sddlZddlZddlZddlZddlmZmZmZddlm Z e e Z GdddZGdddZGdd d ZGd d d eZGd d d eZGdddZGdddZdS)N) ensure_bytesensure_unicodeurlparse) EXCEPTION_MAPc@s,eZdZgdZddZddZddZdS) Monitor)zbefore-parameter-buildzrequest-createdzresponse-receivedz after-callzafter-call-errorcCs||_||_dS)zAbstraction 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)selfadapterZ publisherr n/private/var/folders/cw/wlscbxl13mj6wd668h7l9g9sllkg5j/T/pip-target-b31awkwq/lib/python/botocore/monitoring.py__init__!s zMonitor.__init__cCs|jD] }|||jqdS)z(Register an event emitter to the monitorN)_EVENTS_TO_REGISTERZ register_lastcapture)r Z event_emitterZevent_to_registerr r r register,s zMonitor.registerc Ksdz|j||}|r|j|WdSWdSty1}ztjd||ddWYd}~dSd}~ww)zCaptures 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)rfeedrpublish Exceptionloggerdebug)r event_namepayloadZ monitor_eventer r r r1szMonitor.captureN)__name__ __module__ __qualname__rr rrr r r r rs  rc@sreZdZejfddZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZddZdS)MonitorEventAdaptercCs ||_dS)zAdapts event emitter events to produce monitor events :type time: callable :param time: A callable that produces the current time N)_time)r timer r r r Es zMonitorEventAdapter.__init__cCs||di|S)aFeed 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 Nr ) _get_handler)r Zemitter_event_nameZemitter_payloadr r r rMszMonitorEventAdapter.feedcCs t|d|ddddS)NZ_handle_.r-_)getattrsplitreplace)r rr r r r ]sz MonitorEventAdapter._get_handlercKs t|jj|j|d|d<dS)Nservice operation timestampcurrent_api_call_event) APICallEventZ service_modelZ service_idZ wire_name_get_current_time)r modelcontextkwargsr r r _handle_before_parameter_buildbs z2MonitorEventAdapter._handle_before_parameter_buildcKs6|j}|dj|d}|j|_|j|_||d<dS)Nr+)r*current_api_call_attempt_event)r/new_api_call_attemptr-headersrequest_headersurl)r requestr0r/Znew_attempt_eventr r r _handle_request_createdis z+MonitorEventAdapter._handle_request_createdcKsT|d}|||_|dur%|dd|_|dd|_|d|_|S||_|S)Nr2ResponseMetadataZHTTPStatusCodeZ HTTPHeadersError)pop _get_latencylatencyhttp_status_coderesponse_headersget parsed_errorwire_exception)r Zparsed_responser/ exceptionr0 attempt_eventr r r _handle_response_receivedrs"   z-MonitorEventAdapter._handle_response_receivedcKs |ddd|d_||S)Nr9ZMaxAttemptsReachedFr+)r@retries_exceeded_complete_api_call)r r/parsedr0r r r _handle_after_calls  z&MonitorEventAdapter._handle_after_callcKs|||d_||SNr+)_is_retryable_exceptionrFrG)r r/rCr0r r r _handle_after_call_errors  z,MonitorEventAdapter._handle_after_call_errorcCst|ttdS)NZGENERAL_CONNECTION_ERROR) isinstancetupleRETRYABLE_EXCEPTIONS)r rCr r r rKs z+MonitorEventAdapter._is_retryable_exceptioncCs|d}|||_|SrJ)r;r<r=)r r/Z call_eventr r r rGs  z&MonitorEventAdapter._complete_api_callcCs||jSN)r-r*r eventr r r r<sz MonitorEventAdapter._get_latencycCst|dS)Ni)intrr r r r r-sz%MonitorEventAdapter._get_current_timeN)rrrrr rr r1r8rErIrLrKrGr<r-r r r r rDs   rc@s$eZdZddZddZddZdS)BaseMonitorEventcCs||_||_||_dS)aBase 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 Nr')r r(r)r*r r r r s zBaseMonitorEvent.__init__cCs|jjd|jdS)N()) __class__r__dict__rTr r r __repr__szBaseMonitorEvent.__repr__cCst||jr |j|jkSdS)NF)rMrXrY)r otherr r r __eq__s  zBaseMonitorEvent.__eq__N)rrrr rZr\r r r r rUs rUcs,eZdZ   dfdd ZddZZS)r,NFcs6tj|||d||_||_|durg|_||_dS)aMonitor 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'N)superr r=attemptsrF)r r(r)r*r=r^rFrXr r r s$ zAPICallEvent.__init__cCs"t|j|j|d}|j||S)zInstantiates APICallAttemptEvent associated to the APICallEvent :type timestamp: int :param timestamp: Epoch time in milliseconds to associate to the APICallAttemptEvent r')APICallAttemptEventr(r)r^append)r r*rDr r r r3s   z!APICallEvent.new_api_call_attempt)NNF)rrrr r3 __classcell__r r r_r r,s -r,cs,eZdZ       dfdd ZZS)r`Nc s@tj|||d||_||_||_||_||_| |_| |_dS)aMonitor 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'N) r]r r=r6r>r5r?rArB) r r(r)r*r=r6r>r5r?rArBr_r r r s9 zAPICallAttemptEvent.__init__)NNNNNNN)rrrr rbr r r_r r`sr`c@seZdZdZdZdZdZdZddddZe d e d d Z gd Z d dZ ddZddZddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Z d5d6Z!d7d8Z"d9d:Z#d;d<Z$d=d>Z%d?S)@ CSMSerializeriZXAmznRequestIdZ XAmzRequestIdZXAmzId2)zx-amzn-requestidzx-amz-request-idz x-amz-id-2zSAWS4-HMAC-SHA256 Credential=(?P\w+)/\d+/(?P[a-z0-9-]+)/zAWS (?P\w+):)v4Zs3) r(r)r*r^r=rFr6r5r>r?rArBcCs||||_dS)zSerializes 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_idr rir r r r ds  zCSMSerializer.__init__cCs*t||jkrtd|d|jddS)Nz&The value provided for csm_client_id: z exceeds the maximum length of z characters)len_MAX_CLIENT_ID_LENGTH ValueErrorrjr r r rhnsz!CSMSerializer._validate_client_idcCsf||}||}||d<|jD]}t||d}|dur)t|d||||dqttj|ddS)zSerializes 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 TypeNZ _serialize_) event_type),:) separators)_get_base_event_dict_get_event_type_SERIALIZEABLE_EVENT_PROPERTIESr$rjsondumps)r rR event_dictroattrvaluer r r serializeus    zCSMSerializer.serializecCs d|jdS)N)VersionZClientId)rirQr r r rssz"CSMSerializer._get_base_event_dictcK ||d<dS)NZServicer )r r(rxr0r r r _serialize_service z CSMSerializer._serialize_servicecKr~)NZApir )r r)rxr0r r r _serialize_operationrz"CSMSerializer._serialize_operationcKr~)N Timestampr )r r*rxr0r r r _serialize_timestamprz"CSMSerializer._serialize_timestampcKs(t||d<|r|||ddSdS)NZ AttemptCount)rk_add_fields_from_last_attempt)r r^rxr0r r r _serialize_attemptss z!CSMSerializer._serialize_attemptscCs|jr||j}|dur||d<||j|d<|jdur#|j|d<|jdur0||j|d|jdur?||j|ddSdS)NRegion UserAgentZFinalHttpStatusCodeApiCall)r5 _get_region_get_user_agentr>rA_serialize_parsed_errorrB_serialize_wire_exception)r rx last_attemptregionr r r rs$     z+CSMSerializer._add_fields_from_last_attemptcCs,|dkr ||d<dS|dkr||d<dSdS)NrZLatencyApiCallAttemptZAttemptLatencyr )r r=rxror r r _serialize_latencys   z CSMSerializer._serialize_latencycKs|rdnd|d<dS)Nr|rZMaxRetriesExceededr )r rFrxr0r r r _serialize_retries_exceededsz)CSMSerializer._serialize_retries_exceededcKst|j|d<dS)NZFqdn)rnetloc)r r6rxr0r r r _serialize_urlszCSMSerializer._serialize_urlcKs\|||d<||r|||d<||}|dur ||d<d|vr,|d|d<dSdS)NrZ AccessKeyrzX-Amz-Security-TokenZ SessionToken)r _is_signed_get_access_keyr)r r5rxr0rr r r _serialize_request_headerss   z(CSMSerializer._serialize_request_headerscKr~)NZHttpStatusCoder )r r>rxr0r r r _serialize_http_status_codes z)CSMSerializer._serialize_http_status_codecKs,|jD]\}}||vr||||<qdSrP)"_RESPONSE_HEADERS_TO_EVENT_ENTRIESitems)r r?rxr0headerentryr r r _serialize_response_headerss  z)CSMSerializer._serialize_response_headerscKsH|dkrdnd}||d|j||d<||d|j||d<dS)NrFinalZCodeZ AwsExceptionMessageZAwsExceptionMessage) _truncate_MAX_ERROR_CODE_LENGTH_MAX_MESSAGE_LENGTH)r rArxror0 field_prefixr r r r   z%CSMSerializer._serialize_parsed_errorcKsH|dkrdnd}||jj|j||d<|t||j||d<dS)NrrrZ SdkExceptionZSdkExceptionMessage)rrXr_MAX_EXCEPTION_CLASS_LENGTHstrr)r rBrxror0rr r r rrz'CSMSerializer._serialize_wire_exceptioncCs t|trdSt|trdSdS)Nrr)rMr,r`rQr r r rts  zCSMSerializer._get_event_typecCs"||}||\}}|dS)NZ access_key)_get_auth_value_get_auth_matchgroup)r r5auth_valr# auth_matchr r r rs  zCSMSerializer._get_access_keycCs<||sdS||}||\}}|dkrdS|dS)NrgZsigning_region)rrrr)r r5rsignature_versionrr r r rs   zCSMSerializer._get_regioncCs|t|dd|jS)Nz User-Agentr)rrr@_MAX_USER_AGENT_LENGTHr r5r r r rszCSMSerializer._get_user_agentcCsd|vSN Authorizationr rr r r r szCSMSerializer._is_signedcCs t|dSr)rrr r r r rzCSMSerializer._get_auth_valuecCs2|jD]\}}||}|r||fSqdS)N)NN) _AUTH_REGEXSrmatch)r rrregexrr r r rs   zCSMSerializer._get_auth_matchcCs*t||krtd|||d|S|S)Nz6Truncating following value to maximum length of %s: %s)rkrr)r text max_lengthr r r rs  zCSMSerializer._truncateN)&rrrrlrrrrrrecompilerrur rhr{rsrrrrrrrrrrrrrrtrrrrrrrr r r r rcBsR     rcc@s eZdZdZddZddZdS)SocketPublisheri cCs||_||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 sockethostport serializerr r r r %s  zSocketPublisher.__init__cCsF|j|}t||jkrtdt||jdS|j||jdS)zPublishes 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) rr{rk_MAX_MONITOR_EVENT_LENGTHrrrsendtor)r rRZserialized_eventr r r r:s zSocketPublisher.publishN)rrrrr rr r r r r"s r)rvloggingrrZbotocore.compatrrrZbotocore.retryhandlerrrO getLoggerrrrrrUr,r`rcrr r r r s   ,`<Fa