+
    iT  c                   sf    ^ RI Ht ^ RIHtHtHt ^ RIHtHtHt ^ RI	H
t
HtHt  ! R R]
]4      tR# )i    )Optional)datetime	timedeltatimezone)JSONDecodeErrordumpsloads)PluginIndependentPlugin	PluginOptc                   s   a  ] tR t^t o RtRtRtRtRt^dt	R t
]! RRRR7      ]! R	RR
R7      ]! RRRR7      ]! RR]RR7      ]! RR]RR7      .tV 3R lR ltV 3R lR ltR tR tR tR tR tRtV tR# )!Lokia  
Collects logs and configuration from Loki.

This plugin interacts with the Loki API to fetch logs based on specified
labels and provides options for pagination and label detection.
It also collects relevant configuration files and masks sensitive
information. It works with both charmed and non-charmed Loki.
To fetch internal Loki logs, run it from the Loki container.
You can also run it from another machine and fetch only logs from
Loki API, by providing the following parameters:
    `-k loki.collect-logs=true -k loki.endpoint=LOKI_URL`

Usage:
    sos report -o loki -k loki.collect-logs=true             -k loki.labels=severity:charm -k loki.detect-labels=true             -k loki.paginate=true -k loki.endpoint=LOKI_URL
zLoki servicelokii  collect-logsFzcollect logs from Loki API)defaultdescdetect-labelszTfetch logs for all available labels. May result in multiple files with the same logspaginatez'fetch all available logs from Loki API.labelsZ z1colon-delimited list of labels to fetch logs from)r   Zval_typer   endpointhttp://localhost:3100zDloki endpoint to fetch logs from. Defaults to http://localhost:3100.c                F   < V ^8  d   QhRS[ S[,          RS[ S[,          /# i   startendr    r   Zformat__classdict__   "</usr/lib/python3.14/site-packages/sos/report/plugins/loki.py__annotate__Loki.__annotate__<   s&      HX4F #H-    c                s   V'       g%   \         P                  ! \        P                  4      pV'       g   V\	        ^R7      ,
          pVP                  R4      pVP                  R4      pRV RV RV RV R\        P                   R2pV# )	i   )Zdaysz%Y-%m-%dT%H:%M:%S.%fZcurl -G -s 'z3/loki/api/v1/query_range' --data-urlencode 'query={z!=~".+"}' --data-urlencode 'start=z' --data-urlencode 'end=z' --data-urlencode 'limit=z' )r   nowr   Zutcr   Zstrftimer
   LOKI_QUERY_LIMIT)selfr   labelr   r   Zstart_formattedZend_formattedZcommands   &&&&&   r   query_commandZLoki.query_command<   s    ,,x||,C)++E..)@A%<= 8* %)). 0''6&7 8%%2O 4''+'<'<&=R	A 	 r   c                r   r   r   r   r   r   r   r   O   s&     
$ 
$x/A 
$x(
$r   c                s    V P                  V P                  WW44      4      p \        VR ,          4      #   \         d"    T P	                  RT R24       TR ,          u # i ; i)outputz-An error was returned from Loki API on label z-. Error message stored, not querying further.)Zexec_cmdr%   r   r   Z	_log_warn)r#   r   r$   r   r   r&   s   &&&&& r   get_logsZLoki.get_logsO   so    t11(5NO	$)** 	$NNK$g &JK M (##	$s   5 )A! A!c                s    VR ,          R,          p\        \        P                  ! 4       P                  4       R,          4      pV F0  pVR,           F   p\        V^ ,          4      p\	        W64      pK"  	  K2  	  V# )dataZresult ʚ;Zvalues)intr   r!   	timestampZmin)r#   logsZlog_streamsearliest_logZstreamZlogr+   s   &&     r   get_earliest_log_timestampZLoki.get_earliest_log_timestamp[   si    6l8,8<<>335mCD!Fh''AK	"<; ( " r   c           	     sZ   V P                  WR R 4      pV P                  V R24      ;_uu_ 4       pVP                  \        V^R7      4       R R R 4       \	        V\
        4      '       d   R # V'       Ed   V P                  V4      p\        \        P                  ! 4       P                  4       R,          4      p^ pV\        P                  8  d   Wg8  d   \        P                  ! VR,          4      p	V P                  WR V	4      p
V P                  V RV 24      ;_uu_ 4       pVP                  \        V
^R7      4       R R R 4       \	        V
\
        4      '       d   R # TpV P                  V
4      pV^,          pK  R # R # R #   + '       g   i     ELB; i  + '       g   i     L`; i)Nz.log)Zindentr)   z.log.)r'   Zcollection_fileZwriter   
isinstancestrr.   r*   r   r!   r+   r
   MAX_PAGINATION_ITERATIONSZfromtimestamp)r#   r   r$   r   r,   Zlogfiler-   Zprevious_earliest_logZiterations_countZlog_timestampZnew_logss   &&&&       r   get_logs_for_labelZLoki.get_logs_for_labele   sk   }}XdD9!!UG4.11WMM%Q/0 2dC  8::4@L$'((*=8%!  !"T%C%CC 8 ( 6 6$0=$@!B==$N))UG59I8J*KLL"MM%";< M h,,(4%33H=  !A%  9 D  211" MLs   F%FF	F*	c                s   V P                  R 4      pV P                  V4       V P                  R4      pV P                  V4       V P                  R4       V P                  R4       V P                  R4      '       d   V P                  R4      ;'       g    Rp. V n        V P                  R4      ;p'       dI   \        V\        4      '       d3   V'       d+   V P                  P                  VP                  R4      4       V P                  R	4      '       dL   V P                  R
V R24      p\        VR,          4      pV P                  P                  VR,          4       R# R# R# )z/etc/loki/*.yamlz/etc/worker/*.yamlz/var/log/loki/*zpebble logs loki -n 10000r   r   r   r   Z:r   r    z/loki/api/v1/labels'r&   r(   N)Z	path_joinZadd_copy_specZadd_cmd_output
get_optionr   r/   r0   ZextendZsplitZcollect_cmd_outputr   )r#   Zels_config_fileZcoordinated_workers_config_filer   Zlabels_optionZ
labels_cmdZlabels_jsons   &      r   setupZ
Loki.setup   s)   ..);<?+ +/..9M*N':;,-78??>**z2MM6MHDK $ 99}9mS11mKK&&}':':3'?@//!44"8*,@A
 $Jx$89"";v#67 0 +r   c                s    V P                  R 4      ;'       g    RpV P                   F&  pV P                  R4      pV P                  WV4       K(  	  R# )r   r   r   N)r3   r   r2   )r#   r   r$   r   s   &   r   collectZLoki.collect   sE    ??:.II2I[[Ez2H##HX> !r   c                s    R R.p. ROpRRP                  V4       R2pRRP                  V4       R2pV F'  pV P                  WSR4       V P                  WTR4       K)  	  R# )	Zaccess_key_idZsecret_access_keyZ(Z|z)\s*(:|=)(\S*\n.*?\\n)z)\s*(:|=)\s*[a-zA-Z0-9]*z\1\2*********N)z /etc/loki/loki-local-config.yamlz/etc/loki/config.yamlz/etc/loki/local-config.yamlz/etc/loki/loki.yamlz/etc/worker/config.yaml)ZjoinZdo_file_sub)r#   Zprotect_keysZ
loki_filesZmatch_exp_multilZ	match_expZfiles   &     r   postprocZLoki.postproc   s    


   677MN,/00HI	D    r   )r   N)Zservices)r   )Z__name__Z
__module__Z__qualname__Z__firstlineno__Z__doc__Z
short_descZplugin_nameZprofilesr"   r1   Zpackagesr	   r0   Zoption_listr%   r'   r.   r2   r4   r5   r6   Z__static_attributes__Z__classdictcell__)r   s   @r   r
   r
      s     $  JKH #H 	.%3	5/5J	L 	*e@	B(BJ	L*&==	?K &
$ 
$&>82? r   r
   N)Ztypingr    r   r   r   Zjsonr   r   r   Zsos.report.pluginsr   r   r	   r
   ) r   r   <module>r7      s-     2 2 . . C Ci6$ ir   