blob: 5ec7027a77ea27d6265c19a98e31dc193cbf6462 [file] [log] [blame]
Kevin Cheng6001db32018-10-23 12:34:20 -07001# Copyright 2018 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14r"""Acloud metrics functions."""
15
16import logging
17import os
18import subprocess
19
20from acloud.internal import constants
21# pylint: disable=import-error
Kevin Cheng6001db32018-10-23 12:34:20 -070022
23_METRICS_URL = 'http://asuite-218222.appspot.com/acloud/metrics'
24_VALID_DOMAINS = ["google.com", "android.com"]
25_COMMAND_GIT_CONFIG = ["git", "config", "--get", "user.email"]
26
27logger = logging.getLogger(__name__)
28
29
30def LogUsage():
31 """Log acloud run."""
Erwin Jansen95559242018-11-08 15:38:18 -080032 try:
33 from asuite import asuite_metrics
34 asuite_metrics.log_event(_METRICS_URL, dummy_key_fallback=False,
35 ldap=_GetLdap())
36 except ImportError:
37 logger.debug("No metrics recorder available, not sending metrics.")
38
Kevin Cheng6001db32018-10-23 12:34:20 -070039
40
41def _GetLdap():
42 """Return string email username for valid domains only, None otherwise."""
43 try:
44 acloud_project = os.path.join(
45 os.environ[constants.ENV_ANDROID_BUILD_TOP], "tools", "acloud")
46 email = subprocess.check_output(_COMMAND_GIT_CONFIG,
47 cwd=acloud_project).strip()
48 ldap, domain = email.split("@", 2)
49 if domain in _VALID_DOMAINS:
50 return ldap
51 # pylint: disable=broad-except
52 except Exception as e:
53 logger.debug("error retrieving email: %s", e)
54 return None