Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions awslimitchecker/metrics/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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}
Expand Down
56 changes: 40 additions & 16 deletions awslimitchecker/tests/metrics/test_datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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'
]
}
]
}
Expand Down Expand Up @@ -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'
]
}
]
}
Expand Down