Exemplo n.º 1
0
class TestCassandraJolokiaCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)
        self.collector.list_request = list_request

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    @patch.object(Collector, 'flush')
    def test_should_create_dimension(self, publish_mock):
        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3828)

        metrics = find_metric(self.collector.payload, "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "type", "compaction_history")
        self.assertEquals(metric["type"], "GAUGE")

    @patch.object(Collector, 'flush')
    def test_should_create_type(self, publish_mock):
        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3828)

        metrics = find_metric(self.collector.payload, "org.apache.cassandra.metrics.ColumnFamily.CoordinatorReadLatency.count")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "keyspace", "OpsCenter")
        self.assertEquals(metric["type"], "CUMCOUNTER")
Exemplo n.º 2
0
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)
Exemplo n.º 3
0
class TestCassandraJolokiaCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)

    # Used for all the tests so the expected numbers are all the same.
    def fixture_values_a(self):
        values = [0] * 92
        values[30:56] = [
            3, 3, 1, 1, 8, 5, 6, 1, 6, 5, 3, 8, 9, 10, 7, 8, 7, 5, 5, 5, 3, 3,
            2, 2, 2
        ]
        return values

    def empty_fixture_values(self):
        return [0] * 91

    def expected_percentiles_for_fixture_a(self, percentile_key):
        return {
            'p25': 192.0,
            'p50': 398.0,
            'p75': 824.0,
            'p95': 2050.0,
            'p99': 2952.0
        }[percentile_key]

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    def test_should_compute_percentiles_accurately(self):
        ninety_offsets = self.collector.create_offsets(90)
        percentile_value = self.collector.compute_percentile(
            ninety_offsets, self.fixture_values_a(), 50)
        self.assertEqual(percentile_value, 398.0)

    def test_should_compute_percentiles_accurately_when_empty(self):
        ninety_offsets = self.collector.create_offsets(90)
        self.assertEqual(
            self.collector.compute_percentile(ninety_offsets,
                                              self.empty_fixture_values(), 50),
            0.0)
        self.assertEqual(
            self.collector.compute_percentile(ninety_offsets,
                                              self.empty_fixture_values(), 95),
            0.0)
        self.assertEqual(
            self.collector.compute_percentile(ninety_offsets,
                                              self.empty_fixture_values(), 99),
            0.0)

    @patch.object(Collector, 'publish')
    def test_should_not_collect_non_histogram_attributes(self, publish_mock):
        self.collector.interpret_bean_with_list('RecentReadLatencyMicros',
                                                self.fixture_values_a())
        self.assertPublishedMany(publish_mock, {})

    @patch.object(Collector, 'publish')
    def test_should_collect_metrics_histogram_attributes(self, publish_mock):
        self.collector.interpret_bean_with_list(
            'RecentReadLatencyHistogramMicros', self.fixture_values_a())
        self.assertPublishedMany(
            publish_mock, {
                'RecentReadLatencyHistogramMicros.p50':
                self.expected_percentiles_for_fixture_a('p50'),
                'RecentReadLatencyHistogramMicros.p95':
                self.expected_percentiles_for_fixture_a('p95'),
                'RecentReadLatencyHistogramMicros.p99':
                self.expected_percentiles_for_fixture_a('p99')
            })

    @patch.object(Collector, 'publish')
    def test_should_respect_percentiles_config(self, publish_mock):
        self.collector.update_config({'percentiles': '25,75'})
        self.collector.interpret_bean_with_list(
            'RecentReadLatencyHistogramMicros', self.fixture_values_a())
        self.assertPublishedMany(
            publish_mock, {
                'RecentReadLatencyHistogramMicros.p25':
                self.expected_percentiles_for_fixture_a('p25'),
                'RecentReadLatencyHistogramMicros.p75':
                self.expected_percentiles_for_fixture_a('p75'),
            })

    @patch.object(Collector, 'publish')
    def test_should_respect_histogram_regex_config(self, publish_mock):
        self.collector.update_config({'histogram_regex': '^WackyMetric'})
        self.collector.interpret_bean_with_list('WackyMetricSeventeen',
                                                self.fixture_values_a())
        self.assertPublishedMany(
            publish_mock, {
                'WackyMetricSeventeen.p50':
                self.expected_percentiles_for_fixture_a('p50'),
                'WackyMetricSeventeen.p95':
                self.expected_percentiles_for_fixture_a('p95'),
                'WackyMetricSeventeen.p99':
                self.expected_percentiles_for_fixture_a('p99')
            })
Exemplo n.º 4
0
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)
        self.collector.list_request = list_request
Exemplo n.º 5
0
class TestCassandraJolokiaCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    @patch.object(Collector, 'flush')
    def test_should_create_dimension(self, publish_mock):
        self.collector.list_request = list_request

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(self.collector.payload, "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "type", "compaction_history")
        self.assertEquals(metric["type"], "GAUGE")

        pending_task = find_metric(self.collector.payload,
                                   "org.apache.cassandra.metrics.CommitLog4.2.PendingTasks")
        self.assertNotEqual(len(pending_task), 0)

    def test_patch_host_list_should_filter_out_non_cassandra_hosts(self):
        data = {
            "services": {
                "service1": { "host": "10.0.0.1" },
                "cassandra_1.dc": { "host": "10.0.0.2"}
            }
        }
        with patch("__builtin__.open", mock_open(read_data=json.dumps(data))):
            hosts = self.collector.read_host_list()
        self.assertEqual(
            hosts,
            {
                'cassandra_1': { 'host': '10.0.0.2', 'port': self.collector.config['port'] }
            }
        )

    @patch.object(Collector, 'flush')
    def test_should_have_cassandra_cluster_dimension_in_multi_hosts_mode(self, publish_mock):
        self.collector.list_request = list_request
        self.collector.config['multiple_hosts_mode'] = True
        self.collector.read_host_list = read_host_list

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.collector.config['multiple_hosts_mode'] = False
        self.assertEquals(len(self.collector.payload), 3827 * 2)

        metrics = find_metric(self.collector.payload, "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "cassandra_cluster", "cass_1")
        self.assertEquals(metric["type"], "GAUGE")
        metric = find_by_dimension(metrics, "cassandra_cluster", "cass_2")
        self.assertEquals(metric["type"], "GAUGE")

    @patch.object(Collector, 'flush')
    def test_should_create_type(self, publish_mock):
        self.collector.list_request = list_request
        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(self.collector.payload, "org.apache.cassandra.metrics.ColumnFamily.CoordinatorReadLatency.count")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "keyspace", "OpsCenter")
        self.assertEquals(metric["type"], "CUMCOUNTER")

    @patch.object(Collector, 'flush')
    def test_mbean_blacklisting(self, publish_mock):
        def se(url):
            if url.find("org.apache.cassandra.metrics") > 0:
                return self.getFixture("metrics.json")
            elif url.find("list/org.apache.cassandra.db") > 0:
                return self.getFixture("cas_db.json")
            elif url.find("org.apache.cassandra.db:type=StorageService") > 0:
                return Exception('storage service should be blacklisted')
            elif url.find("list?ifModifiedSince") > 0:
                return self.getFixture("cas_list.json")
            else:
                return self.getFixture("storage_proc.json")
        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))
        self.collector.config['mbean_blacklist'] = [
            'org.apache.cassandra.db:type=StorageService'
        ]

        with patch_urlopen:
            self.collector.collect()
        metrics = find_metric(self.collector.payload, "org.apache.cassandra.db.StorageProxy.cascontentiontimeout")
        self.assertNotEqual(len(metrics), 0)
Exemplo n.º 6
0
class TestCassandraJolokiaCollector(CollectorTestCase):

    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)

    # Used for all the tests so the expected numbers are all the same.
    def fixture_a(self):
        values = [0] * 92
        values[30:56] = [3, 3, 1, 1, 8, 5, 6, 1, 6, 5, 3, 8, 9, 10, 7, 8, 7, 5,
                         5, 5, 3, 3, 2, 2, 2]
        return values

    def empty_fixture_values(self):
        return [0] * 91

    def expected_fixture_a_p(self, percentile_key):
        return {
            'p25': 192.0,
            'p50': 398.0,
            'p75': 824.0,
            'p95': 2050.0,
            'p99': 2952.0
        }[percentile_key]

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    def test_should_compute_percentiles_accurately(self):
        ninety_offsets = self.collector.create_offsets(90)
        percentile_value = self.collector.compute_percentile(
            ninety_offsets, self.fixture_a(), 50)
        self.assertEqual(percentile_value, 398.0)

    def test_should_compute_percentiles_accurately_when_empty(self):
        ninety_offsets = self.collector.create_offsets(90)
        self.assertEqual(self.collector.compute_percentile(
            ninety_offsets, self.empty_fixture_values(), 50), 0.0)
        self.assertEqual(self.collector.compute_percentile(
            ninety_offsets, self.empty_fixture_values(), 95), 0.0)
        self.assertEqual(self.collector.compute_percentile(
            ninety_offsets, self.empty_fixture_values(), 99), 0.0)

    @patch.object(Collector, 'publish')
    def test_should_not_collect_non_histogram_attributes(self, publish_mock):
        self.collector.interpret_bean_with_list(
            'RecentReadLatencyMicros', self.fixture_a())
        self.assertPublishedMany(publish_mock, {})

    @patch.object(Collector, 'publish')
    def test_should_collect_metrics_histogram_attributes(self, publish_mock):
        self.collector.interpret_bean_with_list(
            'RecentReadLatencyHistogramMicros', self.fixture_a())
        self.assertPublishedMany(publish_mock, {
            'RecentReadLatencyHistogramMicros.p50':
            self.expected_fixture_a_p('p50'),
            'RecentReadLatencyHistogramMicros.p95':
            self.expected_fixture_a_p('p95'),
            'RecentReadLatencyHistogramMicros.p99':
            self.expected_fixture_a_p('p99')
        })

    @patch.object(Collector, 'publish')
    # db:columnfamily=HintsColumnFamily,keyspace=system,type=ColumnFamilies:
    def test_should_escape_histogram_attributes(self, publish_mock):
        test_bean = ','.join([
            'db:columnfamily=HintsColumnFamily',
            'keyspace=system',
            'type=ColumnFamilies:RecentReadLatencyHistogramMicros'
        ])
        self.collector.interpret_bean_with_list(test_bean, self.fixture_a())

        expected_base = '.'.join([
            'db.columnfamily_HintsColumnFamily',
            'keyspace_system',
            'type_ColumnFamilies',
            'RecentReadLatencyHistogramMicros'
        ])
        self.assertPublishedMany(publish_mock, {
            '.'.join([expected_base, 'p50']): self.expected_fixture_a_p('p50'),
            '.'.join([expected_base, 'p95']): self.expected_fixture_a_p('p95'),
            '.'.join([expected_base, 'p99']): self.expected_fixture_a_p('p99')
        })

    @patch.object(Collector, 'publish')
    def test_should_respect_percentiles_config(self, publish_mock):
        self.collector.update_config({
            'percentiles': ['25', '75']
        })
        self.collector.interpret_bean_with_list(
            'RecentReadLatencyHistogramMicros', self.fixture_a())
        self.assertPublishedMany(publish_mock, {
            'RecentReadLatencyHistogramMicros.p25':
            self.expected_fixture_a_p('p25'),
            'RecentReadLatencyHistogramMicros.p75':
            self.expected_fixture_a_p('p75'),
        })

    @patch.object(Collector, 'publish')
    def test_should_respect_histogram_regex_config(self, publish_mock):
        self.collector.update_config({
            'histogram_regex': '^WackyMetric'
        })
        self.collector.interpret_bean_with_list(
            'WackyMetricSeventeen', self.fixture_a())
        self.assertPublishedMany(publish_mock, {
            'WackyMetricSeventeen.p50':
            self.expected_fixture_a_p('p50'),
            'WackyMetricSeventeen.p95':
            self.expected_fixture_a_p('p95'),
            'WackyMetricSeventeen.p99':
            self.expected_fixture_a_p('p99')
        })
class TestCassandraJolokiaCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    @patch.object(Collector, 'flush')
    def test_should_create_dimension(self, publish_mock):
        self.collector.list_request = list_request

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(self.collector.payload, "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "type", "compaction_history")
        self.assertEquals(metric["type"], "GAUGE")

        pending_task = find_metric(self.collector.payload,
                                   "org.apache.cassandra.metrics.CommitLog4.2.PendingTasks")
        self.assertNotEqual(len(pending_task), 0)

    @patch.object(Collector, 'flush')
    def test_should_create_type(self, publish_mock):
        self.collector.list_request = list_request
        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(self.collector.payload, "org.apache.cassandra.metrics.ColumnFamily.CoordinatorReadLatency.count")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "keyspace", "OpsCenter")
        self.assertEquals(metric["type"], "CUMCOUNTER")

    @patch.object(Collector, 'flush')
    def test_mbean_blacklisting(self, publish_mock):
        def se(url):
            if url.find("org.apache.cassandra.metrics") > 0:
                return self.getFixture("metrics.json")
            elif url.find("list/org.apache.cassandra.db") > 0:
                return self.getFixture("cas_db.json")
            elif url.find("org.apache.cassandra.db:type=StorageService") > 0:
                return Exception('storage service should be blacklisted')
            elif url.find("list?ifModifiedSince") > 0:
                return self.getFixture("cas_list.json")
            else:
                return self.getFixture("storage_proc.json")
        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))
        self.collector.config['mbean_blacklist'] = [
            'org.apache.cassandra.db:type=StorageService'
        ]

        with patch_urlopen:
            self.collector.collect()
        metrics = find_metric(self.collector.payload, "org.apache.cassandra.db.StorageProxy.cascontentiontimeout")
        self.assertNotEqual(len(metrics), 0)
Exemplo n.º 8
0
class TestCassandraJolokiaCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    @patch.object(Collector, 'flush')
    def test_should_create_dimension(self, publish_mock):
        self.collector.list_request = list_request

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "type", "compaction_history")
        self.assertEquals(metric["type"], "GAUGE")

        pending_task = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.CommitLog4.2.PendingTasks")
        self.assertNotEqual(len(pending_task), 0)

    @patch.object(Collector, 'flush')
    def test_should_create_type(self, publish_mock):
        self.collector.list_request = list_request

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.ColumnFamily.CoordinatorReadLatency.count"
        )
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "keyspace", "OpsCenter")
        self.assertEquals(metric["type"], "CUMCOUNTER")

    @patch.object(Collector, 'flush')
    def test_mbean_blacklisting(self, publish_mock):
        def se(url):
            if url.find("org.apache.cassandra.metrics") > 0:
                return self.getFixture("metrics.json")
            elif url.find("list/org.apache.cassandra.db") > 0:
                return self.getFixture("cas_db.json")
            elif url.find("org.apache.cassandra.db:type=StorageService") > 0:
                return Exception('storage service should be blacklisted')
            elif url.find("list?ifModifiedSince") > 0:
                return self.getFixture("cas_list.json")
            else:
                return self.getFixture("storage_proc.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))
        self.collector.config['mbean_blacklist'] = [
            'org.apache.cassandra.db:type=StorageService'
        ]

        with patch_urlopen:
            self.collector.collect()
        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.db.StorageProxy.cascontentiontimeout")
        self.assertNotEqual(len(metrics), 0)
Exemplo n.º 9
0
class TestCassandraJolokiaCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)

    # Used for all the tests so the expected numbers are all the same.
    def fixture_values_a(self):
        return [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,1,1,8,5,6,1,6,5,3,8,9,10,7,8,7,5,5,5,3,3,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    def empty_fixture_values(self):
        return [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    def expected_percentiles_for_fixture_a(self, percentile_key):
        return {
            'p25': 192.0,
            'p50': 398.0,
            'p75': 824.0,
            'p95': 2050.0,
            'p99': 2952.0
        }[percentile_key]

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    def test_should_compute_percentiles_accurately(self):
        ninety_offsets = self.collector.create_offsets(90)
        percentile_value = self.collector.compute_percentile(ninety_offsets, self.fixture_values_a(), 50)
        self.assertEqual(percentile_value, 398.0)

    def test_should_compute_percentiles_accurately_when_empty(self):
        ninety_offsets = self.collector.create_offsets(90)
        self.assertEqual(self.collector.compute_percentile(ninety_offsets, self.empty_fixture_values(), 50), 0.0)
        self.assertEqual(self.collector.compute_percentile(ninety_offsets, self.empty_fixture_values(), 95), 0.0)
        self.assertEqual(self.collector.compute_percentile(ninety_offsets, self.empty_fixture_values(), 99), 0.0)

    @patch.object(Collector, 'publish')
    def test_should_not_collect_non_histogram_attributes(self, publish_mock):
        self.collector.interpret_bean_with_list('RecentReadLatencyMicros', self.fixture_values_a())
        self.assertPublishedMany(publish_mock, {})

    @patch.object(Collector, 'publish')
    def test_should_collect_metrics_histogram_attributes(self, publish_mock):
        self.collector.interpret_bean_with_list('RecentReadLatencyHistogramMicros', self.fixture_values_a())
        self.assertPublishedMany(publish_mock, {
            'RecentReadLatencyHistogramMicros.p50': self.expected_percentiles_for_fixture_a('p50'),
            'RecentReadLatencyHistogramMicros.p95': self.expected_percentiles_for_fixture_a('p95'),
            'RecentReadLatencyHistogramMicros.p99': self.expected_percentiles_for_fixture_a('p99')
        })

    @patch.object(Collector, 'publish')
    def test_should_respect_percentiles_config(self, publish_mock):
        self.collector.update_config({
            'percentiles': '25,75'
        })
        self.collector.interpret_bean_with_list('RecentReadLatencyHistogramMicros', self.fixture_values_a())
        self.assertPublishedMany(publish_mock, {
            'RecentReadLatencyHistogramMicros.p25': self.expected_percentiles_for_fixture_a('p25'),
            'RecentReadLatencyHistogramMicros.p75': self.expected_percentiles_for_fixture_a('p75'),
        })

    @patch.object(Collector, 'publish')
    def test_should_respect_histogram_regex_config(self, publish_mock):
        self.collector.update_config({
            'histogram_regex': '^WackyMetric'
        })
        self.collector.interpret_bean_with_list('WackyMetricSeventeen', self.fixture_values_a())
        self.assertPublishedMany(publish_mock, {
            'WackyMetricSeventeen.p50': self.expected_percentiles_for_fixture_a('p50'),
            'WackyMetricSeventeen.p95': self.expected_percentiles_for_fixture_a('p95'),
            'WackyMetricSeventeen.p99': self.expected_percentiles_for_fixture_a('p99')
        })
Exemplo n.º 10
0
class TestCassandraJolokiaCollector(CollectorTestCase):
    def setUp(self):
        config = get_collector_config('CassandraJolokiaCollector', {})

        self.collector = CassandraJolokiaCollector(config, None)

    def test_import(self):
        self.assertTrue(CassandraJolokiaCollector)

    @patch.object(Collector, 'flush')
    def test_should_create_dimension(self, publish_mock):
        self.collector.list_request = list_request

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "type", "compaction_history")
        self.assertEquals(metric["type"], "GAUGE")

        pending_task = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.CommitLog4.2.PendingTasks")
        self.assertNotEqual(len(pending_task), 0)

    @patch.object(Collector, 'flush')
    def test_should_create_custom_dimension(self, flush_mock):
        dims = {'localhost': {'dim1': 'v1', 'dim2': 'v2'}}
        self.collector.list_request = list_request
        self.collector.dimension_reader = TestDimensionReader(dims)

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))
        patch_urlopen.start()
        self.collector.collect()
        patch_urlopen.stop()
        self.collector.dimension_readers = CompositeDimensionReader()

        expected_dims = dims['localhost']
        for payload in self.collector.payload:
            actual_dims = payload['dimensions']
            self.assertTrue(dimensions_contain(actual_dims, expected_dims))

    @patch.object(Collector, 'flush')
    def test_should_have_cassandra_instance_dimension_in_kubernetes_mode(
            self, publish_mock):
        self.collector.config.update({
            'mode': 'kubernetes',
            'spec': {
                'dimensions': {
                    'kubernetes': {
                        'cassandra_instance': {
                            'paasta.yelp.com/instance': '.*'
                        }
                    }
                },
                'label_selector': {
                    "yelp.com/paasta_service": "cassandra-operator"
                }
            }
        })
        self.collector.list_request = list_request
        self.collector.process_config()

        def se(url):
            return self.getFixture("metrics.json")

        def kubelet_se():
            return json.loads(self.getFixture('pods.json').getvalue()), None

        with patch('urllib2.urlopen', Mock(side_effect=se)):
            with patch("kubernetes.Kubelet.list_pods",
                       Mock(side_effect=kubelet_se)):
                self.collector.collect()

        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "cassandra_instance", "main")
        self.assertEquals(metric["type"], "GAUGE")
        metric = find_by_dimension(metrics, "cassandra_instance", "main")
        self.assertEquals(metric["type"], "GAUGE")

    @patch.object(Collector, 'flush')
    def test_should_have_cassandra_instance_dimension_in_nerve_mode(
            self, publish_mock):
        self.collector.config.update({
            'mode': 'nerve',
            'spec': {
                'dimensions': {
                    'nerve': {
                        'cassandra_cluster': '^cassandra_([\\w_-]+).',
                        'cassandra_instance':
                        '^cassandra_[\\w_-]+.([\\w_-]+).',
                    }
                },
                'host_id_regex': '^cassandra_[\\w_-]+'
            }
        })
        self.collector.list_request = list_request
        self.collector.process_config()

        def se(url):
            return self.getFixture("metrics.json")

        def nerve_se():
            return json.loads(self.getFixture('nerve.json').getvalue()), None

        with patch('urllib2.urlopen', Mock(side_effect=se)):
            with patch("nerve.read", Mock(side_effect=nerve_se)):
                self.collector.collect()

        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.ColumnFamily.LiveSSTableCount")
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "cassandra_cluster", "dev")
        self.assertEquals(metric["type"], "GAUGE")
        metric = find_by_dimension(metrics, "cassandra_instance", "main")
        self.assertEquals(metric["type"], "GAUGE")

    @patch.object(Collector, 'flush')
    def test_should_create_type(self, publish_mock):
        self.collector.list_request = list_request

        def se(url):
            return self.getFixture("metrics.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))

        with patch_urlopen:
            self.collector.collect()
        self.assertEquals(len(self.collector.payload), 3827)

        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.metrics.ColumnFamily.CoordinatorReadLatency.count"
        )
        self.assertNotEqual(len(metrics), 0)
        metric = find_by_dimension(metrics, "keyspace", "OpsCenter")
        self.assertEquals(metric["type"], "CUMCOUNTER")

    @patch.object(Collector, 'flush')
    def test_mbean_blacklisting(self, publish_mock):
        def se(url):
            if url.find("org.apache.cassandra.metrics") > 0:
                return self.getFixture("metrics.json")
            elif url.find("list/org.apache.cassandra.db") > 0:
                return self.getFixture("cas_db.json")
            elif url.find("org.apache.cassandra.db:type=StorageService") > 0:
                return Exception('storage service should be blacklisted')
            elif url.find("list?ifModifiedSince") > 0:
                return self.getFixture("cas_list.json")
            else:
                return self.getFixture("storage_proc.json")

        patch_urlopen = patch('urllib2.urlopen', Mock(side_effect=se))
        self.collector.config['mbean_blacklist'] = [
            'org.apache.cassandra.db:type=StorageService'
        ]

        with patch_urlopen:
            self.collector.collect()
        metrics = find_metric(
            self.collector.payload,
            "org.apache.cassandra.db.StorageProxy.cascontentiontimeout")
        self.assertNotEqual(len(metrics), 0)