-
Notifications
You must be signed in to change notification settings - Fork 9
Implement App size metrics actions #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
12c7494
Introduce AppSizeMetricsHelper
AliSoftware 62ecdf7
ios_send_app_size_metrics action + specs
AliSoftware 323e622
android_send_app_size_metrics action + specs
AliSoftware f1fc01a
Add CHANGELOG entry
AliSoftware 49e0c21
Rename `api_base_url` to `api_url`
AliSoftware 81ae374
Don't remove the `base-` prefix from the split name
AliSoftware f804caf
Use UI.success instead of UI.message
AliSoftware 41b6e8e
Apply suggestions from code review
AliSoftware b447780
FIx comment mismatching code
AliSoftware a3660c8
Switch code style for meta `nil` removal
AliSoftware fc90b31
Rename module namespace for helper
AliSoftware e8acd9d
Rename `meta` to `metadata` in helper API
AliSoftware ad5935c
Update logic for finding (or provide) apkanalyzer
AliSoftware b3f47b4
Merge branch 'trunk' into app-size-metrics
AliSoftware File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
lib/fastlane/plugin/wpmreleasetoolkit/actions/android/android_send_app_size_metrics.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
require_relative '../../helper/app_size_metrics_helper' | ||
|
||
module Fastlane | ||
module Actions | ||
class AndroidSendAppSizeMetricsAction < Action | ||
def self.run(params) | ||
# Check input parameters | ||
api_url = URI(params[:api_url]) | ||
api_token = params[:api_token] | ||
if (api_token.nil? || api_token.empty?) && !api_url.is_a?(URI::File) | ||
UI.user_error!('An API token is required when using an `api_url` with a scheme other than `file://`') | ||
end | ||
|
||
# Build the payload base | ||
metrics_helper = Fastlane::WPMRT::AppSizeMetricsHelper.new( | ||
Platform: 'Android', | ||
'App Name': params[:app_name], | ||
'App Version': params[:app_version_name], | ||
'Version Code': params[:app_version_code], | ||
'Product Flavor': params[:product_flavor], | ||
'Build Type': params[:build_type], | ||
Source: params[:source] | ||
) | ||
metrics_helper.add_metric(name: 'AAB File Size', value: File.size(params[:aab_path])) | ||
|
||
# Add device-specific 'splits' metrics to the payload if a `:include_split_sizes` is enabled | ||
if params[:include_split_sizes] | ||
check_bundletool_installed! | ||
apkanalyzer_bin = find_apkanalyzer_binary! | ||
UI.message("[App Size Metrics] Generating the various APK splits from #{params[:aab_path]}...") | ||
Dir.mktmpdir('release-toolkit-android-app-size-metrics') do |tmp_dir| | ||
Action.sh('bundletool', 'build-apks', '--bundle', params[:aab_path], '--output-format', 'DIRECTORY', '--output', tmp_dir) | ||
apks = Dir.glob('splits/*.apk', base: tmp_dir).map { |f| File.join(tmp_dir, f) } | ||
UI.message("[App Size Metrics] Generated #{apks.length} APKs.") | ||
|
||
apks.each do |apk| | ||
UI.message("[App Size Metrics] Computing file and download size of #{File.basename(apk)}...") | ||
split_name = File.basename(apk, '.apk').delete_prefix('base-') | ||
file_size = Action.sh(apkanalyzer_bin, 'apk', 'file-size', apk, print_command: false, print_command_output: false).chomp.to_i | ||
download_size = Action.sh(apkanalyzer_bin, 'apk', 'download-size', apk, print_command: false, print_command_output: false).chomp.to_i | ||
metrics_helper.add_metric(name: 'APK File Size', value: file_size, meta: { split: split_name }) | ||
metrics_helper.add_metric(name: 'Download Size', value: download_size, meta: { split: split_name }) | ||
end | ||
|
||
UI.message('[App Size Metrics] Done computing splits sizes.') | ||
end | ||
end | ||
|
||
# Send the payload | ||
metrics_helper.send_metrics( | ||
to: api_url, | ||
api_token: api_token, | ||
use_gzip: params[:use_gzip_content_encoding] | ||
) | ||
end | ||
|
||
def self.check_bundletool_installed! | ||
Action.sh('command', '-v', 'bundletool', print_command: false, print_command_output: false) | ||
rescue StandardError | ||
UI.user_error!('bundletool is required to build the split APKs. Install it with `brew install bundletool`') | ||
raise | ||
end | ||
|
||
def self.find_apkanalyzer_binary! | ||
sdk_root = ENV['ANDROID_SDK_ROOT'] || ENV['ANDROID_HOME'] | ||
apkanalyzer_bin = sdk_root.nil? ? Action.sh('command', '-v', 'apkanalyzer') : File.join(sdk_root, 'cmdline-tools', 'latest', 'bin', 'apkanalyzer') | ||
UI.user_error!('Unable to find apkanalyzer executable. Make sure you installed the Android SDK Command-line Tools') unless File.executable?(apkanalyzer_bin) | ||
apkanalyzer_bin | ||
end | ||
|
||
##################################################### | ||
# @!group Documentation | ||
##################################################### | ||
|
||
def self.description | ||
'Send Android app size metrics to our metrics server' | ||
end | ||
|
||
def self.details | ||
<<~DETAILS | ||
Send Android app size metrics to our metrics server. | ||
|
||
See https://github.com/Automattic/apps-metrics for the API contract expected by the Metrics server you will send those metrics to. | ||
|
||
Tip: If you provide a `file://` URL for the `api_url`, the action will write the payload on disk at the specified path instead of sending | ||
the data to a endpoint over network. This can be useful e.g. to inspect the payload and debug it, or to store the metrics data as CI artefacts. | ||
DETAILS | ||
end | ||
|
||
def self.available_options | ||
[ | ||
FastlaneCore::ConfigItem.new( | ||
key: :api_url, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_API_URL', | ||
description: 'The endpoint API URL to publish metrics to. (Note: you can also point to a `file://` URL to write the payload to a file instead)', | ||
type: String, | ||
optional: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :api_token, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_API_TOKEN', | ||
description: 'The bearer token to call the API. Required, unless `api_url` is a `file://` URL', | ||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :use_gzip_content_encoding, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_USE_GZIP_CONTENT_ENCODING', | ||
description: 'Specify that we should use `Content-Encoding: gzip` and gzip the body when sending the request', | ||
type: FastlaneCore::Boolean, | ||
default_value: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :app_name, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_APP_NAME', | ||
description: 'The name of the app for which we are publishing metrics, to help filter by app in the dashboard', | ||
type: String, | ||
optional: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :app_version_name, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_APP_VERSION_NAME', | ||
description: 'The version name of the app for which we are publishing metrics, to help filter by version in the dashboard', | ||
type: String, | ||
optional: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :app_version_code, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_APP_VERSION_CODE', | ||
description: 'The version code of the app for which we are publishing metrics, to help filter by version in the dashboard', | ||
type: Integer, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :product_flavor, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_PRODUCT_FLAVOR', | ||
description: 'The product flavor for which we are publishing metrics, to help filter by flavor in the dashboard. E.g. `Vanilla`, `Jalapeno`, `Wasabi`', | ||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :build_type, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_BUILD_TYPE', | ||
description: 'The build type for which we are publishing metrics, to help filter by build type in the dashboard. E.g. `Debug`, `Release`', | ||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :source, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_SOURCE', | ||
description: 'The type of event at the origin of that build, to help filter data in the dashboard. E.g. `pr`, `beta`, `final-release`', | ||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :aab_path, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_AAB_PATH', | ||
description: 'The path to the .aab to extract size information from', | ||
type: String, | ||
optional: false, | ||
verify_block: proc do |value| | ||
UI.user_error!('You must provide an path to an existing `.aab` file') unless File.exist?(value) | ||
end | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :include_split_sizes, | ||
env_name: 'FL_ANDROID_SEND_APP_SIZE_METRICS_INCLUDE_SPLIT_SIZES', | ||
description: 'Indicate if we should use `bundletool` and `apkanalyzer` to also compute and send "split apk" sizes per architecture. ' \ | ||
+ 'Setting this to `true` adds a bit of extra time to generate the `.apk` and extract the data, but provides more detailed metrics', | ||
type: FastlaneCore::Boolean, | ||
default_value: true | ||
), | ||
] | ||
end | ||
|
||
def self.return_type | ||
:integer | ||
end | ||
|
||
def self.return_value | ||
'The HTTP return code from the call. Expect a 201 when new metrics were received successfully and entries created in the database' | ||
end | ||
|
||
def self.authors | ||
['automattic'] | ||
end | ||
|
||
def self.is_supported?(platform) | ||
platform == :android | ||
end | ||
end | ||
end | ||
end |
165 changes: 165 additions & 0 deletions
165
lib/fastlane/plugin/wpmreleasetoolkit/actions/ios/ios_send_app_size_metrics.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
require 'plist' | ||
require_relative '../../helper/app_size_metrics_helper' | ||
|
||
module Fastlane | ||
module Actions | ||
class IosSendAppSizeMetricsAction < Action | ||
def self.run(params) | ||
# Check input parameters | ||
api_url = URI(params[:api_url]) | ||
api_token = params[:api_token] | ||
if (api_token.nil? || api_token.empty?) && !api_url.is_a?(URI::File) | ||
UI.user_error!('An API token is required when using an `api_url` with a scheme other than `file://`') | ||
end | ||
|
||
# Build the payload base | ||
metrics_helper = Fastlane::WPMRT::AppSizeMetricsHelper.new( | ||
Platform: 'iOS', | ||
'App Name': params[:app_name], | ||
'App Version': params[:app_version], | ||
'Build Type': params[:build_type], | ||
Source: params[:source] | ||
) | ||
metrics_helper.add_metric(name: 'File Size', value: File.size(params[:ipa_path])) | ||
|
||
# Add app-thinning metrics to the payload if a `.plist` is provided | ||
app_thinning_plist_path = params[:app_thinning_plist_path] || File.join(File.dirname(params[:ipa_path]), 'app-thinning.plist') | ||
if File.exist?(app_thinning_plist_path) | ||
plist = Plist.parse_xml(app_thinning_plist_path) | ||
plist['variants'].each do |_key, variant| | ||
variant_descriptors = variant['variantDescriptors'] || [{ 'device' => 'Universal' }] | ||
variant_descriptors.each do |desc| | ||
variant_metadata = { device: desc['device'], 'OS Version': desc['os-version'] } | ||
metrics_helper.add_metric(name: 'Download Size', value: variant['sizeCompressedApp'], meta: variant_metadata) | ||
metrics_helper.add_metric(name: 'Install Size', value: variant['sizeUncompressedApp'], meta: variant_metadata) | ||
end | ||
end | ||
end | ||
|
||
# Send the payload | ||
metrics_helper.send_metrics( | ||
to: api_url, | ||
api_token: api_token, | ||
use_gzip: params[:use_gzip_content_encoding] | ||
) | ||
end | ||
|
||
##################################################### | ||
# @!group Documentation | ||
##################################################### | ||
|
||
def self.description | ||
'Send iOS app size metrics to our metrics server' | ||
end | ||
|
||
def self.details | ||
<<~DETAILS | ||
Send iOS app size metrics to our metrics server. | ||
|
||
In order to get Xcode generate the `app-thinning.plist` file (during `gym` and the export of the `.xcarchive`), you need to: | ||
(1) Use either `ad-hoc`, `enterprise` or `development` export method (in particular, won't work with `app-store`), | ||
(2) Provide `thinning: '<thin-for-all-variants>'` as part of your `export_options` of `gym` (or in your `options.plist` file if you use raw `xcodebuild`) | ||
See https://help.apple.com/xcode/mac/11.0/index.html#/devde46df08a | ||
|
||
For builds exported with the `app-store` method, `xcodebuild` won't generate an `app-thinning.plist` file; so you will only be able to get | ||
the Universal `.ipa` file size as a metric, but won't get the per-device, broken-down install and download sizes for each thinned variant. | ||
|
||
See https://github.com/Automattic/apps-metrics for the API contract expected by the Metrics server you are expected to send those metrics to. | ||
|
||
Tip: If you provide a `file://` URL for the `api_url`, the action will write the payload on disk at the specified path instead of sending | ||
the data to a endpoint over network. This can be useful e.g. to inspect the payload and debug it, or to store the metrics data as CI artefacts. | ||
DETAILS | ||
end | ||
|
||
def self.available_options | ||
[ | ||
FastlaneCore::ConfigItem.new( | ||
key: :api_url, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_API_URL', | ||
description: 'The endpoint API URL to publish metrics to. (Note: you can also point to a `file://` URL to write the payload to a file instead)', | ||
type: String, | ||
optional: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :api_token, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_API_TOKEN', | ||
description: 'The bearer token to call the API. Required, unless `api_url` is a `file://` URL', | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :use_gzip_content_encoding, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_USE_GZIP_CONTENT_ENCODING', | ||
description: 'Specify that we should use `Content-Encoding: gzip` and gzip the body when sending the request', | ||
type: FastlaneCore::Boolean, | ||
default_value: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :app_name, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_APP_NAME', | ||
description: 'The name of the app for which we are publishing metrics, to help filter by app in the dashboard', | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: String, | ||
optional: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :app_version, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_APP_VERSION', | ||
description: 'The version of the app for which we are publishing metrics, to help filter by version in the dashboard', | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: String, | ||
optional: false | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :build_type, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_BUILD_TYPE', | ||
description: 'The build configuration for which we are publishing metrics, to help filter by build config in the dashboard. E.g. `Debug`, `Release`', | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :source, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_SOURCE', | ||
description: 'The type of event at the origin of that build, to help filter data in the dashboard. E.g. `pr`, `beta`, `final-release`', | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: String, | ||
optional: true | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :ipa_path, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_IPA_PATH', | ||
description: 'The path to the .ipa to extract size information from', | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: String, | ||
optional: false, | ||
default_value: Actions.lane_context[SharedValues::IPA_OUTPUT_PATH], | ||
verify_block: proc do |value| | ||
UI.user_error!('You must provide an path to an existing `.ipa` file') unless File.exist?(value) | ||
end | ||
), | ||
FastlaneCore::ConfigItem.new( | ||
key: :app_thinning_plist_path, | ||
env_name: 'FL_IOS_SEND_APP_SIZE_METRICS_APP_THINNING_PLIST_PATH', | ||
description: 'The path to the `app-thinning.plist` file to extract thinning size information from. ' \ | ||
+ 'By default, will try to use the `app-thinning.plist` file next to the `ipa_path`, if that file exists', | ||
type: String, | ||
optional: true, | ||
default_value_dynamic: true | ||
), | ||
] | ||
end | ||
|
||
def self.return_type | ||
:integer | ||
end | ||
|
||
def self.return_value | ||
'The HTTP return code from the call. Expect a 201 when new metrics were received successfully and entries created in the database' | ||
end | ||
|
||
def self.authors | ||
['automattic'] | ||
end | ||
|
||
def self.is_supported?(platform) | ||
[:ios, :mac].include? platform | ||
AliSoftware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered making the API token an
optional: false
parameter?From a consumer point of view, we don't expect users to call this action with a
file://
API URL, but always and only with anhttps://
URL plus a token. We could ignore the token in thefile://
case here, which is what thesend_metrics
method seems to do, too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I'm following your argument here 😅
Which is why it has to be
optional: true
, otherwise fastlane itself willuser_error!
all by itself if you provide afile://
URL but noapi_token
(and why would you provide an API token when you're usingfile://
URL, and what would that token be anyway… be it for debugging while we don't yet have a metrics server and thus any token to provide, or if you use this action to store the metrics as artefacts on your CI rather than sending them to a metrics server)So, yes, the
token
in thefile://
case is ignored here (that's exactly what those lines 11–13 are for, to ignore it and allow it to be optional provided… only if using a file URL, and be required only when using a non-file URL).Given it has a different optionality depending on the type of
api_url
used, fastlane won't let us useoptional: false
in theConfigItem
otherwise it will complain that it's missing regardless of if it was valid to omit it (withfile://
urls) or not. So the only way in custom fastlane actions to provide conditional optionality is to handle it ourselves, asConfigItem
does not provide a way for that directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I'm re-reading your comment and I get what you meant now (I was just missing some coffee earlier… ☕ ).
But in fact, even if in 80% of cases — especially once we'll have our Metrics server up and running at a stable URL — we will use a
https://
URL forapi_url
… there are still legitimate cases were usingfile://
is not the exception or the "secret" case but a genuine usage we still want to cover properly (instead of treating it as if it was an easter egg):So imho while it definitively won't be the majority of cases, those are still legitimate ones and as such should be considered more than just easter eggs and the optionality of
api_token
depending on it should be handled properly as a result 😛There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, cool. If that's the expectation, then it makes sense to keep the API consistent for both, rather than optimize it only for a scenario.
👍