Skip to content

Instantly share code, notes, and snippets.

@r1979
Forked from twonds/README.md
Created April 13, 2017 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r1979/0471e1d7709bc699342033fc8c173852 to your computer and use it in GitHub Desktop.
Save r1979/0471e1d7709bc699342033fc8c173852 to your computer and use it in GitHub Desktop.
Datadog stream parser for json

dd-udpstream

Parse json events into data dog events

Testing


ubuntu@ip-10-251-55-110:~/dd_udpstream$ export PYTHONPATH=$PYTHONPATH:/opt/datadog-agent/agent/
ubuntu@ip-10-251-55-110:~/dd_udpstream$ python dd_udpstream_test.py
# Use this custom json log parser to send events to datadog
from datetime import datetime
import json
#from dogstream import common
DEFAULT_ALERT = 'normal'
def parse_json(log, line):
event = {}
## XXX - add an exception handler and report the error
event_type, msg = log.strip().split('\t')
json_event = json.loads(msg)
event.update(json_event)
event['event_type'] = event_type
if not event.has_key('palert_ype'):
event['alert_type'] = DEFAULT_ALERT
return event
import unittest
import dd_udpstream
class TestStream(unittest.TestCase):
""" Parsing tests """
def test_basic(self):
log = "category\t{\"wat\": \"this\"} "
res = dd_udpstream.parse_json(log, 1)
self.assertEqual('this', res.get('wat'))
self.assertEqual('category', res.get('event_type'))
self.assertEqual('normal', res.get('alert_type'))
if __name__ == '__main__':
unittest.main()
from udplog import udplog
logger = udplog.UDPLogger()
logger.log('test', {u'message': u'test'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment