Skip to content

Commit

Permalink
analytics: support command and test-bot analytics.
Browse files Browse the repository at this point in the history
These are used to analyse which commands are used and the
success/failure rate of official taps using `brew test-bot`.
  • Loading branch information
MikeMcQuaid committed Mar 7, 2024
1 parent 9259c34 commit fd9ceb0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Library/Homebrew/brew.rb
Expand Up @@ -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(T.must(cmd), args)
Homebrew.send Commands.method_name(cmd)
elsif Commands.external_ruby_v2_cmd_path(cmd)
Utils::Analytics.report_command_run(T.must(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)
Expand Down
32 changes: 32 additions & 0 deletions Library/Homebrew/utils/analytics.rb
Expand Up @@ -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

Check warning on line 108 in Library/Homebrew/utils/analytics.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/analytics.rb#L108

Added line #L108 was not covered by tests

# Tags must have low cardinality.
tags = {}

Check warning on line 111 in Library/Homebrew/utils/analytics.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/analytics.rb#L111

Added line #L111 was not covered by tests

# Fields can have high cardinality.
fields = { command:, args: }

Check warning on line 114 in Library/Homebrew/utils/analytics.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/analytics.rb#L114

Added line #L114 was not covered by tests

report_influx(:command_run, tags, fields)

Check warning on line 116 in Library/Homebrew/utils/analytics.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/analytics.rb#L116

Added line #L116 was not covered by tests
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: }

Check warning on line 128 in Library/Homebrew/utils/analytics.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/analytics.rb#L128

Added line #L128 was not covered by tests

# Fields can have high cardinality.
fields = { command: }

Check warning on line 131 in Library/Homebrew/utils/analytics.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/analytics.rb#L131

Added line #L131 was not covered by tests

report_influx(:test_bot_test, tags, fields)

Check warning on line 133 in Library/Homebrew/utils/analytics.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/analytics.rb#L133

Added line #L133 was not covered by tests
end

def influx_message_displayed?
config_true?(:influxanalyticsmessage)
end
Expand Down
5 changes: 4 additions & 1 deletion docs/Analytics.md
Expand Up @@ -8,14 +8,15 @@ 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?

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.
Expand All @@ -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.

Expand Down

0 comments on commit fd9ceb0

Please sign in to comment.