From ecf962a13c68fe52cb1dc1818e3ef76afc5d1613 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 7 Mar 2024 15:23:54 +0000 Subject: [PATCH] analytics: support command and test-bot analytics. These are used to analyse which commands are used and the success/failure rate of official taps using `brew test-bot`. --- Library/Homebrew/brew.rb | 6 +++++- Library/Homebrew/utils/analytics.rb | 32 +++++++++++++++++++++++++++++ docs/Analytics.md | 5 ++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 78b51cb55ce844..dc8d4e9255bfcf 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -82,7 +82,11 @@ # `Homebrew::Help.help` never returns, except for unknown commands. end - if internal_cmd || Commands.external_ruby_v2_cmd_path(cmd) + if internal_cmd + Utils::Analytics.report_command_run(cmd, args) + Homebrew.send Commands.method_name(cmd) + elsif Commands.external_ruby_v2_cmd_path(cmd) + Utils::Analytics.report_command_run(cmd, args) if OFFICIAL_CMD_TAPS.values.flatten.include?(cmd) Homebrew.send Commands.method_name(cmd) elsif (path = Commands.external_ruby_cmd_path(cmd)) require?(path) diff --git a/Library/Homebrew/utils/analytics.rb b/Library/Homebrew/utils/analytics.rb index 2e7d7b323a3a66..d98729a976ec82 100644 --- a/Library/Homebrew/utils/analytics.rb +++ b/Library/Homebrew/utils/analytics.rb @@ -101,6 +101,38 @@ def report_build_error(exception) report_package_event(:build_error, package_name: formula.name, tap_name: tap.name, options: options) end + sig { params(command: String, cli_args: Homebrew::CLI::Args).void } + def report_command_run(command, cli_args) + return if not_this_run? || disabled? + + args = cli_args.remaining.sort.uniq.to_a + + # Tags must have low cardinality. + tags = {} + + # Fields can have high cardinality. + fields = { command:, args: } + + report_influx(:command_run, tags, fields) + end + + sig { params(command: String, passed: T::Boolean).void } + def report_test_bot_test(command, passed) + return if not_this_run? || disabled? + return if ENV["HOMEBREW_TEST_BOT_ANALYTICS"].blank? + + # ensure passed is a boolean + passed = passed ? true : false + + # Tags must have low cardinality. + tags = { passed: } + + # Fields can have high cardinality. + fields = { command: } + + report_influx(:test_bot_test, tags, fields) + end + def influx_message_displayed? config_true?(:influxanalyticsmessage) end diff --git a/docs/Analytics.md b/docs/Analytics.md index 49d5729bd34294..d233072fb50c28 100644 --- a/docs/Analytics.md +++ b/docs/Analytics.md @@ -8,6 +8,7 @@ Homebrew is provided free of charge and run entirely by volunteers in their spar - If a formula is widely used and is failing often it will enable us to prioritise fixing that formula over others. - Collecting the OS version allows us to decide which versions of macOS to prioritise for support and identify build failures that occur only on single versions. +- If a command is not widely used, it can be deprecated. ## How Long? @@ -15,7 +16,7 @@ Homebrew's anonymous analytics has a 365 day retention period in InfluxDB. ## What? -Homebrew's analytics record some shared information for every event: +Homebrew's analytics record some shared information for every formula or cask event: - Whether the data is being sent from CI, e.g. `true` if the CI environment variable is set. - Whether you are using the default install prefix (e.g. `/opt/homebrew`) or a custom one (e.g. `/home/mike/.brew`). If your prefix is custom, it will be sent as `custom-prefix` to preserve anonymity. @@ -33,6 +34,8 @@ Homebrew's analytics records the following different events: - The `install_on_request` event category and the Homebrew formula from a non-private GitHub tap you have requested to install (e.g. when explicitly named with a `brew install`) plus options. This allows us to differentiate the formulae that users intend to install from those pulled in as dependencies. - The `cask_install` event category and the Homebrew cask from a non-private GitHub tap you install as the action. This allows us to identify which casks where work should be prioritised, as well as how to handle possible deprecation or removal of any. - The `build_error` event category and the Homebrew formula plus options that failed to install as the action, e.g. `wget --HEAD`. This allows us to identify formulae that may need fixing. The details or logs of the build error are not sent. +- The `command_run` event category and the command and flags you run as the action. This allows us to identify which commands and flags are most commonly used and which are not used at all. +- The `test_bot_test` event category is only used in Homebrew's CI environment and is used to record the results of running tests on pull requests. You can also view all the information that is sent by Homebrew's analytics by setting `HOMEBREW_ANALYTICS_DEBUG=1` in your environment. Please note this will also stop any analytics from being sent.