Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions opencensus/stats/measurement_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from datetime import datetime
from opencensus.tags import execution_context


class MeasurementMap(object):
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/stats/test_measurement_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -49,11 +50,21 @@ 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)

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)