From e53eb762a25cbd993192b6f422a15b147e3be01a Mon Sep 17 00:00:00 2001 From: songy23 Date: Tue, 14 Aug 2018 12:00:13 -0700 Subject: [PATCH] Stats: Allow recording against implicit tag_map. --- opencensus/stats/measurement_map.py | 7 +++++-- tests/unit/stats/test_measurement_map.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/opencensus/stats/measurement_map.py b/opencensus/stats/measurement_map.py index 37d6828cc..5bbe8e997 100644 --- a/opencensus/stats/measurement_map.py +++ b/opencensus/stats/measurement_map.py @@ -13,6 +13,7 @@ # limitations under the License. from datetime import datetime +from opencensus.tags import execution_context class MeasurementMap(object): @@ -47,8 +48,10 @@ def measure_float_put(self, measure, value): """associates the measure of type Float with the given value""" self._measurement_map[measure] = value - def record(self, tag_map_tags): - """records all the measures at the same time with an explicit tag_map + def record(self, tag_map_tags=execution_context.get_current_tag_map()): + """records all the measures at the same time with a tag_map. + tag_map could either be explicitly passed to the method, or implicitly + read from current execution context. """ self.measure_to_view_map.record( tags=tag_map_tags, diff --git a/tests/unit/stats/test_measurement_map.py b/tests/unit/stats/test_measurement_map.py index fc4ab57ac..6d125dcc8 100644 --- a/tests/unit/stats/test_measurement_map.py +++ b/tests/unit/stats/test_measurement_map.py @@ -16,6 +16,7 @@ import mock from opencensus.stats import measurement_map as measurement_map_module from opencensus.stats.measure_to_view_map import MeasureToViewMap +from opencensus.tags import execution_context class TestMeasurementMap(unittest.TestCase): @@ -49,7 +50,7 @@ def test_measure_float_put(self): self.assertEqual({'testKey': 1.0}, measurement_map.measurement_map) - def test_record(self): + def test_record_against_explicit_tag_map(self): measure_to_view_map = mock.Mock() measurement_map = measurement_map_module.MeasurementMap( measure_to_view_map=measure_to_view_map) @@ -57,3 +58,13 @@ def test_record(self): tags = {'testtag1': 'testtag1val'} measurement_map.record(tag_map_tags=tags) self.assertTrue(measure_to_view_map.record.called) + + def test_record_against_implicit_tag_map(self): + measure_to_view_map = mock.Mock() + measurement_map = measurement_map_module.MeasurementMap( + measure_to_view_map=measure_to_view_map) + + tags = {'testtag1': 'testtag1val'} + execution_context.set_current_tag_map(tags) + measurement_map.record() + self.assertTrue(measure_to_view_map.record.called)