1
1
import os
2
2
3
- # Progress bar
3
+ # Run command
4
4
import subprocess
5
- import requests
6
- import ipywidgets as widgets
7
- from tqdm .auto import tqdm
8
5
9
6
# Open grafana url
10
7
from IPython .display import HTML , display
15
12
16
13
# logging
17
14
import logging
15
+
18
16
logger = logging .getLogger ()
19
17
logger .setLevel (logging .INFO )
18
+ from typeguard import typechecked
19
+
20
+ # log operations
21
+ from benchmark_runner .jupyterlab .templates .logs_operations .logs_operations import LogsOperations
20
22
21
23
22
24
class AnalyzePrometheusLogs :
@@ -25,76 +27,32 @@ class AnalyzePrometheusLogs:
25
27
"""
26
28
TIMEOUT = 30
27
29
SLEEP = 3
28
- CHUNK_SIZE = 8192
29
30
30
31
def __init__ (self , s3_logs_url : str ):
31
- self .__s3_logs_url = s3_logs_url
32
- self .__logs_dir = os .path .join (os .path .join (os .getcwd (), 'logs' ))
33
- self .__filename = self .__s3_logs_url .split ('/' )[- 1 ]
34
- self .__log_dir_path = os .path .join (self .__logs_dir , self .__filename )
35
-
36
- def cleanup (self ):
37
- """
38
- This method cleans up existing logs and Prometheus images
39
- @return:
40
- """
41
- # Delete Prometheus container
42
- os .system ('podman rmi -f docker.io/prom/prometheus;' )
43
- # delete logs dir if exist
44
- if os .path .exists (self .__logs_dir ):
45
- os .system (f"rm -rf { self .__logs_dir } " )
46
-
47
- def download_s3_logs (self , username : str , password : str ):
48
- """
49
- This method downloads s3 logs
50
- @param username:
51
- @param password:
52
- @return:
53
- """
54
- if not os .path .exists (self .__logs_dir ):
55
- os .mkdir (self .__logs_dir )
56
-
57
- # create a session with the credentials
58
- session = requests .Session ()
59
- session .auth = (username , password )
60
-
61
- # download with progress bar
62
- response = session .get (self .__s3_logs_url , stream = True )
63
- size = int (response .headers .get ('Content-Length' , 0 ))
64
-
65
- progress = widgets .IntProgress (description = 'Downloading' , min = 0 , max = size )
66
- display (progress )
67
-
68
- with open (os .path .join (self .__logs_dir , self .__filename ), 'wb' ) as f :
69
- with tqdm .wrapattr (f , "write" , total = size ) as fileobj :
70
- for chunk in response .iter_content (chunk_size = self .CHUNK_SIZE ):
71
- if chunk :
72
- fileobj .write (chunk )
73
- progress .value += len (chunk )
74
-
75
- logger .info ('Download complete!' )
32
+ self .logs_operations = LogsOperations (s3_logs_url = s3_logs_url )
76
33
77
34
def untar_and_chmod_prometheus_logs (self ):
78
35
"""
79
- This method untars and sets the chmod for Prometheus logs, and returns the path to the 'promdb' directory
80
- @return:
36
+ This method untars and sets the chmod for prometheus logs directory , and returns the path to the prometheus logs directory
37
+ @return: prometheus_logs path
81
38
"""
82
- logger .info (f'untar download file { self .__log_dir_path } ' )
83
- os .system (f"tar -xvf { self .__log_dir_path } -C { self .__logs_dir } " )
84
-
85
- promdb_file = [f for f in os .listdir (self .__log_dir_path .split ('.' )[0 ]) if f .startswith ('promdb' )]
86
-
87
- logger .info (f"untar prometheus file: { os .path .join (self .__logs_dir , self .__filename .split ('.' )[0 ], promdb_file [0 ])} " )
88
- os .system (f"tar -xvf { os .path .join (self .__logs_dir , self .__filename .split ('.' )[0 ], promdb_file [0 ])} -C { os .path .join (self .__logs_dir , self .__filename .split ('.' )[0 ])} " )
89
-
90
- promdb_file = [f for f in os .listdir (self .__log_dir_path .split ('.' )[0 ]) if f .startswith ('promdb' ) and not f .endswith ('tar' )]
91
- promdb_dir_path = f"{ os .path .join (self .__logs_dir , self .__filename .split ('.' )[0 ], promdb_file [0 ])} "
39
+ self .logs_operations .untar_and_chmod_logs ()
40
+ promdb_file = [f for f in os .listdir (self .logs_operations .log_dir_path .split ('.' )[0 ]) if f .startswith ('promdb' )]
41
+ logger .info (
42
+ f"untar prometheus file: { os .path .join (self .logs_operations .logs_dir , self .logs_operations .filename .split ('.' )[0 ], promdb_file [0 ])} " )
43
+ os .system (
44
+ f"tar -xvf { os .path .join (self .logs_operations .logs_dir , self .logs_operations .filename .split ('.' )[0 ], promdb_file [0 ])} -C { os .path .join (self .logs_operations .logs_dir , self .logs_operations .filename .split ('.' )[0 ])} " )
45
+ promdb_file = [f for f in os .listdir (self .logs_operations .log_dir_path .split ('.' )[0 ]) if
46
+ f .startswith ('promdb' ) and not f .endswith ('tar' )]
47
+ promdb_dir_path = f"{ os .path .join (self .logs_operations .logs_dir , self .logs_operations .filename .split ('.' )[0 ], promdb_file [0 ])} "
92
48
93
49
logger .info (f'chmod { promdb_dir_path } ' )
94
50
os .system (f"chmod -R g-s,a+rw { promdb_dir_path } " )
95
51
return promdb_dir_path
96
52
97
- def run_container (self , image_name , command ):
53
+ @staticmethod
54
+ @typechecked
55
+ def run_container (image_name : str , command : str ):
98
56
"""
99
57
This method runs the container and waits until it finishes running
100
58
@param image_name:
@@ -106,21 +64,23 @@ def run_container(self, image_name, command):
106
64
process = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
107
65
108
66
current_wait_time = 0
109
- while current_wait_time <= self .TIMEOUT :
67
+ while current_wait_time <= AnalyzePrometheusLogs .TIMEOUT :
110
68
output = process .stdout .readline ()
111
69
if output == b'' and process .poll () is not None :
112
- time .sleep (self .SLEEP )
70
+ time .sleep (AnalyzePrometheusLogs .SLEEP )
113
71
break
114
72
if output :
115
73
logger .info (output .strip ())
116
- time .sleep (self .SLEEP )
117
- current_wait_time += self .SLEEP
74
+ time .sleep (AnalyzePrometheusLogs .SLEEP )
75
+ current_wait_time += AnalyzePrometheusLogs .SLEEP
118
76
119
77
return_code = process .poll ()
120
78
logger .info (f"Container exited with return code { return_code } " )
121
79
return return_code
122
80
123
- def open_grafana_dashboard (self , promdb_dir_path : str , grafana_dashboard_url : str ):
81
+ @staticmethod
82
+ @typechecked
83
+ def open_grafana_dashboard (promdb_dir_path : str , grafana_dashboard_url : str ):
124
84
"""
125
85
This method opens the Grafana dashboard that is mounted to promdb
126
86
@param promdb_dir_path:
@@ -137,5 +97,4 @@ def open_grafana_dashboard(self, promdb_dir_path: str, grafana_dashboard_url: st
137
97
logger .info (f"Grafana direct link:: { grafana_url } " )
138
98
js_code = f"window.open('{ grafana_url } ')"
139
99
html_code = f"<script>{ js_code } </script>"
140
- if self .__s3_logs_url :
141
- display (HTML (html_code ))
100
+ display (HTML (html_code ))
0 commit comments