
    uP                         S SK r S SKrS SKrS SKrS SKJrJrJr  S SKJ	r
  \R                  " \5      r " S S5      r " S S5      r " S S	5      r " S
 S\5      r " S S\5      r " S S5      r " S S5      rg)    N)ensure_bytesensure_unicodeurlparse)EXCEPTION_MAPc                   .    \ rS rSr/ SQrS rS rS rSrg)Monitor   )zbefore-parameter-buildzrequest-createdzresponse-receivedz
after-callzafter-call-errorc                     Xl         X l        g)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)selfadapter	publishers      &lib/third_party/botocore/monitoring.py__init__Monitor.__init__!   s      #    c                 `    U R                    H  nUR                  X R                  5        M      g)z(Register an event emitter to the monitorN)_EVENTS_TO_REGISTERregister_lastcapture)r   event_emitterevent_to_registers      r   registerMonitor.register,   s&    !%!9!9''(9<<H ":r   c                      U R                   R                  X5      nU(       a  U R                  R                  U5        gg! [         a   n[
        R                  SUUSS9   SnAgSnAff = f)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)r   feedr   publish	Exceptionloggerdebug)r   
event_namepayloadmonitor_eventes        r   r   Monitor.capture1   sb    
	 MM..zCM''6  	LLL	  	s   =A 
A+A&&A+r   N)	__name__
__module____qualname____firstlineno__r   r   r   r   __static_attributes__ r   r   r   r      s    	$I
r   r   c                   v    \ rS rSr\R                  4S jrS rS rS rS r	S r
S rS	 rS
 rS rS rS rSrg)MonitorEventAdapterD   c                     Xl         g)zAdapts event emitter events to produce monitor events

:type time: callable
:param time: A callable that produces the current time
N_time)r   times     r   r   MonitorEventAdapter.__init__E   s	     
r   c                 0    U R                  U5      " S0 UD6$ )aX  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
r.   )_get_handler)r   emitter_event_nameemitter_payloads      r   r   MonitorEventAdapter.feedM   s       !34GGGr   c                 d    [        U SUR                  S5      S   R                  SS5      -   5      $ )N_handle_.r   -_)getattrsplitreplace)r   r$   s     r   r8    MonitorEventAdapter._get_handler]   s6    *z//4Q7??SII
 	
r   c                 x    [        UR                  R                  UR                  U R	                  5       S9US'   g )Nservice	operation	timestampcurrent_api_call_event)APICallEventservice_model
service_id	wire_name_get_current_time)r   modelcontextkwargss       r   _handle_before_parameter_build2MonitorEventAdapter._handle_before_parameter_buildb   s4    ,8''22oo,,.-
()r   c                     UR                   nUS   R                  U R                  5       S9nUR                  Ul        UR
                  Ul        XCS'   g )NrJ   )rI   current_api_call_attempt_event)rQ   new_api_call_attemptrO   headersrequest_headersurl)r   requestrR   rQ   new_attempt_events        r   _handle_request_created+MonitorEventAdapter._handle_request_createdi   sW    //#$


)?)?)A

B 	 -4OO) '4E01r   c                     UR                  S5      nU R                  U5      Ul        Ub2  US   S   Ul        US   S   Ul        UR                  S5      Ul        U$ X5l        U$ )NrV   ResponseMetadataHTTPStatusCodeHTTPHeadersError)pop_get_latencylatencyhttp_status_coderesponse_headersgetparsed_errorwire_exception)r   parsed_responserQ   	exceptionrR   attempt_events         r   _handle_response_received-MonitorEventAdapter._handle_response_receivedr   s      $DE $ 1 1- @&-<".. M* .="..M* *9)<)<W)EM&  ,5(r   c                 ^    US   R                  SS5      US   l        U R                  U5      $ )Nr`   MaxAttemptsReachedFrJ   )ri   retries_exceeded_complete_api_call)r   rQ   parsedrR   s       r   _handle_after_call&MonitorEventAdapter._handle_after_call   s;    =C>

#"E
* 	(): &&w//r   c                 V    U R                  U5      US   l        U R                  U5      $ NrJ   )_is_retryable_exceptionrs   rt   )r   rQ   rm   rR   s       r   _handle_after_call_error,MonitorEventAdapter._handle_after_call_error   s3     "99)D 	$	

&&w//r   c                 :    [        U[        [        S   5      5      $ )NGENERAL_CONNECTION_ERROR)
isinstancetupleRETRYABLE_EXCEPTIONS)r   rm   s     r   rz   +MonitorEventAdapter._is_retryable_exception   s     u12LMN
 	
r   c                 T    UR                  S5      nU R                  U5      Ul        U$ ry   )rd   re   rf   )r   rQ   
call_events      r   rt   &MonitorEventAdapter._complete_api_call   s*    [[!9:
!..z:
r   c                 <    U R                  5       UR                  -
  $ N)rO   rI   r   events     r   re    MonitorEventAdapter._get_latency   s    %%'%//99r   c                 :    [        U R                  5       S-  5      $ )Ni  )intr4   r   s    r   rO   %MonitorEventAdapter._get_current_time   s    4::<$&''r   r3   N)r)   r*   r+   r,   r5   r   r   r8   rS   r]   ro   rv   r{   rz   rt   re   rO   r-   r.   r   r   r0   r0   D   sG     II H 


F"00


:(r   r0   c                   &    \ rS rSrS rS rS rSrg)BaseMonitorEvent   c                 (    Xl         X l        X0l        g)aC  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
NrF   )r   rG   rH   rI   s       r   r   BaseMonitorEvent.__init__   s     ""r   c                 P    U R                   R                   SU R                  < S3$ )N())	__class__r)   __dict__r   s    r   __repr__BaseMonitorEvent.__repr__   s%    ..))*!DMM+<A>>r   c                 j    [        XR                  5      (       a  U R                  UR                  :H  $ g)NF)r   r   r   )r   others     r   __eq__BaseMonitorEvent.__eq__   s'    e^^,,==ENN22r   )rH   rG   rI   N)r)   r*   r+   r,   r   r   r   r-   r.   r   r   r   r      s    #$?r   r   c                   8   ^  \ rS rSr   SU 4S jjrS rSrU =r$ )rK      c                 Z   > [         TU ]  XUS9  X@l        XPl        Uc  / U l        X`l        g)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
rF   N)superr   rf   attemptsrs   )r   rG   rH   rI   rf   r   rs   r   s          r   r   APICallEvent.__init__   s<    H 	I 	 	
  DM 0r   c                 z    [        U R                  U R                  US9nU R                  R	                  U5        U$ )zInstantiates APICallAttemptEvent associated to the APICallEvent

:type timestamp: int
:param timestamp: Epoch time in milliseconds to associate to the
    APICallAttemptEvent
rF   )APICallAttemptEventrG   rH   r   append)r   rI   rn   s      r   rW   !APICallEvent.new_api_call_attempt   s7     ,LLDNNi
 	]+r   )r   rf   rs   )NNF)r)   r*   r+   r,   r   rW   r-   __classcell__r   s   @r   rK   rK      s     +1Z r   rK   c                   :   ^  \ rS rSr       SU 4S jjrSrU =r$ )r      c                 v   > [         TU ]  XUS9  X@l        XPl        X`l        Xpl        Xl        Xl        Xl        g)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)
rF   N)	r   r   rf   rZ   rg   rY   rh   rj   rk   )r   rG   rH   rI   rf   rZ   rg   rY   rh   rj   rk   r   s              r   r   APICallAttemptEvent.__init__   sG    r 	I 	 	
  0. 0(,r   )rg   rf   rj   rY   rh   rZ   rk   )NNNNNNN)r)   r*   r+   r,   r   r-   r   r   s   @r   r   r      s&     B- B-r   r   c                      \ rS rSrSrSrSrSrSrSSSS	.r	\
R                  " S
5      \
R                  " S5      S.r/ SQrS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS  r S! r!S" r"S# r#S$ r$S% r%S& r&S'r'g())CSMSerializeriB           i   XAmznRequestIdXAmzRequestIdXAmzId2)zx-amzn-requestidzx-amz-request-idz
x-amz-id-2zSAWS4-HMAC-SHA256 Credential=(?P<access_key>\w+)/\d+/(?P<signing_region>[a-z0-9-]+)/zAWS (?P<access_key>\w+):)v4s3)rG   rH   rI   r   rf   rs   rZ   rY   rg   rh   rj   rk   c                 2    U R                  U5        Xl        g)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_idr   r   s     r   r   CSMSerializer.__init__d  s     	  /*r   c                 n    [        U5      U R                  :  a  [        SU SU R                   S35      eg )Nz&The value provided for csm_client_id: z exceeds the maximum length of z characters)len_MAX_CLIENT_ID_LENGTH
ValueErrorr   s     r   r   !CSMSerializer._validate_client_idn  sH    } : ::8 H))-)C)C(DKQ  ;r   c                     U R                  U5      nU R                  U5      nX2S'   U R                   H(  n[        XS5      nUc  M  [        U SU-   5      " XRUS9  M*     [	        [
        R                  " USS95      $ )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
TypeN_serialize_)
event_type),:)
separators)_get_base_event_dict_get_event_type_SERIALIZEABLE_EVENT_PROPERTIESrA   r   jsondumps)r   r   
event_dictr   attrvalues         r   	serializeCSMSerializer.serializeu  s     ..u5
))%0
'688DE.E md23* 9 DJJzjIJJr   c                      SU R                   S.$ )N   )VersionClientIdr   r   s     r   r   "CSMSerializer._get_base_event_dict  s    **
 	
r   c                     XS'   g )NServicer.   )r   rG   r   rR   s       r   _serialize_service CSMSerializer._serialize_service  s
     '9r   c                     XS'   g )NApir.   )r   rH   r   rR   s       r   _serialize_operation"CSMSerializer._serialize_operation  s
    %5r   c                     XS'   g )N	Timestampr.   )r   rI   r   rR   s       r   _serialize_timestamp"CSMSerializer._serialize_timestamp  s
    "+;r   c                 X    [        U5      US'   U(       a  U R                  X!S   5        g g )NAttemptCount)r   _add_fields_from_last_attempt)r   r   r   rR   s       r   _serialize_attempts!CSMSerializer._serialize_attempts  s*    %(]
>"..zB<H r   c                    UR                   (       a@  U R                  UR                   5      nUb  X1S'   U R                  UR                   5      US'   UR                  b  UR                  US'   UR                  b  U R                  UR                  US5        UR                  b  U R                  UR                  US5        g g )NRegion	UserAgentFinalHttpStatusCodeApiCall)rY   _get_region_get_user_agentrg   rj   _serialize_parsed_errorrk   _serialize_wire_exception)r   r   last_attemptregions       r   r   +CSMSerializer._add_fields_from_last_attempt  s    '' %%l&B&BCF!'-8$&*&:&:,,'J{# ((40<0M0MJ,-$$0(()):y &&2**++Z 3r   c                 0    US:X  a  XS'   g US:X  a  XS'   g g )Nr   LatencyApiCallAttemptAttemptLatencyr.   )r   rf   r   r   s       r   _serialize_latency CSMSerializer._serialize_latency  s)    "$+y!+++2'( ,r   c                      U(       a  SOSUS'   g )Nr   r   MaxRetriesExceededr.   )r   rs   r   rR   s       r   _serialize_retries_exceeded)CSMSerializer._serialize_retries_exceeded  s     1A1a
'(r   c                 4    [        U5      R                  US'   g )NFqdn)r   netloc)r   rZ   r   rR   s       r   _serialize_urlCSMSerializer._serialize_url  s    %c]11
6r   c                     U R                  U5      US'   U R                  U5      (       a  U R                  U5      US'   U R                  U5      nUb  XBS'   SU;   a	  US   US'   g g )Nr   	AccessKeyr   zX-Amz-Security-TokenSessionToken)r   
_is_signed_get_access_keyr   )r   rY   r   rR   r   s        r   _serialize_request_headers(CSMSerializer._serialize_request_headers  sz     #'"6"6"G
;???++&*&:&:?&KJ{#!!/2#)x !_4)8&*J~& 5r   c                     XS'   g )NHttpStatusCoder.   )r   rg   r   rR   s       r   _serialize_http_status_code)CSMSerializer._serialize_http_status_code  s     (8#$r   c                 d    U R                   R                  5        H  u  pEXA;   d  M  X   X%'   M     g r   )"_RESPONSE_HEADERS_TO_EVENT_ENTRIESitems)r   rh   r   rR   headerentrys         r   _serialize_response_headers)CSMSerializer._serialize_response_headers  s1     "DDJJLMF)$4$<
! Mr   c                     US:X  a  SOSnU R                  US   U R                  5      X%S-   '   U R                  US   U R                  5      X%S-   '   g )Nr   Final CodeAwsExceptionMessageAwsExceptionMessage)	_truncate_MAX_ERROR_CODE_LENGTH_MAX_MESSAGE_LENGTH)r   rj   r   r   rR   field_prefixs         r   r   %CSMSerializer._serialize_parsed_error  sb     #-	"9wr48NN $"="=5

.01 <@>>#T%=%=<

"778r   c                     US:X  a  SOSnU R                  UR                  R                  U R                  5      X%S-   '   U R                  [	        U5      U R
                  5      X%S-   '   g )Nr   r  r  SdkExceptionSdkExceptionMessage)r   r   r)   _MAX_EXCEPTION_CLASS_LENGTHstrr"  )r   rk   r   r   rR   r#  s         r   r   'CSMSerializer._serialize_wire_exception  si     #-	"9wr48NN$$--t/O/O5

.01 <@>>!9!9<

"778r   c                 \    [        U[        5      (       a  g[        U[        5      (       a  gg )Nr   r   )r   rK   r   r   s     r   r   CSMSerializer._get_event_type  s(    e\**233# 4r   c                 l    U R                  U5      nU R                  U5      u  p4UR                  S5      $ )N
access_key)_get_auth_value_get_auth_matchgroup)r   rY   auth_valr@   
auth_matchs        r   r  CSMSerializer._get_access_key  s5    ''8,,X6--r   c                     U R                  U5      (       d  g U R                  U5      nU R                  U5      u  p4US:w  a  g UR                  S5      $ )Nr   signing_region)r
  r/  r0  r1  )r   rY   r2  signature_versionr3  s        r   r   CSMSerializer._get_region  sU    //''8(,(<(<X(F%$ 011r   c                 l    U R                  [        UR                  SS5      5      U R                  5      $ )Nz
User-Agentr  )r   r   ri   _MAX_USER_AGENT_LENGTHr   rY   s     r   r   CSMSerializer._get_user_agent  s1    ~~?..|R@A''
 	
r   c                     SU;   $ NAuthorizationr.   r;  s     r   r
  CSMSerializer._is_signed
  s    /11r   c                     [        US   5      $ r>  )r   r;  s     r   r/  CSMSerializer._get_auth_value  s    oo>??r   c                     U R                   R                  5        H"  u  p#UR                  U5      nU(       d  M  X$4s  $    g)N)NN)_AUTH_REGEXSr  match)r   r2  r7  regexrE  s        r   r0  CSMSerializer._get_auth_match  sA    (,(9(9(?(?(A$KK)Eu(// )B r   c                 \    [        U5      U:  a  [        R                  SUU5        US U $ U$ )Nz6Truncating following value to maximum length of %s: %s)r   r"   r#   )r   text
max_lengths      r   r   CSMSerializer._truncate  s7    t9z!LLK
 $$r   r   N)(r)   r*   r+   r,   r   r(  r!  r:  r"  r  recompilerD  r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r   r   r  r   r   r
  r/  r0  r   r-   r.   r   r   r   r   B  s    "%  ,+*& jj/

 jj45L'#+K(
(&,I
*3H
28
=	
	
$.
2
2@r   r   c                   $    \ rS rSrSrS rS rSrg)SocketPublisheri"  i    c                 ,    Xl         X#4U l        X@l        g)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   sockethostport
serializers        r   r   SocketPublisher.__init__%  s    " %r   c                    U R                   R                  U5      n[        U5      U R                  :  a+  [        R                  S[        U5      U R                  5        gU R                  R                  X R                  5        g)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)	rS  r   r   _MAX_MONITOR_EVENT_LENGTHr"   r#   rQ  sendtorR  )r   r   serialized_events      r   r    SocketPublisher.publish:  sn      ++55e< 4#A#AALL<$%..	 ,mm<r   )rR  rS  rQ  N)r)   r*   r+   r,   rZ  r   r    r-   r.   r   r   rO  rO  "  s     (&*=r   rO  )r   loggingrL  r5   botocore.compatr   r   r   botocore.retryhandlerr   r   	getLoggerr)   r"   r   r0   r   rK   r   r   rO  r.   r   r   <module>rb     s      	  B B G			8	$) )X]( ](@ 89# 9xC-* C-L] ]@(= (=r   