autotest: tko/parse: add a success counter

BUG=chromium:852083
TEST=None

Change-Id: Ie0365d1c677ecf317ad503fc21d71ed84d6d316b
Reviewed-on: https://chromium-review.googlesource.com/1135846
Commit-Ready: Aviv Keshet <akeshet@chromium.org>
Tested-by: Aviv Keshet <akeshet@chromium.org>
Reviewed-by: Prathmesh Prabhu <pprabhu@chromium.org>
diff --git a/tko/parse.py b/tko/parse.py
index 74e2501..7cc97e2 100755
--- a/tko/parse.py
+++ b/tko/parse.py
@@ -702,9 +702,6 @@
     results_dir = os.path.abspath(args[0])
     assert os.path.exists(results_dir)
 
-    site_utils.SetupTsMonGlobalState('tko_parse', indirect=False,
-                                     short_lived=True)
-
     pid_file_manager = pidfile.PidFileManager("parser", results_dir)
 
     if options.write_pidfile:
@@ -752,10 +749,20 @@
         raise
     else:
         pid_file_manager.close_file(0)
-    finally:
-        metrics.Flush()
     duration_secs = (datetime.datetime.now() - start_time).total_seconds()
 
 
 if __name__ == "__main__":
-    main()
+    # We are obliged to use indirect=False, not use the SetupTsMonGlobalState
+    # context manager, and add a manual flush, because tko/parse is expected to
+    # be a very short lived (<1 min) script when working effectively, and we
+    # can't afford to either a) wait for up to 1min for metrics to flush at the
+    # end or b) drop metrics that were sent within the last minute of execution.
+    site_utils.SetupTsMonGlobalState('tko_parse', indirect=False,
+                                     short_lived=True)
+    try:
+        with metrics.SuccessCounter('chromeos/autotest/tko_parse/runs'):
+            main()
+    finally:
+        metrics.Flush()
+