-
Notifications
You must be signed in to change notification settings - Fork 594
Expand file tree
/
Copy pathhttpHandler.py
More file actions
executable file
·69 lines (51 loc) · 1.74 KB
/
httpHandler.py
File metadata and controls
executable file
·69 lines (51 loc) · 1.74 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# coding=utf-8
"""
Send metrics to a http endpoint via POST
#### Configuration
Enable this handler
* handlers = diamond.handler.httpHandler.HttpPostHandler
* url = http://www.example.com/endpoint
"""
from . Handler import Handler
from . pycompat import Request, urlopen
class HttpPostHandler(Handler):
# Inititalize Handler with url and batch size
def __init__(self, config=None):
Handler.__init__(self, config)
self.metrics = []
self.batch_size = int(self.config['batch'])
self.url = self.config.get('url')
def get_default_config_help(self):
"""
Returns the help text for the configuration options for this handler
"""
config = super(HttpPostHandler, self).get_default_config_help()
config.update({
'url': 'Fully qualified url to send metrics to',
'batch': 'How many to store before sending to the graphite server',
})
return config
def get_default_config(self):
"""
Return the default config for the handler
"""
config = super(HttpPostHandler, self).get_default_config()
config.update({
'url': 'http://localhost/blah/blah/blah',
'batch': 100,
})
return config
# Join batched metrics and push to url mentioned in config
def process(self, metric):
self.metrics.append(str(metric))
if len(self.metrics) >= self.batch_size:
self.post()
# Overriding flush to post metrics for every collector.
def flush(self):
"""Flush metrics in queue"""
self.post()
def post(self):
req = Request(self.url, "\n".join(self.metrics))
urlopen(req)
self.metrics = []