blob: 8d83ea0ca42d675cc57c59589dcaef736727d687 [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
Kevin Cheng6001db32018-10-23 12:34:20 -070017
18from acloud.internal import constants
Kevin Cheng6001db32018-10-23 12:34:20 -070019
herbertxue1512f8a2019-06-27 13:56:23 +080020
Kevin Cheng6001db32018-10-23 12:34:20 -070021logger = logging.getLogger(__name__)
22
herbertxue1512f8a2019-06-27 13:56:23 +080023
Sam Chiud09787a2019-07-08 20:45:07 +080024# pylint: disable=broad-except, import-error
Sam Chiue791f602019-05-03 15:18:10 +080025def LogUsage(argv):
26 """Log acloud start event.
Kevin Cheng6001db32018-10-23 12:34:20 -070027
Sam Chiue791f602019-05-03 15:18:10 +080028 Log acloud start event and the usage, following are the data we log:
29 - tool_name: All asuite tools are storing event in the same database. This
30 property is provided to distinguish different tools.
31 - command_line: Log all command arguments.
32 - test_references: Should be a list, we record the acloud sub-command.
33 e.g. create/delete/reconnect/..etc. We could use this property as filter
34 criteria to speed up query time.
35 - cwd: User's current working directory.
36 - os: The platform that users are working at.
37
38 Args:
39 argv: A list of system arguments.
40 """
Sam Chiue791f602019-05-03 15:18:10 +080041 try:
42 from asuite import atest_utils
43 from asuite.metrics import metrics_utils
44 atest_utils.print_data_collection_notice()
45 metrics_utils.send_start_event(tool_name=constants.TOOL_NAME,
46 command_line=' '.join(argv),
47 test_references=[argv[0]])
48 except Exception as e:
49 logger.debug("Failed to send start event:%s", str(e))
Kevin Cheng6001db32018-10-23 12:34:20 -070050
51
Sam Chiue791f602019-05-03 15:18:10 +080052# pylint: disable=broad-except
53def LogExitEvent(exit_code, stacktrace="", logs=""):
54 """Log acloud exit event.
55
56 A start event should followed by an exit event to calculate the consuming
57 time. This function will be run at the end of acloud main process or
58 at the init of the error object.
59
60 Args:
61 exit_code: Integer, the exit code of acloud main process.
62 stacktrace: A string of stacktrace.
63 logs: A string of logs.
64 """
65 try:
66 from asuite.metrics import metrics_utils
67 metrics_utils.send_exit_event(exit_code, stacktrace=stacktrace,
68 logs=logs)
69 except Exception as e:
70 logger.debug("Failed to send exit event:%s", str(e))