module Yabeda::Rails

Minimal set of Rails-specific metrics for using with Yabeda

Constants

LONG_RUNNING_REQUEST_BUCKETS
VERSION

Public Class Methods

controller_handlers() click to toggle source
# File lib/yabeda/rails.rb, line 16
def controller_handlers
  @controller_handlers ||= []
end
install!() click to toggle source

Declare metrics and install event handlers for collecting themya rubocop: disable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize

# File lib/yabeda/rails.rb, line 26
def install!
  Yabeda.configure do
    group :rails

    counter   :requests_total,   comment: "A counter of the total number of HTTP requests rails processed.",
                                 tags: %i[controller action status format method]

    histogram :request_duration, tags: %i[controller action status format method],
                                 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.",
                             tags: %i[controller action status format method]

    histogram :db_runtime, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS,
                           comment: "A histogram of the activerecord execution time.",
                           tags: %i[controller action status format method]

    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,
      }
      labels.merge!(event.payload.slice(*Yabeda.default_tags.keys - labels.keys))

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

      Yabeda::Rails.controller_handlers.each do |handler|
        handler.call(event, labels)
      end
    end
  end
end
ms2s(milliseconds) click to toggle source

rubocop: enable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize

# File lib/yabeda/rails.rb, line 70
def ms2s(milliseconds)
  (milliseconds.to_f / 1000).round(3)
end
on_controller_action(&block) click to toggle source
# File lib/yabeda/rails.rb, line 20
def on_controller_action(&block)
  controller_handlers << block
end