Skip to content

Commit ab59e12

Browse files
authored
Merge pull request #509 from Ensighten/nginx_collector_precision_fix
Allow precision to be set in nginx collector
2 parents f4a523f + 2298b04 commit ab59e12

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

docs/collectors/NginxCollector.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enabled | False | Enable collecting these metrics | bool
3838
measure_collector_time | False | Collect the collector run time in ms | bool
3939
metrics_blacklist | None | Regex to match metrics to block. Mutually exclusive with metrics_whitelist | NoneType
4040
metrics_whitelist | None | Regex to match metrics to transmit. Mutually exclusive with metrics_blacklist | NoneType
41+
precision | 0 | Number of decimal places to report to | int
4142
req_host | localhost | Hostname | str
4243
req_host_header | None | HTTP Host header (required for SSL) | NoneType
4344
req_path | /nginx_status | Path | str

src/collectors/nginx/nginx.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class NginxCollector(diamond.collector.Collector):
3737
def get_default_config_help(self):
3838
config_help = super(NginxCollector, self).get_default_config_help()
3939
config_help.update({
40+
'precision': 'Number of decimal places to report to',
4041
'req_host': 'Hostname',
4142
'req_port': 'Port',
4243
'req_path': 'Path',
@@ -47,6 +48,7 @@ def get_default_config_help(self):
4748

4849
def get_default_config(self):
4950
default_config = super(NginxCollector, self).get_default_config()
51+
default_config['precision'] = 0
5052
default_config['req_host'] = 'localhost'
5153
default_config['req_port'] = 8080
5254
default_config['req_path'] = '/nginx_status'
@@ -80,25 +82,41 @@ def collect(self):
8082
req = urllib2.Request(url=url, headers=headers)
8183
try:
8284
handle = urllib2.urlopen(req)
85+
precision = int(self.config['precision'])
8386
for l in handle.readlines():
8487
l = l.rstrip('\r\n')
8588
if activeConnectionsRE.match(l):
8689
self.publish_gauge(
8790
'active_connections',
88-
int(activeConnectionsRE.match(l).group('conn')))
91+
int(activeConnectionsRE.match(l).group('conn')),
92+
precision)
8993
elif totalConnectionsRE.match(l):
9094
m = totalConnectionsRE.match(l)
9195
req_per_conn = float(m.group('req')) / \
9296
float(m.group('acc'))
93-
self.publish_counter('conn_accepted', int(m.group('conn')))
94-
self.publish_counter('conn_handled', int(m.group('acc')))
95-
self.publish_counter('req_handled', int(m.group('req')))
96-
self.publish_gauge('req_per_conn', float(req_per_conn))
97+
self.publish_counter('conn_accepted',
98+
int(m.group('conn')),
99+
precision)
100+
self.publish_counter('conn_handled',
101+
int(m.group('acc')),
102+
precision)
103+
self.publish_counter('req_handled',
104+
int(m.group('req')),
105+
precision)
106+
self.publish_gauge('req_per_conn',
107+
float(req_per_conn),
108+
precision)
97109
elif connectionStatusRE.match(l):
98110
m = connectionStatusRE.match(l)
99-
self.publish_gauge('act_reads', int(m.group('reading')))
100-
self.publish_gauge('act_writes', int(m.group('writing')))
101-
self.publish_gauge('act_waits', int(m.group('waiting')))
111+
self.publish_gauge('act_reads',
112+
int(m.group('reading')),
113+
precision)
114+
self.publish_gauge('act_writes',
115+
int(m.group('writing')),
116+
precision)
117+
self.publish_gauge('act_waits',
118+
int(m.group('waiting')),
119+
precision)
102120
except IOError, e:
103121
self.log.error("Unable to open %s" % url)
104122
except Exception, e:

0 commit comments

Comments
 (0)