blob: e8877b727c15c9645b325021a42d35fca9c3d0cd [file] [log] [blame]
#!/bin/bash
# Copyright 2022 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Other
### manage build metrics configuration
## usage:
## fx build-metrics status
## Show if build metrics collection is enabled or disabled
##
## fx build-metrics enable
## Enable build metrics collection in fx build.
##
## fx build-metrics disable
## Disable build metrics collection in fx build.
##
## fx build-metrics login
## Authenticate to be able to write data to BQ.
##
## fx build-metrics upload
## Upload any logs/metrics that may have accumulated.
##
## If you opt-in to metrics collection, data will be collected and used by
## Google in accordance with the Google Privacy Policy
## (https://policies.google.com/privacy)
## and Employee Privacy Policy (go/epp).
set -e
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
source "${FUCHSIA_DIR}/tools/devshell/lib/style.sh" || exit $?
readonly BUILD_METRICS_CONFIG="${FUCHSIA_DIR}/.fx-build-metrics-config"
function usage {
show-what-is-collected
fx-command-help
}
function show-what-is-collected {
cat <<EOF
When enabled, 'fx build's with RBE enabled will upload remote execution logs
and metrics, including:
1. Details about every remote build action: inputs, command, outputs,
event timestamps, cache hit/miss, statuses, internal errors.
2. Environment variables and tool parameters, that can include usernames
in paths.
3. Aggregate statistics about remote build actions.
4. Host machine information, such as architecture and operating system.
This data is used for performance analytics, bug reports, and triage sessions.
This data is only accessible to the Fuchsia and RBE teams.
EOF
}
function describe-privacy-policy {
cat << EOF
If you opt-in to logs collection, data will be collected and used by Google
in accordance with the Google Privacy Policy
(https://policies.google.com/privacy)
and Employee Privacy Policy (go/epp).
EOF
}
function styled-status {
local enabled="$1"
local metrics_status
if [[ "$BUILD_METRICS_ENABLED" -eq 1 ]]; then
metrics_status="enabled"
color="green"
else
metrics_status="disabled"
color="red"
fi
style::echo --bold "--${color}" "${metrics_status}"
}
fx-standard-switches "$@"
set -- "${FX_ARGV[@]}"
if [[ $# -lt 1 ]]; then
fx-error Invalid syntax
fx-command-help
exit 1
fi
action="$1"
shift
action_args=("$@")
function build-metrics-read-config {
BUILD_METRICS_ENABLED=0
if [[ ! -f "${BUILD_METRICS_CONFIG}" ]]; then
return 1
fi
source "${BUILD_METRICS_CONFIG}"
return 0
}
function build-metrics-write-config {
local enabled="$1"
local -r tempfile="${BUILD_METRICS_CONFIG}.tmp"
cat > "${tempfile}" <<EOF
# Autogenerated config file for 'fx build-metrics'.
# Run 'fx help build-metrics' for more information.
BUILD_METRICS_ENABLED=${enabled}
EOF
# Only rewrite the config file if content has changed
if ! cmp --silent "${tempfile}" "${BUILD_METRICS_CONFIG}"
then mv -f "${tempfile}" "${BUILD_METRICS_CONFIG}"
else rm -f "${tempfile}"
fi
}
# Read config in a way that exit code is preserved but doesn't exit because
# of "set -e" if config file doesn't exist
__successfully_read_config=0
build-metrics-read-config || __successfully_read_config="$?"
readonly _upload_script=build/rbe/upload_reproxy_logs.sh
case "$action" in
-h|--help)
usage
exit 0
;;
status)
echo "Collection of build metrics is currently $(styled-status "$BUILD_METRICS_ENABLED") for ${FUCHSIA_DIR}"
echo "To change it, run: fx build-metrics <enable|disable>"
if [[ "$BUILD_METRICS_ENABLED" -eq 1 ]]; then
echo
show-what-is-collected
fi
exit 0
;;
enable)
describe-privacy-policy
if [[ "$BUILD_METRICS_ENABLED" -eq 1 ]]; then
echo "Build metrics collection is already enabled."
exit 0
fi
uuidgen_cmd=uuidgen
if ! command -v "$uuidgen_cmd" >/dev/null 2>&1 ; then
fx-error "Command '$uuidgen_cmd' cannot be found, please add it to your PATH."\
"(On Ubuntu/Debian systems, try \`sudo apt install uuid-runtime\`.)"
exit 1
fi
# We don't need to generate a UUID until `fx build` time,
# specifically, when build/rbe/fuchsia-reproxy-wrap.sh is invoked.
# The python scripts that work with logs need protobuf code to
# be generated in advance. This includes building protoc.
_setup_script=build/rbe/proto/refresh.sh
echo "Fetching and generating necessary protobuf code (may take a few minutes)."
"${FUCHSIA_DIR}"/"$_setup_script" > /dev/null || {
fx-error "Re-run $_setup_script manually to look for errors."
exit 1
}
BUILD_METRICS_ENABLED=1
build-metrics-write-config "${BUILD_METRICS_ENABLED}"
echo "Build metrics collection enabled at user's request"
;;
disable)
if [[ "$BUILD_METRICS_ENABLED" -eq 0 ]]; then
if [[ "${__successfully_read_config}" -ne 0 ]]; then
# if metrics is already disabled but the config file does not exist or
# is inconsistent, we rewrite the file before exiting
build-metrics-write-config "${BUILD_METRICS_ENABLED}"
fi
echo "Build metrics collection is already disabled."
exit 0
fi
BUILD_METRICS_ENABLED=0
build-metrics-write-config "${BUILD_METRICS_ENABLED}"
echo "Build metrics collection disabled at user's request"
;;
login)
echo "Logging in to upload metrics and logs to BQ."
"$_upload_script" --auth-only
exit "$?"
;;
upload)
echo "Uploading accumulated build logs and metrics to BQ."
# Does not require that build-metrics be enabled in advance because
# the logs and metrics files are always being produced.
# Scan for log dirs where build/rbe/fuchsia-reproxy-wrap.sh produces them.
_logs_dir="${FUCHSIA_DIR}"/out/.reproxy_logs
"$_upload_script" "${action_args[@]}" "$_logs_dir"/*/reproxy.*
exit 0
;;
*)
fx-error Invalid syntax
fx-command-help
exit 1
;;
esac
echo "Collection of build metrics is now $(styled-status "$BUILD_METRICS_ENABLED") for ${FUCHSIA_DIR}"