Skip to content

Commit

Permalink
Stats: Fix recording with None TagMap. (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
songy23 authored Jan 17, 2019
1 parent 2857864 commit 9a94bec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion opencensus/stats/view_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def get_tag_values(self, tags, columns):

def record(self, context, value, timestamp, attachments=None):
"""records the view data against context"""
tag_values = self.get_tag_values(tags=context.map,
if context is None:
tags = dict()
else:
tags = context.map
tag_values = self.get_tag_values(tags=tags,
columns=self.view.columns)
tuple_vals = tuple(tag_values)
if tuple_vals not in self.tag_value_aggregation_data_map:
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/stats/test_view_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,24 @@ def test_record_with_missing_key_in_context(self):
self.assertTrue(tuple_vals in view_data.tag_value_aggregation_data_map)
sum_data = view_data.tag_value_aggregation_data_map.get(tuple_vals)
self.assertEqual(4, sum_data.sum_data)

def test_record_with_none_context(self):
measure = mock.Mock()
sum_aggregation = aggregation_module.SumAggregation()
view = view_module.View("test_view", "description", ['key1', 'key2'],
measure, sum_aggregation)
start_time = datetime.utcnow()
end_time = datetime.utcnow()
view_data = view_data_module.ViewData(
view=view, start_time=start_time, end_time=end_time)
time = datetime.utcnow().isoformat() + 'Z'
value = 4
view_data.record(
context=None, value=value, timestamp=time, attachments=None)
tag_values = view_data.get_tag_values(
tags={}, columns=view.columns)
tuple_vals = tuple(tag_values)
self.assertEqual([None, None], tag_values)
self.assertTrue(tuple_vals in view_data.tag_value_aggregation_data_map)
sum_data = view_data.tag_value_aggregation_data_map.get(tuple_vals)
self.assertEqual(4, sum_data.sum_data)

0 comments on commit 9a94bec

Please sign in to comment.