Skip to content

Automated fix for refs/heads/prometheus-query #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: prometheus-query
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 31 additions & 34 deletions tools/run_tests/performance/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.


# example usage: python3 prometheus.py --url=http://10.108.4.94:9090
# --pod_type=driver --pod_type=client --container_name=main
# --container_name=sidecar
# --container_name=sidecar
import argparse
import json
import logging
import requests
import statistics
from typing import Any, Dict, List

from dateutil import parser
from typing import Any, Dict, List
import requests


class Prometheus:

def __init__(
self,
url: str,
Expand All @@ -43,52 +43,49 @@ def fetch_by_query(self, query: str) -> Any:
"""Fetches the given query with time range."""
resp = requests.get(
self.url + "/api/v1/query_range",
{"query": query, "start": self.start, "end": self.end, "step": 5},
{
"query": query,
"start": self.start,
"end": self.end,
"step": 5
},
)
resp.raise_for_status()
return resp.json()

def fetch_cpu_for_pod(
self, container_matcher: str, pod_name: str
) -> Dict[str, List[float]]:
def fetch_cpu_for_pod(self, container_matcher: str,
pod_name: str) -> Dict[str, List[float]]:
"""Fetches the cpu data for each pod and construct the container
name to cpu data list Dict."""
query = (
'irate(container_cpu_usage_seconds_total{job="kubernetes-cadvisor",pod="'
+ pod_name
+ '",container='
+ container_matcher
+ "}[100s])"
)
+ pod_name + '",container=' + container_matcher + "}[100s])")
logging.debug("running prometheus query for cpu:" + query)
cpu_data = self.fetch_by_query(query)
logging.debug("raw cpu data:" + str(cpu_data))
cpu_container_name_to_data_list = get_data_list_from_timeseries(cpu_data)
cpu_container_name_to_data_list = get_data_list_from_timeseries(
cpu_data)
return cpu_container_name_to_data_list

def fetch_memory_for_pod(
self, container_matcher: str, pod_name: str
) -> Dict[str, List[float]]:
def fetch_memory_for_pod(self, container_matcher: str,
pod_name: str) -> Dict[str, List[float]]:
"""Fetches the memory data for each pod and construct the
container name to memory data list Dict."""
query = (
'container_memory_usage_bytes{job="kubernetes-cadvisor",pod="'
+ pod_name
+ '",container='
+ container_matcher
+ "}"
)
'container_memory_usage_bytes{job="kubernetes-cadvisor",pod="' +
pod_name + '",container=' + container_matcher + "}")

logging.debug("running prometheus query for memory:" + query)
memory_data = self.fetch_by_query(query)

logging.debug("raw memory data:" + str(memory_data))
memory_container_name_to_data_list = get_data_list_from_timeseries(memory_data)
memory_container_name_to_data_list = get_data_list_from_timeseries(
memory_data)

return memory_container_name_to_data_list

def fetch_cpu_and_memory_data(
self, container_list: List[str], pod_list: List[str]
self, container_list: List[str], pod_list: List[str]
) -> Dict[str, Dict[str, Dict[str, Dict[str, Any]]]]:
"""Fetches and process min, max, mean, std for the memory and cpu
data for each container in the container_list for each pod in
Expand All @@ -102,7 +99,8 @@ def fetch_cpu_and_memory_data(
raw_memory_data = {}
for pod in pod_list:
raw_cpu_data[pod] = self.fetch_cpu_for_pod(container_matcher, pod)
raw_memory_data[pod] = self.fetch_memory_for_pod(container_matcher, pod)
raw_memory_data[pod] = self.fetch_memory_for_pod(
container_matcher, pod)

processed_data["cpu"] = compute_min_max_mean_std(raw_cpu_data)
processed_data["memory"] = compute_min_max_mean_std(raw_memory_data)
Expand Down Expand Up @@ -133,7 +131,8 @@ def get_data_list_from_timeseries(data: Any) -> Dict[str, List[float]]:
if data["status"] != "success":
raise Exception("command failed: " + data["status"] + str(data))
if data["data"]["resultType"] != "matrix":
raise Exception("resultType is not matrix: " + data["data"]["resultType"])
raise Exception("resultType is not matrix: " +
data["data"]["resultType"])

container_name_to_data_list = {}
for res in data["data"]["result"]:
Expand Down Expand Up @@ -174,8 +173,7 @@ def compute_min_max_mean_std(
container_name_to_processed_data = {}
for container_name, data_list in pod_data_dicts.items():
container_name_to_processed_data[
container_name
] = compute_min_max_mean_std_for_each(data_list)
container_name] = compute_min_max_mean_std_for_each(data_list)
pod_name_to_data_dicts[pod_name] = container_name_to_processed_data

return pod_name_to_data_dicts
Expand Down Expand Up @@ -210,8 +208,7 @@ def convert_UTC_to_epoch(utc_timestamp: str) -> str:

def main() -> None:
argp = argparse.ArgumentParser(
description="Fetch cpu and memory stats from prometheus"
)
description="Fetch cpu and memory stats from prometheus")
argp.add_argument("--url", help="Prometheus base url", required=True)
argp.add_argument(
"--scenario_result_file",
Expand All @@ -227,7 +224,8 @@ def main() -> None:
argp.add_argument(
"--pod_type",
action="append",
help="Pod type to query the metrics for, the options are driver, client and server",
help=
"Pod type to query the metrics for, the options are driver, client and server",
choices=["driver", "client", "server"],
required=True,
)
Expand Down Expand Up @@ -263,8 +261,7 @@ def main() -> None:

pod_list = construct_pod_list(args.node_info_file, args.pod_type)
processed_data = p.fetch_cpu_and_memory_data(
container_list=args.container_name, pod_list=pod_list
)
container_list=args.container_name, pod_list=pod_list)

logging.debug(json.dumps(processed_data, sort_keys=True, indent=4))

Expand Down