Skip to content

Commit

Permalink
Merge pull request #5 from electrolux-oss/use-kubecost-v2-api
Browse files Browse the repository at this point in the history
update query based on the latest version of Kubecost
  • Loading branch information
gluckzhang authored Jul 19, 2024
2 parents ea1c543 + cd775e2 commit 3670a6d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions app/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ def run_metrics_loop(self):

def fetch(self):
api = (
f"/allocation/view?aggregate={','.join(self.aggregate)}&window=today&shareIdle=true&idle=true&"
"idleByNode=false&shareTenancyCosts=true&shareNamespaces=&shareCost=NaN&"
"shareSplit=weighted&chartType=costovertime&costUnit=cumulative&step="
f"/allocation/summary?aggregate={','.join(self.aggregate)}&accumulate=true&chartType=costovertime&"
"costUnit=cumulative&external=false&filter=&idle=true&idleByNode=false&includeSharedCostBreakdown=true&"
"shareCost=0&shareIdle=true&shareLabels=&shareNamespaces=&shareSplit=weighted&shareTenancyCosts=true&"
"window=today"
)
response = requests.get(self.endpoint + api)
if response.status_code != 200 or response.json().get("code") != 200:
Expand All @@ -42,15 +43,15 @@ def fetch(self):
% (api, response.status_code, response.text)
)
else:
items = response.json()["data"]["items"]["items"]
for item in items:
allocations = response.json()["data"]["sets"][0]["allocations"]
for item in allocations:
aggregation_labels = {}
names = item["name"].split("/")
names = item.split("/")
for i in range(len(self.aggregate)):
aggregation_labels[self.aggregate[i]] = names[i]

if self.extra_labels:
self.kubernetes_daily_cost_usd.labels(**aggregation_labels, **self.extra_labels).set(item["totalCost"])
self.kubernetes_daily_cost_usd.labels(**aggregation_labels, **self.extra_labels).set(allocations[item]["totalCost"])
else:
self.kubernetes_daily_cost_usd.labels(**aggregation_labels).set(item["totalCost"])
self.kubernetes_daily_cost_usd.labels(**aggregation_labels).set(allocations[item]["totalCost"])
logging.info(f"cost metric {self.name} updated successfully")
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "kubernetes-cost-exporter",
"version": "v1.0.4"
"version": "v1.0.5"
}

0 comments on commit 3670a6d

Please sign in to comment.