Module: Yabeda::Grape

Defined in:
lib/yabeda/grape.rb,
lib/yabeda/grape/version.rb

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

Class Method Summary collapse

Class Method Details

.bind_metricsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yabeda/grape.rb', line 14

def bind_metrics
  Yabeda.configure do
    group :grape

    counter   :requests_total, comment: "A counter of the total number of HTTP requests rails processed.",
                               tags: i[method path status]
    histogram :request_duration, unit: :seconds, buckets: LONG_RUNNING_REQUEST_BUCKETS,
                                 tags: i[method path status],
                                 comment: "A histogram of the response latency."

    ActiveSupport::Notifications.subscribe 'endpoint_run.grape' do |*args|
      event = ActiveSupport::Notifications::Event.new(*args)

      next unless (labels = Yabeda::Grape.extract_labels(event))

      grape_requests_total.increment(labels)
      grape_request_duration.measure(labels, Yabeda::Grape.ms2s(event.duration))
    end
  end
end

.extract_labels(event) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yabeda/grape.rb', line 39

def extract_labels(event)
  return unless (endpoint = event.payload[:endpoint])

  # endpoint.route.path can throw an error in Grape inside_route.rb, which
  # is caught below
  path = endpoint.route&.path # path description (e.g. /user/{id}.json)
  method = endpoint.options[:method]&.first&.downcase # http method
  status = endpoint.status # http code

  { method: method, path: path, status: status }
rescue StandardError
  nil
end

.ms2s(ms) ⇒ Object



35
36
37
# File 'lib/yabeda/grape.rb', line 35

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