Merge "Add in simple metrics for acloud."
diff --git a/Android.bp b/Android.bp
index 843fcba..25437d2 100644
--- a/Android.bp
+++ b/Android.bp
@@ -41,9 +41,10 @@
     libs: [
         "acloud_create",
         "acloud_delete",
-        "acloud_public",
         "acloud_internal",
+	"acloud_metrics",
         "acloud_proto",
+        "acloud_public",
         "acloud_setup",
         "py-apitools",
         "py-dateutil",
@@ -146,3 +147,14 @@
          "delete/*.py",
     ],
 }
+
+python_library_host{
+    name: "acloud_metrics",
+    defaults: ["acloud_default"],
+    srcs: [
+         "metrics/*.py",
+    ],
+    libs: [
+         "asuite_metrics",
+    ],
+}
diff --git a/create/avd_spec.py b/create/avd_spec.py
index f062cca..532baf0 100644
--- a/create/avd_spec.py
+++ b/create/avd_spec.py
@@ -285,7 +285,7 @@
             remote: String, git remote (e.g. "aosp").
         """
         try:
-            android_build_top = os.environ[_ENV_ANDROID_BUILD_TOP]
+            android_build_top = os.environ[constants.ENV_ANDROID_BUILD_TOP]
         except KeyError:
             raise errors.GetAndroidBuildEnvVarError(
                 "Could not get environment var: %s\n"
diff --git a/metrics/__init__.py b/metrics/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/metrics/__init__.py
diff --git a/metrics/metrics.py b/metrics/metrics.py
new file mode 100644
index 0000000..87b439e
--- /dev/null
+++ b/metrics/metrics.py
@@ -0,0 +1,50 @@
+# Copyright 2018 - The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+r"""Acloud metrics functions."""
+
+import logging
+import os
+import subprocess
+
+from acloud.internal import constants
+# pylint: disable=import-error
+from asuite import asuite_metrics
+
+_METRICS_URL = 'http://asuite-218222.appspot.com/acloud/metrics'
+_VALID_DOMAINS = ["google.com", "android.com"]
+_COMMAND_GIT_CONFIG = ["git", "config", "--get", "user.email"]
+
+logger = logging.getLogger(__name__)
+
+
+def LogUsage():
+    """Log acloud run."""
+    asuite_metrics.log_event(_METRICS_URL, dummy_key_fallback=False,
+                             ldap=_GetLdap())
+
+
+def _GetLdap():
+    """Return string email username for valid domains only, None otherwise."""
+    try:
+        acloud_project = os.path.join(
+            os.environ[constants.ENV_ANDROID_BUILD_TOP], "tools", "acloud")
+        email = subprocess.check_output(_COMMAND_GIT_CONFIG,
+                                        cwd=acloud_project).strip()
+        ldap, domain = email.split("@", 2)
+        if domain in _VALID_DOMAINS:
+            return ldap
+    # pylint: disable=broad-except
+    except Exception as e:
+        logger.debug("error retrieving email: %s", e)
+    return None
diff --git a/public/acloud_main.py b/public/acloud_main.py
index e320ea6..d449cea 100644
--- a/public/acloud_main.py
+++ b/public/acloud_main.py
@@ -57,17 +57,18 @@
 OAUTH2_LOGGER.addHandler(logging.FileHandler("/dev/null"))
 
 # pylint: disable=wrong-import-position
+from acloud.create import create
+from acloud.create import create_args
+from acloud.delete import delete
+from acloud.delete import delete_args
 from acloud.internal import constants
+from acloud.metrics import metrics
 from acloud.public import acloud_common
 from acloud.public import config
 from acloud.public import device_driver
 from acloud.public import errors
 from acloud.public.actions import create_cuttlefish_action
 from acloud.public.actions import create_goldfish_action
-from acloud.create import create
-from acloud.create import create_args
-from acloud.delete import delete
-from acloud.delete import delete_args
 from acloud.setup import setup
 from acloud.setup import setup_args
 
@@ -390,6 +391,7 @@
     # Check access.
     # device_driver.CheckAccess(cfg)
 
+    metrics.LogUsage()
     report = None
     if (args.which == create_args.CMD_CREATE
             and args.avd_type == constants.TYPE_GCE):