autotest: add an autoserv job.run duration metric

This adds an (experimental, for now) metric around job.run in
autoserv. This can serve as a both a duration distribution metric, and
also as an overall counter of in- and out-of-container jobs.

BUG=chromium:676696
TEST=moblab tryjob

Change-Id: Id959f7af4023927e6af5dae51e096a5ec3fa1333
Reviewed-on: https://chromium-review.googlesource.com/442088
Commit-Ready: Aviv Keshet <akeshet@chromium.org>
Tested-by: Aviv Keshet <akeshet@chromium.org>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/server/autoserv b/server/autoserv
index 66ee795..fcb3ccb 100755
--- a/server/autoserv
+++ b/server/autoserv
@@ -562,12 +562,19 @@
                             # Ignore any error as the user may not have root
                             # permission to run sudo command.
                             pass
-                    job.run(install_before, install_after,
-                            verify_job_repo_url=verify_job_repo_url,
-                            only_collect_crashinfo=collect_crashinfo,
-                            skip_crash_collection=skip_crash_collection,
-                            job_labels=job_labels,
-                            use_packaging=(not no_use_packaging))
+                    metric_name = ('chromeos/autotest/experimental/'
+                                   'autoserv_job_run_duration')
+                    f = {'in_container': utils.is_in_container(),
+                         'success': False}
+                    with metrics.SecondsTimer(metric_name, fields=f) as c:
+                        job.run(install_before, install_after,
+                                verify_job_repo_url=verify_job_repo_url,
+                                only_collect_crashinfo=collect_crashinfo,
+                                skip_crash_collection=skip_crash_collection,
+                                job_labels=job_labels,
+                                use_packaging=(not no_use_packaging))
+                        c['success'] = True
+
         finally:
             while job.hosts:
                 host = job.hosts.pop()