From 608e9c63a0112b94cb3cce28cc4dacb12b0fd1db Mon Sep 17 00:00:00 2001 From: aceclan Date: Tue, 10 Aug 2021 12:52:11 +0300 Subject: [PATCH] Improve DataDog metric shipping, this way filtering/grouping is much easier. --- awslimitchecker/metrics/datadog.py | 41 ++++++++------ awslimitchecker/tests/metrics/test_datadog.py | 56 +++++++++++++------ 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/awslimitchecker/metrics/datadog.py b/awslimitchecker/metrics/datadog.py index c2b5fc3a..7b1a559e 100644 --- a/awslimitchecker/metrics/datadog.py +++ b/awslimitchecker/metrics/datadog.py @@ -105,22 +105,24 @@ def _validate_auth(self, api_key): ) ) - def _name_for_metric(self, service, limit): + def _tag_for_metric(self, *argv): """ - Return a metric name that's safe for datadog + Return a metric tag that's safe for datadog - :param service: service name - :type service: str - :param limit: limit name - :type limit: str - :return: datadog metric name + :param argv: service group name, service name + :type argv: list + :return: datadog service tag :rtype: str """ - return ('%s%s.%s' % ( - self._prefix, - re.sub(r'[^0-9a-zA-Z]+', '_', service), - re.sub(r'[^0-9a-zA-Z]+', '_', limit) + + tag = ('%s' % ( + re.sub(r'[^0-9a-zA-Z]+', '_', argv[0]) )).lower() + for S in argv[1:]: + tag = tag + ('.%s' % ( + re.sub(r'[^0-9a-zA-Z]+', '_', S) + )).lower() + return tag def flush(self): ts = int(time.time()) @@ -137,20 +139,27 @@ def flush(self): max_usage = 0 else: max_usage = max(u).get_value() - mname = self._name_for_metric(lim.service.service_name, lim.name) + svc_tag = self._tag_for_metric(lim.service.service_name) + sub_svc_tag = self._tag_for_metric( + lim.service.service_name, lim.name + ) + local_tags = self._tags.copy() + local_tags.append('service:' + sub_svc_tag) + local_tags.append('service_group:' + svc_tag) + local_tags.append(svc_tag) series.append({ - 'metric': '%s.max_usage' % mname, + 'metric': '%susage' % self._prefix, 'points': [[ts, max_usage]], 'type': 'gauge', - 'tags': self._tags + 'tags': local_tags }) limit = lim.get_limit() if limit is not None: series.append({ - 'metric': '%s.limit' % mname, + 'metric': '%slimit' % self._prefix, 'points': [[ts, limit]], 'type': 'gauge', - 'tags': self._tags + 'tags': local_tags }) logger.info('POSTing %d metrics to datadog', len(series)) data = {'series': series} diff --git a/awslimitchecker/tests/metrics/test_datadog.py b/awslimitchecker/tests/metrics/test_datadog.py index 77ebf592..2a6cba91 100644 --- a/awslimitchecker/tests/metrics/test_datadog.py +++ b/awslimitchecker/tests/metrics/test_datadog.py @@ -219,13 +219,13 @@ def test_failure(self): ] -class TestNameForMetric(DatadogTester): +class TestTagForMetric(DatadogTester): def test_simple(self): self.cls._prefix = 'foobar.' - assert self.cls._name_for_metric( - 'Service Name*', 'limit NAME .' - ) == 'foobar.service_name_.limit_name_' + assert self.cls._tag_for_metric( + 'Service Group Name*', 'service NAME .' + ) == 'service_group_name_.service_name_' class TestFlush(DatadogTester): @@ -268,22 +268,34 @@ def test_happy_path(self): 'tags': ['tag1', 'tag:2'] }, { - 'metric': 'prefix.svc1.limita.max_usage', + 'metric': 'prefix.usage', 'points': [[ts, 0]], 'type': 'gauge', - 'tags': ['tag1', 'tag:2'] + 'tags': ['tag1', 'tag:2', + 'service:svc1.limita', + 'service_group:svc1', + 'svc1' + ] }, { - 'metric': 'prefix.svc1.limitb.max_usage', + 'metric': 'prefix.usage', 'points': [[ts, 6]], 'type': 'gauge', - 'tags': ['tag1', 'tag:2'] + 'tags': ['tag1', 'tag:2', + 'service:svc1.limitb', + 'service_group:svc1', + 'svc1' + ] }, { - 'metric': 'prefix.svc1.limitb.limit', + 'metric': 'prefix.limit', 'points': [[ts, 10]], 'type': 'gauge', - 'tags': ['tag1', 'tag:2'] + 'tags': ['tag1', 'tag:2', + 'service:svc1.limitb', + 'service_group:svc1', + 'svc1' + ] } ] } @@ -339,22 +351,34 @@ def test_api_error_non_default_host(self): 'tags': ['tag1', 'tag:2'] }, { - 'metric': 'prefix.svc1.limita.max_usage', + 'metric': 'prefix.usage', 'points': [[ts, 0]], 'type': 'gauge', - 'tags': ['tag1', 'tag:2'] + 'tags': ['tag1', 'tag:2', + 'service:svc1.limita', + 'service_group:svc1', + 'svc1' + ] }, { - 'metric': 'prefix.svc1.limitb.max_usage', + 'metric': 'prefix.usage', 'points': [[ts, 6]], 'type': 'gauge', - 'tags': ['tag1', 'tag:2'] + 'tags': ['tag1', 'tag:2', + 'service:svc1.limitb', + 'service_group:svc1', + 'svc1' + ] }, { - 'metric': 'prefix.svc1.limitb.limit', + 'metric': 'prefix.limit', 'points': [[ts, 10]], 'type': 'gauge', - 'tags': ['tag1', 'tag:2'] + 'tags': ['tag1', 'tag:2', + 'service:svc1.limitb', + 'service_group:svc1', + 'svc1' + ] } ] }