Skip to content

Commit

Permalink
#6 add more validations for the config file (#7)
Browse files Browse the repository at this point in the history
* #6 add more validations for the config file
* #6 fix typos
  • Loading branch information
gluckzhang authored Mar 1, 2024
1 parent 57b1a18 commit 1193720
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
3 changes: 2 additions & 1 deletion exporter_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
EnvironmentName: dev
54 changes: 33 additions & 21 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
#!/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!")
sys.exit(1)

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(
Expand All @@ -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()
Expand All @@ -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)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "aws-cost-exporter",
"version": "v1.0.2"
"version": "v1.0.3"
}

0 comments on commit 1193720

Please sign in to comment.