Skip to content

This is a work in progress. Rack::Cache::Monitor monitors Rack::Cache and reports related stats.

License

Notifications You must be signed in to change notification settings

s-andringa/rack-cache-monitor

Repository files navigation

Rack::Cache::Monitor

The Rack::Cache::Monitor middleware lets you monitor Rack::Cache and reports related stats, such as the number of fresh hits, passes, or its hitrate. Behind the scenes it simply counts Rack::Cache traces from the X-Rack-Cache header and yields its results to a user defined reporter on a configurable interval. It is designed to work in the context of threaded and multi-process web servers.

Installation

Add this line to your application's Gemfile:

gem 'rack-cache-monitor'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-cache-monitor

Usage

Rack::Cache::Monitor is a Rack middleware and must be inserted into the middleware stack before Rack::Cache. Its constructor takes a block which acts as the reporter.

Basic

# In rackup file:
require 'rack/cache'
require 'rack/cache/monitor'

use Rack::Cache::Monitor, **options do |report|
  STDOUT.puts "Rack::Cache hitrate: #{report.hitrate}%" if report.hitrate
end

use Rack::Cache #, ....

run app

With Rails

# In environment config file:
config.middleware.insert_before "Rack::Cache", "Rack::Cache::Monitor", **options do |report|
  STDOUT.puts "Rack::Cache hitrate: #{report.hitrate}%" if report.hitrate
end

Options

:interval_in_seconds
Stats are flushed and reported on this interval. In seconds. Default is 20.
:log_level
Log level of the default logger.
:logger
Custom logger object. The default logger writes to STDOUT.
:report_on_exit
Flush and report one more time before the program / web server exits. Default is true.

Things to know

  • When an exception is raised from the reporter block, the exception is logged and the monitor is gracefully shut down. The web server continues to run without Rack::Cache monitoring.
  • The reporter block is called in a plain Ruby thread. If you're on a GIL-enabled Ruby you might want to avoid doing anything expensive other than IO operations, as it may slow down your application.
  • The interval starts after the reporter is finished handling a report. I.e. if your reporter takes 30 seconds to do its job, and the interval is also set to 30 seconds, then the reporter is called only once a minute.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/s-andringa/rack-cache-monitor.

License

The gem is available as open source under the terms of the MIT License.

About

This is a work in progress. Rack::Cache::Monitor monitors Rack::Cache and reports related stats.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published