
    N                         d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddl	Zddl
ZddlZddlZddlZddlZddlZddlZddlmZ ddlmZ ddlmZ ddlZd	Zd
Zd Zd Z G d de      Zd Z G d de      Zy)zA library for logs tailing.    )absolute_import)division)unicode_literalsN)apis)
gapic_util)log   zMhttps://cloud.google.com/logging/docs/reference/tools/gcloud-logging#tailing.c           
         t         j                  j                  dt         j                  j                  dt         j                  j                  dt         j                  j
                  dt         j                  j                  di}| j                         t         j                  j                  k(  ry ||         |dj                  |j                  | j                         d      | j                                      y)	z(Handles _MultiThreadedRendezvous errors.zInvalid argument.z&There are too many tail sessions open.zInternal error.z-Access is denied or has changed for resource.zTThe maximum duration for tail has been met. The command may be repeated to continue.Nz{} ({})zUnknown error encountered.)grpc
StatusCodeINVALID_ARGUMENTRESOURCE_EXHAUSTEDINTERNALPERMISSION_DENIEDOUT_OF_RANGEcode	CANCELLEDformatgetdetails)
rendezvousoutput_debugoutput_warningerror_messages_by_codes       -lib/googlecloudsdk/api_lib/logging/tailing.py_HandleGrpcRendezvousr   0   s     oo&&

oo((
2
oo

oo''
9
oo""6 __$//333
z!!  !2!=?     c                 P   t        j                  dd      }|j                  j                  j                  }|j
                  j                  d|j
                  j                  di}| j                         D ]/  \  }}|j                  |dj                  |            } |||       1 y)zHandles supression counts.loggingv2zLogging API backend rate limitz,client not consuming messages quickly enoughzUNKNOWN REASON: {}N)r   GetGapicClientClasstypesTailLogEntriesResponseSuppressionInfoReason
RATE_LIMITNOT_CONSUMEDitemsr   r   )counts_by_reasonhandlerclient_classsuppression_infosuppression_reason_stringsreasoncountreason_strings           r   _HandleSuppressionCountsr1   K   s    )))T:,"((??OO ((
***
8	  (--/mfe.22$++F35MM5! 0r   c                   @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)_SuppressionInfoAccumulatorzKAccumulates and outputs information about suppression for the tail session.c                     || _         || _        || _        t        j                         | _        t        j                         | _         |       | _        y N)_get_now_warning_errorcollectionsCounter_count_by_reason_delta_count_by_reason_cumulative_last_flush)selfget_nowr   output_errors       r   __init__z$_SuppressionInfoAccumulator.__init___   sC    DM"DMDK"-"5"5"7D'2':':'<D$yDr   c                 L    | j                  dj                  t                     y )Nz$Find guidance for suppression at {}.)r7   r   _HELP_PAGE_LINKr>   s    r   _OutputSuppressionHelpMessagez9_SuppressionInfoAccumulator._OutputSuppressionHelpMessageg   s    MM.55oFHr   c                 f    | j                         | j                  z
  j                         t        kD  S r5   )r6   r=   total_seconds&_SUPPRESSION_INFO_FLUSH_PERIOD_SECONDSrD   s    r   _ShouldFlushz(_SuppressionInfoAccumulator._ShouldFlushk   s,    MMOd...]_EF Fr   c                 F    | j                  dj                  ||             y )Nz Suppressed {} entries due to {}.)r8   r   r>   r0   r/   s      r   _OutputSuppressionDeltaMessagez:_SuppressionInfoAccumulator._OutputSuppressionDeltaMessageo   s    KK299%OPr   c                 F    | j                  dj                  ||             y )Nz+In total, suppressed {} messages due to {}.)r7   r   rK   s      r   #_OutputSuppressionCumulativeMessagez?_SuppressionInfoAccumulator._OutputSuppressionCumulativeMessager   s!    MM?FF} r   c                     | j                         | _        t        | j                  | j                         | xj
                  | j                  z  c_        | j                  j                          y r5   )r6   r=   r1   r;   rL   r<   clearrD   s    r   _Flushz"_SuppressionInfoAccumulator._Flushv   sS    }}DT88!@@B$$(C(CC$%%'r   c                     | j                          t        | j                  | j                         | j                  r| j	                          y y r5   )rQ   r1   r<   rN   rE   rD   s    r   Finishz"_SuppressionInfoAccumulator.Finish}   s?    KKMT==!EEG''
((* (r   c           	          | xj                   t        j                  |D ci c]  }|j                  |j                   c}      z  c_         | j                         r| j                          y y c c}w r5   )r;   r9   r:   r.   suppressed_countrI   rQ   )r>   r,   infos      r   Addz_SuppressionInfoAccumulator.Add   se    ;#6#68HI8Hd++	+8HI$K K
kkm  	Js   A.N)__name__
__module____qualname____doc__rA   rE   rI   rL   rN   rQ   rS   rW    r   r   r3   r3   \   s0    S!HFQ(+r   r3   c              #     K   |j                          t        | ||      }d}|j                  rN	 |j                         }|j                  |j                         |j                  D ]  }	|	  |j                  rN|rt        |||       |j                          |j                          y# t        j
                  $ r}|}Y d}~Jd}~ww xY ww)a  Streams entries back from the Logging API.

  Args:
    get_now: A callable that returns the current time.
    output_warning: A callable that outputs the argument as a warning.
    output_error: A callable that outputs the argument as an error.
    output_debug: A callable that outputs the argument as debug info.
    tail_stub: The `BidiRpc` stub to use.

  Yields:
    Entries included in the tail session.
  N)openr3   	is_activerecvr   RpcErrorrW   r,   entriesr   rS   close)
r?   r   r@   r   	tail_stubsuppression_info_accumulatorerrorresponseeentrys
             r   _StreamEntriesrj      s      ..!<~|"-
%!h !$$X%>%>?!!k " 	  %~>%%'// == es3   ,CB, <C<0C,C?CCCCc                       e Zd ZdZd Zdej                  j                  ej                  ej                  e
j                  j                  fdZd Zy)	LogTailerzStreams logs using gRPC.c                 H    t        j                  dd      | _        d | _        y )Nr   r    )r   GetGapicClientInstanceclientrd   rD   s    r   rA   zLogTailer.__init__   s    --i>DKDNr   Nc              #     K   | j                   j                  j                         }|j                  j	                  |       ||_        t        j                  | j                   | j                   j                  j                  j                  |      | _        |rt        j                  |      |_        t        ||||| j                        D ]  }	|	  yw)a  Tails log entries from the Cloud Logging API.

    Args:
      resource_names: The resource names to tail.
      logs_filter: The Cloud Logging filter identifying entries to include in
        the session.
      buffer_window_seconds: The amount of time that Cloud Logging should buffer
        entries to get correct ordering, or None if the backend should use its
        default.
      output_warning: A callable that outputs the argument as a warning.
      output_error: A callable that outputs the argument as an error.
      output_debug: A callable that outputs the argument as debug.
      get_now: A callable that returns the current time.

    Yields:
      Entries for the tail session.
    )initial_request)secondsN)ro   r"   TailLogEntriesRequestresource_namesextendfilterr   MakeBidiRpcr   	transporttail_log_entriesrd   datetime	timedeltabuffer_windowrj   )
r>   rt   logs_filterbuffer_window_secondsr   r@   r   r?   requestri   s
             r   TailLogszLogTailer.TailLogs   s     2 kk557G!!.1 GN++T[[((22CC!DN &009NOg ,dnn>k>s   CCc                 R    | j                   r| j                   j                          y y r5   )rd   rc   rD   s    r   StopzLogTailer.Stop   s    ~~
nn r   )rX   rY   rZ   r[   rA   r   errPrintrf   debugrz   nowr   r   r\   r   r   rl   rl      sB      &*!ggmmIIII((,,$Lr   rl   )r[   
__future__r   r   r   r9   rz   +google.appengine.logging.v1.request_log_pb2google.google.cloud.appengine_v1.proto.audit_data_pb23google.cloud.appengine_v1alpha.proto.audit_data_pb22google.cloud.appengine_v1beta.proto.audit_data_pb25google.cloud.bigquery_logging_v1.proto.audit_data_pb2,google.cloud.cloud_audit.proto.audit_log_pb2.google.cloud.iam_admin_v1.proto.audit_data_pb2$google.iam.v1.logging.audit_data_pb2google.type.money_pb2googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r   r   rH   rC   r   r1   objectr3   rj   rl   r\   r   r   <module>r      sz    " &  '   3 5 : 9 < 3 5 +  - * #  *+ &a6"",& ,^"J/ /r   