Skip to content

Commit

Permalink
fix prometheus client types
Browse files Browse the repository at this point in the history
  • Loading branch information
cmelone committed Feb 5, 2024
1 parent fd7ecfa commit a93a528
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions gantry/clients/prometheus/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, base_url: str, auth_cookie: str = ""):

self.base_url = base_url

async def query_single(self, query: str | dict, time: int) -> dict:
async def query_single(self, query: str | dict, time: int) -> list:
"""Query Prometheus for a single value
args:
Expand All @@ -39,7 +39,7 @@ async def query_single(self, query: str | dict, time: int) -> dict:
url = f"{self.base_url}/query?query={query}&time={time}"
return await self._query(url)

async def query_range(self, query: str | dict, start: int, end: int) -> dict:
async def query_range(self, query: str | dict, start: int, end: int) -> list:
"""Query Prometheus for a range of values
args:
Expand All @@ -64,7 +64,7 @@ async def query_range(self, query: str | dict, start: int, end: int) -> dict:
)
return await self._query(url)

async def _query(self, url: str) -> dict:
async def _query(self, url: str) -> list:
"""Query Prometheus with a query string"""
async with aiohttp.ClientSession(raise_for_status=True) as session:
# submit cookie with request
Expand All @@ -78,7 +78,7 @@ async def _query(self, url: str) -> dict:
)
return {}

def prettify_res(self, response: dict) -> dict:
def prettify_res(self, response: dict) -> list:
"""Process Prometheus response into an arrray of dicts with {label: value}"""
result_type = response.get("data", {}).get("resultType")
values_dict = {
Expand All @@ -88,7 +88,7 @@ def prettify_res(self, response: dict) -> dict:

if result_type not in values_dict:
logging.error(f"Prometheus response type {result_type} not supported")
return {}
return []

return [
{"labels": result["metric"], "values": result[values_dict[result_type]]}
Expand Down
4 changes: 2 additions & 2 deletions gantry/clients/prometheus/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def query_to_str(metric: str, filters: dict) -> str:
return f"{metric}{{{filters_str}}}"


def process_resources(res: dict) -> dict:
def process_resources(res: list) -> dict:
"""
Processes the resource limits and requests from a Prometheus response into
readable format.
Expand All @@ -54,7 +54,7 @@ def process_resources(res: dict) -> dict:
return processed


def process_usage(res: dict) -> dict:
def process_usage(res: list) -> dict:
"""
Processes the usage data from a Prometheus response into readable format.
This could either be CPU usage or memory usage.
Expand Down

0 comments on commit a93a528

Please # to comment.