为您的 Rails 应用程序收集性能配置文件。
Profiling is critical to providing an understanding of an application's performance.
AppProfiler aims to provide a common framework for performance profiling for Rails applications.
To install app_profiler you need to include it in your Gemfile.
app_profiler requires Ruby 3.2.1 or newer.
This gem automatically injects the AppProfiler::Middleware middleware into your Rails application.
This middleware can be disabled by using:
AppProfiler.middleware.disabled = true
# OR
Rails.application.config.app_profiler.middleware_disabled = trueProfiling can be triggered in one of two ways:
profile key in the query string of the URL./?[key=value]&...X-Profile key in the request headers.X-Profile header format: [=];...If async query string is provided, then the profile will be uploaded later, in an async manner. One use case for this is when we want to profile a certain % of traffic without incurring costs of inline profile uploads. Async background processing provides three callbacks:
upload_queue_max_length exceeded.These callbacks can be configured the following manner:
AppProfiler.profile_enqueue_success = ->() { StatsD.increment("profile_enqueue_success") }
# OR
Rails.application.config.app_profiler.profile_enqueue_success = ->() { StatsD.increment("profile_enqueue_success") }AppProfiler.profile_enqueue_failure = ->(profile) { Rails.logger.info("Profile #{profile.inspect} could not be enqueued.") }
# OR
Rails.application.config.app_profiler.profile_enqueue_failure = ->(profile) { Rails.logger.info("Profile #{profile.inspect} could not be enqueued.")}AppProfiler.after_process_queue = ->(num_success, num_failures) { StatsD.gauge("async_profile_upload", tags: { sucessful: num_success, failed: num_failures} )}
# OR
Rails.application.config.app_profiler.after_process_queue = ->(num_success, num_failures) { StatsD.gauge("async_profile_upload", tags: { sucessful: num_success, failed: num_failures} )}You can configure the profile header using:
AppProfiler.profile_header = "X-Profile"
# OR
Rails.application.config.app_profiler.profile_header = "X-Profile"You can configure the profile query parameter using
AppProfiler.profile_param = "x_profile"
# OR
Rails.application.config.app_profiler.profile_param = "x_profile"/?profile=cpu&interval=2000&autoredirect=1&ignore_gc=1X-Profile to mode=wall;interval=1000;context=test-directory;autoredirect=1| Key | Value | Notes |
|---|---|---|
| profile/mode | Supported profiling modes: cpu, wall, object for stackprof. |
Use profile in (1), and mode in (2). Vernier backend only supports wall and retained at present time. |
| async | Upload profile in a background thread. When this is set, profile redirect headers are not present in the response. | |
| interval | Sampling interval in microseconds. | |
| ignore_gc | Ignore garbage collection frames | |
| autoredirect | Redirect request automatically to Speedscope's page after profiling. | |
| context | Directory within the specified bucket in the selected storage where raw profile data should be written. | Only supported in (2). Defaults to Rails.env if not specified. |
| backend | Profiler to use, either stackprof or vernier. Defaults to stackprof. |
Note that the autoredirect feature can be turned on for all requests by doing the following:
AppProfiler.autoredirect = true
# OR
Rails.application.config.app_profiler.autoredirect = trueFile names of profiles are prefixed by default with timezoned date and time, follow by profile mode, an id, and hostname of the machine where it was capture. For example: 20221006-121110-cpu-613fa8d2cdde5820d5312dea1cfa43d9-macbook-pro-work.lan.json. To customize the prefix you can provide a proc:
AppProfiler.profile_file_prefix = -> { "custom-prefix" }
# OR
Rails.application.config.app_profiler.profile_file_prefix = -> { "custom-prefix" }
As opposed to profile_file_prefix, which is used to customize the prefix of a file name, profile_file_name is used to provide the actual file name. If both are provided, profile_file_name takes precedence.
AppProfiler.profile_file_name = ->(metadata) { "profile-#{metadata[:id]}-#{metadata[:hostname]}.json" }
# OR
Rails.application.config.app_profiler.profile_file_name = ->(metadata) { "profile-#{metadata[:id]}-#{metadata[:hostname]}.json" }To customize the redirect location you can provide a proc:
AppProfiler.profile_url_formatter = ->(upload) { "https://host.com/custom/#{upload.name}" }
# OR
Rails.application.config.app_profiler.profile_url_formatter = ->(upload) { "https://host.com/custom/#{upload.name}" }Apps using OpenTelemetry can add attributes to traces. This can be useful for correlating profiling data with traces.
To enable this feature, you can set otel_instrumentation_enabled to true. The gem does not require the opentelemetry gem to be installed. If the gem is not installed or if it is not loaded, enabling it will raise an ArgumentError. By default, the feature is disabled.
AppProfiler.otel_instrumentation_enabled = true
# OR
Rails.application.config.app_profiler.otel_instrumentation_enabled = trueWhen profiling is triggered, the middleware will generate the profile through StackProf and upload the profiles to your specified storage. For example, the default configuration would upload profiles to file storage.
When using a cloud storage provider, you can configure the target bucket name using:
AppProfiler.storage.bucket_name = "new-bucket-name"
# OR
Rails.application.config.app_profiler.storage_bucket_name = "new-bucket-name"You may restrict the storing of profiling results by defining your own Middleware based on AppProfiler::Middleware and changing the after_profile hook method to return false for such cases.
For example, the following middleware only stores the profiling results if a disallow_profiling key was not added to the request.env while processing the request.
class AppProfilerAuthorizedMiddleware "value" }
# OR
Rails.application.config.app_profiler.storage_credentials = { "key" => "value" }Note that in development and test modes the file isn't uploaded. Instead, it is viewed via the Middleware::ViewAction. If you want to change that, use the middleware_action configuration:
Rails.application.config.app_profiler.middleware_action = AppProfiler::Middleware::UploadActionIt is possible to configure AppProfiler to use the vernier or stackprof. To use vernier, it must be added separately in the application Gemfile.
The backend can be selected dynamically at runtime using the backend parameter. The default backend to use when this parameter is not specified can be configured with:
AppProfiler.backend = AppProfiler::StackprofBackend # or AppProfiler::VernierBackend
# OR
Rails.application.config.app_profiler.backend = AppProfiler::StackprofBackend # or AppProfiler::VernierBackendBy default, the stackprof backend will be used.
In local development, changing the backend will change whether the profile is viewed in Speedscope or Firefox Profiler.
The AppProfiler middleware can be configured to sample a percentage of requests for profiling. This can be useful for profiling a subset of requests in production without profiling every request.
To enable profile sampling, you can either set profile_sampler_enabled to true or provide your own custom Proc:
AppProfiler.profile_sampler_enabled = true
# OR
Rails.application.config.app_profiler.profile_sampler_enabled = trueAppProfiler.profile_sampler_enabled = -> { sampler_enabled? }
# OR
Rails.application.config.app_profiler.profile_sampler_enabled = -> { sampler_enabled? }Both backends, StackProf and Vernier, can be configured separately.
These can be overridden like:
…All the configuration parameters are optional and have default values. The default values are:
…Apps do not need have to configure anything if they are happy with the default values.
bin/setup && bundle exec rake暂无开放 Issues,或尚未同步最近议题。