Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
oryband committed Oct 28, 2015
1 parent 33a1a57 commit 2edea81
Showing 1 changed file with 22 additions and 36 deletions.
58 changes: 22 additions & 36 deletions newrelic_plugin_agent/plugins/couchbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,34 @@ def add_datapoints(self, data):
"""
# fetch metrics for each metric type (cluster, nodes, buckets)
for typ, stats in data.iteritems():
for metric in [m for m in self.METRICS if m['type'] == typ]:
if typ == 'cluster':
items = stats
name_key = 'name'
elif typ == 'nodes':
items = stats['nodes']
name_key = 'hostname'
elif typ == 'buckets':
items = stats
name_key = 'name'
# set items to fetch stats from,
# and set item key name, where the item's name will be fetched from
if typ == 'cluster':
items = stats
name_key = 'name'
elif typ == 'nodes':
items = stats['nodes']
name_key = 'hostname'
elif typ == 'buckets':
items = stats
name_key = 'name'

for metric in [m for m in self.METRICS if m['type'] == typ]:
# count score for scoreboard metrics,
# otherwise just add the gauge value from the API repsonse
#
# A "scoreboard" metric is a counter of a specific value for
# a field. E.g How many times the pair {"status": "healthy"}
# is found in a list of objects.
#
# NOTE that scoreboard metrics are meant to be used on lists
# of objects e.g. a list of nodes.
if metric.get('scoreboard', False):
self.add_gauge_value(
'%s/_scoreboard/%s' % (typ, metric['label']),
metric['suffix'],
self._get_metric_score(metric, items))
sum(d[metric['label']] == metric['score_value']
for d in items))
else:
# add gauge value for current metric.
# NOTE cluster metrics are not repeated,
Expand Down Expand Up @@ -115,29 +125,6 @@ def _add_gauge_value(self, metric, typ, name, items):
self.add_gauge_value('%s/%s/%s' % (typ, name, label),
metric['suffix'], value)

def _get_metric_score(self, metric, items):
"""Calculate metric score for scoreboard type metrics.
A "scoreboard" metric is a counter of a specific value for a field.
E.g How many times the pair {"status": "healthy"} is found in a
list of object.
NOTE that scoreboard metrics are meant to be used on lists of objects,
i.e. a list of node objects.
:param dict metric: metric to calculate score for.
:param items metric: stats to calculate score from.
:rtype: int
"""
score = 0
label = metric['label']
score_value = metric['score_value']
for d in items:
score += 1 if d[label] == score_value else 0
return score

def fetch_data(self):
"""Fetch data from multiple couchbase stats URLs.
Expand All @@ -152,7 +139,6 @@ def fetch_data(self):
('pools/nodes', 'nodes'),
('pools/default/buckets', 'buckets')]:
res = self.http_get(self.stats_url + path)
if res:
data[typ] = res.json() if res else {}
data[typ] = res and res.json() or {}

return data

0 comments on commit 2edea81

Please sign in to comment.