Module: Yabeda::Hanami

Defined in:
lib/yabeda/hanami.rb,
lib/yabeda/hanami/event.rb,
lib/yabeda/hanami/config.rb,
lib/yabeda/hanami/version.rb

Defined Under Namespace

Classes: Config, Error, Event

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.1.1"

Class Method Summary collapse

Class Method Details

.configObject



69
70
71
# File 'lib/yabeda/hanami.rb', line 69

def config
  @config ||= Config.new
end

.install!Object

Declare and install metrics



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yabeda/hanami.rb', line 19

def install!
  Yabeda.configure do
    _yabeda_hanami_config = ::Yabeda::Hanami.config

    group :hanami do
      counter :requests_total,
        comment: "A counter of the total number of HTTP requests hanami processed.",
        tags: %i[method path remote_ip status]

      counter :responses_total,
        comment: "A counter of the total number of HTTP requests hanami processed.",
        tags: %i[method path remote_ip status]

      histogram :processing_duration,
        unit: :seconds,
        buckets: LONG_RUNNING_REQUEST_BUCKETS,
        comment: "A histogram of the processing duration.",
        tags: %i[method path remote_ip status]

      counter :rack_errors_total,
        comment: "A counter of the total number of rack errors.",
        tags: %i[method path remote_ip status]
    end
  end
end

.subscribe!Object

Subscribe to events



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yabeda/hanami.rb', line 46

def subscribe!
  yabeda_hanami_config = ::Yabeda::Hanami.config

  yabeda_hanami_config.notifications.subscribe(:"rack.request.start") do |event|
    event = Yabeda::Hanami::Event.new(event.id, event.payload)

    Yabeda.hanami_requests_total.increment(event.labels)
  end

  yabeda_hanami_config.notifications.subscribe(:"rack.request.stop") do |event|
    event = Yabeda::Hanami::Event.new(event.id, event.payload)

    Yabeda.hanami_responses_total.increment(event.labels)
    Yabeda.hanami_processing_duration.measure(event.labels, event.duration)
  end

  yabeda_hanami_config.notifications.subscribe(:"rack.request.error") do |event|
    event = Yabeda::Hanami::Event.new(event.id, event.payload)

    Yabeda.hanami_rack_errors_total.increment(event.labels)
  end
end