Add Configurable metric_name_prefix

R=sergeyberezin@chromium.org
BUG=624470

Review-Url: https://codereview.chromium.org/2119933003
Cr-Mirrored-From: https://chromium.googlesource.com/infra/infra
Cr-Mirrored-Commit: db2a299c44a6a970cfb31c45669b1764eadee73d
diff --git a/ts_mon/common/interface.py b/ts_mon/common/interface.py
index 61232e2..df3f502 100644
--- a/ts_mon/common/interface.py
+++ b/ts_mon/common/interface.py
@@ -77,6 +77,8 @@
     self.store = store_ctor(self)
     # Cached time of the last flush. Useful mostly in AppEngine apps.
     self.last_flushed = datetime.datetime.utcfromtimestamp(0)
+    # Metric name prefix
+    self.metric_name_prefix = '/chrome/infra/'
 
   def reset_for_unittest(self):
     self.metrics = {}
diff --git a/ts_mon/common/metrics.py b/ts_mon/common/metrics.py
index 4596f4c..33a5437 100644
--- a/ts_mon/common/metrics.py
+++ b/ts_mon/common/metrics.py
@@ -94,7 +94,7 @@
     """
 
     metric_pb = collection_pb.data.add()
-    metric_pb.metric_name_prefix = '/chrome/infra/'
+    metric_pb.metric_name_prefix = interface.state.metric_name_prefix
     metric_pb.name = self._name
     if self._description is not None:
       metric_pb.description = self._description
diff --git a/ts_mon/config.py b/ts_mon/config.py
index 4e0b25d..8b8e75f 100644
--- a/ts_mon/config.py
+++ b/ts_mon/config.py
@@ -159,6 +159,10 @@
       help='number (e.g. for replication) of this instance of this task '
            '(default: %(default)s)')
 
+  parser.add_argument(
+      '--ts-mon-metric-name-prefix',
+      default='/chrome/infra/',
+      help='metric name prefix for all metrics (default: %(default)s)')
 
 def process_argparse_options(args):
   """Process command line arguments to initialize the global monitor.
@@ -212,6 +216,7 @@
         hostname,
         args.ts_mon_task_number)
 
+  interface.state.metric_name_prefix = args.ts_mon_metric_name_prefix
   interface.state.global_monitor = monitors.NullMonitor()
 
   if endpoint.startswith('file://'):
diff --git a/ts_mon/test/config_test.py b/ts_mon/test/config_test.py
index 6b5d203..9bef02f 100644
--- a/ts_mon/test/config_test.py
+++ b/ts_mon/test/config_test.py
@@ -304,6 +304,13 @@
     with self.assertRaises(SystemExit):
       config.process_argparse_options(args)
 
+  def test_metric_name_prefix(self):
+    p = argparse.ArgumentParser()
+    config.add_argparse_options(p)
+    args = p.parse_args(['--ts-mon-metric-name-prefix', '/test/random/'])
+    config.process_argparse_options(args)
+    self.assertEqual('/test/random/', interface.state.metric_name_prefix)
+
   @mock.patch('infra_libs.ts_mon.common.monitors.NullMonitor', autospec=True)
   def test_no_args(self, fake_monitor):
     singleton = mock.Mock()
@@ -313,6 +320,7 @@
     args = p.parse_args([])
     config.process_argparse_options(args)
     self.assertEqual(1, len(fake_monitor.mock_calls))
+    self.assertEqual('/chrome/infra/', interface.state.metric_name_prefix)
     self.assertIs(interface.state.global_monitor, singleton)
 
   @mock.patch('requests.get', autospec=True)