|
17 | 17 | import mock |
18 | 18 |
|
19 | 19 | from opencensus.stats import measure_to_view_map as measure_to_view_map_module |
| 20 | +from opencensus.stats.aggregation import CountAggregation |
20 | 21 | from opencensus.stats.measure import BaseMeasure |
21 | 22 | from opencensus.stats.measure import MeasureInt |
22 | 23 | from opencensus.stats.view import View |
23 | 24 | from opencensus.stats.view_data import ViewData |
| 25 | +from opencensus.tags import tag_key as tag_key_module |
| 26 | + |
| 27 | + |
| 28 | +METHOD_KEY = tag_key_module.TagKey("method") |
| 29 | +REQUEST_COUNT_MEASURE = MeasureInt( |
| 30 | + "request_count", "number of requests", "1") |
| 31 | +REQUEST_COUNT_VIEW_NAME = "request_count_view" |
| 32 | +COUNT = CountAggregation() |
| 33 | +REQUEST_COUNT_VIEW = View( |
| 34 | + REQUEST_COUNT_VIEW_NAME, |
| 35 | + "number of requests broken down by methods", |
| 36 | + [METHOD_KEY], REQUEST_COUNT_MEASURE, COUNT) |
24 | 37 |
|
25 | 38 |
|
26 | 39 | class TestMeasureToViewMap(unittest.TestCase): |
@@ -378,3 +391,24 @@ def test_export(self): |
378 | 391 | measure_to_view_map._registered_measures = {} |
379 | 392 | measure_to_view_map.export(view_data) |
380 | 393 | self.assertTrue(True) |
| 394 | + |
| 395 | + def test_export_duplicates_viewdata(self): |
| 396 | + """Check that we copy view data on export.""" |
| 397 | + mtvm = measure_to_view_map_module.MeasureToViewMap() |
| 398 | + |
| 399 | + exporter = mock.Mock() |
| 400 | + mtvm.exporters.append(exporter) |
| 401 | + |
| 402 | + timestamp1 = mock.Mock() |
| 403 | + timestamp2 = mock.Mock() |
| 404 | + view_data = ViewData(REQUEST_COUNT_VIEW, timestamp1, timestamp2) |
| 405 | + mtvm.export([view_data]) |
| 406 | + mtvm.export([view_data]) |
| 407 | + self.assertEqual(exporter.export.call_count, 2) |
| 408 | + |
| 409 | + exported_call1, exported_call2 = exporter.export.call_args_list |
| 410 | + exported_vd1 = exported_call1[0][0][0] |
| 411 | + exported_vd2 = exported_call2[0][0][0] |
| 412 | + self.assertIsNot(exported_vd1, exported_vd2) |
| 413 | + self.assertIsNot(exported_vd1.end_time, view_data.end_time) |
| 414 | + self.assertIsNot(exported_vd2.end_time, view_data.end_time) |
0 commit comments