module Evil::Metrics::Rails

Constants

LONG_RUNNING_REQUEST_BUCKETS

Public Class Methods

controller_handlers() click to toggle source
# File lib/evil/metrics/rails.rb, line 15
def controller_handlers
  @controller_handlers ||= []
end
install!() click to toggle source
# File lib/evil/metrics/rails.rb, line 23
def install!
  Evil::Metrics.configure do
    group :rails

    counter   :requests_total, comment: "A counter of the total number of HTTP requests rails processed."
    histogram :request_duration, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS,
                                 comment: "A histogram of the response latency."
    histogram :view_runtime, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS,
                             comment: "A histogram of the view rendering time."
    histogram :db_runtime, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS,
                           comment: "A histogram of the activerecord execution time."

    ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
      event = ActiveSupport::Notifications::Event.new(*args)
      labels = {
        controller: event.payload[:params]["controller"],
        action: event.payload[:params]["action"],
        status: event.payload[:status],
        format: event.payload[:format],
        method: event.payload[:method].downcase,
      }

      rails_requests_total.increment(labels)
      rails_request_duration.measure(labels, Evil::Metrics::Rails.ms2s(event.duration))
      rails_view_runtime.measure(labels, Evil::Metrics::Rails.ms2s(event.payload[:view_runtime]))
      rails_db_runtime.measure(labels, Evil::Metrics::Rails.ms2s(event.payload[:db_runtime]))

      Evil::Metrics::Rails.controller_handlers.each do |handler|
        handler.call(event, labels)
      end
    end
  end
end
ms2s(ms) click to toggle source
# File lib/evil/metrics/rails.rb, line 57
def ms2s(ms)
  (ms.to_f / 1000).round(3)
end
on_controller_action(&block) click to toggle source
# File lib/evil/metrics/rails.rb, line 19
def on_controller_action(&block)
  controller_handlers << block
end