Ruby production code coverage collection and reporting (line of code usage)
Ruby production code coverage collection and reporting (line of code usage)
Key Features • Installation • Coverband Web UI • Advanced Config • Newer Features • License • Change Log / Roadmap • Code of Conduct
A gem to measure production code usage, showing a counter for the number of times each line of code is executed. Coverband allows easy configuration to collect and report on production code usage. It reports in the background via a thread, can be used as Rack middleware, or can be manually configured to meet any need.
Note: Coverband is not intended for test code coverage; for that we recommend using SimpleCov.
The primary goal of Coverband is to give you deep insight into the production runtime usage of your application code, while having the least impact on performance possible.
Coverband stores coverage data in Redis. The Redis endpoint is looked for in this order:
ENV['COVERBAND_REDIS_URL']ENV['REDIS_URL']localhost:6379The redis store can also be explicitly defined within the config/coverband.rb. See advanced config.
For Redis servers that require TLS (such as AWS serverless ElastiCache), use the rediss:// URL scheme instead of redis://. The Redis gem automatically enables TLS when it detects this scheme:
# Example with AWS serverless ElastiCache
REDIS_URL=rediss://my-elasticache.abcdef.cache.amazonaws.com:6379Or in config/coverband.rb:
config.store = Coverband::Adapters::RedisStore.new(
Redis.new(url: "rediss://my-elasticache-endpoint:6379")
)The rediss:// scheme works with:
Add this line to your application's Gemfile, remember to bundle install after updating:
gem 'coverband'With older versions of Coverband, projects would report to redis using rack or sidekiq middleware. After Coverband 4.0, this should no longer be required and could cause performance issues. Reporting to redis is now automatically done within a background thread with no custom code needed.
See changelog.
The Railtie integration means you shouldn't need to do anything else other than ensure Coverband is required after Rails within your Gemfile.
For the best coverage, you want this loaded as early as possible. We recommend requiring cover band directly in the config.ru. Requiring Coverband within an initializer could also work, but you may end up missing some boot up coverage. To start collection require Coverband as early as possible.
require 'coverband'
require File.dirname(__FILE__) + '/config/environment'
use Coverband::BackgroundMiddleware
run ActionController::Dispatcher.newYou can check it out locally by running the Coverband Demo App.
View a shared demo at https://coverband-rails-example.onrender.com/
View overall coverage information
Drill into individual file coverage
View individual file details
Clear Coverage - disabled by default as it could be considered a dangerous operation in production. Enable with config.web_enable_clear or leave off and clear from the rake task.
Clear coverage report
This will clear the coverage data. This wipes out all collected data.
Clear individual file coverage
This will clear the details of the file you are looking at. This is helpful if you don't want to lose all Coverage data but made a change that you expect would impact a particular file.
Force coverage collection
This triggers coverage collection on the current webserver process. Useful in development but confusing in production environments where many ruby processes are usually running.
The columns in the web UI are as follows:
end statements.When viewing an individual file, a line of code such as a class or method definition may appear green because it is eager loaded by the application, but still not be hit at all in runtime by actual users.
In the context of Coverband, a file is considered relevant for monitoring if it is located within certain specified project paths. Specifically, Coverband monitors all *.rb files located in the following paths:
app/
lib/
config/
This applies as long as these files are not included in the list of exclusions defined in "config.ignore".
This attribute shows the exact date and time when Coverband first detected the file. This detection happens when:
The file is loaded by Ruby (e.g., during application startup in Rails via autoload or require).
The file becomes relevant for monitoring, even if no lines within it have been executed yet.
By default, Coverband resets a file’s coverage data and updates its “Coverage first seen” timestamp whenever the file changes, ensuring the coverage reflects the file’s new state.
This attribute shows the most recent time when any line in the file was executed at runtime. It specifically reflects runtime execution and does not include the file’s load time.
When any line in the file is executed at runtime (e.g., a method is called, a route is triggered, or a conditional is evaluated), Coverband updates the “Last activity recorded” timestamp.
This only occurs when the file’s lines are actually executed. If a file is loaded but none of its lines are executed, the “Last activity recorded” timestamp will not be updated.
It helps determine if a file is actively being used in the application. If a file has no recent activity, it may be a candidate for review or removal.
If a file has a “Coverage first seen” timestamp but no “Last activity recorded”, this indicates that the file is being loaded but no runtime activity is occurring (e.g., it contains unused methods or classes).
If both attributes are populated and the “Last activity recorded” timestamp is recent, the file is actively used in the application.
Coverband comes with a mountable rack app for viewing reports. For Rails this can be done in config/routes.rb with:
Rails.application.routes.draw do
mount Coverband::Reporters::Web.new, at: '/coverage'
endBut don't forget to protect your source code with proper authentication. Something like this when using devise:
Rails.application.routes.draw do
authenticate :user, lambda { |u| u.admin? } do
mount Coverband::Reporters::Web.new, at: '/coverage'
end
endor you can enable basic auth by setting ENV['COVERBAND_PASSWORD'] or via your configuration config.password = <YOUR_COMPLEX_UNGUESSABLE_PASSWORD>
The coverage server can also be started standalone with a rake task:
bundle exec rake coverband:coverage_serverThe web UI should then be available here: http://localhost:9022/
If you want to run on an alternative port:
COVERBAND_COVERAGE_PORT=8086 bundle exec rake coverband:coverage_serverThis is especially useful for projects that are api only and cannot support the mounted rack app. To get production coverage, point Coverband at your production redis server and ensure to checkout the production version of your project code locally.
COVERBAND_REDIS_URL=redis://username:password@redis_production_server:2322 bundle exec rake coverband:coverage_serverTake Coverband for a spin on the live Heroku deployed Coverband Demo. The full source code for the demo is available to help with installation, configuration, and understanding of basic usage.
If you need to configure Coverband, this can be done by creating a config/coverband.rb file relative to your project root.
ENV['COVERBAND_REDIS_URL'], ENV['REDIS_URL'], or localhostBelow is an example config file for a Rails 5 app:
…Do you use figaro, mc-settings, dotenv or something else to inject environment variables into your app? If so ensure you have that done BEFORE Coverband is required.
For example if you use dotenv, you need to do this, see https://github.com/bkeepers/dotenv#load-order
gem 'dotenv', require: 'dotenv/load'
gem 'coverband'
gem 'other-gem-that-requires-env-variables'Sometimes you have files that are known to be valuable, perhaps in other environments or something that is just run very infrequently. Opposed to having to mentally filter them out of the report, you can just have them ignored in the Coverband reporting by using config.ignore as shown below. Ignore takes a string but can also match with regex rules see how below ignores all rake tasks as an example.
config.ignore += ['config/application.rb',
'config/boot.rb',
'config/puma.rb',
'config/schedule.rb',
'bin/.*',
'config/environments/.*',
'lib/tasks/.*']Ignoring Custom Gem Locations: Note, if you have your gems in a custom location under your app folder you likely want to add them to config.ignore. For example, if you have your gems not in the default ignored location of app/vendor but in app/gems, you would need to add gems/* to your ignore list.
Coverband allows an optional feature to track all view files that are used by an application.
This feature is enabled by default. To stop this feature, disable the feature in your Coverband config.
config.track_views = false
Coverband can also track ViewComponent renders. This requires:
# coNo open issues yet, or sync has not completed.