Class: Grape::Middleware::Lograge

Inherits:
Globals
  • Object
show all
Defined in:
lib/grape/middleware/lograge.rb

Constant Summary collapse

BACKSLASH =
'/'.freeze
STATUS_CODE_TO_SYMBOL =
Rack::Utils::SYMBOL_TO_STATUS_CODE.each_with_object({}) do |(symbol, status_code), hash|
  hash[status_code] = symbol
end

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_, options = {}) ⇒ Lograge

Returns a new instance of Lograge.



28
29
30
31
# File 'lib/grape/middleware/lograge.rb', line 28

def initialize(_, options = {})
  super
  @options[:filter] ||= self.class.filter
end

Class Attribute Details

.filterObject

Returns the value of attribute filter.



13
14
15
# File 'lib/grape/middleware/lograge.rb', line 13

def filter
  @filter
end

Class Method Details

.custom_optionsObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/grape/middleware/lograge.rb', line 15

def custom_options
  -> (event) {
    {
      params:     event.payload[:params],
      user_agent: event.payload[:user_agent],
      request_id: event.payload[:request_id],
      remote_ip:  event.payload[:remote_ip],
      version:    event.payload[:version]
    }
  }
end

Instance Method Details

#action_nameObject



134
135
136
# File 'lib/grape/middleware/lograge.rb', line 134

def action_name
  endpoint.options[:path].map { |path| path.to_s.sub(BACKSLASH, '') }.join(BACKSLASH)
end

#after(payload, status) ⇒ Object



77
78
79
80
81
82
# File 'lib/grape/middleware/lograge.rb', line 77

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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/grape/middleware/lograge.rb', line 84

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

  ActionDispatch::ExceptionWrapper.rescue_responses[class_name] = STATUS_CODE_TO_SYMBOL[status]
end

#after_failure(payload, error) ⇒ Object



96
97
98
99
100
# File 'lib/grape/middleware/lograge.rb', line 96

def after_failure(payload, error)
  ActiveSupport::Notifications.unsubscribe(@db_subscription) if @db_subscription

  after(payload, error[:status])
end

#beforeObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grape/middleware/lograge.rb', line 33

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) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/grape/middleware/lograge.rb', line 49

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

#controllerObject



130
131
132
# File 'lib/grape/middleware/lograge.rb', line 130

def controller
  endpoint.options[:for].to_s
end

#endpointObject



126
127
128
# File 'lib/grape/middleware/lograge.rb', line 126

def endpoint
  env[Grape::Env::API_ENDPOINT]
end

#parametersObject



102
103
104
105
106
107
108
109
110
# File 'lib/grape/middleware/lograge.rb', line 102

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_payloadObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/grape/middleware/lograge.rb', line 112

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']
  }
end