Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #44 from meltwater/datadog-event-type
Browse files Browse the repository at this point in the history
Added standard Datadog tag type:change
  • Loading branch information
rasjoh authored Sep 1, 2016
2 parents 6e1e45f + 6347ae9 commit bdcb568
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/lighter/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ 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:
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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/lighter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
25 changes: 17 additions & 8 deletions src/lighter/test/datadog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -23,25 +23,31 @@ 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')
self.assertEquals(mock_jsonRequest.call_count, 0)

@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'):
Expand All @@ -65,7 +71,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):
Expand All @@ -79,7 +86,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):
Expand All @@ -93,7 +101,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])
Expand Down

0 comments on commit bdcb568

Please sign in to comment.