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 #42 from meltwater/datadog-custom-tags
Browse files Browse the repository at this point in the history
Improved Datadog event tagging support
  • Loading branch information
rasjoh authored Aug 31, 2016
2 parents 5d34054 + 9273b99 commit a4f2337
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ To send [Datadog deployment events](http://docs.datadoghq.com/guides/overview/#e
```
datadog:
token: '123abc'
tags:
- subsystem:example
```

For deployments of Docker containers, Lighter will add Marathon appid as a Docker container label in order for Datadog to tag services, [see: collect_labels_as_tags](https://github.com/DataDog/dd-agent/blob/master/conf.d/docker_daemon.yaml.example)
Expand Down
5 changes: 3 additions & 2 deletions src/lighter/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import lighter.util as util

class Datadog(object):
def __init__(self, token):
def __init__(self, token, tags=[]):
self._token = token
self._url = 'https://app.datadoghq.com'
self._tags = tags + ['source:lighter']

def notify(self, title, message, id, tags=[], priority='normal', alert_type='info'):
if not title or not message or not id:
Expand All @@ -17,7 +18,7 @@ def notify(self, title, message, id, tags=[], priority='normal', alert_type='inf
'title': title,
'text': message,
'aggregation_key': 'lighter_' + id,
'tags': tags,
'tags': list(tags) + self._tags,
'priority': priority,
'alert_type': alert_type
})
Expand Down
4 changes: 3 additions & 1 deletion src/lighter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ def deploy(marathonurl, filenames, noop=False, force=False, targetdir=None):
)

# Send Datadog deployment notification
datadog = Datadog(util.rget(service.document, 'datadog', 'token'))
datadog = Datadog(
util.rget(service.document, 'datadog', 'token'),
util.toList(util.rget(service.document, 'datadog', 'tags')))
datadog.notify(
id=service.id,
title="Deployed %s to the %s environment" % (service.id, service.environment),
Expand Down
40 changes: 40 additions & 0 deletions src/lighter/test/datadog_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest
from mock import patch, ANY
from lighter.datadog import Datadog
import lighter.main as lighter
from lighter.util import jsonRequest

class DatadogTest(unittest.TestCase):
@patch('lighter.util.jsonRequest')
Expand Down Expand Up @@ -40,3 +42,41 @@ def testNoMessage(self, mock_jsonRequest):
def testNoID(self, mock_jsonRequest):
Datadog('abc').notify(title='test title', message='test message', id='', tags=['environment:test'], priority='normal', alert_type='info')
self.assertEquals(mock_jsonRequest.call_count, 0)

def _createJsonRequestWrapper(self, marathonurl='http://localhost:1'):
appurl = '%s/v2/apps/myproduct/myservice' % marathonurl

def wrapper(url, method='GET', data=None, *args, **kwargs):
if url.startswith('file:'):
return jsonRequest(url, data, *args, **kwargs)
if url == appurl and method == 'PUT' and data:
return {}
if url == appurl and method == 'GET':
return {'app': {}}
return None
return wrapper

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

expected = [
'environment:default',
u'service:/myproduct/myservice',
'source:lighter']
self.assertEquals(expected, mock_jsonRequest.call_args[1]['data']['tags'])

def testConfiguredTags(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/events?api_key=abc', data=ANY, method='POST')

expected = [
'environment:default',
u'service:/myproduct/myservice',
'somekey:someval',
'anotherkey:anotherval',
'justakey',
'source:lighter']
self.assertEquals(expected, mock_jsonRequest.call_args[1]['data']['tags'])
19 changes: 19 additions & 0 deletions src/resources/yaml/integration/datadog-config-tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
maven:
groupid: 'com.meltwater'
artifactid: 'myservice'
version: '[1.0.0,1.1.0)'
override:
cpus: 1.0
env:
DATABASE: 'database:3306'
variables:
avar: '123'
bvar: '%{avar}'
cvar: '%{avar}'

datadog:
token: 'abc'
tags:
- somekey:someval
- anotherkey:anotherval
- justakey
15 changes: 15 additions & 0 deletions src/resources/yaml/integration/datadog-default-tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
maven:
groupid: 'com.meltwater'
artifactid: 'myservice'
version: '[1.0.0,1.1.0)'
override:
cpus: 1.0
env:
DATABASE: 'database:3306'
variables:
avar: '123'
bvar: '%{avar}'
cvar: '%{avar}'

datadog:
token: 'abc'

0 comments on commit a4f2337

Please sign in to comment.