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_name_to_data_map['opencensus_myorg_views_video_size_test2']
        ])

        self.assertIsInstance(
            exporter.collector.
            view_name_to_data_map['opencensus_myorg_views_video_size_test2'],
            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)
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
Exemple #3
0
def setup_opencensus_prom_exporter():
    exporter = prometheus.new_stats_exporter(prometheus.Options(namespace="alicek106", port=8088))

    view_manager = stats_module.stats.view_manager
    view_manager.register_exporter(exporter)
    view_manager.register_view(visitor_count_view)
    view_manager.register_view(visitor_sum_view)
Exemple #4
0
def setup_openCensus_and_prometheus_exporter() -> None:
    stats = stats_module.stats
    view_manager = stats.view_manager
    exporter = prometheus.new_stats_exporter(
        prometheus.Options(namespace="oc_python"))
    view_manager.register_exporter(exporter)
    register_all_views(view_manager)
Exemple #5
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_total counter',
                      contents)
        self.assertIn(
            b'opencensus_request_count_view_total'
            b'{method="some method"} 1.0', contents)
        self.assertIn(
            b'opencensus_request_count_view_total'
            b'{method="some other method"} 2.0', contents)
 def __init__(self, test_id):
     logging.info(f"setting up prometheus for test id: {test_id}")
     self.test_id = test_id
     # create tags
     self.tag_map = tag_map_module.TagMap()
     self.create_tags("TEST_ID", test_id)
     stats = stats_module.stats
     self.view_manager = stats.view_manager
     self.stats_recorder = stats.stats_recorder
     exporter = prometheus.new_stats_exporter(
         prometheus.Options(namespace="glasswall"))
     self.view_manager.register_exporter(exporter)
     # create measurements and views
     home_page_measurement = self.create_measurement_view("home_page")
     download_brochure_measurement = self.create_measurement_view(
         "download_brochure")
     self.measurements = {
         "home_page": home_page_measurement,
         "download_brochure": download_brochure_measurement
     }
     aggregation_data.SumAggregationDataFloat = aggregation_data.SumAggregationData
 def test_exporter_constructor_no_namespace(self):
     with self.assertRaisesRegexp(ValueError,
                                  'Namespace can not be empty string.'):
         prometheus.new_stats_exporter(prometheus.Options())