Beispiel #1
0
    def testMetricsGroup(self):
        now = round(time.time(), METRICS_TIMESTAMP_PRECISION)
        metrics_group = PanoptesMetricsGroup(self.__panoptes_resource, 'test', 120)
        self.assertEqual(metrics_group.group_type, 'test')
        self.assertEqual(metrics_group.interval, 120)
        self.assertEqual(metrics_group.schema_version, '0.2')
        self.assertGreaterEqual(metrics_group.creation_timestamp, now)

        dimension_one = PanoptesMetricDimension('if_alias', 'bar')
        dimension_two = PanoptesMetricDimension('if_alias', 'foo')

        metrics_group.add_dimension(dimension_one)

        with self.assertRaises(KeyError):
            metrics_group.add_dimension(dimension_two)

        self.assertEqual(len(metrics_group.dimensions), 1)
        self.assertEqual(metrics_group.contains_dimension_by_name('if_alias'), True)
        self.assertEqual(metrics_group.contains_dimension_by_name('baz'), False)
        metrics_group.delete_dimension_by_name('if_alias')
        self.assertEqual(metrics_group.contains_dimension_by_name('if_alias'), False)
        self.assertEqual(len(metrics_group.dimensions), 0)
        self.assertEqual(metrics_group.get_dimension_by_name('foo'), None)

        metrics_group.add_dimension(dimension_two)
        dimension_three = PanoptesMetricDimension('if_alias', 'bar')
        metrics_group.upsert_dimension(dimension_three)
        self.assertEqual(len(metrics_group.dimensions), 1)
        self.assertEqual(metrics_group.get_dimension_by_name('if_alias').value, 'bar')
        dimension_four = PanoptesMetricDimension('if_name', 'eth0')
        metrics_group.upsert_dimension(dimension_four)
        self.assertEqual(len(metrics_group.dimensions), 2)

        with self.assertRaises(AssertionError):
            metrics_group.add_metric(None)

        metric = PanoptesMetric('test_metric', 0, PanoptesMetricType.GAUGE)
        metrics_group.add_metric(metric)
        to_json = metrics_group.json
        metrics = PanoptesMetricsGroup.flatten_metrics(json.loads(to_json)['metrics'])
        self.assertEquals(metrics['gauge']['test_metric']['value'], 0)

        metrics_group_two = PanoptesMetricsGroup(self.__panoptes_resource, 'test', 120)
        metrics_group_two.add_dimension(dimension_two)
        metrics_group_two.upsert_dimension(dimension_three)
        metrics_group_two.upsert_dimension(dimension_four)
        metrics_group_two.add_metric(metric)

        self.assertEqual(metrics_group, metrics_group_two)
    def testMetricsGroup(self):
        now = round(time.time(), METRICS_TIMESTAMP_PRECISION)
        metrics_group = PanoptesMetricsGroup(self.__panoptes_resource, u'test',
                                             120)
        self.assertEqual(metrics_group.group_type, u'test')
        self.assertEqual(metrics_group.interval, 120)
        self.assertEqual(metrics_group.schema_version, u'0.2')
        self.assertGreaterEqual(metrics_group.creation_timestamp, now)

        with patch(u'yahoo_panoptes.framework.metrics.time', mock_time):
            metrics_group = PanoptesMetricsGroup(self.__panoptes_resource,
                                                 u'test', 120)

            dimension_one = PanoptesMetricDimension(u'if_alias', u'bar')
            dimension_two = PanoptesMetricDimension(u'if_alias', u'foo')

            metrics_group.add_dimension(dimension_one)

            with self.assertRaises(KeyError):
                metrics_group.add_dimension(dimension_two)

            #  Test basic dimension operations
            self.assertEqual(len(metrics_group.dimensions), 1)
            self.assertTrue(
                metrics_group.contains_dimension_by_name(u'if_alias'))
            self.assertFalse(metrics_group.contains_dimension_by_name(u'baz'))
            self.assertEqual(
                metrics_group.get_dimension_by_name(u'if_alias').value, u'bar')
            metrics_group.delete_dimension_by_name(u'if_alias')
            self.assertFalse(
                metrics_group.contains_dimension_by_name(u'if_alias'))
            self.assertEqual(len(metrics_group.dimensions), 0)
            self.assertEqual(metrics_group.get_dimension_by_name(u'foo'), None)

            metrics_group.add_dimension(dimension_two)
            dimension_three = PanoptesMetricDimension(u'if_alias', u'test')
            metrics_group.upsert_dimension(dimension_three)
            self.assertEqual(len(metrics_group.dimensions), 1)
            self.assertEqual(
                metrics_group.get_dimension_by_name(u'if_alias').value,
                u'test')
            dimension_four = PanoptesMetricDimension(u'if_name', u'eth0')
            metrics_group.upsert_dimension(dimension_four)
            self.assertEqual(len(metrics_group.dimensions), 2)

            #  Test basic metric operations
            with self.assertRaises(AssertionError):
                metrics_group.add_metric(None)

            metric = PanoptesMetric(u'test_metric', 0,
                                    PanoptesMetricType.GAUGE)
            metrics_group.add_metric(metric)
            with self.assertRaises(KeyError):
                metrics_group.add_metric(metric)

            to_json = metrics_group.json
            metrics = PanoptesMetricsGroup.flatten_metrics(
                json.loads(to_json)[u'metrics'])
            self.assertEquals(metrics[u'gauge'][u'test_metric'][u'value'], 0)

            metrics_group_two = PanoptesMetricsGroup(self.__panoptes_resource,
                                                     u'test', 120)
            metrics_group_two.add_dimension(dimension_two)
            metrics_group_two.upsert_dimension(dimension_three)
            metrics_group_two.upsert_dimension(dimension_four)
            metrics_group_two.add_metric(metric)

            self.assertEqual(metrics_group, metrics_group_two)

            # Check PanoptesMetricsGroup.__eq__
            panoptes_resource_two = PanoptesResource(
                resource_site=u'test2',
                resource_class=u'test2',
                resource_subclass=u'test2',
                resource_type=u'test2',
                resource_id=u'test2',
                resource_endpoint=u'test2',
                resource_plugin=u'test2')

            metrics_group_two = PanoptesMetricsGroup(panoptes_resource_two,
                                                     u'test', 120)
            metrics_group_three = PanoptesMetricsGroup(
                self.__panoptes_resource, u'test', 120)
            with self.assertRaises(AssertionError):
                assert metrics_group_two == metrics_group_three

            metrics_group_three = metrics_group.copy()

            with self.assertRaises(AssertionError):
                assert metrics_group == dimension_one
            assert metrics_group == metrics_group_three

            metrics_group_three.delete_dimension_by_name(u"if_name")
            with self.assertRaises(AssertionError):
                assert metrics_group == metrics_group_three
            metrics_group_three.upsert_dimension(dimension_four)
            assert metrics_group == metrics_group_three

            metric_two = PanoptesMetric(u'test_metric_2', 1,
                                        PanoptesMetricType.GAUGE)
            metrics_group_three.add_metric(metric_two)
            with self.assertRaises(AssertionError):
                assert metrics_group == metrics_group_three

            #  Test PanoptesMetricsGroup.__repr__
            _METRICS_GROUP_REPR = u'PanoptesMetricsGroup[' \
                                  u'resource:plugin|test|site|test|class|test|subclass|test|type|test|id|' \
                                  u'test|endpoint|test,' \
                                  u'interval:120,schema_version:0.2,group_type:test,creation_timestamp:{},' \
                                  u'dimensions:[PanoptesMetricDimension[if_alias|test],' \
                                  u'PanoptesMetricDimension[if_name|eth0]],' \
                                  u'metrics:[PanoptesMetric[test_metric|0|GAUGE|{}]]]'.format(mock_time.return_value,
                                                                                              mock_time.return_value)

            self.assertEqual(repr(metrics_group), _METRICS_GROUP_REPR)

            dimensions_as_dicts = [{
                u'dimension_name': dimension.name,
                u'dimension_value': dimension.value
            } for dimension in metrics_group.dimensions]
            self.assertEqual(
                PanoptesMetricsGroup.flatten_dimensions(dimensions_as_dicts), {
                    u'if_alias': u'test',
                    u'if_name': u'eth0'
                })
    def testMetricsGroup(self):
        now = round(time.time(), METRICS_TIMESTAMP_PRECISION)
        metrics_group = PanoptesMetricsGroup(self.__panoptes_resource, 'test',
                                             120)
        self.assertEqual(metrics_group.group_type, 'test')
        self.assertEqual(metrics_group.interval, 120)
        self.assertEqual(metrics_group.schema_version, '0.2')
        self.assertGreaterEqual(metrics_group.creation_timestamp, now)

        with patch('yahoo_panoptes.framework.metrics.time', mock_time):
            metrics_group = PanoptesMetricsGroup(self.__panoptes_resource,
                                                 'test', 120)

            dimension_one = PanoptesMetricDimension('if_alias', 'bar')
            dimension_two = PanoptesMetricDimension('if_alias', 'foo')

            metrics_group.add_dimension(dimension_one)

            with self.assertRaises(KeyError):
                metrics_group.add_dimension(dimension_two)

            #  Test basic dimension operations
            self.assertEqual(len(metrics_group.dimensions), 1)
            self.assertTrue(
                metrics_group.contains_dimension_by_name('if_alias'))
            self.assertFalse(metrics_group.contains_dimension_by_name('baz'))
            self.assertEqual(
                metrics_group.get_dimension_by_name('if_alias').value, 'bar')
            metrics_group.delete_dimension_by_name('if_alias')
            self.assertFalse(
                metrics_group.contains_dimension_by_name('if_alias'))
            self.assertEqual(len(metrics_group.dimensions), 0)
            self.assertEqual(metrics_group.get_dimension_by_name('foo'), None)

            metrics_group.add_dimension(dimension_two)
            dimension_three = PanoptesMetricDimension('if_alias', 'test')
            metrics_group.upsert_dimension(dimension_three)
            self.assertEqual(len(metrics_group.dimensions), 1)
            self.assertEqual(
                metrics_group.get_dimension_by_name('if_alias').value, 'test')
            dimension_four = PanoptesMetricDimension('if_name', 'eth0')
            metrics_group.upsert_dimension(dimension_four)
            self.assertEqual(len(metrics_group.dimensions), 2)

            #  Test basic metric operations
            with self.assertRaises(AssertionError):
                metrics_group.add_metric(None)

            metric = PanoptesMetric('test_metric', 0, PanoptesMetricType.GAUGE)
            metrics_group.add_metric(metric)
            with self.assertRaises(KeyError):
                metrics_group.add_metric(metric)

            to_json = metrics_group.json
            metrics = PanoptesMetricsGroup.flatten_metrics(
                json.loads(to_json)['metrics'])
            self.assertEquals(metrics['gauge']['test_metric']['value'], 0)

            metrics_group_two = PanoptesMetricsGroup(self.__panoptes_resource,
                                                     'test', 120)
            metrics_group_two.add_dimension(dimension_two)
            metrics_group_two.upsert_dimension(dimension_three)
            metrics_group_two.upsert_dimension(dimension_four)
            metrics_group_two.add_metric(metric)

            self.assertEqual(metrics_group, metrics_group_two)

            # Check PanoptesMetricsGroup.__eq__
            panoptes_resource_two = PanoptesResource(resource_site='test2',
                                                     resource_class='test2',
                                                     resource_subclass='test2',
                                                     resource_type='test2',
                                                     resource_id='test2',
                                                     resource_endpoint='test2',
                                                     resource_plugin='test2')

            metrics_group_two = PanoptesMetricsGroup(panoptes_resource_two,
                                                     'test', 120)
            metrics_group_three = PanoptesMetricsGroup(
                self.__panoptes_resource, 'test', 120)
            with self.assertRaises(AssertionError):
                assert metrics_group_two == metrics_group_three

            metrics_group_three = metrics_group.copy()

            with self.assertRaises(AssertionError):
                assert metrics_group == dimension_one
            assert metrics_group == metrics_group_three

            metrics_group_three.delete_dimension_by_name("if_name")
            with self.assertRaises(AssertionError):
                assert metrics_group == metrics_group_three
            metrics_group_three.upsert_dimension(dimension_four)
            assert metrics_group == metrics_group_three

            metric_two = PanoptesMetric('test_metric_2', 1,
                                        PanoptesMetricType.GAUGE)
            metrics_group_three.add_metric(metric_two)
            with self.assertRaises(AssertionError):
                assert metrics_group == metrics_group_three

            #  Test PanoptesMetricsGroup.__repr__
            _METRICS_GROUP_REPR = "{{'metrics_group_interval': 120, " \
                                  "'resource': plugin|test|site|test|class|test|subclass|test|type|test|id|test|" \
                                  "endpoint|test, 'dimensions': set([{{'dimension_name': 'if_alias', " \
                                  "'dimension_value': 'test'}}, " \
                                  "{{'dimension_name': 'if_name', 'dimension_value': 'eth0'}}]), " \
                                  "'metrics_group_type': 'test', " \
                                  "'metrics': set([{{'metric_creation_timestamp': {}, " \
                                  "'metric_type': 'gauge', 'metric_name': 'test_metric', 'metric_value': 0}}]), " \
                                  "'metrics_group_creation_timestamp': {}, " \
                                  "'metrics_group_schema_version': '0.2'}}".format(mock_time.return_value,
                                                                                   mock_time.return_value)
            self.assertEqual(repr(metrics_group), _METRICS_GROUP_REPR)

            dimensions_as_dicts = [{
                'dimension_name': dimension.name,
                'dimension_value': dimension.value
            } for dimension in metrics_group.dimensions]
            self.assertEqual(
                PanoptesMetricsGroup.flatten_dimensions(dimensions_as_dicts), {
                    'if_alias': 'test',
                    'if_name': 'eth0'
                })