Skip to content

Commit

Permalink
feat: fix get cluster_info error if there is no metric from start date
Browse files Browse the repository at this point in the history
Signed-off-by: Youngjin Jo <youngjinjo@megazone.com>
  • Loading branch information
yjinjo committed May 17, 2024
1 parent e07527b commit aba29b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/plugin/connector/mimir_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def _get_unix_timestamp(start: str) -> (str, str):
def get_kubecost_cluster_info(
self,
prometheus_query_endpoint: str,
start: str,
service_account_id: str,
secret_data: dict,
) -> dict:
Expand All @@ -118,7 +119,16 @@ def get_kubecost_cluster_info(

response.raise_for_status()

result = response.json()
result = response.json().get("data", {}).get("result", [{}])

if result:
result = response.json()
else:
result = {}
_LOGGER.debug(
f"[get_kubecost_cluster_info] Agent has no metric on your start date: {start}"
)

return result
except requests.HTTPError as http_err:
_LOGGER.error(
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/manager/cost_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_data(

prometheus_query_endpoint = f"{secret_data['mimir_endpoint']}/api/v1/query"
cluster_info = self.mimir_connector.get_kubecost_cluster_info(
prometheus_query_endpoint, service_account_id, secret_data
prometheus_query_endpoint, start, service_account_id, secret_data
)

if promql_response:
Expand Down Expand Up @@ -153,7 +153,7 @@ def _make_cost_data(
x_scope_orgid: str,
) -> dict:
cluster_metric = (
cluster_info.get("data", {}).get("result", [])[0].get("metric", {})
cluster_info.get("data", {}).get("result", [{}])[0].get("metric", {})
)
costs_data = []
for result in results:
Expand Down

0 comments on commit aba29b7

Please sign in to comment.