Skip to content

Commit

Permalink
Allow TimeSeries to have empty label values (census-instrumentation#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t committed Apr 11, 2019
1 parent c90b4b6 commit 0fcae52
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions opencensus/metrics/export/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class TimeSeries(object):
""" # noqa

def __init__(self, label_values, points, start_timestamp):
if not label_values:
raise ValueError("label_values must not be null or empty")
if label_values is None:
raise ValueError("label_values must not be None")
if not points:
raise ValueError("points must not be null or empty")
self._label_values = label_values
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/metrics/export/test_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def test_init_invalid(self):
time_series.TimeSeries(LABEL_VALUES, POINTS, None)
with self.assertRaises(ValueError):
time_series.TimeSeries(None, POINTS, START_TIMESTAMP)
with self.assertRaises(ValueError):
time_series.TimeSeries([], POINTS, START_TIMESTAMP)
with self.assertRaises(ValueError):
time_series.TimeSeries(LABEL_VALUES, None, START_TIMESTAMP)
with self.assertRaises(ValueError):
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/stats/test_metric_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,36 @@ def test_view_data_to_metric(self):
]
for args in args_list:
self.do_test_view_data_to_metric(*args)

def test_convert_view_without_labels(self):
mock_measure = mock.Mock(spec=measure.MeasureFloat)
mock_aggregation = mock.Mock(spec=aggregation.DistributionAggregation)
mock_aggregation.aggregation_type = aggregation.Type.DISTRIBUTION

vd = mock.Mock(spec=view_data.ViewData)
vd.view = view.View(
name=mock.Mock(),
description=mock.Mock(),
columns=[],
measure=mock_measure,
aggregation=mock_aggregation)
vd.start_time = '2019-04-11T22:33:44.555555Z'

mock_point = mock.Mock(spec=point.Point)
mock_point.value = mock.Mock(spec=value.ValueDistribution)

mock_agg = mock.Mock(spec=aggregation_data.SumAggregationDataFloat)
mock_agg.to_point.return_value = mock_point

vd.tag_value_aggregation_data_map = {tuple(): mock_agg}

current_time = '2019-04-11T22:33:55.666666Z'
metric = metric_utils.view_data_to_metric(vd, current_time)

self.assertEqual(metric.descriptor.label_keys, [])
self.assertEqual(len(metric.time_series), 1)
[ts] = metric.time_series
self.assertEqual(ts.label_values, [])
self.assertEqual(len(ts.points), 1)
[pt] = ts.points
self.assertEqual(pt, mock_point)

0 comments on commit 0fcae52

Please sign in to comment.