Skip to content

Commit

Permalink
Merge pull request #36 from yjinjo/master
Browse files Browse the repository at this point in the history
Fix get cluster_info error if there is no metric from start date
  • Loading branch information
yjinjo authored May 17, 2024
2 parents 7629500 + aba29b7 commit ab2e6d1
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 ab2e6d1

Please sign in to comment.