module Yabeda::Grape

Constants

LONG_RUNNING_REQUEST_BUCKETS
VERSION

Public Class Methods

bind_metrics() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/yabeda/grape.rb, line 35
def ms2s(ms)
  (ms.to_f / 1000).round(3)
end