Skip to content

Repository files navigation

ezmetrics

Gem Version

A simple tool for capturing and displaying Rails metrics.

Installation

gem 'ezmetrics'

Usage

Getting started

This tool captures and aggregates Rails application metrics such as

  • duration
  • views
  • db
  • queries
  • status

and stores them for the timeframe you specified, 60 seconds by default.

You can change the timeframe according to your needs and save the metrics by calling log method:

# Store the metrics for 60 seconds (default behaviour)EZmetrics.new.log(duration: 100.5,views: 40.7,db: 59.8,queries: 4,status: 200)

or

# Store the metrics for 10 minutesEZmetrics.new(10.minutes).log(duration: 100.5,views: 40.7,db: 59.8,queries: 4,status: 200)

For displaying metrics you need call show method:

# Aggregate and show metrics for last 60 seconds (default behaviour)EZmetrics.new.show

or

# Aggregate and show metrics for last 10 minutesEZmetrics.new(10.minutes).show

Please note that you can combine these timeframes, for example - store for 10 minutes, display for 5 minutes.

Capture metrics

Just add an initializer to your application:

# config/initializers/ezmetrics.rbActiveSupport::Notifications.subscribe("sql.active_record")do |*args|
event=ActiveSupport::Notifications::Event.new(*args)unlessevent.payload[:name] == "SCHEMA"Thread.current[:queries] ||= 0Thread.current[:queries] += 1endendActiveSupport::Notifications.subscribe("process_action.action_controller")do |*args|
event=ActiveSupport::Notifications::Event.new(*args)EZmetrics.new.log(duration: event.duration.to_f,views: event.payload[:view_runtime].to_f,db: event.payload[:db_runtime].to_f,status: event.payload[:status].to_i || 500,queries: Thread.current[:queries].to_i,)end

Display metrics

As simple as:

EZmetrics.new.show

This will return a hash with the following structure:

{duration: {avg: 5569,max: 9675},views: {avg: 12,max: 240},db: {avg: 155,max: 4382},queries: {avg: 26,max: 76},requests: {all: 2000,grouped: {"2xx"=>1900,"3xx"=>15,"4xx"=>80,"5xx"=>5}}}

Performance

The implementation is based on Redis commands such as:

which are extremely fast.

You can check the aggregation time by running:

EZmetrics::Benchmark.new.measure_aggregation

The result of running this benchmark on a 2017 Macbook Pro 2.9 GHz Intel Core i7 with 16 GB of RAM:

IntervalDuration (seconds)
1 minute0.0
1 hour0.11
12 hours1.6
24 hours3.5

About

A simple tool for capturing and displaying Rails metrics.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages