def test_emit(self):
        options = prometheus.Options(namespace="opencensus", port=9005)
        stats = stats_module.Stats()
        view_manager = stats.view_manager
        stats_recorder = stats.stats_recorder
        exporter = prometheus.new_stats_exporter(options)
        view_manager.register_exporter(exporter)
        view_manager.register_view(VIDEO_SIZE_VIEW)
        tag_value = tag_value_module.TagValue(str(1000))
        tag_map = tag_map_module.TagMap()
        tag_map.insert(FRONTEND_KEY, tag_value)
        measure_map = stats_recorder.new_measurement_map()
        measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB)
        measure_map.record(tag_map)
        exporter.export([
            exporter.collector.view_data[(
                'opencensus_my.org/views/video_size_test2-my.org'
                '/keys/frontend')]
        ])

        self.assertIsInstance(
            exporter.collector.view_data[(
                'opencensus_my.org/views/video_size_test2-my.org'
                '/keys/frontend')], view_data_module.ViewData)
        self.assertEqual(REGISTERED_VIEW2, exporter.collector.registered_views)
        self.assertEqual(options, exporter.options)
        self.assertEqual(options.registry, exporter.gatherer)
        self.assertIsNotNone(exporter.collector)
        self.assertIsNotNone(exporter.transport)
Example #2
0
def main():
    stats = stats_module.Stats()
    view_manager = stats.view_manager
    stats_recorder = stats.stats_recorder

    exporter = prometheus.new_stats_exporter(
        prometheus.Options(namespace="opencensus"))
    view_manager.register_exporter(exporter)

    # Register view.
    view_manager.register_view(VIDEO_SIZE_VIEW)

    # Sleep for [0, 10] milliseconds to fake work.
    time.sleep(random.randint(1, 10) / 1000.0)

    # Process video.
    # Record the processed video size.
    tag_value = tag_value_module.TagValue(str(random.randint(1, 10000)))
    tag_map = tag_map_module.TagMap()
    tag_map.insert(FRONTEND_KEY, tag_value)
    measure_map = stats_recorder.new_measurement_map()
    measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB)
    measure_map.record(tag_map)

    # Get aggregated stats and print it to console.
    view_data = view_manager.get_view(VIDEO_SIZE_VIEW_NAME)
    pprint(vars(view_data))
    for k, v in view_data.tag_value_aggregation_data_map.items():
        pprint(k)
        pprint(vars(v))

    # Prevent main from exiting to see the data on prometheus
    # localhost:8000/metrics
    while True:
        pass
Example #3
0
    def test_prometheus_stats(self):

        method_key = tag_key_module.TagKey("method")
        request_count_measure = measure_module.MeasureInt(
            "request_count", "number of requests", "1")
        request_count_view_name = "request_count_view"
        count_agg = aggregation_module.CountAggregation()
        request_count_view = view_module.View(
            request_count_view_name,
            "number of requests broken down by methods", [method_key],
            request_count_measure, count_agg)
        stats = stats_module.Stats()
        view_manager = stats.view_manager
        stats_recorder = stats.stats_recorder

        exporter = prometheus.new_stats_exporter(
            prometheus.Options(namespace="opencensus", port=9303))
        view_manager.register_exporter(exporter)

        view_manager.register_view(request_count_view)

        time.sleep(random.randint(1, 10) / 1000.0)

        method_value_1 = tag_value_module.TagValue("some method")
        tag_map_1 = tag_map_module.TagMap()
        tag_map_1.insert(method_key, method_value_1)
        measure_map_1 = stats_recorder.new_measurement_map()
        measure_map_1.measure_int_put(request_count_measure, 1)
        measure_map_1.record(tag_map_1)

        method_value_2 = tag_value_module.TagValue("some other method")
        tag_map_2 = tag_map_module.TagMap()
        tag_map_2.insert(method_key, method_value_2)
        measure_map_2 = stats_recorder.new_measurement_map()
        measure_map_2.measure_int_put(request_count_measure, 1)
        measure_map_2.record(tag_map_2)
        measure_map_2.record(tag_map_2)

        if sys.version_info > (3, 0):
            import urllib.request
            contents = urllib.request.urlopen(
                "http://localhost:9303/metrics").read()
        else:
            import urllib2
            contents = urllib2.urlopen("http://localhost:9303/metrics").read()

        self.assertIn(b'# TYPE opencensus_request_count_view counter',
                      contents)
        self.assertIn(
            b'opencensus_request_count_view'
            b'{method="some method"} 1.0', contents)
        self.assertIn(
            b'opencensus_request_count_view'
            b'{method="some other method"} 2.0', contents)
    def test_prometheus_stats(self):
        import random
        import time
        import sys

        from opencensus.stats import aggregation as aggregation_module
        from opencensus.stats.exporters import prometheus_exporter as prometheus
        from opencensus.stats import measure as measure_module
        from opencensus.stats import stats as stats_module
        from opencensus.stats import view as view_module
        from opencensus.tags import tag_key as tag_key_module
        from opencensus.tags import tag_map as tag_map_module
        from opencensus.tags import tag_value as tag_value_module

        MiB = 1 << 20
        FRONTEND_KEY = tag_key_module.TagKey("my.org/keys/frontend")
        VIDEO_SIZE_MEASURE = measure_module.MeasureInt(
            "my.org/measures/video_size", "size of processed videos", "By")
        VIDEO_SIZE_VIEW_NAME = "my.org/views/video_size"
        VIDEO_SIZE_DISTRIBUTION = aggregation_module.CountAggregation(
            256.0 * MiB)
        VIDEO_SIZE_VIEW = view_module.View(VIDEO_SIZE_VIEW_NAME,
                                           "processed video size over time",
                                           [FRONTEND_KEY],
                                           VIDEO_SIZE_MEASURE,
                                           VIDEO_SIZE_DISTRIBUTION)
        stats = stats_module.Stats()
        view_manager = stats.view_manager
        stats_recorder = stats.stats_recorder

        exporter = prometheus.new_stats_exporter(prometheus.Options(namespace="opencensus", port=9303))
        view_manager.register_exporter(exporter)

        view_manager.register_view(VIDEO_SIZE_VIEW)

        time.sleep(random.randint(1, 10) / 1000.0)

        tag_value = tag_value_module.TagValue(str(random.randint(1, 10000)))
        tag_map = tag_map_module.TagMap()
        tag_map.insert(FRONTEND_KEY, tag_value)
        measure_map = stats_recorder.new_measurement_map()
        measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB)
        measure_map.record(tag_map)

        if sys.version_info > (3, 0):
            import urllib.request
            contents = urllib.request.urlopen("http://localhost:9303/metrics").read()
        else:
            import urllib2
            contents = urllib2.urlopen("http://localhost:9303/metrics").read()

        self.assertIn(b'# TYPE opencensus_my.org/views/video_size counter', contents)
        self.assertIn(b'opencensus_my.org/views/video_size 268435456.0', contents)
Example #5
0
def setup_open_census():
    stats_stats = stats.Stats()
    app.view_manager = stats_stats.view_manager
    app.stats_recorder = stats_stats.stats_recorder
    response_time_view = view.View("response_time",
                                   "The time it took to respond",
                                   [key_method, key_status, key_error],
                                   m_response_ms,
                                   aggregation.LastValueAggregation())

    app.exporter = prometheus.new_stats_exporter(
        prometheus.Options(namespace="flask_app", port=8000))

    app.view_manager.register_exporter(app.exporter)
    app.view_manager.register_view(response_time_view)
 def test_exporter_constructor_no_namespace(self):
     with self.assertRaisesRegexp(ValueError,
                                  'Namespace can not be empty string.'):
         prometheus.new_stats_exporter(prometheus.Options())