diff --git a/metrics.py b/metrics.py index c2c88e0d8e..674c78b9a5 100644 --- a/metrics.py +++ b/metrics.py @@ -161,6 +161,11 @@ class MetricsCollector(object): with self._metrics_lock: self._reported_metrics[name] = value + def add_repeated(self, name, value): + if self._collect_custom_metrics: + with self._metrics_lock: + self._reported_metrics.setdefault(name, []).append(value) + @contextlib.contextmanager def pause_metrics_collection(self): collect_custom_metrics = self._collect_custom_metrics diff --git a/tests/metrics_test.py b/tests/metrics_test.py index a97c2673fb..d80f5aca78 100644 --- a/tests/metrics_test.py +++ b/tests/metrics_test.py @@ -574,6 +574,23 @@ class MetricsCollectorTest(unittest.TestCase): self.config_file, {'is-googler': True, 'countdown': 0, 'opt-in': None, 'version': 5}) + def test_add_repeated(self): + """Tests that we can add repeated metrics.""" + self.FileRead.side_effect = [ + '{"is-googler": true, "countdown": 0, "opt-in": true}' + ] + + @self.collector.collect_metrics('fun') + def fun(): + self.collector.add_repeated('fun', 1) + self.collector.add_repeated('fun', 2) + self.collector.add_repeated('fun', 5) + + fun() + + # Assert that we collected all metrics for fun. + self.assert_collects_metrics({'fun': [1, 2, 5]}) + if __name__ == '__main__': unittest.main()