示例#1
0
    def test_wrong_format(self):
        self.data = {
            "name": "logged_users_total",
            "doc": "Logged users in the application",
            "const_labels": {"app": "my_app"},
        }

        f = TextFormatter()

        c = Collector(**self.data)

        with self.assertRaises(TypeError) as context:
            f.marshall_collector(c)

        self.assertEqual("Not a valid object format", str(context.exception))
示例#2
0
    def test_summary_format_timestamp(self):
        data = {
            "name": "prometheus_target_interval_length_seconds",
            "doc": "Actual intervals between scrapes.",
            "const_labels": {},
        }

        labels = {"interval": "5s"}
        values = [3, 5.2, 13, 4]

        result_regex = r"""# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary
prometheus_target_interval_length_seconds{interval="5s",quantile="0.5"} 4.0 \d*(?:.\d*)?
prometheus_target_interval_length_seconds{interval="5s",quantile="0.9"} 5.2 \d*(?:.\d*)?
prometheus_target_interval_length_seconds{interval="5s",quantile="0.99"} 5.2 \d*(?:.\d*)?
prometheus_target_interval_length_seconds_count{interval="5s"} 4 \d*(?:.\d*)?
prometheus_target_interval_length_seconds_sum{interval="5s"} 25.2 \d*(?:.\d*)?$"""

        s = Summary(**data)

        for i in values:
            s.add(labels, i)

        f = TextFormatter(True)
        result = f.marshall_collector(s)

        self.assertTrue(re.match(result_regex, result))
示例#3
0
    def test_summary_format_text(self):
        data = {
            "name": "prometheus_target_interval_length_seconds",
            "doc": "Actual intervals between scrapes.",
            "const_labels": {},
        }

        labels = {"interval": "5s"}
        values = [3, 5.2, 13, 4]

        valid_result = """# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary
prometheus_target_interval_length_seconds{interval="5s",quantile="0.5"} 4.0
prometheus_target_interval_length_seconds{interval="5s",quantile="0.9"} 5.2
prometheus_target_interval_length_seconds{interval="5s",quantile="0.99"} 5.2
prometheus_target_interval_length_seconds_count{interval="5s"} 4
prometheus_target_interval_length_seconds_sum{interval="5s"} 25.2"""

        s = Summary(**data)

        for i in values:
            s.add(labels, i)

        f = TextFormatter()
        result = f.marshall_collector(s)

        self.assertEqual(valid_result, result)
示例#4
0
    def test_summary_format_timestamp(self):
        data = {
            "name": "prometheus_target_interval_length_seconds",
            "doc": "Actual intervals between scrapes.",
            "const_labels": {},
        }

        labels = {"interval": "5s"}
        values = [3, 5.2, 13, 4]

        result_regex = r"""# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary
prometheus_target_interval_length_seconds{interval="5s",quantile="0.5"} 4.0 \d*(?:.\d*)?
prometheus_target_interval_length_seconds{interval="5s",quantile="0.9"} 5.2 \d*(?:.\d*)?
prometheus_target_interval_length_seconds{interval="5s",quantile="0.99"} 5.2 \d*(?:.\d*)?
prometheus_target_interval_length_seconds_count{interval="5s"} 4 \d*(?:.\d*)?
prometheus_target_interval_length_seconds_sum{interval="5s"} 25.2 \d*(?:.\d*)?$"""

        s = Summary(**data)

        for i in values:
            s.add(labels, i)

        f = TextFormatter(True)
        result = f.marshall_collector(s)

        self.assertTrue(re.match(result_regex, result))
示例#5
0
    def test_summary_format_text(self):
        data = {
            "name": "prometheus_target_interval_length_seconds",
            "doc": "Actual intervals between scrapes.",
            "const_labels": {},
        }

        labels = {"interval": "5s"}
        values = [3, 5.2, 13, 4]

        valid_result = """# HELP prometheus_target_interval_length_seconds Actual intervals between scrapes.
# TYPE prometheus_target_interval_length_seconds summary
prometheus_target_interval_length_seconds{interval="5s",quantile="0.5"} 4.0
prometheus_target_interval_length_seconds{interval="5s",quantile="0.9"} 5.2
prometheus_target_interval_length_seconds{interval="5s",quantile="0.99"} 5.2
prometheus_target_interval_length_seconds_count{interval="5s"} 4
prometheus_target_interval_length_seconds_sum{interval="5s"} 25.2"""

        s = Summary(**data)

        for i in values:
            s.add(labels, i)

        f = TextFormatter()
        result = f.marshall_collector(s)

        self.assertEqual(valid_result, result)
示例#6
0
    def test_wrong_format(self):
        self.data = {
            "name": "logged_users_total",
            "doc": "Logged users in the application",
            "const_labels": {
                "app": "my_app"
            },
        }

        f = TextFormatter()

        c = Collector(**self.data)

        with self.assertRaises(TypeError) as context:
            f.marshall_collector(c)

        self.assertEqual("Not a valid object format", str(context.exception))
示例#7
0
    def test_gauge_format_with_timestamp(self):
        self.data = {
            "name": "logged_users_total",
            "doc": "Logged users in the application",
            "const_labels": {},
        }
        g = Gauge(**self.data)

        counter_data = ({"country": "ch", "device": "mobile"}, 654)

        g.set_value(counter_data[0], counter_data[1])

        result_regex = r"""# HELP logged_users_total Logged users in the application
# TYPE logged_users_total gauge
logged_users_total{country="ch",device="mobile"} 654 \d*(?:.\d*)?$"""

        f_with_ts = TextFormatter(True)
        result = f_with_ts.marshall_collector(g)

        self.assertTrue(re.match(result_regex, result))
示例#8
0
    def test_gauge_format_with_timestamp(self):
        self.data = {
            "name": "logged_users_total",
            "doc": "Logged users in the application",
            "const_labels": {},
        }
        g = Gauge(**self.data)

        counter_data = ({"country": "ch", "device": "mobile"}, 654)

        g.set_value(counter_data[0], counter_data[1])

        result_regex = r"""# HELP logged_users_total Logged users in the application
# TYPE logged_users_total gauge
logged_users_total{country="ch",device="mobile"} 654 \d*(?:.\d*)?$"""

        f_with_ts = TextFormatter(True)
        result = f_with_ts.marshall_collector(g)

        self.assertTrue(re.match(result_regex, result))
示例#9
0
    def test_single_gauge_format_text(self):

        name = "prometheus_local_storage_indexing_queue_capacity"
        doc = "The capacity of the indexing queue."

        valid_result = """# HELP prometheus_local_storage_indexing_queue_capacity The capacity of the indexing queue.
# TYPE prometheus_local_storage_indexing_queue_capacity gauge
prometheus_local_storage_indexing_queue_capacity 16384"""

        data = ((None, 16384), )

        # Create the counter
        g = Gauge(name=name, doc=doc, const_labels={})

        for i in data:
            g.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(g)

        self.assertEqual(valid_result, result)
示例#10
0
    def test_single_counter_format_text(self):

        name = "prometheus_dns_sd_lookups_total"
        doc = "The number of DNS-SD lookups."

        valid_result = """# HELP prometheus_dns_sd_lookups_total The number of DNS-SD lookups.
# TYPE prometheus_dns_sd_lookups_total counter
prometheus_dns_sd_lookups_total 10"""

        data = ((None, 10), )

        # Create the counter
        c = Counter(name=name, doc=doc, const_labels={})

        for i in data:
            c.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(c)

        self.assertEqual(valid_result, result)
示例#11
0
    def test_single_gauge_format_text(self):

        name = "prometheus_local_storage_indexing_queue_capacity"
        doc = "The capacity of the indexing queue."

        valid_result = """# HELP prometheus_local_storage_indexing_queue_capacity The capacity of the indexing queue.
# TYPE prometheus_local_storage_indexing_queue_capacity gauge
prometheus_local_storage_indexing_queue_capacity 16384"""

        data = ((None, 16384),)

        # Create the counter
        g = Gauge(name=name, doc=doc, const_labels={})

        for i in data:
            g.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(g)

        self.assertEqual(valid_result, result)
示例#12
0
    def test_single_counter_format_text(self):

        name = "prometheus_dns_sd_lookups_total"
        doc = "The number of DNS-SD lookups."

        valid_result = """# HELP prometheus_dns_sd_lookups_total The number of DNS-SD lookups.
# TYPE prometheus_dns_sd_lookups_total counter
prometheus_dns_sd_lookups_total 10"""

        data = ((None, 10),)

        # Create the counter
        c = Counter(name=name, doc=doc, const_labels={})

        for i in data:
            c.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(c)

        self.assertEqual(valid_result, result)
示例#13
0
    def test_gauge_format_text(self):

        name = "container_memory_max_usage_bytes"
        doc = "Maximum memory usage ever recorded in bytes."

        valid_result = """# HELP container_memory_max_usage_bytes Maximum memory usage ever recorded in bytes.
# TYPE container_memory_max_usage_bytes gauge
container_memory_max_usage_bytes{id="4f70875bb57986783064fe958f694c9e225643b0d18e9cde6bdee56d47b7ce76",name="prometheus"} 0
container_memory_max_usage_bytes{id="89042838f24f0ec0aa2a6c93ff44fd3f3e43057d35cfd32de89558112ecb92a0",name="calendall_web_run_3"} 0
container_memory_max_usage_bytes{id="d11c6bc95459822e995fac4d4ae527f6cac442a1896a771dbb307ba276beceb9",name="db"} 0
container_memory_max_usage_bytes{id="e4260cc9dca3e4e50ad2bffb0ec7432442197f135023ab629fe3576485cc65dd",name="container-extractor"} 0
container_memory_max_usage_bytes{id="f30d1caaa142b1688a0684ed744fcae6d202a36877617b985e20a5d33801b311",name="calendall_db_1"} 0
container_memory_max_usage_bytes{id="f835d921ffaf332f8d88ef5231ba149e389a2f37276f081878d6f982ef89a981",name="cocky_fermat"} 0"""

        data = (
            (
                {
                    "id":
                    "4f70875bb57986783064fe958f694c9e225643b0d18e9cde6bdee56d47b7ce76",
                    "name": "prometheus",
                },
                0,
            ),
            (
                {
                    "id":
                    "89042838f24f0ec0aa2a6c93ff44fd3f3e43057d35cfd32de89558112ecb92a0",
                    "name": "calendall_web_run_3",
                },
                0,
            ),
            (
                {
                    "id":
                    "d11c6bc95459822e995fac4d4ae527f6cac442a1896a771dbb307ba276beceb9",
                    "name": "db",
                },
                0,
            ),
            (
                {
                    "id":
                    "e4260cc9dca3e4e50ad2bffb0ec7432442197f135023ab629fe3576485cc65dd",
                    "name": "container-extractor",
                },
                0,
            ),
            (
                {
                    "id":
                    "f30d1caaa142b1688a0684ed744fcae6d202a36877617b985e20a5d33801b311",
                    "name": "calendall_db_1",
                },
                0,
            ),
            (
                {
                    "id":
                    "f835d921ffaf332f8d88ef5231ba149e389a2f37276f081878d6f982ef89a981",
                    "name": "cocky_fermat",
                },
                0,
            ),
        )

        # Create the counter
        g = Gauge(name=name, doc=doc, const_labels={})

        for i in data:
            g.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(g)

        self.assertEqual(valid_result, result)
示例#14
0
    def test_counter_format_text(self):

        name = "container_cpu_usage_seconds_total"
        doc = "Total seconds of cpu time consumed."

        valid_result = """# HELP container_cpu_usage_seconds_total Total seconds of cpu time consumed.
# TYPE container_cpu_usage_seconds_total counter
container_cpu_usage_seconds_total{id="110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",name="container-extractor",type="kernel"} 0
container_cpu_usage_seconds_total{id="110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",name="container-extractor",type="user"} 0
container_cpu_usage_seconds_total{id="7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",name="calendall_db_1",type="kernel"} 0
container_cpu_usage_seconds_total{id="7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",name="calendall_db_1",type="user"} 0
container_cpu_usage_seconds_total{id="c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",name="calendall_web_run_8",type="kernel"} 0
container_cpu_usage_seconds_total{id="c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",name="calendall_web_run_8",type="user"} 0
container_cpu_usage_seconds_total{id="cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",name="prometheus",type="kernel"} 0
container_cpu_usage_seconds_total{id="cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",name="prometheus",type="user"} 0"""

        data = (
            (
                {
                    "id":
                    "110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",
                    "name": "container-extractor",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id":
                    "110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",
                    "name": "container-extractor",
                    "type": "user",
                },
                0,
            ),
            (
                {
                    "id":
                    "7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",
                    "name": "calendall_db_1",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id":
                    "7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",
                    "name": "calendall_db_1",
                    "type": "user",
                },
                0,
            ),
            (
                {
                    "id":
                    "c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",
                    "name": "calendall_web_run_8",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id":
                    "c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",
                    "name": "calendall_web_run_8",
                    "type": "user",
                },
                0,
            ),
            (
                {
                    "id":
                    "cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",
                    "name": "prometheus",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id":
                    "cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",
                    "name": "prometheus",
                    "type": "user",
                },
                0,
            ),
        )

        # Create the counter
        c = Counter(name=name, doc=doc, const_labels={})

        for i in data:
            c.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(c)

        self.assertEqual(valid_result, result)
示例#15
0
    def test_gauge_format_text(self):

        name = "container_memory_max_usage_bytes"
        doc = "Maximum memory usage ever recorded in bytes."

        valid_result = """# HELP container_memory_max_usage_bytes Maximum memory usage ever recorded in bytes.
# TYPE container_memory_max_usage_bytes gauge
container_memory_max_usage_bytes{id="4f70875bb57986783064fe958f694c9e225643b0d18e9cde6bdee56d47b7ce76",name="prometheus"} 0
container_memory_max_usage_bytes{id="89042838f24f0ec0aa2a6c93ff44fd3f3e43057d35cfd32de89558112ecb92a0",name="calendall_web_run_3"} 0
container_memory_max_usage_bytes{id="d11c6bc95459822e995fac4d4ae527f6cac442a1896a771dbb307ba276beceb9",name="db"} 0
container_memory_max_usage_bytes{id="e4260cc9dca3e4e50ad2bffb0ec7432442197f135023ab629fe3576485cc65dd",name="container-extractor"} 0
container_memory_max_usage_bytes{id="f30d1caaa142b1688a0684ed744fcae6d202a36877617b985e20a5d33801b311",name="calendall_db_1"} 0
container_memory_max_usage_bytes{id="f835d921ffaf332f8d88ef5231ba149e389a2f37276f081878d6f982ef89a981",name="cocky_fermat"} 0"""

        data = (
            (
                {
                    "id": "4f70875bb57986783064fe958f694c9e225643b0d18e9cde6bdee56d47b7ce76",
                    "name": "prometheus",
                },
                0,
            ),
            (
                {
                    "id": "89042838f24f0ec0aa2a6c93ff44fd3f3e43057d35cfd32de89558112ecb92a0",
                    "name": "calendall_web_run_3",
                },
                0,
            ),
            (
                {
                    "id": "d11c6bc95459822e995fac4d4ae527f6cac442a1896a771dbb307ba276beceb9",
                    "name": "db",
                },
                0,
            ),
            (
                {
                    "id": "e4260cc9dca3e4e50ad2bffb0ec7432442197f135023ab629fe3576485cc65dd",
                    "name": "container-extractor",
                },
                0,
            ),
            (
                {
                    "id": "f30d1caaa142b1688a0684ed744fcae6d202a36877617b985e20a5d33801b311",
                    "name": "calendall_db_1",
                },
                0,
            ),
            (
                {
                    "id": "f835d921ffaf332f8d88ef5231ba149e389a2f37276f081878d6f982ef89a981",
                    "name": "cocky_fermat",
                },
                0,
            ),
        )

        # Create the counter
        g = Gauge(name=name, doc=doc, const_labels={})

        for i in data:
            g.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(g)

        self.assertEqual(valid_result, result)
示例#16
0
    def test_counter_format_text(self):

        name = "container_cpu_usage_seconds_total"
        doc = "Total seconds of cpu time consumed."

        valid_result = """# HELP container_cpu_usage_seconds_total Total seconds of cpu time consumed.
# TYPE container_cpu_usage_seconds_total counter
container_cpu_usage_seconds_total{id="110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",name="container-extractor",type="kernel"} 0
container_cpu_usage_seconds_total{id="110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",name="container-extractor",type="user"} 0
container_cpu_usage_seconds_total{id="7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",name="calendall_db_1",type="kernel"} 0
container_cpu_usage_seconds_total{id="7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",name="calendall_db_1",type="user"} 0
container_cpu_usage_seconds_total{id="c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",name="calendall_web_run_8",type="kernel"} 0
container_cpu_usage_seconds_total{id="c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",name="calendall_web_run_8",type="user"} 0
container_cpu_usage_seconds_total{id="cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",name="prometheus",type="kernel"} 0
container_cpu_usage_seconds_total{id="cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",name="prometheus",type="user"} 0"""

        data = (
            (
                {
                    "id": "110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",
                    "name": "container-extractor",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id": "110863b5395f7f3476d44e7cb8799f2643abbd385dd544bcc379538ac6ffc5ca",
                    "name": "container-extractor",
                    "type": "user",
                },
                0,
            ),
            (
                {
                    "id": "7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",
                    "name": "calendall_db_1",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id": "7c1ae8f404be413a6413d0792123092446f694887f52ae6403356215943d3c36",
                    "name": "calendall_db_1",
                    "type": "user",
                },
                0,
            ),
            (
                {
                    "id": "c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",
                    "name": "calendall_web_run_8",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id": "c863b092d1ecdc68f54a6a4ed0d24fe629696be2337ccafb44c279c7c2d1c172",
                    "name": "calendall_web_run_8",
                    "type": "user",
                },
                0,
            ),
            (
                {
                    "id": "cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",
                    "name": "prometheus",
                    "type": "kernel",
                },
                0,
            ),
            (
                {
                    "id": "cefa0b389a634a0b2f3c2f52ade668d71de75e5775e91297bd65bebe745ba054",
                    "name": "prometheus",
                    "type": "user",
                },
                0,
            ),
        )

        # Create the counter
        c = Counter(name=name, doc=doc, const_labels={})

        for i in data:
            c.set_value(i[0], i[1])

        # Select format
        f = TextFormatter()

        result = f.marshall_collector(c)

        self.assertEqual(valid_result, result)