当前位置: 首页>>代码示例>>Python>>正文


Python CollectorRegistry.collect方法代码示例

本文整理汇总了Python中prometheus_client.CollectorRegistry.collect方法的典型用法代码示例。如果您正苦于以下问题:Python CollectorRegistry.collect方法的具体用法?Python CollectorRegistry.collect怎么用?Python CollectorRegistry.collect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在prometheus_client.CollectorRegistry的用法示例。


在下文中一共展示了CollectorRegistry.collect方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestCounter

# 需要导入模块: from prometheus_client import CollectorRegistry [as 别名]
# 或者: from prometheus_client.CollectorRegistry import collect [as 别名]
class TestCounter(unittest.TestCase):

    def setUp(self):
        self.registry = CollectorRegistry()

    def test_initialize(self):
        c = Counter('test_value', 'Testing roller', registry=self.registry)
        r = CounterRoller(c, registry=self.registry)

        self.assertEqual(r.name, 'test_value_sum_rolled')

    def test_initialize_errors(self):
        # Raise error because tried to use wrong type of item
        def wrong_type_exception():
            h = Histogram('test_value', 'Testing roller', registry=self.registry)
            roller = CounterRoller(h, registry=self.registry)
        self.assertRaises(ValueError, wrong_type_exception)

        # Update seconds must be > 0
        def update_seconds_lt_1_exception():
            c = Counter('test_value', 'Testing roller', registry=self.registry)
            roller = CounterRoller(c, registry=self.registry, options={
                'update_seconds': 0
            })
        self.assertRaises(ValueError, update_seconds_lt_1_exception)

        # Update seconds must be a multiple of 1
        def update_seconds_not_divisible_by_1_exception():
            c = Counter('test_value', 'Testing roller', registry=self.registry)
            roller = CounterRoller(c, registry=self.registry, options={
                'update_seconds': 2.5
            })
        self.assertRaises(ValueError, update_seconds_not_divisible_by_1_exception)

    def test_collect(self):
        c = Counter('test_value', 'Testing roller', registry=self.registry)
        r = CounterRoller(c, registry=self.registry)

        r.collect()
        nchecks = 0
        for m in self.registry.collect():
            if m.name.endswith('sum_rolled'):
                for name, labels, val in m.samples:
                    self.assertEqual(val, 0.0)
                    nchecks += 1
        self.assertTrue(nchecks > 0)

        c.inc()
        c.inc(1.5)
        r.collect()

        nchecks = 0
        for m in self.registry.collect():
            if m.name.endswith('sum_rolled'):
                for name, labels, val in m.samples:
                    self.assertEqual(val, 2.5)
                    nchecks += 1
        self.assertTrue(nchecks > 0)
开发者ID:turtlemonvh,项目名称:prometheus_python_roller,代码行数:60,代码来源:test_rollers.py

示例2: TestHistogram

# 需要导入模块: from prometheus_client import CollectorRegistry [as 别名]
# 或者: from prometheus_client.CollectorRegistry import collect [as 别名]
class TestHistogram(unittest.TestCase):

    def setUp(self):
        self.registry = CollectorRegistry()

    def get_rolled_samples(self):
        """Get all 'rolled' type gauges in the current registry
        """
        for m in self.registry.collect():
            if m.name.endswith('_rolled'):
                for name, labels, val in m.samples:
                    yield name, labels, val

    def get_hist_samples(self):
        """Get all histogram buckets in the current registry
        """
        for m in self.registry.collect():
            if m.name == 'test_value':
                for name, labels, val in m.samples:
                    if name.endswith('_bucket'):
                        yield name, labels, val

    def test_initialize(self):
        h = Histogram('test_value', 'Testing roller', registry=self.registry)
        roller = HistogramRoller(h, registry=self.registry)

        n_buckets = 0
        for name, _, _ in self.get_hist_samples():
            if name.endswith('_bucket'):
                n_buckets += 1

        n_created_guages = 0
        for _, _, _ in self.get_rolled_samples():
            n_created_guages += 1

        # Check that roller gauges don't exist until values are added
        self.assertTrue(n_buckets > 0)
        self.assertTrue(n_created_guages == 0)

        self.assertEqual(roller.name, 'test_value_sum_rolled')

    def test_initialize_errors(self):
        # Raise error because tried to use wrong type of item
        def wrong_type_exception():
            c = Counter('test_value', 'Testing roller', registry=self.registry)
            roller = HistogramRoller(c, registry=self.registry)
        self.assertRaises(ValueError, wrong_type_exception)

        # Update seconds must be > 0
        def update_seconds_lt_1_exception():
            h = Histogram('test_value', 'Testing roller', registry=self.registry)
            roller = HistogramRoller(h, registry=self.registry, options={
                'update_seconds': 0
            })
        self.assertRaises(ValueError, update_seconds_lt_1_exception)

        # Update seconds must be a multiple of 1
        def update_seconds_not_divisible_by_1_exception():
            h = Histogram('test_value', 'Testing roller', registry=self.registry)
            roller = HistogramRoller(h, registry=self.registry, options={
                'update_seconds': 2.5
            })
        self.assertRaises(ValueError, update_seconds_not_divisible_by_1_exception)

    def test_collect(self):
        h = Histogram('test_value', 'Testing roller', registry=self.registry)
        roller = HistogramRoller(h, registry=self.registry)

        # Get values
        roller.collect()

        n_buckets = 0
        for _, _, _ in self.get_hist_samples():
            n_buckets += 1

        n_created_guages = 0
        for _, _, _ in self.get_rolled_samples():
            n_created_guages += 1

        self.assertTrue(n_buckets > 0)
        self.assertTrue(n_created_guages > 0)
        self.assertEqual(n_buckets, n_created_guages)

        # Check that roller values are still 0.0 after initial collection
        for name, labels, value in self.get_rolled_samples():
            self.assertEqual(value, 0.0)

        # Add some samples
        for i in range(100):
            h.observe(pow(2, i/10 - 2))

        # Collect hisogram values
        hist_values = dict()
        for name, labels, value in self.get_hist_samples():
            hist_values[labels['le']] = value

        # Make sure they are still equal after collection
        for name, labels, value in self.get_rolled_samples():
            self.assertEqual(value, 0.0)

#.........这里部分代码省略.........
开发者ID:turtlemonvh,项目名称:prometheus_python_roller,代码行数:103,代码来源:test_rollers.py


注:本文中的prometheus_client.CollectorRegistry.collect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。