diff --git a/app/exporter.py b/app/exporter.py index 0ee8b48..5790c74 100644 --- a/app/exporter.py +++ b/app/exporter.py @@ -4,6 +4,7 @@ import time import boto3 +import botocore from datetime import datetime from dateutil.relativedelta import relativedelta from prometheus_client import Gauge @@ -30,7 +31,14 @@ def __init__(self, polling_interval_seconds, aws_access_key, aws_access_secret, def run_metrics_loop(self): while True: - self.fetch() + for aws_account in self.targets: + logging.info("querying cost data for aws account %s" % + aws_account["Publisher"]) + try: + self.fetch(aws_account) + except Exception as e: + logging.error(e) + continue time.sleep(self.polling_interval_seconds) def get_aws_account_session(self, account_id): @@ -77,53 +85,51 @@ def query_aws_cost_explorer(self, aws_client, group_by): ) return response["ResultsByTime"] - def fetch(self): - for aws_account in self.targets: - logging.info("querying cost data for aws account %s" % - aws_account["Publisher"]) - aws_credentials = self.get_aws_account_session( - aws_account["Publisher"]) - aws_client = boto3.client( - "ce", - aws_access_key_id=aws_credentials["AccessKeyId"], - aws_secret_access_key=aws_credentials["SecretAccessKey"], - aws_session_token=aws_credentials["SessionToken"], - region_name="us-east-1" - ) - cost_response = self.query_aws_cost_explorer( - aws_client, self.group_by) + def fetch(self, aws_account): + aws_credentials = self.get_aws_account_session( + aws_account["Publisher"]) - for result in cost_response: - if not self.group_by["enabled"]: - cost = float(result["Total"]["UnblendedCost"]["Amount"]) - self.aws_daily_cost_usd.labels( - **aws_account, ChargeType="Usage").set(cost) - else: - merged_minor_cost = 0 - for item in result["Groups"]: - cost = float(item["Metrics"] - ["UnblendedCost"]["Amount"]) + aws_client = boto3.client( + "ce", + aws_access_key_id=aws_credentials["AccessKeyId"], + aws_secret_access_key=aws_credentials["SecretAccessKey"], + aws_session_token=aws_credentials["SessionToken"], + region_name="us-east-1" + ) + cost_response = self.query_aws_cost_explorer( + aws_client, self.group_by) - group_key_values = dict() - for i in range(len(self.group_by["groups"])): - if self.group_by["groups"][i]["type"] == "TAG": - value = item["Keys"][i].split("$")[1] - else: - value = item["Keys"][i] - group_key_values.update( - {self.group_by["groups"][i]["label_name"]: value}) + for result in cost_response: + if not self.group_by["enabled"]: + cost = float(result["Total"]["UnblendedCost"]["Amount"]) + self.aws_daily_cost_usd.labels( + **aws_account, ChargeType="Usage").set(cost) + else: + merged_minor_cost = 0 + for item in result["Groups"]: + cost = float(item["Metrics"] + ["UnblendedCost"]["Amount"]) - if self.group_by["merge_minor_cost"]["enabled"] and \ - cost < self.group_by["merge_minor_cost"]["threshold"]: - merged_minor_cost += cost + group_key_values = dict() + for i in range(len(self.group_by["groups"])): + if self.group_by["groups"][i]["type"] == "TAG": + value = item["Keys"][i].split("$")[1] else: - self.aws_daily_cost_usd.labels( - **aws_account, **group_key_values, ChargeType="Usage").set(cost) + value = item["Keys"][i] + group_key_values.update( + {self.group_by["groups"][i]["label_name"]: value}) - if merged_minor_cost > 0: - group_key_values = dict() - for i in range(len(self.group_by["groups"])): - group_key_values.update( - {self.group_by["groups"][i]["label_name"]: self.group_by["merge_minor_cost"]["tag_value"]}) + if self.group_by["merge_minor_cost"]["enabled"] and \ + cost < self.group_by["merge_minor_cost"]["threshold"]: + merged_minor_cost += cost + else: self.aws_daily_cost_usd.labels( - **aws_account, **group_key_values, ChargeType="Usage").set(merged_minor_cost) + **aws_account, **group_key_values, ChargeType="Usage").set(cost) + + if merged_minor_cost > 0: + group_key_values = dict() + for i in range(len(self.group_by["groups"])): + group_key_values.update( + {self.group_by["groups"][i]["label_name"]: self.group_by["merge_minor_cost"]["tag_value"]}) + self.aws_daily_cost_usd.labels( + **aws_account, **group_key_values, ChargeType="Usage").set(merged_minor_cost) diff --git a/package.json b/package.json index 28b81c6..c28e50f 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ { "name": "aws-cost-exporter", - "version": "v1.0.1" - } \ No newline at end of file + "version": "v1.0.2" + } diff --git a/requirements.txt b/requirements.txt index c68da42..5285cd0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ -boto3==1.21.23 -prometheus-client==0.17.0 +boto3==1.28.62 +botocore==1.31.65 envyaml==1.10.211231 -python-dateutil==2.8.2 \ No newline at end of file +prometheus_client==0.17.0 +python_dateutil==2.8.2