forked from DataDog/dd-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.py
52 lines (42 loc) · 1.66 KB
/
emitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import urllib, urllib2
import httplib
import zlib
from pprint import pformat as pp
from util import json, md5
def format_body(message):
payload = json.dumps(message)
return zlib.compress(payload)
def post_headers(agentConfig, payload):
return {
'User-Agent': 'Datadog Agent/%s' % agentConfig['version'],
'Content-Type': 'application/json',
'Content-Encoding': 'deflate',
'Accept': 'text/html, */*',
'Content-MD5': md5(payload).hexdigest()
}
def http_emitter(message, logger, agentConfig):
try:
logger.debug('http_emitter: start')
# Post back the data
postBackData = format_body(message)
logger.debug('http_emitter: attempting postback to ' + agentConfig['dd_url'])
# Build the request handler
apiKey = message.get('apiKey', None)
if apiKey:
url = "%s/intake?api_key=%s" % (agentConfig['dd_url'], apiKey)
headers = post_headers(agentConfig, postBackData)
request = urllib2.Request(url, postBackData, headers)
# Do the request, log any errors
response = urllib2.urlopen(request)
logger.debug('http_emitter: postback response: ' + str(response.read()))
else:
logger.error("No api key, not sending payload")
except urllib2.HTTPError, e:
if e.code != 202:
logger.exception('http_emitter: HTTPError = ' + str(e))
except urllib2.URLError, e:
logger.exception('http_emitter: URLError = ' + str(e))
except httplib.HTTPException, e:
logger.exception('http_emitter: HTTPException = ' + str(e))
except:
logger.exception('http_emitter')