-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
78 lines (65 loc) · 2.41 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
# -*- coding:utf-8 -*-
# Filename: main.py
import argparse
from app.exporter import MetricExporter
from prometheus_client import start_http_server
import logging
class key_value_arg(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, dict())
for kvpair in values:
assert len(kvpair.split("=")) == 2
key, value = kvpair.split("=")
getattr(namespace, self.dest)[key] = value
def get_args():
parser = argparse.ArgumentParser(
description="Kubernetes Cost Exporter, exposing Kubernetes cost data as Prometheus metrics."
)
parser.add_argument("-p", "--port", default=9090, type=int, help="Exporter's port (default: 9090)")
parser.add_argument("-i", "--interval", default=60, type=int, help="Update interval in seconds (default: 60)")
parser.add_argument(
"-n",
"--name",
default="kubernetes_daily_cost_usd",
help="Name of the exposed metric (default: kubernetes_daily_cost_usd)",
)
parser.add_argument(
"-e",
"--endpoint",
default="http://kubecost-cost-analyzer.monitoring.svc:9003",
help="Kubecost service endpoint (default: http://kubecost-cost-analyzer.monitoring.svc:9003)",
)
parser.add_argument(
"-a",
"--aggregate",
action="append",
help="Aggregation level (default: namespace), e.g., cluster, namespace, deployment, pod, etc. Multiple options are supported, e.g., -a namespace -a deployment",
)
parser.add_argument(
"-l",
"--label",
nargs="*",
action=key_value_arg,
help="Additional labels that need to be added to the metric, \
may be used several times, e.g., --label project=foo environment=dev",
)
args = parser.parse_args()
return args
def main(args):
if args.aggregate is None:
args.aggregate = ["namespace"]
app_metrics = MetricExporter(
endpoint=args.endpoint,
aggregate=args.aggregate,
interval=args.interval,
name=args.name,
extra_labels=args.label,
)
start_http_server(args.port)
app_metrics.run_metrics_loop()
if __name__ == "__main__":
logger_format = "%(asctime)-15s %(levelname)-8s %(message)s"
logging.basicConfig(level=logging.INFO, format=logger_format)
args = get_args()
main(args)