Example #1
0
    def test_update_metric_should_update_metric_metadata(self):
        metric_name = "test_update_metric_should_update_metric_metadata.a.b.c"
        initial_metadata = MetricMetadata.create(
            aggregator=Aggregator.total,
            retention=Retention.from_string("42*1s"),
            carbon_xfilesfactor=0.5,
        )
        create_date = datetime.datetime(2014, 1, 1)
        update_date = datetime.datetime(2018, 1, 1)
        new_metadata = MetricMetadata.create(
            aggregator=Aggregator.average,
            retention=Retention.from_string("43*100s"),
            carbon_xfilesfactor=0.25,
        )
        with freezegun.freeze_time(create_date):
            metric = bg_test_utils.make_metric(metric_name, initial_metadata)
            self.accessor.create_metric(metric)
            self.accessor.flush()
        with freezegun.freeze_time(update_date):
            self.accessor.update_metric(metric_name, new_metadata)
            self.accessor.flush()

        metric = self.accessor.get_metric(metric_name)
        self.assertEqual(update_date, metric.updated_on)
        self.assertEqual(new_metadata, metric.metadata)
Example #2
0
    def test_update_metric_should_update_metric_metadata(self):
        metric_name = "test_update_metric_should_update_metric_metadata.a.b.c"
        initial_metadata = MetricMetadata.create(
            aggregator=Aggregator.total,
            retention=Retention.from_string("42*1s"),
            carbon_xfilesfactor=0.5,
        )
        create_date = datetime.datetime(2014, 1, 1)
        update_date = datetime.datetime(2018, 1, 1)
        new_metadata = MetricMetadata.create(
            aggregator=Aggregator.average,
            retention=Retention.from_string("43*100s"),
            carbon_xfilesfactor=0.25,
        )
        with freezegun.freeze_time(create_date):
            metric = bg_test_utils.make_metric_with_defaults(
                metric_name, initial_metadata)
            self.accessor.create_metric(metric)
            self.accessor.flush()
        with freezegun.freeze_time(update_date):
            self.accessor.update_metric(metric_name, new_metadata)
            self.accessor.flush()

        metric = self.accessor.get_metric(metric_name)
        self.assertEqual(update_date, metric.updated_on)
        self.assertEqual(new_metadata, metric.metadata)
def _create_default_metadata():
    aggregator = Aggregator.maximum
    retention_str = "42*1s:43*60s"
    retention = Retention.from_string(retention_str)
    carbon_xfilesfactor = 0.5
    metadata = MetricMetadata(aggregator, retention, carbon_xfilesfactor)
    return metadata
Example #4
0
class TestCommandStats(bg_test_utils.TestCaseWithFakeAccessor):

    metrics = ["metric1", "metric2"]
    metadata = MetricMetadata.create(retention=Retention.from_string("1440*60s"))

    @patch("sys.stdout", new_callable=StringIO)
    def get_output(self, args, mock_stdout):
        self.accessor.drop_all_metrics()
        for metric in self.metrics:
            self.accessor.create_metric(
                bg_test_utils.make_metric(metric, self.metadata)
            )

        cmd = command_stats.CommandStats()

        parser = argparse.ArgumentParser(add_help=False)
        bg_settings.add_argparse_arguments(parser)
        cmd.add_arguments(parser)

        opts = parser.parse_args(args)
        cmd.run(self.accessor, opts)
        return mock_stdout.getvalue()

    def test_simple(self):
        output = self.get_output([])
        self.assertIn("Namespace      Metrics    Points", output)
        self.assertIn("2      2880", output)

    def test_graphite(self):
        output = self.get_output(["--format", "graphite"])
        self.assertIn("metrics.total 2", output)
        self.assertIn("points.total 2880", output)
Example #5
0
def _create_default_metadata():
    aggregator = Aggregator.maximum
    retention_str = "42*1s:43*60s"
    retention = Retention.from_string(retention_str)
    carbon_xfilesfactor = 0.5
    metadata = MetricMetadata.create(aggregator, retention, carbon_xfilesfactor)
    return metadata
Example #6
0
    def test_document_from_metric_should_build_a_document_from_a_metric(self):
        p0 = "foo"
        p1 = "bar"
        p2 = "baz"
        metric_name = "%s.%s.%s" % (p0, p1, p2)
        metric_id = uuid.uuid5(
            uuid.UUID("{00000000-1111-2222-3333-444444444444}"), metric_name)

        aggregator = Aggregator.maximum
        retention_str = "42*1s:43*60s"
        retention = Retention.from_string(retention_str)
        carbon_xfilesfactor = 0.5
        metadata = MetricMetadata.create(aggregator, retention,
                                         carbon_xfilesfactor)
        metric = bg_metric.Metric(
            metric_name,
            metric_id,
            metadata,
            created_on=datetime.datetime(2017, 1, 1),
            updated_on=datetime.datetime(2018, 2, 2),
        )

        document = bg_elasticsearch.document_from_metric(metric)

        self.__check_document_value(document, "depth", 2)
        self.__check_document_value(document, "uuid", metric_id)
        self.__check_document_value(document, "p0", p0)
        self.__check_document_value(document, "p1", p1)
        self.__check_document_value(document, "p2", p2)

        self.assertTrue("config" in document)
        document_config = document["config"]
        self.__check_document_value(document_config, "aggregator",
                                    aggregator.name)
        self.__check_document_value(document_config, "retention",
                                    retention_str)
        self.__check_document_value(document_config, "carbon_xfilesfactor",
                                    "%f" % carbon_xfilesfactor)

        self.assertTrue("created_on" in document)
        self.assertTrue(isinstance(document["created_on"], datetime.datetime))
        self.assertEqual(metric.created_on, document["created_on"])
        self.assertTrue("updated_on" in document)
        self.assertTrue(isinstance(document["updated_on"], datetime.datetime))
        self.assertEqual(metric.updated_on, document["updated_on"])
        self.assertTrue("read_on" in document)
        self.assertEqual(document["read_on"], None)
Example #7
0
class TestCommandDu(bg_test_utils.TestCaseWithFakeAccessor):

    metrics = ["metric1", "metric2"]
    metadata = MetricMetadata(retention=Retention.from_string("1440*60s"))

    @patch("sys.stdout", new_callable=StringIO)
    def get_output(self, args, mock_stdout):
        self.accessor.drop_all_metrics()
        for metric in self.metrics:
            self.accessor.create_metric(
                bg_test_utils.make_metric(metric, self.metadata)
            )

        cmd = command_du.CommandDu()

        parser = argparse.ArgumentParser(add_help=False)
        bg_settings.add_argparse_arguments(parser)
        cmd.add_arguments(parser)

        opts = parser.parse_args(args)
        cmd.run(self.accessor, opts)
        return mock_stdout.getvalue()

    def test_1_metric_default_args(self):
        output = self.get_output(["-h", "-s", "metric1"])
        self.assertIn("metric1", output)
        self.assertIn("33.8K", output)

    def test_glob_default_args(self):
        output = self.get_output(["-h", "-s", "*"])
        for elmt in self.metrics + ["33.8K", "67.5K", "TOTAL"]:
            self.assertIn(elmt, output)

    def test_glob_no_unit_no_total(self):
        output = self.get_output(["*"])
        self.assertIn("34560", output)
        self.assertNotIn("TOTAL", output)

    def test_human_size(self):
        du = command_du.CommandDu()
        self.assertEqual(du._human_size_of_points(1), "24B")
        self.assertEqual(
            du._human_size_of_points(1023 // command_du._BYTES_PER_POINT), "1008B"
        )
        self.assertEqual(
            du._human_size_of_points(1024 // (command_du._BYTES_PER_POINT - 1)), "1.0K"
        )
Example #8
0
    def test_document_from_metric_should_build_a_document_from_a_metric(self):
        p0 = "foo"
        p1 = "bar"
        p2 = "baz"
        metric_name = "%s.%s.%s" % (p0, p1, p2)
        metric_id = uuid.uuid5(
            uuid.UUID("{00000000-1111-2222-3333-444444444444}"), metric_name
        )

        aggregator = Aggregator.maximum
        retention_str = "42*1s:43*60s"
        retention = Retention.from_string(retention_str)
        carbon_xfilesfactor = 0.5
        metadata = MetricMetadata.create(aggregator, retention, carbon_xfilesfactor)
        metric = bg_metric.Metric(
            metric_name,
            metric_id,
            metadata,
            created_on=datetime.datetime(2017, 1, 1),
            updated_on=datetime.datetime(2018, 2, 2),
        )

        document = bg_elasticsearch.document_from_metric(metric)

        self.__check_document_value(document, "depth", 2)
        self.__check_document_value(document, "uuid", metric_id)
        self.__check_document_value(document, "p0", p0)
        self.__check_document_value(document, "p1", p1)
        self.__check_document_value(document, "p2", p2)

        self.assertTrue("config" in document)
        document_config = document["config"]
        self.__check_document_value(document_config, "aggregator", aggregator.name)
        self.__check_document_value(document_config, "retention", retention_str)
        self.__check_document_value(
            document_config, "carbon_xfilesfactor", "%f" % carbon_xfilesfactor
        )

        self.assertTrue("created_on" in document)
        self.assertTrue(isinstance(document["created_on"], datetime.datetime))
        self.assertEqual(metric.created_on, document["created_on"])
        self.assertTrue("updated_on" in document)
        self.assertTrue(isinstance(document["updated_on"], datetime.datetime))
        self.assertEqual(metric.updated_on, document["updated_on"])
        self.assertTrue("read_on" in document)
        self.assertEqual(document["read_on"], None)