Module: Yabeda::Rails

Defined in:
lib/yabeda/rails.rb,
lib/yabeda/rails/railtie.rb,
lib/yabeda/rails/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

LONG_RUNNING_REQUEST_BUCKETS =
[
  0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, # standard
  30, 60, 120, 300, 600, # Sometimes requests may be really long-running
].freeze
VERSION =
"0.7.1"

Class Method Summary collapse

Class Method Details

.controller_handlersObject



14
15
16
# File 'lib/yabeda/rails.rb', line 14

def controller_handlers
  @controller_handlers ||= []
end

.install!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/yabeda/rails.rb', line 22

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



64
65
66
# File 'lib/yabeda/rails.rb', line 64

def ms2s(ms)
  (ms.to_f / 1000).round(3)
end

.on_controller_action(&block) ⇒ Object



18
19
20
# File 'lib/yabeda/rails.rb', line 18

def on_controller_action(&block)
  controller_handlers << block
end