From acf6cf52ee230e3cc0255dac94ce53110c446f2d Mon Sep 17 00:00:00 2001 From: Long Zhang Date: Mon, 14 Aug 2023 11:38:42 +0200 Subject: [PATCH] make the metric name more descriptive and improve doc --- README.md | 16 +++++++++++++++- app/exporter.py | 10 +++++----- deployment/k8s/deployment.yaml | 4 ++-- package.json | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f6bb324..1da6d36 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,20 @@ Cloud providers like AWS and Azure usually provide cost management portals, dash This AWS Cost Metrics Exporter helps users to fetch AWS cost information using AWS Cost Explorer APIs and exposes them as standard Prometheus metrics. This enables users to have cost-related metrics present in the same place where their business metrics are. The design also makes it possible to collect cost data from different providers and design one single dashboard for all the costs. +## Sample Output + +``` +# HELP aws_daily_cost_usd Daily cost of an AWS account in USD +# TYPE aws_daily_cost_usd gauge +aws_daily_cost_usd{ChargeType="Usage",EnvironmentName="sandbox",ProjectName="myproject",Publisher="",RegionName="eu-central-1"} 10.1827240691 +aws_daily_cost_usd{ChargeType="Usage",EnvironmentName="sandbox",ProjectName="myproject",Publisher="",RegionName="other"} 4.258201088100001 +aws_daily_cost_usd{ChargeType="Usage",EnvironmentName="prod",ProjectName="myproject",Publisher="",RegionName="eu-central-1"} 68.6121380948 +aws_daily_cost_usd{ChargeType="Usage",EnvironmentName="prod",ProjectName="myproject",Publisher="",RegionName="other"} 2.6191806712 +... +``` + +*ps: As the metric name indicate, the metric shows the daily costs in USD. `Daily` is based a fixed 24h time window, from UTC 00:00 to UTC 24:00. `EnvironmentName` and `ProjectName` are the custom labels that can be configured. `RegionName` is a label based on `group_by` configuration.* + ## How Does This Work AWS Cost Metrics Exporter fetches cost data from a list of AWS accounts, each of which provides a necessary IAM role for the exporter. It regularly queries the AWS [GetCostAndUsage](https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsage.html) to get the whole AWS account's cost. It is configurable to have different queries, such as group by services and tags, merge minor cost to one single category, etc. The following figure describes how AWS Cost Metrics Exporter works. @@ -61,7 +75,7 @@ Modify the `exporter_config.yaml` file first, then use one of the following meth ### Docker ``` -docker run --rm -v ./exporter_config.yaml:/app/exporter_config.yaml -p 9090:9090 -e AWS_ACCESS_KEY=${AWS_ACCESS_KEY} -e AWS_ACCESS_SECRET=${AWS_ACCESS_SECRET} opensourceelectrolux/aws-cost-exporter:v1.0.0 +docker run --rm -v ./exporter_config.yaml:/app/exporter_config.yaml -p 9090:9090 -e AWS_ACCESS_KEY=${AWS_ACCESS_KEY} -e AWS_ACCESS_SECRET=${AWS_ACCESS_SECRET} opensourceelectrolux/aws-cost-exporter:v1.0.1 ``` ### Kubernetes diff --git a/app/exporter.py b/app/exporter.py index 6c13166..0ee8b48 100644 --- a/app/exporter.py +++ b/app/exporter.py @@ -25,8 +25,8 @@ def __init__(self, polling_interval_seconds, aws_access_key, aws_access_secret, if group_by["enabled"]: for group in group_by["groups"]: self.labels.add(group["label_name"]) - self.aws_cost = Gauge( - "aws_cost", "Daily cost of an AWS account", self.labels) + self.aws_daily_cost_usd = Gauge( + "aws_daily_cost_usd", "Daily cost of an AWS account in USD", self.labels) def run_metrics_loop(self): while True: @@ -96,7 +96,7 @@ def fetch(self): for result in cost_response: if not self.group_by["enabled"]: cost = float(result["Total"]["UnblendedCost"]["Amount"]) - self.aws_cost.labels( + self.aws_daily_cost_usd.labels( **aws_account, ChargeType="Usage").set(cost) else: merged_minor_cost = 0 @@ -117,7 +117,7 @@ def fetch(self): cost < self.group_by["merge_minor_cost"]["threshold"]: merged_minor_cost += cost else: - self.aws_cost.labels( + self.aws_daily_cost_usd.labels( **aws_account, **group_key_values, ChargeType="Usage").set(cost) if merged_minor_cost > 0: @@ -125,5 +125,5 @@ def fetch(self): 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_cost.labels( + self.aws_daily_cost_usd.labels( **aws_account, **group_key_values, ChargeType="Usage").set(merged_minor_cost) diff --git a/deployment/k8s/deployment.yaml b/deployment/k8s/deployment.yaml index c94ac98..32b2e6b 100644 --- a/deployment/k8s/deployment.yaml +++ b/deployment/k8s/deployment.yaml @@ -6,7 +6,7 @@ metadata: app.kubernetes.io/name: aws-cost-exporter app.kubernetes.io/part-of: finops app.kubernetes.io/component: backend - app.kubernetes.io/version: "v1.0.0" + app.kubernetes.io/version: "v1.0.1" app.kubernetes.io/language: python spec: replicas: 1 @@ -24,7 +24,7 @@ spec: spec: containers: - name: aws-cost-exporter - image: "opensourceelectrolux/aws-cost-exporter:v1.0.0" + image: "opensourceelectrolux/aws-cost-exporter:v1.0.1" command: [ "python", "main.py", "-c", "/exporter_config.yaml" ] imagePullPolicy: Always env: diff --git a/package.json b/package.json index 7ee54d5..28b81c6 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ { "name": "aws-cost-exporter", - "version": "v1.0.0" + "version": "v1.0.1" } \ No newline at end of file