From 1193720d3216194650f0377191283c5b3a82230f Mon Sep 17 00:00:00 2001 From: Long Zhang Date: Fri, 1 Mar 2024 15:38:59 +0100 Subject: [PATCH] #6 add more validations for the config file (#7) * #6 add more validations for the config file * #6 fix typos --- exporter_config.yaml | 3 ++- main.py | 54 +++++++++++++++++++++++++++----------------- package.json | 2 +- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/exporter_config.yaml b/exporter_config.yaml index 60a91ff..d5b3de2 100644 --- a/exporter_config.yaml +++ b/exporter_config.yaml @@ -10,6 +10,7 @@ group_by: enabled: true # Cost data can be groupped using up to two different groups: DIMENSION, TAG, COST_CATEGORY. # ref: https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsageWithResources.html + # note: label_name should be unique, and different from the labes in target_aws_accounts groups: - type: DIMENSION key: SERVICE @@ -28,4 +29,4 @@ target_aws_accounts: # it should be guaranteed that all the AWS accounts have the same set of keys (in this example they are Publisher, ProjectName, and EnvironmentName) - Publisher: 123456789012 ProjectName: myproject - EnvironmentName: dev \ No newline at end of file + EnvironmentName: dev diff --git a/main.py b/main.py index fccdce6..d6b7edc 100644 --- a/main.py +++ b/main.py @@ -1,43 +1,57 @@ #!/usr/bin/python # -*- coding:utf-8 -*- -# Filename: exporter.py +# Filename: main.py +import argparse +import logging import os import sys -import argparse -from app.exporter import MetricExporter + from envyaml import EnvYAML from prometheus_client import start_http_server -import logging + +from app.exporter import MetricExporter def get_configs(): - parser = argparse.ArgumentParser( - description="AWS Cost Exporter, exposing AWS cost data as Prometheus metrics.") - parser.add_argument("-c", "--config", required=True, - help="The config file (exporter_config.yaml) for the exporter") + parser = argparse.ArgumentParser(description="AWS Cost Exporter, exposing AWS cost data as Prometheus metrics.") + parser.add_argument( + "-c", + "--config", + required=True, + help="The config file (exporter_config.yaml) for the exporter", + ) args = parser.parse_args() - if (not os.path.exists(args.config)): - logging.error( - "AWS Cost Exporter config file does not exist, or it is not a file!") + if not os.path.exists(args.config): + logging.error("AWS Cost Exporter config file does not exist, or it is not a file!") sys.exit(1) config = EnvYAML(args.config) + return config + - # config validation +def validate_configs(config): if config["group_by.enabled"]: if len(config["group_by.groups"]) < 1 or len(config["group_by.groups"]) > 2: - logging.error( - "If group_by is enabled, there should be at leaest one group, and at most two groups!") + logging.error("If group_by is enabled, there should be at least one group, and at most two groups!") sys.exit(1) + group_label_names = set() + for group in config["group_by.groups"]: + if group["label_name"] in group_label_names: + logging.error("Group label names should be unique!") + sys.exit(1) + else: + group_label_names.add(group["label_name"]) if len(config["target_aws_accounts"]) == 0: - logging.error( - "There should be at leaest one target AWS accounts defined in the config!") + logging.error("There should be at least one target AWS accounts defined in the config!") sys.exit(1) labels = config["target_aws_accounts"][0].keys() + if group_label_names and (group_label_names & set(labels)): + logging.error("Some label names in group_by are the same as AWS account labels!") + sys.exit(1) if "Publisher" not in labels: logging.error("Publisher is a mandatory key in target_aws_accounts!") @@ -45,12 +59,9 @@ def get_configs(): for i in range(1, len(config["target_aws_accounts"])): if labels != config["target_aws_accounts"][i].keys(): - logging.error( - "All the target AWS accounts should have the same set of keys (labels)!") + logging.error("All the target AWS accounts should have the same set of keys (labels)!") sys.exit(1) - return config - def main(config): app_metrics = MetricExporter( @@ -60,7 +71,7 @@ def main(config): aws_access_secret=config["aws_access_secret"], aws_assumed_role_name=config["aws_assumed_role_name"], group_by=config["group_by"], - targets=config["target_aws_accounts"] + targets=config["target_aws_accounts"], ) start_http_server(config["exporter_port"]) app_metrics.run_metrics_loop() @@ -70,4 +81,5 @@ def main(config): logger_format = "%(asctime)-15s %(levelname)-8s %(message)s" logging.basicConfig(level=logging.INFO, format=logger_format) config = get_configs() + validate_configs(config) main(config) diff --git a/package.json b/package.json index c28e50f..27588ca 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ { "name": "aws-cost-exporter", - "version": "v1.0.2" + "version": "v1.0.3" }