From fe9e0a24ae698c89f0127541f0447a2040eed826 Mon Sep 17 00:00:00 2001 From: Mikael Johansson Date: Wed, 31 Aug 2016 13:11:44 +0200 Subject: [PATCH 1/2] Added standard Datadog tag type:change to simplify filtering when there's many sources of changes (terraform, ..) --- src/lighter/datadog.py | 2 +- src/lighter/test/datadog_test.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lighter/datadog.py b/src/lighter/datadog.py index 3aa1536..dfbd207 100644 --- a/src/lighter/datadog.py +++ b/src/lighter/datadog.py @@ -7,7 +7,7 @@ class Datadog(object): def __init__(self, token, tags=[]): self._token = token self._url = 'https://app.datadoghq.com' - self._tags = tags + ['source:lighter'] + self._tags = tags + ['source:lighter', 'type:change'] def notify(self, title, message, id, tags=[], priority='normal', alert_type='success'): if not title or not message or not id: diff --git a/src/lighter/test/datadog_test.py b/src/lighter/test/datadog_test.py index 825048c..412922b 100644 --- a/src/lighter/test/datadog_test.py +++ b/src/lighter/test/datadog_test.py @@ -65,7 +65,8 @@ def testDefaultTags(self): expected = [ 'environment:default', u'service:/myproduct/myservice', - 'source:lighter'] + 'source:lighter', + 'type:change'] self.assertEquals(expected, mock_jsonRequest.call_args[1]['data']['tags']) def testConfiguredTags(self): @@ -79,7 +80,8 @@ def testConfiguredTags(self): 'somekey:someval', 'anotherkey:anotherval', 'justakey', - 'source:lighter'] + 'source:lighter', + 'type:change'] self.assertEquals(expected, mock_jsonRequest.call_args[1]['data']['tags']) def testDeploymentMetric(self): @@ -93,7 +95,8 @@ def testDeploymentMetric(self): 'somekey:someval', 'anotherkey:anotherval', 'justakey', - 'source:lighter'] + 'source:lighter', + 'type:change'] data = mock_jsonRequest.call_args_list[-2][1]['data']['series'][0] self.assertEquals('lighter.deployments', data['metric']) self.assertEquals(1, data['points'][0][1]) From 6347ae91c7cb712c2fee0fb1d38c13b6bb45e260 Mon Sep 17 00:00:00 2001 From: Mikael Johansson Date: Wed, 31 Aug 2016 16:31:03 +0200 Subject: [PATCH 2/2] Avoid aggegating Datadog deployment events accross envs --- src/lighter/datadog.py | 8 ++++---- src/lighter/main.py | 2 +- src/lighter/test/datadog_test.py | 16 +++++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/lighter/datadog.py b/src/lighter/datadog.py index dfbd207..b542667 100644 --- a/src/lighter/datadog.py +++ b/src/lighter/datadog.py @@ -9,9 +9,9 @@ def __init__(self, token, tags=[]): self._url = 'https://app.datadoghq.com' self._tags = tags + ['source:lighter', 'type:change'] - def notify(self, title, message, id, tags=[], priority='normal', alert_type='success'): - if not title or not message or not id: - logging.warn('Datadog title, message and id required') + def notify(self, title, message, aggregation_key, tags=[], priority='normal', alert_type='success'): + if not title or not message or not aggregation_key: + logging.warn('Datadog title, message and aggregation_key required') return merged_tags = list(tags) + self._tags @@ -28,7 +28,7 @@ def notify(self, title, message, id, tags=[], priority='normal', alert_type='suc self._call('/api/v1/events', { 'title': title, 'text': message, - 'aggregation_key': 'lighter_' + id, + 'aggregation_key': 'lighter_' + aggregation_key, 'tags': merged_tags, 'priority': priority, 'alert_type': alert_type, diff --git a/src/lighter/main.py b/src/lighter/main.py index 1e3c9fe..b2ca8e3 100755 --- a/src/lighter/main.py +++ b/src/lighter/main.py @@ -248,7 +248,7 @@ def deploy(marathonurl, filenames, noop=False, force=False, targetdir=None): util.rget(service.document, 'datadog', 'token'), util.toList(util.rget(service.document, 'datadog', 'tags'))) datadog.notify( - id=service.id, + aggregation_key="%s_%s" % (service.environment, service.id), title="Deployed %s to the %s environment" % (service.id, service.environment), message="%%%%%% \n Lighter deployed **%s** with image **%s** to **%s** (%s) \n %%%%%%" % ( service.id, service.image, service.environment, parsedMarathonUrl.netloc), diff --git a/src/lighter/test/datadog_test.py b/src/lighter/test/datadog_test.py index 412922b..f58d46a 100644 --- a/src/lighter/test/datadog_test.py +++ b/src/lighter/test/datadog_test.py @@ -10,7 +10,7 @@ def testNotify(self, mock_jsonRequest): Datadog('abc').notify( title='test title', message='test message', - id='/jenkins/test', + aggregation_key='/jenkins/test', tags=['environment:test'], priority='normal', alert_type='info') @@ -23,7 +23,7 @@ def testNoApiKey(self, mock_jsonRequest): Datadog('').notify( title='test title', message='test message', - id='/jenkins/test', + aggregation_key='/jenkins/test', tags=['environment:test'], priority='normal', alert_type='info') @@ -31,17 +31,23 @@ def testNoApiKey(self, mock_jsonRequest): @patch('lighter.util.jsonRequest') def testNoTitle(self, mock_jsonRequest): - Datadog('abc').notify(title='', message='test message', id='/jenkins/test', tags=['environment:test'], priority='normal', alert_type='info') + Datadog('abc').notify( + title='', + message='test message', + aggregation_key='/jenkins/test', + tags=['environment:test'], + priority='normal', + alert_type='info') self.assertEquals(mock_jsonRequest.call_count, 0) @patch('lighter.util.jsonRequest') def testNoMessage(self, mock_jsonRequest): - Datadog('abc').notify(title='test title', message='', id='/jenkins/test', tags=['environment:test'], priority='normal', alert_type='info') + Datadog('abc').notify(title='test title', message='', aggregation_key='/jenkins/test', tags=['environment:test'], priority='normal', alert_type='info') self.assertEquals(mock_jsonRequest.call_count, 0) @patch('lighter.util.jsonRequest') def testNoID(self, mock_jsonRequest): - Datadog('abc').notify(title='test title', message='test message', id='', tags=['environment:test'], priority='normal', alert_type='info') + Datadog('abc').notify(title='test title', message='test message', aggregation_key='', tags=['environment:test'], priority='normal', alert_type='info') self.assertEquals(mock_jsonRequest.call_count, 0) def _createJsonRequestWrapper(self, marathonurl='http://localhost:1'):