
                            S 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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rSSKrSSKrSS	KJr  SS
KJ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r%Sr&Sr'Sr(Sr)Sr*Sr+\RX                  " S5      r-\R\                  R_                  \R\                  Ra                  SS5      5      r1Sr20 SS_SS_SS _S!S"_S#S$_S%S&_S'S(_S)S*_S+S,_S-S._S/S0_S1S2_S3S4_S5S6_S7S8_S9S:_S;S<_S=S>S?S@SASBSCSDSESFSGSH.Er3 " SI SJ\45      r5SK r6SL r7\Rp                  \6SM 5       5       r9\6     STSN j5       r:\6SO 5       r;\6SP 5       r<\6SQ 5       r=\6SR 5       r>SUSS jr?g)Vz:Static data and helper functions for collecting user data.    )absolute_import)print_function)division)unicode_literalsN)defaultdictwraps)input)urllib)VERSION)Metric)system_util)CalculateThroughput)HumanReadableToBytesz(https://ssl.google-analytics.com/collectzUA-36037335-16zUA-36037335-17CommandRetryableError
FatalErrorPerformanceSummaryz.*google\.com$~z.gsutil/analytics-uuidDISABLEDzEvent CategoryeczEvent ActioneazEvent LabelelzEvent ValueevCommand Namecd1Global Optionscd2Command-Level Optionscd3Configcd4Command Aliascd5Fatal Errorcd6Parallelism Strategycd7Source URL Typecd8Provider Typescd9	Timestampcd10Execution Timecm1Retryable Errorscm2Is Google Corp Usercm3cm4cm5cm6cm7cm8cm9cm10cm11cm12cm13cm14)Num ProcessesNum Threads#Number of Files/Objects Transferred!Size of Files/Objects TransferredAverage Overall ThroughputNum Retryable Service ErrorsNum Retryable Network ErrorsThread Idle Time PercentSlowest Thread ThroughputFastest Thread ThroughputDisk I/O Timec                      \ rS rSrSr\\4S jrSrSr	S r
\\4S j5       r\S 5       r\S 5       r\   SS	 j5       r\SS
 j5       r\S 5       rS rS r\SS4S jr " S S\5      rS rS rS rS r   SS jrSrg)MetricsCollectorq   zA singleton class to handle metrics reporting to Google Analytics (GA).

This class is not thread or process-safe, and logging directly to the
MetricsCollector instance can only be done by a single thread.
c                 b   [        5       U l        X l        [        R                  " 5       U l        [        R                  5       n[        R                  R                  SS5      nU(       a  [        nU R                  5       nSn[        R                  [        R                   " 5       5      (       a  SnSSSUSUS	S
["        S   U["        S   U0U l        SR'                  [(        R*                  " 5       [(        R,                  " 5       S9U l        / U l        [3        [4        5      U l        SU l        g)zInitialize a new MetricsCollector.

This should only be invoked through the GetCollector or StartTestCollector
functions.

Args:
  ga_tid: The Google Analytics tracking ID to use for metrics collection.
  endpoint: The URL to send requests to.
GSUtiluse_test_GA_propertyr      v1tidcidteventr!   r3   z{system}/{release})systemreleaseN)_GetTimeInMillis
start_timeendpointlogging	getLoggerloggerrL   _GetCIDbotoconfiggetbool_GA_TID_TESTING_ValidateAndGetConfigValues_GOOGLE_CORP_HOST_REmatchsocketgethostname_GA_LABEL_MAP	ga_paramsformatplatformrX   rY   
user_agent_metricsr   intretryable_errorsperf_sum_params)selfga_tidr\   rU   use_test_propertyconfig_valuesis_corp_users           platform/gsutil/gslib/metrics.py__init__MetricsCollector.__init__x   s    '(DOM##%DK 
"
"
$C ++H6LMf 446M L!!&"4"4"677l 	SvsWh+,lDN +119J:B:J:J:L 2 NDO DM (,D  D    Nc                 p  ^
^^ / m
SmU
U4S jnS H  u  p#U" UUS S9  M     SmS H  u  p$U" UUU4S jS9  M     S	 H  u  p%U" UUS
 S9  M     S HK  u  p&[         R                  R                  X&5      nU(       d  M-   [        U5      nT
R	                  Xh45        MM     U" SSS S9  U" SSS S9  U" SSS S9  U" SSS S9  U" SSS S9  SR                  [        T
 V	s/ s H  n	SR                  U	S   U	S   5      PM     sn	5      5      $ ! [
         a    T
R	                  UT45         M  f = fs  sn	f )zParses the user's config file to aggregate non-PII config values.

Returns:
  A comma-delimited string of config values explicitly set by the user in
  key:value pairs, sorted alphabetically by key.
INVALIDc                    >  [         R                  R                  X5      nU(       a   U" U5      (       a  TR                  X45        g U(       a  TR                  UT45        g g !   TR                  UT45         g = fN)ra   rb   	get_valueappend)sectioncategoryvalidation_fnconfig_valuerv   invalid_value_strings       rx   GetAndValidateConfigValueOMetricsCollector._ValidateAndGetConfigValues.<locals>.GetAndValidateConfigValue   sq    ?{{,,W?M,77


7
8


*>?
@ 
?h(<=>s   AA% 	A% %A<))Botohttps_validate_certificates)rO   disable_analytics_prompt)rO   use_magicfile)rO   tab_completion_time_logsc                 :    [        U 5      R                  5       S;   $ )N)truefalse)strlowervals    rx   <lambda>>MetricsCollector._ValidateAndGetConfigValues.<locals>.<lambda>   s    #c(..:J 1;2r{   )r   r   r   i  ))r   debug)r   http_socket_timeout)r   num_retries)r   max_retry_delay)rO   default_api_version)rO   %sliced_object_download_max_components)rO   parallel_process_count)rO   parallel_thread_count)rO   software_update_check_period)rO   tab_completion_timeout)OAuth2oauth2_refresh_retriesc                 `   > [        U 5      R                  5       =(       a    [        U 5      T:  $ r   )r   isdigitrp   )r   small_int_thresholds    rx   r   r      s)    #c(:J:J:L ;? #C+> >;?r{   ))rO   resumable_threshold)rO   rsync_buffer_lines)rO   task_estimation_thresholdc                 4    [        U 5      R                  5       $ r   )r   r   r   s    rx   r   r      s    #c(:J:J:Lr{   ))rO   (parallel_composite_upload_component_size)rO   #parallel_composite_upload_threshold)rO   %sliced_object_download_component_size)rO    sliced_object_download_thresholdrO   check_hashesc                     U S;   $ )N)if_fast_else_failif_fast_else_skipalwaysnever r   s    rx   r   r     s    #E#Fr{   content_languagec                 L    U R                  5       =(       a    [        U 5      S:*  $ )N   )isalphalenr   s    rx   r   r     s    #++-"ACHM"Ar{   json_api_versionc                 b    U S   R                  5       S:H  =(       a    U SS  R                  5       $ )Nr   rR   rQ   )r   r   r   s    rx   r   r     s(    #a&,,.C"7"MCGOO<M"Mr{   
prefer_apic                     U S;   $ )N)jsonxmlr   r   s    rx   r   r     s	    8Nr{   r   token_cachec                     U S;   $ )N)file_system	in_memoryr   r   s    rx   r   r     s    :9;r{   ,z{0}:{1}r   rQ   )	ra   rb   r   r   r   
ValueErrorjoinsortedrl   )rs   r   r   bool_categorysmall_int_categorylarge_int_categorydata_size_categoryr   size_in_bytesrb   rv   r   r   s             @@@rx   re   ,MetricsCollector._ValidateAndGetConfigValues   s    M %?#K  )6/23	#K (#  );/?@(&(G#  );.LN	(G(# [[**7Gl		K.|<-


 2B
C(  FG #AC #MO h'3,NP h'4-;<
 88AN
ANvIVAYq	2
 	 7  	K


 24HI
J	K8
s    D""D3D0/D0c                     [         R                  5       (       a  g[         R                  (       d  [        U 5      [         l        [         R                  $ )zDReturns the singleton MetricsCollector instance or None if disabled.N)rL   
IsDisabled	_instance)rt   s    rx   GetCollectorMetricsCollector.GetCollector#  s9     ""$$%%#3F#; %%%r{   c                  l    [         R                  c  [         R                  5         [         R                  $ )z6Returns whether metrics collection should be disabled.)rL   _disabled_cache_CheckAndSetDisabledCacher   r{   rx   r   MetricsCollector.IsDisabled-  s(     ''/002+++r{   c                    [         R                  R                  S5      S:X  a  SU l        g	[         R                  R                  S5      S:X  a  SU l        U R	                  5         g	[
        R                  R                  SSS5      (       a  SU l        g	[        R                  " 5       (       a*  [         R                  R                  S5      (       + U l        g	[         R                  R                  [        5      (       a5  [        [        5       nUR                  5       [        :H  U l        S	S	S	5        g	SU l        g	! , (       d  f       g	= f)
z1Sets _disabled_cache based on user opt-in or out.GSUTIL_TEST_ANALYTICSrS   T2FrO   use_gcloud_storageGA_CIDN)osenvirongetr   StartTestCollectorra   rb   rc   r   InvokedViaCloudSdkpathexists_UUID_FILE_PATHopenread_DISABLED_TEXT)clsfs     rx   r   *MetricsCollector._CheckAndSetDisabledCache4  s     
zz~~-.#5 c 
/	0C	7!c	 
		X';U	C	C c		'	'	)	) "

x 88c		(	( A vvx>9 !  !c ! s   D>>
Ec                 (   U R                  5       (       a  S[        R                  S'   SU l        U " [        U5      U l        Uc  SSS.nX0R
                  l        X R
                  l        [        R                  S   S:w  a  S	U R
                  l        gg)
a2  Reset the singleton MetricsCollector with testing parameters.

Should only be used for tests, where we want to change the default
parameters.

Args:
  endpoint: str, URL to post to
  user_agent: str, User-Agent string for header.
  ga_params: A list of two-dimensional string tuples to send as parameters.
0r   FNbd)acr   r   )	r   r   r   r   rd   r   rk   rn   r[   )r   r\   rn   rk   s       rx   r   #MetricsCollector.StartTestCollectorL  s}      ~~,/bjj()C2CM#&i'MM)MM	zz)*c1!"cmm 2r{   c                 D    S[         R                  S'   SU l        Xl        g)zReset the MetricsCollector with default parameters after testing.

Args:
  original_instance: The original instance of the MetricsCollector so we can
    set the collector back to its original state.
rS   r   N)r   r   r   r   )r   original_instances     rx   StopTestCollector"MetricsCollector.StopTestCollectorg  s      +.BJJ&'C%Mr{   c                     [         R                  R                  [        5      (       a1  [	        [        5       n U R                  5       nSSS5        W(       a  U$ [         R                  R                  S5      $ ! , (       d  f       N6= f)z|Gets the client id from the UUID file or the SDK opt-in, or returns None.

Returns:
  str, The hex string of the client id.
Nr   )r   r   r   r   r   r   r   r   )r   rU   s     rx   r`   MetricsCollector._GetCIDs  sW     
ww~~o&& Affh !	
 ::>>(## ! s   A99
Bc                 :    U R                   R                  U5        g)zExtends self.ga_params to include new parameters.

This is only used to record parameters that are sent with every event type,
such as global and command-level options.

Args:
  new_params: A dictionary of key-value parameters to send.
N)rk   update)rs   
new_paramss     rx   ExtendGAParamsMetricsCollector.ExtendGAParams  s     	NN*%r{   c                 F    U R                   R                  [        U   5      $ )zConvenience function for getting a ga_param of the collector.

Args:
  param_name: The descriptive name of the param (e.g. 'Command Name'). Must
    be a key in _GA_LABEL_MAP.

Returns:
  The GA parameter specified, or None.
)rk   r   rj   )rs   
param_names     rx   
GetGAParamMetricsCollector.GetGAParam  s     >>mJ788r{   r   c           	         SU4SU4SU4SU4[         S   [        5       4/nUR                  [        R                  " U5       VV	s/ s H  u  pU	c  M
  X4PM     sn	n5        UR                  [        R                  " U R
                  5       VV	s/ s H  u  pU	c  M
  X4PM     sn	n5        Uc  [        5       U R                  -
  nUR                  [         S   U45        [        R                  R                  [        U5      5      n
U R                  R                  [        U R                  SU
U R                  S	95        gs  sn	nf s  sn	nf )
a  Adds a GA metric with the given parameters to the metrics queue.

Args:
  category: str, the GA Event category.
  action: str, the GA Event action.
  label: str, the GA Event label.
  value: int, the GA Event value.
  execution_time: int, the execution time to record in ms.
  **custom_params: A dictionary of key, value pairs containing custom
      metrics and dimensions to send with the GA Event.
r   r   r   r   r-   Nr/   POST)r\   methodbodyrn   )rj   rZ   extendsix	iteritemsrk   r[   r   r   parse	urlencoder   ro   r   r\   rn   )rs   r   actionlabelvalueexecution_timecustom_paramsparamskrR   datas              rx   CollectGAMetric MetricsCollector.CollectGAMetric  s&   $ Xvue}[)+;+=>@F
MM==774117  MM==8841A8 
 ')DOO;n
MM=!12NCD<<!!&.1DMM//	+,s   	E
E
	E
E
c                   4    \ rS rSrSrS r " S S\5      rSrg))MetricsCollector._PeformanceSummaryParamsi  zEThis class contains information to create a PerformanceSummary event.c                    SU l         SU l        SU l        SU l        [	        5       U l        [        R                  (       a  [        R                  " 5       U l	        SU l
        SU l        SU l        SU l        [        U R                  5      U l        S U l        S U l        S U l        SU l        SU l        SU l        SU l        SU l        SU l        g )Nr   F)num_processesnum_threadsnum_retryable_service_errorsnum_retryable_network_errorssetprovider_typesr   IS_LINUXGetDiskCountersdisk_counters_startuses_fan
uses_slicethread_idle_timethread_execution_timer   _ThreadThroughputInformationthread_throughputsavg_throughputtotal_elapsed_timetotal_bytes_transferrednum_objects_transferredis_daisy_chainhas_file_dsthas_cloud_dsthas_file_srchas_cloud_srcrs   s    rx   ry   2MetricsCollector._PeformanceSummaryParams.__init__  s    dd*+d'*+d'Ed 
		#.#>#>#@  dmdo  d#$d  !,D,M,M Nd !d $d%)d"%&d" "dd dd dr{   c                   0    \ rS rSrSrS rS rS rS rSr	g)	FMetricsCollector._PeformanceSummaryParams._ThreadThroughputInformationi  zDA class to keep track of throughput information for a single thread.c                 <    SU l         SU l        S U l        S U l        g )Nr   )r'  r&  task_start_time	task_sizer.  s    rx   ry   OMetricsCollector._PeformanceSummaryParams._ThreadThroughputInformation.__init__  s%    '($"#  $ r{   c                     Xl         X l        g r   )r3  r4  )rs   r[   bytes_to_transfers      rx   LogTaskStartSMetricsCollector._PeformanceSummaryParams._ThreadThroughputInformation.LogTaskStart  s    )*r{   c                     U =R                   XR                  -
  -  sl         U =R                  U R                  -  sl        S U l        S U l        g r   )r&  r3  r'  r4  )rs   end_times     rx   
LogTaskEndQMetricsCollector._PeformanceSummaryParams._ThreadThroughputInformation.LogTaskEnd   s@    8.B.B#BB$$6$#r{   c                 B    [        U R                  U R                  5      $ r   )r   r'  r&  r.  s    rx   GetThroughputTMetricsCollector._PeformanceSummaryParams._ThreadThroughputInformation.GetThroughput  s     "4#?#?#'#:#:< 	<r{   )r4  r3  r'  r&  N)
__name__
__module____qualname____firstlineno____doc__ry   r8  r<  r?  __static_attributes__r   r{   rx   r#  r1    s    P	+<r{   r#  )r%  r  r+  r-  r*  r,  r)  r(  r  r  r  r  r  r"  r!  r$  r'  r&  r  r   N)	rA  rB  rC  rD  rE  ry   objectr#  rF  r   r{   rx   _PeformanceSummaryParamsr    s    O%!N<v <r{   rH  c                    U R                  S5      S;  a  gU R                  c  U R                  5       U l        SU;   a  U R                  US   S9  g[        R
                  " U5       H  u  p#US;   a3  [        U R                  U5      nU(       d  [        U R                  X#5        US;   a/  [        U R                  U5      n[        U R                  X$U-   5        US;   a1  [        U R                  U5      nXC:  a  [        U R                  X#5        US	:X  d  M  U R                  R                  R                  U5        M     g)
a  Updates the _PeformanceSummaryParams object.

Args:
  params: A dictionary of keyword arguments.
    - uses_fan: True if the command uses fan parallelism.
    - uses_slice: True if the command uses slice parallelism.
    - avg_throughput: The average throughput of the data transfer.
    - is_daisy_chain: True if the transfer uses the daisy-chain method.
    - has_file_dst: True if the transfer's destination is a file URL.
    - has_cloud_dst: True if the transfer's destination is in the cloud.
    - has_file_src: True if the transfer has a file URL as a source.
    - has_cloud_src: True if the transfer has a cloud URL as a source.
    - total_elapsed_time: The total amount of time spent on Apply.
    - total_bytes_transferred: The total number of bytes transferred.
    - thread_idle_time: The additional amount of time that threads spent
                        idle in Apply.
    - thread_execution_time: The additional amount of time that threads
                             spent executing in Apply.
    - num_retryable_service_errors: The additional number of retryable
                                    service errors that occurred.
    - num_retryable_network_errors: The additional number of retryable
                                    network errors that occurred.
    - num_processes: The number of processes used in a call to Apply.
    - num_threads: The number of threads used in a call to Apply.
    - num_objects_transferred: The total number of objects transferred, as
                               specified by a ProducerThreadMessage.
    - provider_types: A list of additional provider types used.
    - file_message: A FileMessage used to calculate thread throughput and
                    number of objects transferred in the non-parallel case.
r   )cprsyncNfile_message)rL  )r  r   r%  r)  r*  r+  r,  r-  r&  r'  r(  )r!  r"  r  r  )r  r  r  )
r   rr   rH  _ProcessFileMessager  r  getattrsetattrr  r   )rs   r  r   param	cur_values        rx   UpdatePerformanceSummaryParams/MetricsCollector.UpdatePerformanceSummaryParams
  s6   > ~&o=#!::<d
 
F>,BC ]]62
 
 N 
N D00*=	
$&&

: 
 8 
8 D00*=	$$je2CD 
7	7D00*=	
$&&

: 
'	'++22597 3r{   c                    U R                   R                  UR                  UR                  4   nUR                  (       aq  U R                   R
                  (       d:  U R                   R                  (       d  U R                   =R                  S-  sl        UR                  UR                  5        gUR                  UR                  UR                  5        g)a<  Processes FileMessages for thread throughput calculations.

Update a thread's throughput based on the FileMessage, which marks the start
or end of a file or component transfer. The FileMessage provides the number
of bytes transferred as well as start and end time.

Args:
  file_message: The FileMessage to process.
rQ   N)rr   r$  
process_id	thread_idfinishedr   r  r(  r<  timer8  size)rs   rL  thread_infos      rx   rM  $MetricsCollector._ProcessFileMessageR  s     ''::!7!7<9 :K ""--1E1E1N1N4494\../|00,2C2CDr{   c           	         U R                  S5      nU(       aC  U R                  " S[        US.[        S   [	        U R
                  R                  5       5      0D6  [        R                  " U R
                  5       H'  u  p#U R                  " S[        US.[        S   U0D6  M)     U R                  S5      nU(       a  U R                  [        US9  gg)zDAggregates command and error info and adds them to the metrics list.r   )r   r	  r1   r%   Nr   )r   r  _GA_COMMANDS_CATEGORYrj   sumrq   valuesr  r  _GA_ERRORRETRY_CATEGORY_GA_ERRORFATAL_CATEGORY)rs   command_name
error_type
num_errorsfatal_error_types        rx   _CollectCommandAndErrorMetrics/MetricsCollector._CollectCommandAndErrorMetricsh  s     ??>2L
 $9".  --?@#&t'<'<'C'C'E#F #&--0E0E"F

 N$;",N+,>?LN #G }5
$;"2  4 r{   c           
      v   U R                   c  g0 nS H%  u  p#[        U R                   U5      U[        U   '   M'     [        R                  (       a  U R                   R
                  n[        R                  " 5       n[        UR                  5        Vs/ s H  ofS   US   -   PM     sn5      [        UR                  5        Vs/ s H  ofS   US   -   PM     sn5      -
  U[        S   '   U R                   R                  (       a   U R                   R                  (       a  SOSnOSnXq[        S	   '   U R                   R                  (       a   U R                   R                  (       a  SOS
nOU R                   R                  (       a  SOSnX[        S   '   U R                   R                  U R                   R                  -   n	U	(       a5  [        U R                   R                  5      [        U	5      -  U[        S   '   U R                   R                   (       al  U R                   R                   R                  5        V
s/ s H  n
U
R#                  5       PM     nn
[%        U5      U[        S   '   ['        U5      U[        S   '   SR)                  [+        U R                   R,                  5      5      U[        S   '   U R                   R                  =(       a    U R                   R.                  U R                   R                  =(       a    U R                   R0                  U R                   R2                  U R                   R                  =(       a    U R                   R.                  U R                   R                  =(       a    U R                   R0                  S.nSR)                  [+        [4        R6                  " U5       VVs/ s H  u  pU(       d  M  UPM     snn5      5      n[9        U R                   R:                  5      nU R<                  " S[>        UUS.UD6  gs  snf s  snf s  sn
f s  snnf )zCAggregates PerformanceSummary info and adds the metric to the list.N))r  r@   )r  rA   )r  rE   )r  rF   )r%  rD   )r(  rB   )r'  rC         rJ   bothcloudfiler)   fanslicenoner'   rG   rH   rI   r   r+   )CloudToCloudCloudToFile
DaisyChainFileToCloud
FileToFile)r   r	  r  r   ) rr   rN  rj   r   r  r  r  r^  r_  r-  r,  r  r   r!  r"  floatr$  r?  minmaxr   r   r  r+  r*  r)  r  r  rZ   r&  r  _GA_PERFSUM_CATEGORY)rs   r  	attr_namer
  
disk_startdisk_endstatsrc_url_typestrategy
total_timethreadthroughputstransfer_typestransfer_typecondr	  apply_execution_times                    rx    _CollectPerformanceSummaryMetric1MetricsCollector._CollectPerformanceSummaryMetric  s   #M	 -4D4H4H4=-?mM%() '';;j,,.h X__->?->TAwa ->?
@
Z->->-@A-@TAwa -@A
BC M/23
 ))#33@@Vgll6B- 123 $$//::h 00;;h;C- 678 &&77&&<<=J
$$55
6z9J
J M"<=> .. ,,??FFHHf 


 H   CF
CmM"=>?BE
CmM"=>? 69XXt##22365M- 012   .. 3$$22  .. 2$$11  //  -- 3$$22  -- 2$$11N  XX'*}}^'D
'D# 'D
 	F ,//1 	 *"6 &(<* )*S @A2@
s   P&P+
P0	P5P5c                    U R                  5         U R                  5         U R                  (       d  gU(       d  U R                  R	                  5       n[
        R                  R                  S5      S:X  a  [        R                  n[        R                  " SS9n[        R                  " UR                  5      nU   [        R                   " U R                  U5        SSS5        [        R"                  " U R                  5        / U l        Ub  [        R                  " SU-  5      n[        R                  " SR%                  XRU5      5      n[&        R(                  SU/n[
        R                  R+                  5       n[
        R,                  R/                  [&        R0                  5      US	'   [3        5       n	[        R4                  " U5       H2  u  p[        R                  " U5      U	[        R                  " U
5      '   M4      [6        R8                  " UU	[        R:                  =(       a    [<        R>                  S
9nU R                  R#                  S5        U(       a,  URA                  5         U R                  R#                  S5        gg! , (       d  f       GN= f! [B         aG    U R                  R#                  S5         [
        RD                  " UR                  5         g!     g= ff = f)a2  Reports the collected metrics using a separate async process.

Args:
  wait_for_report: bool, True if the main process should wait for the
    subprocess to exit for testing purposes.
  log_level: int, The subprocess logger's level of debugging for testing
    purposes.
  log_file_path: str, The file that the metrics_reporter module should
    write its logs to. If not supplied, the metrics_reporter module will
    use a predetermined default path. This parameter is intended for use
    by tests that need to evaluate the contents of the file at this path.
Nr   r   F)deletezr"%s"z_from gslib.metrics_reporter import ReportMetrics; ReportMetrics(r"{0}", {1}, log_file_path={2})z-c
PYTHONPATH)envshellz$Metrics reporting process started...z#Metrics reporting process finished.z*Metrics reporting process failed to start.)#rf  r  ro   r_   getEffectiveLevelr   r   r   r]   WARNtempfileNamedTemporaryFiler  
ensure_strnamepickledumpr   rl   sys
executablecopypathsepr   r   dictr  
subprocessPopenPY3r   
IS_WINDOWScommunicateOSErrorunlink)rs   wait_for_report	log_levellog_file_pathtemp_metrics_filetemp_metrics_file_namereporting_codeexecution_argsexec_envsm_envr  rR   ps                rx   ReportMetricsMetricsCollector.ReportMetrics  s      	'')))+==++//1i	zz~~-.#5,,i 335A ^^,=,B,BC	kk$--!23 
MM$-- DM  nnW}%<=m^^	88>"}9>?N nndN;Nzz HZZ__SXX6H\VFh'"%.."3fS^^A ( 

>%"%''"Dk.D.DGa kk>?	 	
?@	 
; 
	D   kkDE
		#(()s1   ="JBJ$ 
J!$%K5
 K,,K1.K51K5)ro   r\   rk   r_   rr   rq   r[   rn   )zhttps://example.comzuser-agent-007Nr   )FNN)rA  rB  rC  rD  rE  _GA_TID_GA_ENDPOINTry   r   r   re   staticmethodr   r   classmethodr   r   r   r`   r   r   r   r  rG  rH  rR  rM  rf  r  r  rF  r   r{   rx   rL   rL   q   s     $l 6 p )/m^ ! & & , , ! !. "7$4#'# #4 	& 	& $ $ 	&
9 $%)%,RD< D<LF:PE,40f*R %*""&Gr{   rL   c                 0   ^  [        T 5      U 4S j5       nU$ )zFunction decorator to capture and log any exceptions.

This is extra insurance that analytics collection will not hinder the command
being run upon an error.

Args:
  func: The function to wrap.

Returns:
  The wrapped function.
c                     >  T" U 0 UD6$ ! [          a=  n[        R                  " S5      nUR                  STR                  U5         S nAg S nAff = f)Nmetricsz6Exception captured in %s during metrics collection: %s)	Exceptionr]   r^   r   rA  )argskwdser_   funcs       rx   Wrapper'CaptureAndLogException.<locals>.Wrapper?  sU    %4 4   %  +fllK==!% %%s    
A3AAr   r  r  s   ` rx   CaptureAndLogExceptionr  2  s"     	;% % 
.r{   c                 0   ^  [        T 5      U 4S j5       nU$ )aL  Function decorator to ignore an exception on collecting thread stats.

An exception can happen if the thread_stats dictionary's manager gets shutdown
before the thread's process is successfully killed. See
_ThreadStat.AggregateStat for how we handle that case.

Args:
  func: The function to wrap.

Returns:
  The wrapped function.
c                  $   >  T" U 0 UD6$ !    g = fr   r   )r  r  r  s     rx   r  +CaptureThreadStatException.<locals>.WrapperY  s"    4 4  
s    r   r  s   ` rx   CaptureThreadStatExceptionr  K  s"     	;  
.r{   c                  \    [         R                  5       n U (       a  U R                  5         gg)z9Reports the metrics that were collected upon termination.N)rL   r   r  )	collectors    rx   Shutdownr  f  s&     ++-) r{   c           
         [         R                  5       nU(       d  gU (       a0  UR                  S5      (       d  UR                  [        S   U 05        U(       al  UR                  S5      (       dV  SR                  [        U Vs/ s H  ofS   R                  S5      PM     sn5      5      nUR                  [        S   U05        UR                  S5      n U (       d  gU(       a;  SR                  U SR                  U5      5      nUR                  [        S   U05        U(       al  UR                  S	5      (       dV  SR                  [        U Vs/ s H  ofS   R                  S5      PM     sn5      5      n	UR                  [        S	   U	05        U(       a2  UR                  S
5      (       d  UR                  [        S
   U05        gggs  snf s  snf )az  Logs info about the gsutil command being run.

This only updates the collector's ga_params. The actual command metric will
be collected once ReportMetrics() is called at shutdown.

Args:
  command_name: str, The official command name (e.g. version instead of ver).
  subcommands: A list of subcommands as strings already validated by
    RunCommand. We do not log subcommands for the help or test commands.
  global_opts: A list of string tuples already parsed by __main__.
  sub_opts: A list of command-level options as string tuples already parsed
    by RunCommand.
  command_alias: str, The supported alias that the user inputed.
Nr   r   r   r   -z{0} {1} r   r#   )	rL   r   r   r   rj   r   r   striprl   )
rb  subcommandsglobal_optssub_optscommand_aliasr  optglobal_opts_stringfull_command_namesub_opts_strings
             rx   LogCommandParamsr  o  s   ( ++-)	
 )..~>>mN;\JK--.>??[9[cAS![9:<	'	(*<=?
 %%n5,	
!((sxx7LMmN;=NOPi**+BCChhvH&MHS1v||C'8H&MNOO	.	/AC9//@@mO<mLM A]! : 'Ns   GGc                     [         R                  5       nU(       aF  UR                  U R                  ==   S-  ss'   U R                  (       a
  [        SS9  g[        SS9  gg)zLogs that a retryable error was caught for a gsutil command.

Args:
  message: The RetryableErrorMessage posted to the global status queue.
rQ   )r  )r  N)rL   r   rq   rc  is_service_errorLogPerformanceSummaryParams)messager  s     rx   LogRetryableErrorr    sO     ++-)w112a72!qA!qA r{   c                     [         R                  5       nU(       a/  UR                  [        S   U R                  R
                  05        gg)zwLogs that a fatal error was caught for a gsutil command.

Args:
  exception: The exception that the command failed on.
r%   N)rL   r   r   rj   	__class__rA  )	exceptionr  s     rx   LogFatalErrorr    sB     ++-)	}	%y':':'C'CDF r{   c                  ^    [         R                  5       nU(       a  UR                  U 5        gg)a  Logs parameters necessary for a PerformanceSummary.

gsutil periodically monitors its own threads; at the end of the execution of
each cp/rsync command, it will present a performance summary of the command
run.

Args:
  **params_to_update: A dictionary. See UpdatePerformanceSummaryParams for
      details. The argument ambiguity at this level allows for flexibility in
      dealing with arguments that are processed similarly.
N)rL   r   rR  )params_to_updater  s     rx   r  r    s)     ++-),,-=> r{   c                     [         R                  R                  SS5      n [        R                  R                  [        5      (       d  U (       d  [        R                  " 5       (       d  [        S[        R                  " S5      -   S-   5      n[        nUR                  5       S   S:X  a  [        R                  " 5       R                   n[        R"                  " [        R                  R%                  [        5      5        ['        [        S5       nUR)                  U5        S	S	S	5        g	g	g	g	! , (       d  f       g	= f)
zAsks a user to opt-in to data collection if a UUID file does not exist.

If the user agrees, generates a UUID file. Will not prompt if part of SDK.
rO   r   
zgsutil developers rely on user feedback to make improvements to the tool. Would you like to send anonymous usage statistics to help improve gsutil? [y/N]r  r   ywN)ra   rb   r   r   r   r   r   r   r   r
   textwrapfillr   r   uuiduuid4hexCreateDirIfNeededdirnamer   write)disable_promptenable_analyticstext_to_writer   s       rx   'CheckAndMaybePromptForAnalyticsEnablingr    s     ;;((3MN.
''..
)
).

(
(
*
*THMM	 %! ! $'' (
 #M"c)jjl&&m!!"''///"BC	os	#qggm 
$	# + 3A
) 
$	#s   D00
D>c                 N    U c  [         R                   " 5       n [        U S-  5      $ )Ni  )rX  rp   )time_in_secs    rx   rZ   rZ     s#    ))+K	[4	  r{   )NNNNNr   )@rE  
__future__r   r   r   r   atexitcollectionsr   	functoolsr	   r]   r   r  rm   rerh   r  r  r  r  rX  r  r  	six.movesr
   r   ra   gslibr   gslib.metrics_tupler   gslib.utilsr   gslib.utils.unit_utilr   r   r  r  rd   r]  r`  ra  ry  compilerf   r   
expanduserr   r   r   rj   rG  rL   r  r  registerr  r  r  r  r  r  rZ   r   r{   rx   <module>r     sy   A & %  '  #   	   	   
     
     & # 5 69
"! * & +  zz"34 ''$$RWW\\#2J&L M dD 4 4	
 E e U e U 5 E u e  e  !" 5#$ +0)."'$)$* &!'!'9@~
v ~
B26    "&!%!%"#'	.N .Nb B B" 	F 	F ? ?"  *!r{   