
    n                        S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSK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 S\.5      r/ " S  S!\.5      r0SCS" jr1SCS# jr2 " S$ S%\.5      r3 " S& S'\.5      r4 " S( S)\.5      r5 " S* S+\.5      r6  SDS, jr7S- r8S. r9S/ r:S0 r;S1 r<S2 r=\9S3 5       r>S4 r?S5 r@\9S6 5       rA\9  SES7 j5       rB\9S8 5       rC\9SFS9 j5       rD\9SGS: j5       rE\9S; 5       rF\9S< 5       rG\9S= 5       rH\9S> 5       rI\9S? 5       rJ\R                  S@ 5       rL\9SA 5       rM\9SB 5       rNg)Hz&Used to collect anonymous SDK metrics.    )absolute_import)division)unicode_literalsN)config)execution_utils)log)
properties)console_attr)
console_io)encoding)	platformsInstallsCommandsHelpError
ExecutionsTestExecutionsCustomloadruntotalremotelocalstartzhttps://play.googleapis.com/logevent_metadata
error_typec                       \ rS rSrS rSrg)_EventD   c                 4    Xl         X l        X0l        X@l        g Ncategoryactionlabelvalue)selfr#   r$   r%   r&   s        "lib/googlecloudsdk/core/metrics.py__init___Event.__init__F   s    MKJJ    )r$   r#   r%   r&   N__name__
__module____qualname____firstlineno__r)   __static_attributes__ r+   r(   r   r   D   s    r+   r   c                       \ rS rSrSrS rSrg)CommonParamsM   z+Parameters common to all metrics reporters.c                    [         R                  " 5       nUR                  S5      (       a  SOSn[        R                  R                  5       n[        R                  " 5       U l        X0l	        [        U5      U l        [        R                  R                  U l        X l        [        R                   " 5       U l        [$        R&                  " SSS9U l        [*        R,                  " 5       U l        [        R.                  R0                  R2                  R5                  5       U l        [$        R8                  " 5       U l        [<        R>                  " 5       RA                  5       U l!        g )Nz.google.comGoogleExternalT)error	heuristic)"socketgethostnameendswithr   PlatformCurrentr   GetCID	client_idcurrent_platformGetUserAgent
user_agentINSTALLATION_CONFIGrelease_channelinstall_typer	   GetMetricsEnvironmentmetrics_environmentr   IsInteractiveis_interactiveplatformpython_versionVALUESmetricsenvironment_versionGetmetrics_environment_versionIsRunFromShellScriptis_run_from_shell_scriptr
   GetConsoleAttrGetTermIdentifierterm_identifier)r'   hostnamerG   rB   s       r(   r)   CommonParams.__init__P   s    !!#H'00??8ZL ))113]]_DN,"#34DO!55EED$)??AD$22ND"113D(2(9(9)01D1DSSU 	$$.$C$C$ED!'668JJLDr+   )rA   rB   rG   rK   rT   rI   rR   rM   rF   rW   rD   N)r-   r.   r/   r0   __doc__r)   r1   r2   r+   r(   r4   r4   M   s    3Mr+   r4   c                 h    [        [        U =(       d    [        R                  " 5       S-  5      5      $ )Ni  )introundtime)	time_secss    r(   GetTimeMillisr`   d   s"    	UI,45	66r+   c                     U =(       d    [         R                  R                  5       n SR                  [        R
                  U R                  5       S9$ )zConstructs a user agent string from config and platform fragments.

Args:
  current_platform: Optional platforms.Platform for pulling
    platform-specific user agent details.

Returns:
  str, The user agent for the current client.
zCloudSDK/{version} {fragment})versionfragment)r   r>   r?   formatr   CLOUD_SDK_VERSIONUserAgentFragment)rB   s    r(   rC   rC   h   sK     &E););)C)C)E	(	/	/&&113 
0 
5 5r+   c                       \ rS rSrS rSrg)_TimedEventy   c                     Xl         X l        g r!   nametime_millis)r'   rl   rm   s      r(   r)   _TimedEvent.__init__{   s    I"r+   rk   Nr,   r2   r+   r(   rh   rh   y   s    #r+   rh   c                   @    \ rS rSrSrS rS rS rSS jrS r	S	 r
S
rg)_CommandTimer   z.A class for timing the execution of a command.c                 t    SU l         / U l        SU l        SU l        SU l        SU l        S U l        S U l        g )Nr   unknown)_CommandTimer__start_CommandTimer__events!_CommandTimer__total_rpc_duration#_CommandTimer__total_local_duration_CommandTimer__category_CommandTimer__action_CommandTimer__label_CommandTimer__flag_namesr'   s    r(   r)   _CommandTimer.__init__   s=    DLDM !D"#DDODMDLDr+   c                 4    Xl         X l        X0l        X@l        g r!   rx   ry   rz   r{   )r'   r#   r$   r%   
flag_namess        r(   
SetContext_CommandTimer.SetContext   s    OML"r+   c                 ^    U R                   U R                  U R                  U R                  4$ r!   r   r|   s    r(   
GetContext_CommandTimer.GetContext   s#    ??DMM4<<9J9JJJr+   Nc                     [        U5      nU[        L a  X0l        g U R                  R	                  [        X5      5        U[        L a3  X0R                  -
  U l        U =R                  U R                  -  sl        g g r!   )	r`   _START_EVENTrt   ru   appendrh   _TOTAL_EVENTrw   rv   )r'   rl   
event_timerm   s       r(   Event_CommandTimer.Event   sf    
+K| lMMT78|$/,,$>d!
!!T%>%>>! r+   c                 .    U =R                   U-  sl         g r!   )rv   r'   duration_in_mss     r(   AddRPCDuration_CommandTimer.AddRPCDuration   s    /r+   c                     / nU R                    H7  nUR                  UR                  UR                  U R                  -
  45        M9     UR                  [        U R                  4[        U R                  4/5        U$ )z,Returns the timings for the recorded events.)
ru   r   rl   rm   rt   extend_LOCAL_EVENTrw   _REMOTE_EVENTrv   )r'   timingsevents      r(   
GetTimings_CommandTimer.GetTimings   so    Gnnejj%"3"3dll"BCD  NN	t223	112  Nr+   )__action
__category__events__flag_names__label__start__total_local_duration__total_rpc_durationr!   )r-   r.   r/   r0   rZ   r)   r   r   r   r   r   r1   r2   r+   r(   rp   rp      s$    6#K?0
r+   rp   c                   j    \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	   SS jr
S	 rS
 rSrg)_ClearcutMetricsReporter   z3A class for handling reporting metrics to Clearcut.c           	      6   UR                   U l        SSUR                  R                  R                  0S.SUR
                  S.U l        SUR                  4SUR                  4SUR                  4S	UR                  4S
UR                  4SUR                  4SUR                  4SUR                  4/nU Vs/ s H"  nUS   [        R                   " US   5      S.PM$     snU l        [$        R&                  nUSUR
                  S.U l        / U l        g s  snf )NDESKTOPos)client_typedesktop_client_infoCONCORD)client_infolog_source_namezwieback_cookierF   rG   environmentinteractiverM   rP   from_scripttermr      keyr&   CloudSDK)release_versionconsole_typeclient_install_id)rD   _user_agentrB   operating_systemidrA   _clearcut_request_paramsrF   rG   rI   rK   rM   rR   rT   rW   six	text_type _clearcut_concord_event_metadatar   re   _clearcut_concord_event_params_clearcut_concord_timed_events)r'   common_paramsr   paramcloud_sdk_versions        r(   r)   !_ClearcutMetricsReporter.__init__   s7   $//D %m44EEHH$
 %(22	%D! 
M99:	334	99:	445	=778	 I IJ	>>?	../	N "-#!% Qx#--a"9.!-#D) 00,"*44+D' +-D'-#s   8)Dc                     U R                   $ r!   )r   r|   s    r(   r   '_ClearcutMetricsReporter.event_metadata   s    000r+   c                     U R                   $ r!   )r   r|   s    r(   event_params%_ClearcutMetricsReporter.event_params   s    ...r+   c                     U R                   $ r!   )r   r|   s    r(   request_params'_ClearcutMetricsReporter.request_params   s    (((r+   Nc                    [        U R                  5      nUR                  US'   UR                  US'   [	        U R
                  5      U[        '   / nUb(  UR                  S[        R                  " U5      S.5        Ub  UR                  [        US.5        Ub  UR                  SUS.5        UR                  [        L a  UR                  SUR                  S.5        OUR                  [        L a  UR                  SUR                  S.5        OUR                  [        L a#  UR                  [        UR                  S.5        OmUR                  [        L a  UR                  S	UR                  S.5        O;UR                  [         L a(  UR                  UR                  UR"                  S.5        U[           R%                  U5        U R&                  R                  U[)        5       45        g)
  Records the given event.

Args:
  event: _Event, The event to process.
  flag_names: str, Comma separated list of flag names used with the action.
  error: class, The class (not the instance) of the Exception if a user
    tried to run a command that produced an error.
  error_extra_info_json: {str: json-serializable}, A json serializable dict
    of extra info that we want to log with the error. This enables us to
    write queries that can understand the keys and values in this dict.

event_type
event_nameNr   r   extra_error_infobinary_version	help_modecomponent_version)dictr   r#   r$   listr   _CLEARCUT_EVENT_METADATA_KEYr   r   r   _CLEARCUT_ERROR_TYPE_KEY_EXECUTIONS_CATEGORYr%   _HELP_CATEGORY_ERROR_CATEGORY_INSTALLS_CATEGORY_CUSTOM_CATEGORYr&   r   r   r`   )r'   r   r   r9   error_extra_info_jsonconcord_eventr   s          r(   Record_ClearcutMetricsReporter.Record   s     **+M"'..M,"',,M,263M./ N==, 	 $<uMN($6&;= > ~~--$4u{{KL	>	)K%++FG	?	**U[[
AC	-	-$7%++NO	+	+EKK%++FG./66~F''..0=0A Br+   c                     SnUR                  5       n/ nU H6  nU(       d  US   [        :X  a  US   nUR                  US   US   S.5        M8     X$4$ )z"Extracts relevant data from timer.Nr   r   )r   
latency_ms)r   r   r   )r'   timertotal_latencyr   sub_event_latenciestimings         r(   Timings _ClearcutMetricsReporter.Timings  sh    M GvayL8q	  q	" 		  --r+   c                    [        U R                  5      n[        5       US'   U R                  U5      u  p4SnU R                   H-  u  pgUS   [
        L a	  U(       a  M  X6S'   XFS'   US   [
        L nM/     / US'   U R                   H/  u  phUS   R                  [        R                  " USS9US	.5        M1     [        R                  " USS9n	S
U R                  0n
[        SX4$ )z*Collect the required clearcut HTTP beacon.request_time_msFr   r   sub_event_latency_ms	log_eventT	sort_keys)source_extension_jsonevent_time_msz
user-agentPOST)r   r   r`   r   r   _COMMANDS_CATEGORYr   jsondumpsr   _CLEARCUT_ENDPOINT)r'   r   clearcut_requestevent_latencyr   command_latency_setr   _r   dataheaderss              r(   ToHTTPBeacon%_ClearcutMetricsReporter.ToHTTPBeacon,  s    D//0*7/&')-e)<&M ??

%);
;
$1L!.A*+),7;MM @ %'[!(,(K(K${#**#'::mt#L(, 	 )L ::&$7DT--.G66r+   )r   r   r   r   r   NNN)r-   r.   r/   r0   rZ   r)   propertyr   r   r   r   r   r   r1   r2   r+   r(   r   r      sc    ;#-J 1 1 / / ) )
 #'	2Bh."7r+   r   c                       \ rS rSrSrSrSrSr\S 5       r	\S 5       r
\SS j5       r\S 5       rS rS	 rS
 r  SS jrS rSS jr   SS jrS rS rSS jrSrg)_MetricsCollectoriG  z.A singleton class to handle metrics reporting.Nc                  "    [         R                  $ r!   )r  	_instancer2   r+   r(   GetCollectorIfExists&_MetricsCollector.GetCollectorIfExistsN  s    &&&r+   c                      [         R                  5       (       a  g[         R                  (       d  [        5       [         l        [         R                  $ )zEReturns the singleton _MetricsCollector instance or None if disabled.N)r  _IsDisabledr  r2   r+   r(   GetCollector_MetricsCollector.GetCollectorR  s7     $$&&&&$5$7!&&&r+   c                     U [         l        [         R                  5       (       a  S[         l        g[        5       [         l        g)a  Reset the singleton _MetricsCollector and reinitialize it.

This should only be used for tests, where we want to collect some metrics
but not others, and we have to reinitialize the collector with a different
Google Analytics tracking id.

Args:
  disable_cache: Metrics collector keeps an internal cache of the disabled
      state of metrics. This controls the value to reinitialize the cache.
      None means we will refresh the cache with the default values.
      True/False forces a specific value.
N)r  _disabled_cacher	  r  )disable_caches    r(   ResetCollectorInstance(_MetricsCollector.ResetCollectorInstance\  s1     )6%$$&&$(!$5$7!r+   c                     [         R                  c  S[        R                  ;   a  S[         l        [         R                  $ [        R
                  " 5       (       d  S[         l        [         R                  $ [        R                  R                  R                  R                  5       n U c  [        R                  R                  n U [         l        [         R                  $ )z6Returns whether metrics collection should be disabled._ARGCOMPLETET)r  r  r   environr	   IsDefaultUniverserN   coredisable_usage_reportingGetBoolr   rE   )disableds    r(   r	  _MetricsCollector._IsDisabledp  s     ((0	2::	%,0) ,,, ++--,0) ,,, $$))AAIIK//GG(,4),,,r+   c                    [        5       n[        U5      /U l        [        5       U l        / U l        SU l        [        R                  R                  5       nUR                  5       U l        [        R                  " S5        g)zInitialize a new MetricsCollector.

This should only be invoked through the static GetCollector() function or
the static ResetCollectorInstance() function.
r   z Metrics collector initialized...N)r4   r   _metrics_reportersrp   _timer_metrics_action_levelr   r>   r?   AsyncPopenArgs_async_popen_argsr   debug)r'   r   rB   s      r(   r)   _MetricsCollector.__init__  sn     !NM 	!/D  /DKDM D ))113-<<>DII01r+   c                 .    U =R                   S-  sl         g Nr   r  r|   s    r(   IncrementActionLevel&_MetricsCollector.IncrementActionLevel      !r+   c                 .    U =R                   S-  sl         g r$  r%  r|   s    r(   DecrementActionLevel&_MetricsCollector.DecrementActionLevel  r(  r+   c                 f    U R                   S:X  d  U(       d  U R                  R                  XS9  gg)zRecords the time when a particular event happened.

Args:
  name: str, Name of the event.
  record_only_on_top_level: bool, Whether to record only on top level.
  event_time: float, Time when the event happened in secs since epoch.
r   )r   N)r  r  r   )r'   rl   record_only_on_top_levelr   s       r(   RecordTimedEvent"_MetricsCollector.RecordTimedEvent  s/     Q&>
kk4 '?r+   c                 :    U R                   R                  U5        g)zwRecords the time when a particular event happened.

Args:
  duration_in_ms: int, Duration of the RPC in milli seconds.
N)r  r   r   s     r(   RecordRPCDuration#_MetricsCollector.RecordRPCDuration  s     	KK~.r+   c                     U[         L a  U R                  S:w  a  gU[        L a.  U R                  S:w  a  U R                  R	                  5       u  pR  nU R                  R                  XX45        g)a;  Sets the context for which the timer is collecting timed events.

Args:
  category: str, Category of the action being timed.
  action: str, Name of the action being timed.
  label: str, Additional information about the action being timed.
  flag_names: str, Comma separated list of flag names used with the action.
r   N)r   r  r   r  r   r   )r'   r#   r$   r%   r   r   s         r(   SetTimerContext!_MetricsCollector.SetTimerContext  s`     %%$*<*<*A ?"t'9'9Q'>..0oaAKK8U?r+   c                 N    U R                    H  nUR                  UUUUS9  M     g)r   r   r9   r   N)r  r   )r'   r   r   r9   r   metrics_reporters         r(   r   _MetricsCollector.Record  s4      !33
 5	  7 4r+   c                 ~    U R                    H-  nUR                  U R                  5      nU R                  " U6   M/     g r!   )r  r   r  CollectHTTPBeacon)r'   r8  http_beacons      r(   CollectMetrics _MetricsCollector.CollectMetrics  s5     33$11$++>k
k* 4r+   c                 >    U R                   R                  XX445        ga  Record a custom event to an arbitrary endpoint.

Args:
  url: str, The full url of the endpoint to hit.
  method: str, The HTTP method to issue.
  body: str, The body to send with the request.
  headers: {str: str}, A map of headers to values to include in the request.
N)r  r   )r'   urlmethodbodyr   s        r(   r;  #_MetricsCollector.CollectHTTPBeacon  s     	MM#t56r+   c                 6   U R                   (       d  g[        R                  " SS9nU   [        R                  " U R                   U5        / U l         SSS5        [
        R                  " [        5      n[        R                  R                  [        R                  R                  [        R                  R                  U5      S5      5      n[        R                  " XBR                  5      nU Vs/ s H  n[
        R                   " U5      PM     nn[        R"                  R%                  5       n[
        R&                  " US[        R(                  R                  [*        R                  5      5         [,        R.                  " U4SU0U R0                  D6n[2        R4                  " S5        U(       a'  WR9                  5         [2        R4                  " S	5        gg! , (       d  f       GN= fs  snf ! [6         a    [2        R4                  " S5         Nhf = f)
z=Reports the collected metrics using a separate async process.NF)deletezmetrics_reporter.py
PYTHONPATHenvz$Metrics reporting process started...z*Metrics reporting process failed to start.z#Metrics reporting process finished.)r  tempfileNamedTemporaryFilepickledumpr   Decode__file__r   pathrealpathjoindirnamer   ArgsForPythonToolrl   Encoder  copySetEncodedValuepathsepsys
subprocessPopenr   r   r!  OSErrorcommunicate)	r'   wait_for_reporttemp_metrics_file	this_filereporting_script_pathexecution_argsaexec_envps	            r(   ReportMetrics_MetricsCollector.ReportMetrics  sz   == 335A	kk$--!23dm 
 )IGG,,
RWW__Y/1FGI$66557N 3AA.Qhooa(.NAzz HX|RZZ__SXX5NO>


> 5x 5!335a	ii67
  mmo	ii56	 5 
	 B  > 
ii<=>s#   )G2 G069G5 
G-5 HH)r  r   r  r  r  r!   )FNNNr   )F)r-   r.   r/   r0   rZ   r  r  
test_groupstaticmethodr  r
  r  r	  r)   r&  r*  r.  r1  r4  r   r=  r;  re  r1   r2   r+   r(   r  r  G  s    6/)*' ' ' ' 8 8& - -"2. =B"&
5/@* #'	7.+
	7$7r+   r  c                 ^   [         R                  5       nU(       d  g[         R                  (       a  U [        La  [         R                  n[	        XX#S9nUR                  UUUUS9  U [        [        4;   a  UR                  XUS9  gU [        [        [        4;   a  UR                  XX$S9  gg)z$Common code for processing an event.Nr"   r7  r   )r  r
  rh  r   r   r   r   r   r4  r   _TEST_EXECUTIONS_CATEGORY)	r#   r$   r%   r&   r   r9   r   	collectorr   s	            r(   _RecordEventAndSetTimerContextrn    s      ,,.)	
 !!ho&E((E
(
L%1	  3 $&:;;h:FO^-/ /hM/r+   c                 N    U c  gU (       d  gSR                  [        U 5      5      $ )N z==NONE==,)rQ  sortedrk  s    r(   _GetFlagNameStringrs  1  s%    		&$	%%r+   c                    ^  U 4S jnU$ )z5Function decorator to capture and log any exceptions.c                  d   >  T" U 0 UD6$ !   [         R                  " STR                  SS9   g = f)NzException captured in %sT)exc_info)r   r!  r-   )argskwdsfuncs     r(   Wrapper'CaptureAndLogException.<locals>.Wrapper>  s6    J4 4  J	ii*DMMDIs    "/r2   )ry  rz  s   ` r(   CaptureAndLogExceptionr|  <  s    J 
.r+   c                 d    [         R                  S5        U [         l        [        [        UU SS9  g )NFr   )r&   )r  r  rh  rn  rl  )test_group_idtest_methods     r(   StartTestMetricsr  G  s-    **51!. 	r+   c                      [         R                  5       n U (       a  U R                  SS9  S [         l        [         R	                  S5        g )NT)r]  )r  r  re  rh  r  rm  s    r(   StopTestMetricsr  Q  s;    446)D1!%**40r+   c                  `    [         R                  5       (       a  g[        R                  " 5       $ )zGets the client id if metrics collection is enabled.

Returns:
  str, The hex string of the client id if metrics is enabled, else an empty
  string.
rp  )r  r	  r   r@   r2   r+   r(   GetCIDIfMetricsEnabledr  Y  s$     ""$$ 	r+   c                  J    [         R                  5       (       d
  [        5       $ g)zGets the user agent if metrics collection is enabled.

Returns:
  The complete user agent string if metrics is enabled, else None.
N)r  r	  rC   r2   r+   r(   GetUserAgentIfMetricsEnabledr  i  s     
	&	&	(	(>	r+   c                      [         R                  5       n U (       a6  U R                  [        5        U R	                  5         U R                  5         gg)z(Reports the metrics that were collected.N)r  r  r.  r   r=  re  r  s    r(   Shutdownr  v  s@      446)|, r+   c                 n    U (       a'   SR                  U R                  U R                  5      $ g!    g= f)zGets a friendly exception name for the given error.

Args:
  error: An exception class.

Returns:
  str, The name of the exception to log.
z{0}.{1}rs   N)rd   r.   r-   )r9   s    r(   _GetExceptionNamer    s8     e..?? 
s   %0 4c                 <    U (       a  [         R                  " U SS9$ g)aQ  Serializes the extra info into a json string for logging.

Args:
  error_extra_info: {str: json-serializable}, A json serializable dict of
    extra info that we want to log with the error. This enables us to write
    queries that can understand the keys and values in this dict.

Returns:
  str, The value to pass to Clearcut or None.
Tr   N)r   r   )error_extra_infos    r(   _GetErrorExtraInfor    s     ::&$77	r+   c                 $    [        [        X5        g)zLogs that an SDK component was installed.

Args:
  component_id: str, The component id that was installed.
  version_string: str, The version of the component.
N)rn  r   )component_idversion_strings     r(   r   r     s     !,8r+   c           
      \    [        [        X[        U5      [        U5      [	        U5      S9  g)aP  Logs that a gcloud command was run.

Args:
  command_path: [str], The '.' separated name of the calliope command.
  version_string: [str], The version of the command.
  flag_names: [str], The names of the flags that were used during this
    execution.
  error: class, The class (not the instance) of the Exception if a user
    tried to run a command that produced an error.
  error_extra_info: {str: json-serializable}, A json serializable dict of
    extra info that we want to log with the error. This enables us to write
    queries that can understand the keys and values in this dict.
r7  N)rn  r   rs  r  r  )command_pathr  r   r9   r  s        r(   r   r     s+      !,#J/e$./?@	Br+   c                 $    [        [        X5        g)zLogs that help for a gcloud command was run.

Args:
  command_path: str, The '.' separated name of the calliope command.
  mode: str, The way help was invoked (-h, --help, help).
N)rn  r   )r  modes     r(   r   r     s     !Dr+   c           	      \    [        [        U [        U5      [        U5      [	        U5      S9  g)a  Logs that a top level Exception was caught for a gcloud command.

Args:
  command_path: str, The '.' separated name of the calliope command.
  error: class, The class (not the instance) of the exception that was
    caught.
  flag_names: [str], The names of the flags that were used during this
    execution.
  error_extra_info: {str: json-serializable}, A json serializable dict of
    extra info that we want to log with the error. This enables us to write
    queries that can understand the keys and values in this dict.
)r   r   N)rn  r   r  rs  r  )r  r9   r   r  s       r(   r   r     s+     !|%6u%=#J/./?@Br+   c                 $    [        [        X5        g)zLogs that a top level SDK script was run.

Args:
  command_name: str, The script name.
  version_string: str, The version of the command.
N)rn  r   )command_namer  s     r(   r   r     s     !L:r+   c                 &    [        [        XU5        g)zRecord a custom key/value metric for a given command.

Args:
  command_path: str, The '.' separated name of the calliope command.
  key: str, The key recorded for the event.
  value: str. The value recorded for the event.
N)rn  r   )r  r   r&   s      r(   CustomKeyValuer    s     !!1<eLr+   c                 f    [         R                  5       nU(       a  UR                  [        SU S9  gg)zqRecord the time when the command was started.

Args:
  start_time: float, The start time in seconds since epoch.
T)rl   r-  r   N)r  r
  r.  r   )
start_timerm  s     r(   Startedr    s5      ,,.)L8<*4  6 r+   c                      [         R                  5       n U (       a%  U R                  [        SS9  U R	                  5         gg)z3Record the time when command loading was completed.Trl   r-  N)r  r
  r.  _LOAD_EVENTr&  r  s    r(   Loadedr  
  s>      ,,.)K8<  >""$ r+   c                      [         R                  5       n U (       a%  U R                  5         U R                  [        SS9  gg)z3Record the time when command running was completed.Tr  N)r  r
  r*  r.  
_RUN_EVENTr  s    r(   Ranr    s>      ,,.)""$J8<  > r+   c                 ^    [         R                  5       nU(       a  UR                  U 5        gg)zRecord the time when a custom event was completed.

Args:
  event_name: The name of the event. This must match the pattern
    "[a-zA-Z0-9_]+".
N)r  r
  r.  )r   rm  s     r(   CustomTimedEventr    s(      ,,.)z* r+   c              #   F   #    [        U S-   5        Sv   [        U 5        g7f)zRecord duration of a span of time.

Two timestamps will be sent, and the duration in between will be considered as
the client side latency of this span.

Args:
  span_name: str, The name of the span to time.

Yields:
  None
_startN)r  )	span_names    r(   RecordDurationr  +  s      9x'(9s   !c                 p    [         R                  5       nU(       a  UR                  [        U 5      5        gg)zoRecord the time taken to perform an RPC.

Args:
  duration_in_secs: float, The duration of the RPC in seconds.
N)r  r
  r1  r`   )duration_in_secsrm  s     r(   RPCDurationr  =  s.      ,,.).> ?@ r+   c                 `    [         R                  5       nU(       a  UR                  XX#5        ggr@  )r  r
  r;  )rA  rB  rC  r   rm  s        r(   CustomBeaconr  I  s*      ,,.)T; r+   r!   )r   NNN)rs   NNNrg  )rs   )OrZ   
__future__r   r   r   
contextlibr   r   rK  rL   r;   rY  rX  rI  r^   googlecloudsdk.corer   r   r   r	   googlecloudsdk.core.consoler
   r   googlecloudsdk.core.utilr   r   r   six.moves.urllib.errorsix.moves.urllib.parsesix.moves.urllib.requestr   r   r   r   r   rl  r   r  r  r   r   r   r   r   r   r   objectr   r4   r`   rC   rh   rp   r   r  rn  rs  r|  r  r  r  r  r  r  r  r   r   r   r   r   r  r  r  r  r  contextmanagerr  r  r  r2   r+   r(   <module>r     sn    - &  '   	     
   & / # * 4 2 - . 
       # ,  
6 / ' V M6 M.75"#& #0F 0fQ7v Q7hJ7 J7\ 26&*N:&1 	  $  8 8 @D*.B B, E E B B& : : M M 
6 
6 % % > > 	+ 	+  " A A < <r+   