Skip to content

Commit 118a560

Browse files
Zero rates when queue is idle
1 parent 277931a commit 118a560

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

rabbit/conf.d/rabbitmq.pyconf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ modules {
2828
param metric_group {
2929
value = "rmq"
3030
}
31+
32+
param zero_rates_when_idle {
33+
value = "True"
34+
}
3135

3236
}
3337
}

rabbit/python_modules/rabbitmq.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
keyToPath['rmq_consumers'] = "%s{0}consumers".format(JSON_PATH_SEPARATOR)
4242
keyToPath['rmq_messages'] = "%s{0}messages".format(JSON_PATH_SEPARATOR)
4343

44+
RATE_METRICS = [
45+
'rmq_backing_queue_ack_egress_rate',
46+
'rmq_backing_queue_ack_ingress_rate',
47+
'rmq_backing_queue_egress_rate',
48+
'rmq_backing_queue_ingress_rate'
49+
]
50+
4451
QUEUE_METRICS = ['rmq_messages_ready',
4552
'rmq_messages_unacknowledged',
4653
'rmq_backing_queue_ack_egress_rate',
@@ -77,8 +84,6 @@ def metric_cleanup():
7784
def dig_it_up(obj,path):
7885
try:
7986
path = path.split(JSON_PATH_SEPARATOR)
80-
print "obj is", obj
81-
print "path is", path
8287
return reduce(lambda x,y:x[y],path,obj)
8388
except:
8489
print "Exception"
@@ -141,18 +146,14 @@ def getQueueStat(name):
141146
print name
142147
stat_name, queue_name, vhost = name.split(METRIC_TOKEN_SEPARATOR)
143148

144-
print "vhost is ", vhost
145-
print "stat_name is ", stat_name
146-
print "queue_name is ", queue_name
147149
vhost = vhost.replace('-', '/') #decoding vhost from metric name
148150
# Run refreshStats to get the result object
149151
result = compiled_results[('queues', vhost)]
150152

151-
print "keyToPath[stat_name]", keyToPath[stat_name]
152-
tmp_res = keyToPath[stat_name] % queue_name
153-
print "tmp_res is ", tmp_res
154153
value = dig_it_up(result, keyToPath[stat_name] % queue_name)
155-
print name, value
154+
155+
if zero_rates_when_idle and stat_name in RATE_METRICS and 'idle_since' in result[queue_name].keys():
156+
value = 0
156157

157158
#Convert Booleans
158159
if value is True:
@@ -189,20 +190,31 @@ def product(*args, **kwds):
189190
result = [x+[y] for x in result for y in pool]
190191
for prod in result:
191192
yield tuple(prod)
193+
194+
def str2bool(string):
195+
if string.lower() in ("yes", "true"):
196+
return True
197+
if string.lower() in ("no", "false"):
198+
return False
199+
raise Exception("Invalid value of the 'zero_rates_when_idle' param, use one of the ('true', 'yes', 'false', 'no')")
192200

193201
def metric_init(params):
194202
''' Create the metric definition object '''
195-
global descriptors, stats, vhost, username, password, urlstring, url_template, compiled_results, STATS, vhosts
203+
global descriptors, stats, vhost, username, password, urlstring, url_template, compiled_results, STATS, vhosts, zero_rates_when_idle
196204
print 'received the following params:'
197205
#Set this globally so we can refresh stats
198206
if 'host' not in params:
199207
params['host'], params['vhost'],params['username'],params['password'],params['port'] = "localhost", "/", "guest", "guest", "15672"
208+
if 'zero_rates_when_idle' not in params:
209+
params['zero_rates_when_idle'] = "false"
200210

201211
# Set the vhosts as a list split from params
202212
vhosts = params['vhost'].split(',')
203213
username, password = params['username'], params['password']
204214
host = params['host']
205215
port = params['port']
216+
217+
zero_rates_when_idle = str2bool(params['zero_rates_when_idle'])
206218

207219
url = 'http://%s:%s/api/$stats/$vhost' % (host,port)
208220
base_url = 'http://%s:%s/api' % (host,port)
@@ -290,10 +302,10 @@ def metric_cleanup():
290302
url_template = Template(url)
291303
print "url_template is ", url_template
292304
### in config files we use '/' in vhosts names but we should convert '/' to '-' when calculating a metric
293-
parameters = {"vhost":"/", "username":"guest","password":"guest", "metric_group":"rabbitmq"}
305+
parameters = {"vhost":"/", "username":"guest","password":"guest", "metric_group":"rabbitmq", "zero_rates_when_idle": "yes"}
294306
metric_init(parameters)
295307
result = refreshStats(stats = ('queues', 'nodes'), vhosts = ('/'))
296308
print '***'*20
297-
getQueueStat('rmq_messages_ready___hello___-')
298-
# getNodeStat('rmq_disk_free.rmqone@inrmq01d1#/')
299-
# getNodeStat('rmq_mem_used.rmqone@inrmq01d1#/')
309+
getQueueStat('rmq_backing_queue_ack_egress_rate___nfl_client___-')
310+
getNodeStat('rmq_disk_free___rmqone@inrmq01d1___-')
311+
getNodeStat('rmq_mem_used___rmqone@inrmq01d1___-')

0 commit comments

Comments
 (0)