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 #43 from meltwater/datadog-deployment-metric
Browse files Browse the repository at this point in the history
Added Datadog deployment metric
  • Loading branch information
rasjoh authored Aug 31, 2016
2 parents a4f2337 + c3ca4b3 commit 1e49ed6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/lighter/datadog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import urllib2
import time
import lighter.util as util

class Datadog(object):
Expand All @@ -13,14 +14,25 @@ def notify(self, title, message, id, tags=[], priority='normal', alert_type='inf
logging.warn('Datadog title, message and id required')
return

merged_tags = list(tags) + self._tags
now = int(time.time())

logging.debug("Sending Datadog deployment metric: %s", message)
self._call('/api/v1/series', {'series': [{
'metric': 'lighter.deployments',
'points': [[now, 1]],
'tags': merged_tags
}]})

logging.debug("Sending Datadog event: %s", message)
self._call('/api/v1/events', {
'title': title,
'text': message,
'aggregation_key': 'lighter_' + id,
'tags': list(tags) + self._tags,
'tags': merged_tags,
'priority': priority,
'alert_type': alert_type
'alert_type': alert_type,
'date_happened': now
})

def _call(self, endpoint, data):
Expand Down
20 changes: 19 additions & 1 deletion src/lighter/test/datadog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def testNotify(self, mock_jsonRequest):
tags=['environment:test'],
priority='normal',
alert_type='info')
self.assertEquals(mock_jsonRequest.call_count, 1)
self.assertEquals(mock_jsonRequest.call_count, 2)
mock_jsonRequest.assert_any_call('https://app.datadoghq.com/api/v1/events?api_key=abc', data=ANY, method='POST')
mock_jsonRequest.assert_any_call('https://app.datadoghq.com/api/v1/series?api_key=abc', data=ANY, method='POST')

@patch('lighter.util.jsonRequest')
def testNoApiKey(self, mock_jsonRequest):
Expand Down Expand Up @@ -80,3 +81,20 @@ def testConfiguredTags(self):
'justakey',
'source:lighter']
self.assertEquals(expected, mock_jsonRequest.call_args[1]['data']['tags'])

def testDeploymentMetric(self):
with patch('lighter.util.jsonRequest', wraps=self._createJsonRequestWrapper()) as mock_jsonRequest:
lighter.deploy('http://localhost:1/', filenames=['src/resources/yaml/integration/datadog-config-tags.yml'])
mock_jsonRequest.assert_any_call('https://app.datadoghq.com/api/v1/series?api_key=abc', data=ANY, method='POST')

tags = [
'environment:default',
u'service:/myproduct/myservice',
'somekey:someval',
'anotherkey:anotherval',
'justakey',
'source:lighter']
data = mock_jsonRequest.call_args_list[-2][1]['data']['series'][0]
self.assertEquals('lighter.deployments', data['metric'])
self.assertEquals(1, data['points'][0][1])
self.assertEquals(tags, data['tags'])
4 changes: 3 additions & 1 deletion src/lighter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,10 @@ def jsonRequest(url, data=None, headers={}, method='GET', contentType='applicati
logging.debug('%sing url %s', method, url)
response = openRequest(buildRequest(url, data, headers, method, contentType), timeout=timeout)
content = response.read()
logging.debug('Got response HTTP ' + str(response.getcode()) + ': ' + str(content)[0:250])

contenttype = response.info().gettype()
if contenttype == 'application/json' or contenttype == 'text/plain' or 'docker.distribution.manifest' in contenttype:
if contenttype == 'application/json' or contenttype == 'text/json' or contenttype == 'text/plain' or 'docker.distribution.manifest' in contenttype:
return json.loads(content)

logging.debug('Content-Type %s is not json %s', response.info().gettype(), content)
Expand Down

0 comments on commit 1e49ed6

Please sign in to comment.