Move autotest_stats stats to monarch metrics

BUG=chromium:667171
TEST=utils/unittest_suite

Change-Id: I8cc4ca866df514e791845c676409d2230615cff3
Reviewed-on: https://chromium-review.googlesource.com/414187
Commit-Ready: Ningning Xia <nxia@chromium.org>
Tested-by: Ningning Xia <nxia@chromium.org>
Reviewed-by: Ningning Xia <nxia@chromium.org>
diff --git a/site_utils/abort_suite.py b/site_utils/abort_suite.py
index 3f23377..63c4de9 100755
--- a/site_utils/abort_suite.py
+++ b/site_utils/abort_suite.py
@@ -28,17 +28,14 @@
 import logging
 import os
 import sys
-from datetime import datetime
 
 import common
-from autotest_lib.client.common_lib.cros.graphite import autotest_stats
 from autotest_lib.server import frontend
 from autotest_lib.server import utils
 
 
 LOG_NAME_TEMPLATE = 'abort_suite-%s.log'
 SUITE_JOB_NAME_TEMPLATE = '%s-test_suites/control.%s'
-_timer = autotest_stats.Timer('abort_suites')
 
 
 def parse_args():
@@ -54,7 +51,6 @@
     return parser.parse_args()
 
 
-@_timer.decorate
 def abort_suites(afe, substring):
     """
     Abort the suite.
@@ -92,6 +88,7 @@
 
     afe = frontend.AFE()
     name = SUITE_JOB_NAME_TEMPLATE % (args.build, args.name)
+
     abort_suites(afe, name)
     return 0
 
diff --git a/site_utils/check_hung_proc.py b/site_utils/check_hung_proc.py
index 56abe48..e8b72cd 100755
--- a/site_utils/check_hung_proc.py
+++ b/site_utils/check_hung_proc.py
@@ -17,15 +17,13 @@
 """
 
 
-import socket
 import subprocess
 
-import common
-from autotest_lib.client.common_lib.cros.graphite import autotest_stats
+from autotest_lib.server import site_utils
+from chromite.lib import metrics
 
 
-STATS_KEY = 'hung_processes.%s' % socket.gethostname().replace('.', '_')
-
+PROGRAM_TO_CHECK_SET = set(['gsutil', 'autoserv'])
 
 def check_proc(prog, max_elapsed_sec):
     """Check the number of long-running processes for a given program.
@@ -41,12 +39,20 @@
     cmd = ('ps -eo etimes,args | grep "%s" | awk \'{if($1 > %d) print $0}\' | '
            'wc -l' % (prog, max_elapsed_sec))
     count = int(subprocess.check_output(cmd, shell = True))
-    autotest_stats.Gauge(STATS_KEY).send(prog, count)
+
+    if prog not in PROGRAM_TO_CHECK_SET:
+        prog = 'unknown'
+
+    metrics.Gauge('chromeos/autotest/hung_processes').set(
+            count, fields={'program': prog}
+    )
 
 
 def main():
-    for p in ('gsutil', 'autoserv'):
-        check_proc(p, 86400)
+    """Main script. """
+    with site_utils.SetupTsMonGlobalState('check_hung_proc', short_lived=True):
+        for p in PROGRAM_TO_CHECK_SET:
+            check_proc(p, 86400)
 
 
 if __name__ == '__main__':
diff --git a/site_utils/collect_host_stats.py b/site_utils/collect_host_stats.py
index 5177e6f..89e8dae 100755
--- a/site_utils/collect_host_stats.py
+++ b/site_utils/collect_host_stats.py
@@ -18,7 +18,6 @@
 from chromite.lib import metrics
 from chromite.lib import ts_mon_config
 from autotest_lib.client.common_lib import time_utils
-from autotest_lib.client.common_lib.cros.graphite import autotest_stats
 from autotest_lib.site_utils import gmail_lib
 from autotest_lib.site_utils import host_history
 from autotest_lib.site_utils import host_history_utils
@@ -94,14 +93,6 @@
         print 'Machine utilization rate  = %-4.2f%%' % (100*mur)
         print 'Machine availability rate = %-4.2f%%' % (100*mar)
 
-    autotest_stats.Gauge('machine_utilization_rate').send('%s_hours.%s.%s' %
-                                                          (span, board, pool),
-                                                          mur)
-    autotest_stats.Gauge('machine_availability_rate').send('%s_hours.%s.%s' %
-                                                           (span, board, pool),
-                                                           mar)
-    autotest_stats.Gauge('machine_idle_rate').send('%s_hours.%s.%s' %
-                                                   (span, board, pool), mir)
     fields = {'board': board,
               'pool': pool}
     if span == 1:
diff --git a/site_utils/count_jobs.py b/site_utils/count_jobs.py
index 1da4a82..19d61fb 100755
--- a/site_utils/count_jobs.py
+++ b/site_utils/count_jobs.py
@@ -13,8 +13,8 @@
 import common
 from autotest_lib.frontend import setup_django_environment
 from autotest_lib.frontend.afe import models
-from autotest_lib.client.common_lib.cros.graphite import autotest_stats
-
+from autotest_lib.server import site_utils
+from chromite.lib import metrics
 
 def number_of_jobs_since(delta):
     """Returns the number of jobs kicked off in the last |duration| minutes.
@@ -31,7 +31,11 @@
         description=('A script which records the number of afe jobs run in a time interval.'))
     parser.parse_args(sys.argv[1:])
     count = number_of_jobs_since(timedelta(days=1))
-    autotest_stats.Gauge('jobs_rate').send('afe_daily_count', count)
+
+    with site_utils.SetupTsMonGlobalState('count_jobs', short_lived=True):
+        # TODO: Reporting a stat for each job created from the afe directly could be better.
+        # More discussions are needed to decide whether to remove this file.
+        metrics.Gauge('chromeos/autotest/experimental/jobs_rate/afe_daily_count').set(count)
 
 
 if __name__ == '__main__':
diff --git a/site_utils/gmail_lib.py b/site_utils/gmail_lib.py
index 3917deb..77b6a60 100755
--- a/site_utils/gmail_lib.py
+++ b/site_utils/gmail_lib.py
@@ -26,7 +26,7 @@
 
 import common
 from autotest_lib.client.common_lib import global_config
-from autotest_lib.client.common_lib.cros.graphite import autotest_stats
+from autotest_lib.server import site_utils
 
 try:
   from apiclient.discovery import build as apiclient_build
@@ -40,9 +40,9 @@
 # of a sys.path war between chromite and autotest crbug.com/622988
 from autotest_lib.server import utils as server_utils
 from chromite.lib import retry_util
+from chromite.lib import metrics
 
 
-EMAIL_COUNT_KEY = 'emails.%s'
 DEFAULT_CREDS_FILE = global_config.global_config.get_config_value(
         'NOTIFICATIONS', 'gmail_api_credentials', default=None)
 RETRY_DELAY = 5
@@ -164,13 +164,13 @@
             logging.warning('Will retry error %s', exc)
         return should_retry
 
-    autotest_stats.Counter(EMAIL_COUNT_KEY % 'total').increment()
+    metrics.Counter('chromeos/autotest/send_email/total').increment()
     try:
         retry_util.GenericRetry(
                 handler, retry_count, _run, sleep=RETRY_DELAY,
                 backoff_factor=RETRY_BACKOFF_FACTOR)
     except Exception:
-        autotest_stats.Counter(EMAIL_COUNT_KEY % 'fail').increment()
+        metrics.Counter('chromeos/autotest/send_email/fail').increment()
         raise
 
 
@@ -188,4 +188,6 @@
         sys.exit(1)
 
     message_text = sys.stdin.read()
-    send_email(','.join(args.recipients), args.subject , message_text)
+
+    with site_utils.SetupTsMonGlobalState('gmail_lib', short_lived=True):
+        send_email(','.join(args.recipients), args.subject , message_text)