From 8390894e0b5f546caceecf7ffdd96c04d6739ffa Mon Sep 17 00:00:00 2001 From: Alan Boudreault Date: Fri, 22 Jul 2016 19:23:34 -0400 Subject: [PATCH 1/2] Changed per-cluster metrics path and added ability to define an explicit stats name --- cassandra/metrics.py | 24 +++++++++++++++++----- tests/integration/standard/test_metrics.py | 11 +++++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cassandra/metrics.py b/cassandra/metrics.py index 61502717..d56d7859 100644 --- a/cassandra/metrics.py +++ b/cassandra/metrics.py @@ -111,12 +111,14 @@ class Metrics(object): the driver currently has open. """ + _stats_counter = 0 + def __init__(self, cluster_proxy): log.debug("Starting metric capture") - # TODO, modify the path to /cassandra/{clusterid} in 4.0 - self.stats_id = str(id(cluster_proxy)) - self.stats = scales.collection('/_cassandra/{0}'.format(self.stats_id), + self.stats_name = 'cassandra-{0}'.format(str(self._stats_counter)) + Metrics._stats_counter += 1 + self.stats = scales.collection(self.stats_name, scales.PmfStat('request_timer'), scales.IntStat('connection_errors'), scales.IntStat('write_timeouts'), @@ -137,7 +139,7 @@ class Metrics(object): # TODO, to be removed in 4.0 # /cassandra contains the metrics of the first cluster registered if 'cassandra' not in scales._Stats.stats: - scales._Stats.stats['cassandra'] = scales._Stats.stats['_cassandra'][self.stats_id] + scales._Stats.stats['cassandra'] = scales._Stats.stats[self.stats_name] self.request_timer = self.stats.request_timer self.connection_errors = self.stats.connection_errors @@ -176,4 +178,16 @@ class Metrics(object): """ Returns the metrics for the registered cluster instance. """ - return scales.getStats()['_cassandra'][self.stats_id] + return scales.getStats()[self.stats_name] + + def set_stats_name(self, stats_name): + """ + Set the metrics stats name. + The stats_name is a string used to access the metris through scales: scales.getStats()[] + Default is 'cassandra-'. + """ + + stats = scales._Stats.stats[self.stats_name] + del scales._Stats.stats[self.stats_name] + self.stats_name = stats_name + scales._Stats.stats[self.stats_name] = stats diff --git a/tests/integration/standard/test_metrics.py b/tests/integration/standard/test_metrics.py index d33bb0e2..595bb285 100644 --- a/tests/integration/standard/test_metrics.py +++ b/tests/integration/standard/test_metrics.py @@ -26,7 +26,7 @@ from cassandra import ConsistencyLevel, WriteTimeout, Unavailable, ReadTimeout from cassandra.cluster import Cluster, NoHostAvailable from tests.integration import get_cluster, get_node, use_singledc, PROTOCOL_VERSION, execute_until_pass - +from greplin import scales def setup_module(): use_singledc() @@ -197,14 +197,23 @@ class MetricsTests(unittest.TestCase): finally: get_node(1).resume() + # Change the scales stats_name of the cluster2 + cluster2.metrics.set_stats_name('cluster2-metrics') + stats_cluster1 = self.cluster.metrics.get_stats() stats_cluster2 = cluster2.metrics.get_stats() + # Test direct access to stats self.assertEqual(1, self.cluster.metrics.stats.write_timeouts) self.assertEqual(0, cluster2.metrics.stats.write_timeouts) + # Test direct access to a child stats self.assertNotEqual(0.0, self.cluster.metrics.request_timer['mean']) self.assertEqual(0.0, cluster2.metrics.request_timer['mean']) + # Test access via metrics.get_stats() self.assertNotEqual(0.0, stats_cluster1['request_timer']['mean']) self.assertEqual(0.0, stats_cluster2['request_timer']['mean']) + + # Test access by stats_name + self.assertEqual(0.0, scales.getStats()['cluster2-metrics']['request_timer']['mean']) From c1dd8a3dea8640efe76f500efbf7e1b3071fbf04 Mon Sep 17 00:00:00 2001 From: Alan Boudreault Date: Mon, 25 Jul 2016 11:07:13 -0400 Subject: [PATCH 2/2] add simple validation in set_stats_name --- cassandra/metrics.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cassandra/metrics.py b/cassandra/metrics.py index d56d7859..644e4485 100644 --- a/cassandra/metrics.py +++ b/cassandra/metrics.py @@ -187,6 +187,9 @@ class Metrics(object): Default is 'cassandra-'. """ + if stats_name in scales._Stats.stats: + raise ValueError('"{0}" already exists in stats.'.format(stats_name)) + stats = scales._Stats.stats[self.stats_name] del scales._Stats.stats[self.stats_name] self.stats_name = stats_name