|
| 1 | +# Copyright 2018, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import random |
| 17 | +import time |
| 18 | +import unittest |
| 19 | +from retrying import retry |
| 20 | +from pprint import pprint |
| 21 | +from opencensus.stats import aggregation as aggregation_module |
| 22 | +from opencensus.stats.exporters import stackdriver_exporter as stackdriver |
| 23 | +from opencensus.stats import measure as measure_module |
| 24 | +from opencensus.stats import stats as stats_module |
| 25 | +from opencensus.stats import view as view_module |
| 26 | +from opencensus.tags import tag_key as tag_key_module |
| 27 | +from opencensus.tags import tag_map as tag_map_module |
| 28 | +from opencensus.tags import tag_value as tag_value_module |
| 29 | +from opencensus.common.transports import sync |
| 30 | +from google.cloud import monitoring_v3 |
| 31 | + |
| 32 | +MiB = 1 << 20 |
| 33 | + |
| 34 | +PROJECT = os.environ.get('GCLOUD_PROJECT_PYTHON') |
| 35 | +RETRY_WAIT_PERIOD = 10000 # Wait 10 seconds between each retry |
| 36 | +RETRY_MAX_ATTEMPT = 10 # Retry 10 times |
| 37 | + |
| 38 | +class TestBasicStats(unittest.TestCase): |
| 39 | + |
| 40 | + def test_stats_record_sync(self): |
| 41 | + # We are using sufix in order to prevent cached objects |
| 42 | + sufix = str(os.getgid()) |
| 43 | + |
| 44 | + tag_key = "SampleKeySyncTest%s" % sufix |
| 45 | + measure_name = "SampleMeasureNameSyncTest%s" % sufix |
| 46 | + measure_description = "SampleDescriptionSyncTest%s" % sufix |
| 47 | + view_name = "SampleViewNameSyncTest%s" % sufix |
| 48 | + view_description = "SampleViewDescriptionSyncTest%s" % sufix |
| 49 | + |
| 50 | + FRONTEND_KEY = tag_key_module.TagKey(tag_key) |
| 51 | + VIDEO_SIZE_MEASURE = measure_module.MeasureInt( |
| 52 | + measure_name, measure_description, "By") |
| 53 | + VIDEO_SIZE_VIEW_NAME = view_name |
| 54 | + VIDEO_SIZE_DISTRIBUTION = aggregation_module.DistributionAggregation( |
| 55 | + [0.0, 16.0 * MiB, 256.0 * MiB]) |
| 56 | + VIDEO_SIZE_VIEW = view_module.View(VIDEO_SIZE_VIEW_NAME, |
| 57 | + view_description, |
| 58 | + [FRONTEND_KEY], |
| 59 | + VIDEO_SIZE_MEASURE, |
| 60 | + VIDEO_SIZE_DISTRIBUTION) |
| 61 | + |
| 62 | + stats = stats_module.Stats() |
| 63 | + view_manager = stats.view_manager |
| 64 | + stats_recorder = stats.stats_recorder |
| 65 | + |
| 66 | + client = monitoring_v3.MetricServiceClient() |
| 67 | + exporter = stackdriver.StackdriverStatsExporter(options=stackdriver.Options(project_id=PROJECT), |
| 68 | + client=client, |
| 69 | + transport=sync.SyncTransport) |
| 70 | + view_manager.register_exporter(exporter) |
| 71 | + |
| 72 | + # Register view. |
| 73 | + view_manager.register_view(VIDEO_SIZE_VIEW) |
| 74 | + |
| 75 | + # Sleep for [0, 10] milliseconds to fake work. |
| 76 | + time.sleep(random.randint(1, 10) / 1000.0) |
| 77 | + |
| 78 | + # Process video. |
| 79 | + # Record the processed video size. |
| 80 | + tag_value = tag_value_module.TagValue("1200") |
| 81 | + tag_map = tag_map_module.TagMap() |
| 82 | + tag_map.insert(FRONTEND_KEY, tag_value) |
| 83 | + measure_map = stats_recorder.new_measurement_map() |
| 84 | + measure_map.measure_int_put(VIDEO_SIZE_MEASURE, 25 * MiB) |
| 85 | + |
| 86 | + measure_map.record(tag_map) |
| 87 | + |
| 88 | + # Sleep for [0, 10] milliseconds to fake wait. |
| 89 | + time.sleep(random.randint(1, 10) / 1000.0) |
| 90 | + |
| 91 | + @retry(wait_fixed=RETRY_WAIT_PERIOD, stop_max_attempt_number=RETRY_MAX_ATTEMPT) |
| 92 | + def get_metric_descriptors(self, exporter, view_description): |
| 93 | + name = exporter.client.project_path(PROJECT) |
| 94 | + list_metrics_descriptors = exporter.client.list_metric_descriptors(name) |
| 95 | + element = next((element for element in list_metrics_descriptors if element.description == view_description), None) |
| 96 | + |
| 97 | + self.assertIsNotNone(element) |
| 98 | + self.assertEqual(element.description, view_description) |
| 99 | + self.assertEqual(element.unit, "By") |
| 100 | + |
| 101 | + get_metric_descriptors(self, exporter, view_description) |
| 102 | + |
| 103 | + def test_stats_record_async(self): |
| 104 | + # We are using sufix in order to prevent cached objects |
| 105 | + sufix = str(os.getpid()) |
| 106 | + |
| 107 | + tag_key = "SampleKeyAsyncTest%s" % sufix |
| 108 | + measure_name = "SampleMeasureNameAsyncTest%s" % sufix |
| 109 | + measure_description = "SampleDescriptionAsyncTest%s" % sufix |
| 110 | + view_name = "SampleViewNameAsyncTest%s" % sufix |
| 111 | + view_description = "SampleViewDescriptionAsyncTest%s" % sufix |
| 112 | + |
| 113 | + FRONTEND_KEY_ASYNC = tag_key_module.TagKey(tag_key) |
| 114 | + VIDEO_SIZE_MEASURE_ASYNC = measure_module.MeasureInt( |
| 115 | + measure_name, measure_description, "By") |
| 116 | + VIDEO_SIZE_VIEW_NAME_ASYNC = view_name |
| 117 | + VIDEO_SIZE_DISTRIBUTION_ASYNC = aggregation_module.DistributionAggregation( |
| 118 | + [0.0, 16.0 * MiB, 256.0 * MiB]) |
| 119 | + VIDEO_SIZE_VIEW_ASYNC = view_module.View(VIDEO_SIZE_VIEW_NAME_ASYNC, |
| 120 | + view_description, |
| 121 | + [FRONTEND_KEY_ASYNC], |
| 122 | + VIDEO_SIZE_MEASURE_ASYNC, |
| 123 | + VIDEO_SIZE_DISTRIBUTION_ASYNC) |
| 124 | + |
| 125 | + stats = stats_module.Stats() |
| 126 | + view_manager = stats.view_manager |
| 127 | + stats_recorder = stats.stats_recorder |
| 128 | + |
| 129 | + exporter = stackdriver.new_stats_exporter(stackdriver.Options(project_id=PROJECT)) |
| 130 | + view_manager.register_exporter(exporter) |
| 131 | + |
| 132 | + # Register view. |
| 133 | + view_manager.register_view(VIDEO_SIZE_VIEW_ASYNC) |
| 134 | + |
| 135 | + # Sleep for [0, 10] milliseconds to fake work. |
| 136 | + time.sleep(random.randint(1, 10) / 1000.0) |
| 137 | + |
| 138 | + # Process video. |
| 139 | + # Record the processed video size. |
| 140 | + tag_value = tag_value_module.TagValue("1200") |
| 141 | + tag_map = tag_map_module.TagMap() |
| 142 | + tag_map.insert(FRONTEND_KEY_ASYNC, tag_value) |
| 143 | + measure_map = stats_recorder.new_measurement_map() |
| 144 | + measure_map.measure_int_put(VIDEO_SIZE_MEASURE_ASYNC, 25 * MiB) |
| 145 | + |
| 146 | + measure_map.record(tag_map) |
| 147 | + |
| 148 | + @retry(wait_fixed=RETRY_WAIT_PERIOD, stop_max_attempt_number=RETRY_MAX_ATTEMPT) |
| 149 | + def get_metric_descriptors(self, exporter, view_description): |
| 150 | + name = exporter.client.project_path(PROJECT) |
| 151 | + list_metrics_descriptors = exporter.client.list_metric_descriptors(name) |
| 152 | + element = next((element for element in list_metrics_descriptors if element.description == view_description), None) |
| 153 | + self.assertIsNotNone(element) |
| 154 | + self.assertEqual(element.description, view_description) |
| 155 | + self.assertEqual(element.unit, "By") |
| 156 | + |
| 157 | + get_metric_descriptors(self, exporter, view_description) |
0 commit comments