Forráskód Böngészése

metrics: Add function to collect repeated metrics.

Bug: 897394
Change-Id: Iece4507c7bf92d7a8aad045d31b4f06e54b3af15
Reviewed-on: https://chromium-review.googlesource.com/c/1292243
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Edward Lemur 6 éve
szülő
commit
149834e67d
2 módosított fájl, 22 hozzáadás és 0 törlés
  1. 5 0
      metrics.py
  2. 17 0
      tests/metrics_test.py

+ 5 - 0
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

+ 17 - 0
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()