• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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
17
18from acloud.internal import constants
19_NO_METRICS = "--no-metrics"
20_NO_METRICS_COMMANDS = ["delete"]
21
22
23logger = logging.getLogger(__name__)
24
25
26# pylint: disable=broad-except, import-error
27def LogUsage(argv):
28    """Log acloud start event.
29
30    Log acloud start event and the usage, following are the data we log:
31    - tool_name: All asuite tools are storing event in the same database. This
32      property is provided to distinguish different tools.
33    - command_line: Log all command arguments.
34    - test_references: Should be a list, we record the acloud sub-command.
35      e.g. create/delete/reconnect/..etc. We could use this property as filter
36      criteria to speed up query time.
37    - cwd: User's current working directory.
38    - os: The platform that users are working at.
39
40    Args:
41        argv: A list of system arguments.
42
43    Returns:
44        True if start event is sent and need to pair with end event.
45    """
46    if _NO_METRICS in argv:
47        return False
48    if len(argv) > 0 and argv[0] in _NO_METRICS_COMMANDS:
49        return False
50
51    try:
52        from asuite.metrics import metrics_utils
53        metrics_utils.print_data_collection_notice()
54        metrics_utils.send_start_event(tool_name=constants.TOOL_NAME,
55                                       command_line=' '.join(argv),
56                                       test_references=[argv[0]])
57        return True
58    except Exception as e:
59        logger.debug("Failed to send start event:%s", str(e))
60
61    return False
62
63
64# pylint: disable=broad-except
65def LogExitEvent(exit_code, stacktrace="", logs=""):
66    """Log acloud exit event.
67
68    A start event should followed by an exit event to calculate the consuming
69    time. This function will be run at the end of acloud main process or
70    at the init of the error object.
71
72    Args:
73        exit_code: Integer, the exit code of acloud main process.
74        stacktrace: A string of stacktrace.
75        logs: A string of logs.
76    """
77    try:
78        from asuite.metrics import metrics_utils
79        metrics_utils.send_exit_event(exit_code, stacktrace=stacktrace,
80                                      logs=logs)
81    except Exception as e:
82        logger.debug("Failed to send exit event:%s", str(e))
83