From 71dcc9c27acb741aa9e01e93fbd3d7826597dc3a Mon Sep 17 00:00:00 2001 From: Shane Starcher Date: Tue, 7 Feb 2017 14:03:10 -0500 Subject: [PATCH 1/4] adding prometheus output --- awslimitchecker/metrics/prometheus.py | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 awslimitchecker/metrics/prometheus.py diff --git a/awslimitchecker/metrics/prometheus.py b/awslimitchecker/metrics/prometheus.py new file mode 100644 index 00000000..f6291d00 --- /dev/null +++ b/awslimitchecker/metrics/prometheus.py @@ -0,0 +1,60 @@ +import logging +import time + +from string import maketrans +from awslimitchecker.checker import AwsLimitChecker +from prometheus_client import start_http_server, Summary, Gauge + + +REQUEST_TIME = Summary('update_processing_seconds', 'Time spent querying aws for limits') +gauges = {} +trantab = maketrans(' -.', '___') + +def main(): + logger = logging.getLogger() + logger.setLevel(logging.ERROR) + + checkers = {} + for region in ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']: + checkers[region]= AwsLimitChecker(region=region) + + start_http_server(8000) + for region, checker in checkers.iteritems(): + update(checker, region) + time.sleep(60) + + + + +@REQUEST_TIME.time() +def update(checker, region): + checker.find_usage() + + labels = {'region': region} + for service, svc_limits in sorted(checker.get_limits().items()): + for limit_name, limit in sorted(svc_limits.items()): + path = '.'.join([service, limit_name]) + usage = limit.get_current_usage() + metric = limit.get_limit() + update_service(path, usage, metric, region) + +def update_service(path, usage, limit, region): + g = gauge(path) + + g.labels(region=region, type='limit').set(limit) + for resource in usage: + metric_type = 'current' + if resource.resource_id: + metric_type = resource.resource_id + g.labels(region=region, type=metric_type).set(resource.get_value()) + +def gauge(path): + path = path.lower().translate(trantab, '()') + g = gauges.get(path, None) + if g is None: + g = Gauge(path, '', ['region', 'type']) + gauges[path] = g + return g + +if __name__ == "__main__": + main() From 9d9d15897cef44f9b6503b26340e56ad85dc6adc Mon Sep 17 00:00:00 2001 From: Shane Starcher Date: Wed, 15 Feb 2017 18:05:15 -0500 Subject: [PATCH 2/4] fixing linting issues and adding env variables for port and sleep time --- awslimitchecker/metrics/prometheus.py | 39 +++++++++++++++++---------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/awslimitchecker/metrics/prometheus.py b/awslimitchecker/metrics/prometheus.py index f6291d00..ff1e84dc 100644 --- a/awslimitchecker/metrics/prometheus.py +++ b/awslimitchecker/metrics/prometheus.py @@ -1,42 +1,51 @@ import logging import time +import os from string import maketrans from awslimitchecker.checker import AwsLimitChecker from prometheus_client import start_http_server, Summary, Gauge -REQUEST_TIME = Summary('update_processing_seconds', 'Time spent querying aws for limits') +REQUEST_TIME = Summary( + 'update_processing_seconds', + 'Time spent querying aws for limits' + ) gauges = {} trantab = maketrans(' -.', '___') + def main(): + port = int(os.environ.get('ALC_PORT', '8080')) + interval = int(os.environ.get('ALC_INTERVAL', '60')) logger = logging.getLogger() logger.setLevel(logging.ERROR) checkers = {} for region in ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']: - checkers[region]= AwsLimitChecker(region=region) + checkers[region] = AwsLimitChecker(region=region) - start_http_server(8000) + start_http_server(port) for region, checker in checkers.iteritems(): update(checker, region) - time.sleep(60) - - + time.sleep(interval) @REQUEST_TIME.time() def update(checker, region): - checker.find_usage() + try: + checker.find_usage() + + labels = {'region': region} + for service, svc_limits in sorted(checker.get_limits().items()): + for limit_name, limit in sorted(svc_limits.items()): + path = '.'.join([service, limit_name]) + usage = limit.get_current_usage() + metric = limit.get_limit() + update_service(path, usage, metric, region) + except Exception as e: + logging.exception("message") - labels = {'region': region} - for service, svc_limits in sorted(checker.get_limits().items()): - for limit_name, limit in sorted(svc_limits.items()): - path = '.'.join([service, limit_name]) - usage = limit.get_current_usage() - metric = limit.get_limit() - update_service(path, usage, metric, region) def update_service(path, usage, limit, region): g = gauge(path) @@ -48,6 +57,7 @@ def update_service(path, usage, limit, region): metric_type = resource.resource_id g.labels(region=region, type=metric_type).set(resource.get_value()) + def gauge(path): path = path.lower().translate(trantab, '()') g = gauges.get(path, None) @@ -56,5 +66,6 @@ def gauge(path): gauges[path] = g return g + if __name__ == "__main__": main() From 4bbed43cd90ee2b2e33aec8895ebe5d2bb857f15 Mon Sep 17 00:00:00 2001 From: Shane Starcher Date: Wed, 15 Feb 2017 18:07:28 -0500 Subject: [PATCH 3/4] moving to docs --- docs/examples/prometheus.py | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 docs/examples/prometheus.py diff --git a/docs/examples/prometheus.py b/docs/examples/prometheus.py new file mode 100644 index 00000000..ff1e84dc --- /dev/null +++ b/docs/examples/prometheus.py @@ -0,0 +1,71 @@ +import logging +import time +import os + +from string import maketrans +from awslimitchecker.checker import AwsLimitChecker +from prometheus_client import start_http_server, Summary, Gauge + + +REQUEST_TIME = Summary( + 'update_processing_seconds', + 'Time spent querying aws for limits' + ) +gauges = {} +trantab = maketrans(' -.', '___') + + +def main(): + port = int(os.environ.get('ALC_PORT', '8080')) + interval = int(os.environ.get('ALC_INTERVAL', '60')) + logger = logging.getLogger() + logger.setLevel(logging.ERROR) + + checkers = {} + for region in ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']: + checkers[region] = AwsLimitChecker(region=region) + + start_http_server(port) + for region, checker in checkers.iteritems(): + update(checker, region) + time.sleep(interval) + + +@REQUEST_TIME.time() +def update(checker, region): + try: + checker.find_usage() + + labels = {'region': region} + for service, svc_limits in sorted(checker.get_limits().items()): + for limit_name, limit in sorted(svc_limits.items()): + path = '.'.join([service, limit_name]) + usage = limit.get_current_usage() + metric = limit.get_limit() + update_service(path, usage, metric, region) + except Exception as e: + logging.exception("message") + + +def update_service(path, usage, limit, region): + g = gauge(path) + + g.labels(region=region, type='limit').set(limit) + for resource in usage: + metric_type = 'current' + if resource.resource_id: + metric_type = resource.resource_id + g.labels(region=region, type=metric_type).set(resource.get_value()) + + +def gauge(path): + path = path.lower().translate(trantab, '()') + g = gauges.get(path, None) + if g is None: + g = Gauge(path, '', ['region', 'type']) + gauges[path] = g + return g + + +if __name__ == "__main__": + main() From 6acb5bd0475a26f8cd12a2bd1b8b6b1dc9940148 Mon Sep 17 00:00:00 2001 From: Shane Starcher Date: Wed, 15 Feb 2017 18:07:40 -0500 Subject: [PATCH 4/4] moving to docs --- awslimitchecker/metrics/prometheus.py | 71 --------------------------- 1 file changed, 71 deletions(-) delete mode 100644 awslimitchecker/metrics/prometheus.py diff --git a/awslimitchecker/metrics/prometheus.py b/awslimitchecker/metrics/prometheus.py deleted file mode 100644 index ff1e84dc..00000000 --- a/awslimitchecker/metrics/prometheus.py +++ /dev/null @@ -1,71 +0,0 @@ -import logging -import time -import os - -from string import maketrans -from awslimitchecker.checker import AwsLimitChecker -from prometheus_client import start_http_server, Summary, Gauge - - -REQUEST_TIME = Summary( - 'update_processing_seconds', - 'Time spent querying aws for limits' - ) -gauges = {} -trantab = maketrans(' -.', '___') - - -def main(): - port = int(os.environ.get('ALC_PORT', '8080')) - interval = int(os.environ.get('ALC_INTERVAL', '60')) - logger = logging.getLogger() - logger.setLevel(logging.ERROR) - - checkers = {} - for region in ['us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']: - checkers[region] = AwsLimitChecker(region=region) - - start_http_server(port) - for region, checker in checkers.iteritems(): - update(checker, region) - time.sleep(interval) - - -@REQUEST_TIME.time() -def update(checker, region): - try: - checker.find_usage() - - labels = {'region': region} - for service, svc_limits in sorted(checker.get_limits().items()): - for limit_name, limit in sorted(svc_limits.items()): - path = '.'.join([service, limit_name]) - usage = limit.get_current_usage() - metric = limit.get_limit() - update_service(path, usage, metric, region) - except Exception as e: - logging.exception("message") - - -def update_service(path, usage, limit, region): - g = gauge(path) - - g.labels(region=region, type='limit').set(limit) - for resource in usage: - metric_type = 'current' - if resource.resource_id: - metric_type = resource.resource_id - g.labels(region=region, type=metric_type).set(resource.get_value()) - - -def gauge(path): - path = path.lower().translate(trantab, '()') - g = gauges.get(path, None) - if g is None: - g = Gauge(path, '', ['region', 'type']) - gauges[path] = g - return g - - -if __name__ == "__main__": - main()