def test_collector_collect(self): agg = aggregation_module.LastValueAggregation(256) view = view_module.View("new_view", "processed video size over time", [FRONTEND_KEY], VIDEO_SIZE_MEASURE, agg) registry = mock.Mock() view_data = mock.Mock() options = prometheus.Options("test2", 8001, "localhost", registry) collector = prometheus.Collector(options=options, view_data=view_data) collector.register_view(view) desc = collector.registered_views[ 'test2_new_view-my.org/keys/frontend'] collector.to_metric(desc=desc, view=view) registry = mock.Mock() view_data = mock.Mock() options = prometheus.Options("test1", 8001, "localhost", registry) collector = prometheus.Collector(options=options, view_data=view_data) collector.register_view(VIDEO_SIZE_VIEW) desc = collector.registered_views[list(REGISTERED_VIEW)[0]] metric = collector.to_metric(desc=desc, view=VIDEO_SIZE_VIEW) self.assertEqual(desc['name'], metric.name) self.assertEqual(desc['documentation'], metric.documentation) self.assertEqual('histogram', metric.type) self.assertEqual(5, len(metric.samples))
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)
def test_collector_to_metric_histogram(self): registry = mock.Mock() options = prometheus.Options("test1", 8001, "localhost", registry) collector = prometheus.Collector(options=options) collector.register_view(VIDEO_SIZE_VIEW) desc = collector.registered_views[list(REGISTERED_VIEW)[0]] distribution = copy.deepcopy(VIDEO_SIZE_DISTRIBUTION.aggregation_data) distribution.add_sample(280.0 * MiB, None, None) metric = collector.to_metric( desc=desc, tag_values=[tag_value_module.TagValue("ios")], agg_data=distribution) self.assertEqual(desc['name'], metric.name) self.assertEqual(desc['documentation'], metric.documentation) self.assertEqual('histogram', metric.type) expected_samples = [ Sample(metric.name + '_bucket', {"myorg_keys_frontend": "ios", "le": str(16.0 * MiB)}, 0), Sample(metric.name + '_bucket', {"myorg_keys_frontend": "ios", "le": str(256.0 * MiB)}, 0), Sample(metric.name + '_bucket', {"myorg_keys_frontend": "ios", "le": "+Inf"}, 1), Sample(metric.name + '_count', {"myorg_keys_frontend": "ios"}, 1), Sample(metric.name + '_sum', {"myorg_keys_frontend": "ios"}, 280.0 * MiB)] self.assertEqual(expected_samples, metric.samples)
def test_options_constructor_with_params(self): registry = mock.Mock() option = prometheus.Options("test1", 8001, "localhost", registry) self.assertEqual(option.namespace, "test1") self.assertEqual(option.port, 8001) self.assertEqual(option.address, "localhost") self.assertEqual(option.registry, registry)
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
def test_collector_register_view(self): registry = mock.Mock() view_data = mock.Mock() options = prometheus.Options("test1", 8001, "localhost", registry) collector = prometheus.Collector(options=options, view_data=view_data) collector.register_view(VIDEO_SIZE_VIEW) collector.collect() self.assertEqual(REGISTERED_VIEW, collector.registered_views)
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)
def test_collector_to_metric_histogram(self): registry = mock.Mock() view_data = mock.Mock() options = prometheus.Options("test1", 8001, "localhost", registry) collector = prometheus.Collector(options=options, view_data=view_data) collector.register_view(VIDEO_SIZE_VIEW) desc = collector.registered_views[list(REGISTERED_VIEW)[0]] metric = collector.to_metric(desc=desc, view=VIDEO_SIZE_VIEW) self.assertEqual(desc['name'], metric.name) self.assertEqual(desc['documentation'], metric.documentation) self.assertEqual('histogram', metric.type) self.assertEqual(5, len(metric.samples))
def test_collector_constructor_with_params(self): registry = mock.Mock() view_data = mock.Mock() options = prometheus.Options("test1", 8001, "localhost", registry) self.assertEqual(options.namespace, "test1") self.assertEqual(options.port, 8001) self.assertEqual(options.address, "localhost") self.assertEqual(options.registry, registry) collector = prometheus.Collector(options=options, view_data=view_data) self.assertEqual(options, collector.options) self.assertEqual(view_data, collector.view_data) self.assertEqual({}, collector.registered_views) self.assertEqual(registry, collector.registry)
def test_collector_add_view_data(self): registry = mock.Mock() start_time = datetime.utcnow() end_time = datetime.utcnow() view_data = view_data_module.ViewData(view=VIDEO_SIZE_VIEW, start_time=start_time, end_time=end_time) options = prometheus.Options("test1", 8001, "localhost", registry) collector = prometheus.Collector(options=options, view_data={}) collector.register_view(VIDEO_SIZE_VIEW) collector.add_view_data(view_data) v_data = {list(REGISTERED_VIEW)[0]: view_data} collector.collect() self.assertEqual(v_data, collector.view_data)
def test_collector_to_metric_invalid_dist(self): agg = mock.Mock() view = view_module.View(VIDEO_SIZE_VIEW_NAME, "processed video size over time", [FRONTEND_KEY], VIDEO_SIZE_MEASURE, agg) registry = mock.Mock() options = prometheus.Options("test1", 8001, "localhost", registry) collector = prometheus.Collector(options=options) collector.register_view(view) desc = collector.registered_views[list(REGISTERED_VIEW)[0]] with self.assertRaisesRegexp( ValueError, 'unsupported aggregation type <class \'mock.mock.Mock\'>'): collector.to_metric(desc=desc, tag_values=[], agg_data=agg)
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_collector_to_metric_last_value(self): agg = aggregation_module.LastValueAggregation(256) view = view_module.View(VIDEO_SIZE_VIEW_NAME, "processed video size over time", [FRONTEND_KEY], VIDEO_SIZE_MEASURE, agg) registry = mock.Mock() view_data = mock.Mock() options = prometheus.Options("test1", 8001, "localhost", registry) collector = prometheus.Collector(options=options, view_data=view_data) collector.register_view(view) desc = collector.registered_views[list(REGISTERED_VIEW)[0]] metric = collector.to_metric(desc=desc, view=view) self.assertEqual(desc['name'], metric.name) self.assertEqual(desc['documentation'], metric.documentation) self.assertEqual('gauge', metric.type) self.assertEqual(1, len(metric.samples))
def test_collector_collect_with_none_label_value(self): agg = aggregation_module.LastValueAggregation(256) view = view_module.View("new_view", "processed video size over time", [FRONTEND_KEY], VIDEO_SIZE_MEASURE, agg) registry = mock.Mock() options = prometheus.Options("test3", 8001, "localhost", registry) collector = prometheus.Collector(options=options) collector.register_view(view) desc = collector.registered_views['test3_new_view'] metric = collector.to_metric( desc=desc, tag_values=[None], agg_data=agg.aggregation_data) self.assertEqual(1, len(metric.samples)) sample = metric.samples[0] # Sample is a namedtuple # ('Sample', ['name', 'labels', 'value', 'timestamp', 'exemplar']) label_map = sample[1] self.assertEqual({"myorg_keys_frontend": ""}, label_map)
def test_collector_collect(self): agg = aggregation_module.LastValueAggregation(256) view = view_module.View("new_view", "processed video size over time", [FRONTEND_KEY], VIDEO_SIZE_MEASURE, agg) registry = mock.Mock() options = prometheus.Options("test2", 8001, "localhost", registry) collector = prometheus.Collector(options=options) collector.register_view(view) desc = collector.registered_views['test2_new_view'] metric = collector.to_metric( desc=desc, tag_values=[tag_value_module.TagValue("value")], agg_data=agg.aggregation_data) self.assertEqual(desc['name'], metric.name) self.assertEqual(desc['documentation'], metric.documentation) self.assertEqual('gauge', metric.type) expected_samples = [ Sample(metric.name, {"myorg_keys_frontend": "value"}, 256)] self.assertEqual(expected_samples, metric.samples)
def test_collector_constructor(self): options = prometheus.Options("test1") self.assertEqual(options.namespace, "test1") collector = prometheus.Collector(options) self.assertEqual(options, collector.options)
def test_options_constructor(self): option = prometheus.Options("test1") self.assertEqual(option.namespace, "test1")
def test_exporter_constructor_no_namespace(self): with self.assertRaisesRegexp(ValueError, 'Namespace can not be empty string.'): prometheus.new_stats_exporter(prometheus.Options())