Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 3c02724

Browse files
authored
Stats: Allow recording against implicit tag_map. (#258)
1 parent 8de87b0 commit 3c02724

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

opencensus/stats/measurement_map.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
from datetime import datetime
16+
from opencensus.tags import execution_context
1617

1718

1819
class MeasurementMap(object):
@@ -47,8 +48,10 @@ def measure_float_put(self, measure, value):
4748
"""associates the measure of type Float with the given value"""
4849
self._measurement_map[measure] = value
4950

50-
def record(self, tag_map_tags):
51-
"""records all the measures at the same time with an explicit tag_map
51+
def record(self, tag_map_tags=execution_context.get_current_tag_map()):
52+
"""records all the measures at the same time with a tag_map.
53+
tag_map could either be explicitly passed to the method, or implicitly
54+
read from current execution context.
5255
"""
5356
self.measure_to_view_map.record(
5457
tags=tag_map_tags,

tests/unit/stats/test_measurement_map.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mock
1717
from opencensus.stats import measurement_map as measurement_map_module
1818
from opencensus.stats.measure_to_view_map import MeasureToViewMap
19+
from opencensus.tags import execution_context
1920

2021

2122
class TestMeasurementMap(unittest.TestCase):
@@ -49,11 +50,21 @@ def test_measure_float_put(self):
4950

5051
self.assertEqual({'testKey': 1.0}, measurement_map.measurement_map)
5152

52-
def test_record(self):
53+
def test_record_against_explicit_tag_map(self):
5354
measure_to_view_map = mock.Mock()
5455
measurement_map = measurement_map_module.MeasurementMap(
5556
measure_to_view_map=measure_to_view_map)
5657

5758
tags = {'testtag1': 'testtag1val'}
5859
measurement_map.record(tag_map_tags=tags)
5960
self.assertTrue(measure_to_view_map.record.called)
61+
62+
def test_record_against_implicit_tag_map(self):
63+
measure_to_view_map = mock.Mock()
64+
measurement_map = measurement_map_module.MeasurementMap(
65+
measure_to_view_map=measure_to_view_map)
66+
67+
tags = {'testtag1': 'testtag1val'}
68+
execution_context.set_current_tag_map(tags)
69+
measurement_map.record()
70+
self.assertTrue(measure_to_view_map.record.called)

0 commit comments

Comments
 (0)