-
Notifications
You must be signed in to change notification settings - Fork 8
/
prometheus.py
153 lines (134 loc) · 7.12 KB
/
prometheus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import requests
from .constants import PROMETHEUS
class PromClient:
def __init__(self) -> None:
pass
def prom_response_postprocess(self, response):
try:
response = response.json()["data"]["result"]
except:
print(response.json())
exit()
plot_values = [[] for _ in range(len(response))]
times = [[] for _ in range(len(response))]
try:
for val in range(len(response)):
data = response[val]["values"]
for d in data:
plot_values[val].append((float(d[1])))
times[val].append(float(d[0]))
output = plot_values[0], times[0]
except IndexError:
output = None, None
return output
def get_memory_usage(
self,
pod_name: str,
namespace: str,
container: str,
duration: int,
need_max: bool = False,
):
query = f"container_memory_usage_bytes{{pod=~'{pod_name}.*', container='{container}', namespace='{namespace}'}}[{duration}m:1s]"
if need_max:
query = f"max_over_time(container_memory_usage_bytes{{pod=~'{pod_name}.*', container='{container}', namespace='{namespace}'}}[{duration}m])"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_usage_count(
self, pod_name: str, namespace: str, container: str, duration: int
):
query = f"container_cpu_usage_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_usage_rate(
self, pod_name: str, namespace: str, container: str, duration: int, rate=120
):
query = f"rate(container_cpu_usage_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{rate}s])[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_throttled_count(
self, pod_name: str, namespace: str, container: str, duration: int
):
query = f"container_cpu_cfs_throttled_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_throttled_rate(
self, pod_name: str, namespace: str, container: str, duration: int, rate=120
):
query = f"rate(container_cpu_cfs_throttled_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{rate}s])[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_request_per_second(
self, pod_name, namespace, container, duration, rate=120
):
query = f"rate(model_infer_request_duration_count{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{rate}s])[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_input_rps(self, pod_name, namespace, container, duration, rate=120):
query = f"rate(input_requests_count{{pod='{pod_name}', namespace='{namespace}', container='{container}'}}[{rate}s])[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
class TritonNodePromClient:
def __init__(self) -> None:
pass
def prom_response_postprocess(self, response):
try:
response = response.json()["data"]["result"]
except:
print(response.json())
exit()
plot_values = [[] for _ in range(len(response))]
times = [[] for _ in range(len(response))]
try:
for val in range(len(response)):
data = response[val]["values"]
for d in data:
plot_values[val].append((float(d[1])))
times[val].append(float(d[0]))
output = plot_values[0], times[0]
except IndexError:
output = None, None
return output
def get_memory_usage(
self,
pod_name: str,
namespace: str,
container: str,
duration: int,
need_max: bool = False,
):
query = f"container_memory_usage_bytes{{pod=~'{pod_name}.*', container='{container}', namespace='{namespace}'}}[{duration}m:1s]"
if need_max:
query = f"max_over_time(container_memory_usage_bytes{{pod=~'{pod_name}.*', container='{container}', namespace='{namespace}'}}[{duration}m])"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_usage_count(
self, pod_name: str, namespace: str, container: str, duration: int
):
query = f"container_cpu_usage_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_usage_rate(
self, pod_name: str, namespace: str, container: str, duration: int, rate=120
):
query = f"rate(container_cpu_usage_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{rate}s])[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_throttled_count(
self, pod_name: str, namespace: str, container: str, duration: int
):
query = f"container_cpu_cfs_throttled_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_cpu_throttled_rate(
self, pod_name: str, namespace: str, container: str, duration: int, rate=120
):
query = f"rate(container_cpu_cfs_throttled_seconds_total{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{rate}s])[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)
def get_request_per_second(
self, pod_name, namespace, container, duration, rate=120
):
query = f"rate(model_infer_request_duration_count{{pod=~'{pod_name}.*', namespace='{namespace}', container='{container}'}}[{rate}s])[{duration}m:1s]"
response = requests.get(PROMETHEUS + "/api/v1/query", params={"query": query})
return self.prom_response_postprocess(response)