blob: a323f2f3a57507831a0eca9673e504f2e3f8c925 [file] [log] [blame] [edit]
#!/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.
##
## 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)
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 builds will upload anonymized remote build logs
to collect valuable build performance data.
1. For every "fx build" invocation that uses remote building (RBE):
- Reproxy logs containing details about every remote build action,
e.g. timing breakdown, cache hit/miss, statuses, internal errors.
Each build can be identified by a UUID (not traceable to any user),
but can be voluntarily referenced in bug reports and triage sessions.
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)
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"
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="$?"
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"
;;
*)
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}"