class Grape::Middleware::Lograge

Constants

BACKSLASH
STATUS_CODE_TO_SYMBOL

Attributes

filter[RW]

Public Class Methods

new(_, options = {}) click to toggle source
Calls superclass method
# File lib/grape/middleware/lograge.rb, line 16
def initialize(_, options = {})
  super
  @options[:filter] ||= self.class.filter
end

Public Instance Methods

action_name() click to toggle source
# File lib/grape/middleware/lograge.rb, line 124
def action_name
  endpoint.options[:path].map { |path| path.to_s.sub(BACKSLASH, '') }.join(BACKSLASH)
end
after(payload, status) click to toggle source
# File lib/grape/middleware/lograge.rb, line 65
def after(payload, status)
  payload[:status]     = status
  payload[:format]     = env['api.format']
  payload[:version]    = env['api.version']
  payload[:db_runtime] = @db_duration
end
after_exception(payload, e) click to toggle source
# File lib/grape/middleware/lograge.rb, line 72
def after_exception(payload, e)
  ActiveSupport::Notifications.unsubscribe(@db_subscription) if @db_subscription

  class_name = e.class.name
  status = e.respond_to?(:status) ? e.status : 500

  payload[:exception] = [class_name, e.message]
  payload[:backtrace] = e.backtrace

  unless ActionDispatch::ExceptionWrapper.rescue_responses[class_name].present?
    ActionDispatch::ExceptionWrapper.rescue_responses[class_name] = STATUS_CODE_TO_SYMBOL[status]
  end
end
after_failure(payload, error) click to toggle source
# File lib/grape/middleware/lograge.rb, line 86
def after_failure(payload, error)
  ActiveSupport::Notifications.unsubscribe(@db_subscription) if @db_subscription

  after(payload, error[:status])
end
before() click to toggle source
Calls superclass method
# File lib/grape/middleware/lograge.rb, line 21
def before
  super

  @db_duration = 0

  @db_subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    @db_duration += event.duration
  end if defined?(ActiveRecord)

  ActiveSupport::Notifications.instrument("start_processing.grape", raw_payload)
end
call!(env) click to toggle source

@note Error and exception handling are required for the after hooks

Exceptions are logged as a 500 status and re-raised
Other "errors" are caught, logged and re-thrown
# File lib/grape/middleware/lograge.rb, line 37
def call!(env)
  @env = env

  before

  ActiveSupport::Notifications.instrument("process_action.grape", raw_payload) do |payload|
    error = catch(:error) do
      begin
        @app_response = @app.call(@env)
      rescue => e
        after_exception(payload, e)
        raise e
      end

      nil
    end

    if error
      after_failure(payload, error)
      throw(:error, error)
    else
      after(payload, response.status)
    end

    @app_response
  end
end
controller() click to toggle source
# File lib/grape/middleware/lograge.rb, line 120
def controller
  endpoint.options[:for].to_s
end
endpoint() click to toggle source
# File lib/grape/middleware/lograge.rb, line 116
def endpoint
  env[Grape::Env::API_ENDPOINT]
end
parameters() click to toggle source
# File lib/grape/middleware/lograge.rb, line 92
def parameters
  request_params = env[Grape::Env::GRAPE_REQUEST_PARAMS].to_hash
  request_params.merge!(env['action_dispatch.request.request_parameters'.freeze] || {}) # for Rails
  if @options[:filter]
    @options[:filter].filter(request_params)
  else
    request_params
  end
end
raw_payload() click to toggle source
# File lib/grape/middleware/lograge.rb, line 102
def raw_payload
  {
    params:     parameters.merge(
      'action' => action_name.empty? ? 'index' : action_name,
      'controller' => controller
    ),
    method:     env[Grape::Env::GRAPE_REQUEST].request_method,
    path:       env[Grape::Env::GRAPE_REQUEST].path,
    user_agent: env['HTTP_USER_AGENT'],
    request_id: env['action_dispatch.request_id'],
    remote_ip:  env['action_dispatch.remote_ip'].to_s
  }
end