[chromite] Capture job_name and git timing for ts_mon in cbuildbot runs.

Simple overload of existing configuration to provide a means to capture
the builder config as part of the metric schema.  It is easy to overload
the schema but using the Task schema limits options for capturing more
fields.  Will need to examine options for passing metric fields to the
lower level calls.

Wraps RunGit command in SecondsTimer metric to collect overall durations
of git calls in metric distributions.

BUG=chromium:829289
TEST=`./cbuildbot/run_tests` PASS

Change-Id: I23f0fd6e8f4870dffdb87a06b4384f351d93e6c4
Reviewed-on: https://chromium-review.googlesource.com/1050954
Commit-Ready: Mike Nichols <mikenichols@chromium.org>
Tested-by: Mike Nichols <mikenichols@chromium.org>
Reviewed-by: Jason Clinton <jclinton@chromium.org>
diff --git a/scripts/cbuildbot_launch.py b/scripts/cbuildbot_launch.py
index 449bc71..c7dca52 100644
--- a/scripts/cbuildbot_launch.py
+++ b/scripts/cbuildbot_launch.py
@@ -424,58 +424,61 @@
       'tryjob': options.remote_trybot,
   }
 
-  # Does the entire build pass or fail.
-  with metrics.Presence(METRIC_ACTIVE, metrics_fields), \
-       metrics.SuccessCounter(METRIC_COMPLETED, metrics_fields) as s_fields:
+  with ts_mon_config.SetupTsMonGlobalState(
+      'cbuildbot_launch', indirect=True,
+      job_name=metrics_fields['build_config']):
+    # Does the entire build pass or fail.
+    with metrics.Presence(METRIC_ACTIVE, metrics_fields), \
+         metrics.SuccessCounter(METRIC_COMPLETED, metrics_fields) as s_fields:
 
-    # Preliminary set, mostly command line parsing.
-    with metrics.SuccessCounter(METRIC_INVOKED, metrics_fields):
-      if options.enable_buildbot_tags:
-        logging.EnableBuildbotMarkers()
-      ConfigureGlobalEnvironment()
+      # Preliminary set, mostly command line parsing.
+      with metrics.SuccessCounter(METRIC_INVOKED, metrics_fields):
+        if options.enable_buildbot_tags:
+          logging.EnableBuildbotMarkers()
+        ConfigureGlobalEnvironment()
 
-    # Prepare the buildroot with source for the build.
-    with metrics.SuccessCounter(METRIC_PREP, metrics_fields):
-      site_config = config_lib.GetConfig()
-      manifest_url = site_config.params['MANIFEST_INT_URL']
-      repo = repository.RepoRepository(manifest_url, buildroot,
-                                       branch=branchname,
-                                       git_cache_dir=options.git_cache_dir)
-      previous_build_state = GetLastBuildState(root)
+      # Prepare the buildroot with source for the build.
+      with metrics.SuccessCounter(METRIC_PREP, metrics_fields):
+        site_config = config_lib.GetConfig()
+        manifest_url = site_config.params['MANIFEST_INT_URL']
+        repo = repository.RepoRepository(manifest_url, buildroot,
+                                         branch=branchname,
+                                         git_cache_dir=options.git_cache_dir)
+        previous_build_state = GetLastBuildState(root)
 
-      # Clean up the buildroot to a safe state.
-      with metrics.SecondsTimer(METRIC_CLEAN, fields=metrics_fields):
-        build_state = GetCurrentBuildState(options, branchname)
-        CleanBuildRoot(root, repo, metrics_fields, build_state)
+        # Clean up the buildroot to a safe state.
+        with metrics.SecondsTimer(METRIC_CLEAN, fields=metrics_fields):
+          build_state = GetCurrentBuildState(options, branchname)
+          CleanBuildRoot(root, repo, metrics_fields, build_state)
 
-      # Get a checkout close enough to the branch that cbuildbot can handle it.
-      if options.sync:
-        with metrics.SecondsTimer(METRIC_INITIAL, fields=metrics_fields):
-          InitialCheckout(repo)
+        # Get a checkout close enough to the branch that cbuildbot can
+        # handle it.
+        if options.sync:
+          with metrics.SecondsTimer(METRIC_INITIAL, fields=metrics_fields):
+            InitialCheckout(repo)
 
-      # Get a checkout close enough to the branch that cbuildbot can handle it.
-      with metrics.SecondsTimer(METRIC_DEPOT_TOOLS, fields=metrics_fields):
-        DepotToolsEnsureBootstrap(depot_tools_path)
+        # Get a checkout close enough to the branch that cbuildbot can
+        # handle it.
+        with metrics.SecondsTimer(METRIC_DEPOT_TOOLS, fields=metrics_fields):
+          DepotToolsEnsureBootstrap(depot_tools_path)
 
-    # Run cbuildbot inside the full ChromeOS checkout, on the specified branch.
-    with metrics.SecondsTimer(METRIC_CBUILDBOT, fields=metrics_fields):
-      if previous_build_state.is_valid():
-        argv.append('--previous-build-state')
-        argv.append(base64.b64encode(previous_build_state.to_json()))
+      # Run cbuildbot inside the full ChromeOS checkout, on the
+      # specified branch.
+      with metrics.SecondsTimer(METRIC_CBUILDBOT, fields=metrics_fields):
+        if previous_build_state.is_valid():
+          argv.append('--previous-build-state')
+          argv.append(base64.b64encode(previous_build_state.to_json()))
 
-      result = Cbuildbot(buildroot, depot_tools_path, argv)
-      s_fields['success'] = (result == 0)
+        result = Cbuildbot(buildroot, depot_tools_path, argv)
+        s_fields['success'] = (result == 0)
 
-      build_state.status = (
-          constants.BUILDER_STATUS_PASSED
-          if result == 0 else constants.BUILDER_STATUS_FAILED)
-      SetLastBuildState(root, build_state)
+        build_state.status = (
+            constants.BUILDER_STATUS_PASSED
+            if result == 0 else constants.BUILDER_STATUS_FAILED)
+        SetLastBuildState(root, build_state)
 
-      CleanupChroot(buildroot)
-      return result
-
+        CleanupChroot(buildroot)
+        return result
 
 def main(argv):
-  # Enable Monarch metrics gathering.
-  with ts_mon_config.SetupTsMonGlobalState('cbuildbot_launch', indirect=True):
-    return _main(argv)
+  return _main(argv)