Skip to content

Commit

Permalink
Merge pull request #1 from opensourceelectrolux/adjust-metric-name-an…
Browse files Browse the repository at this point in the history
…d-add-more-doc
  • Loading branch information
gluckzhang authored Aug 14, 2023
2 parents dd47e9c + acf6cf5 commit e736ee5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="<aws_account_1>",RegionName="eu-central-1"} 10.1827240691
aws_daily_cost_usd{ChargeType="Usage",EnvironmentName="sandbox",ProjectName="myproject",Publisher="<aws_account_1>",RegionName="other"} 4.258201088100001
aws_daily_cost_usd{ChargeType="Usage",EnvironmentName="prod",ProjectName="myproject",Publisher="<aws_account_2>",RegionName="eu-central-1"} 68.6121380948
aws_daily_cost_usd{ChargeType="Usage",EnvironmentName="prod",ProjectName="myproject",Publisher="<aws_account_2>",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.
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions app/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -117,13 +117,13 @@ 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:
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_cost.labels(
self.aws_daily_cost_usd.labels(
**aws_account, **group_key_values, ChargeType="Usage").set(merged_minor_cost)
4 changes: 2 additions & 2 deletions deployment/k8s/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
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.0"
"version": "v1.0.1"
}

0 comments on commit e736ee5

Please sign in to comment.