Class: Grape::Middleware::Lograge
- Inherits:
-
Globals
- Object
- Globals
- Grape::Middleware::Lograge
- 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
-
.filter ⇒ Object
Returns the value of attribute filter.
Instance Method Summary collapse
- #action_name ⇒ Object
- #after(payload, status) ⇒ Object
- #after_exception(payload, e) ⇒ Object
- #after_failure(payload, error) ⇒ Object
- #before ⇒ Object
- #call!(env) ⇒ Object
- #controller ⇒ Object
- #endpoint ⇒ Object
-
#initialize(_, options = {}) ⇒ Lograge
constructor
A new instance of Lograge.
- #parameters ⇒ Object
- #raw_payload ⇒ Object
Constructor Details
#initialize(_, options = {}) ⇒ Lograge
Returns a new instance of Lograge.
16 17 18 19 |
# File 'lib/grape/middleware/lograge.rb', line 16 def initialize(_, = {}) super [:filter] ||= self.class.filter end |
Class Attribute Details
.filter ⇒ Object
Returns the value of attribute filter.
13 14 15 |
# File 'lib/grape/middleware/lograge.rb', line 13 def filter @filter end |
Instance Method Details
#action_name ⇒ Object
124 125 126 |
# File 'lib/grape/middleware/lograge.rb', line 124 def action_name endpoint.[:path].map { |path| path.to_s.sub(BACKSLASH, '') }.join(BACKSLASH) end |
#after(payload, status) ⇒ Object
65 66 67 68 69 70 |
# 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) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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.] 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) ⇒ Object
86 87 88 89 90 |
# 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 ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# 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) ⇒ 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 ⇒ Object
120 121 122 |
# File 'lib/grape/middleware/lograge.rb', line 120 def controller endpoint.[:for].to_s end |
#endpoint ⇒ Object
116 117 118 |
# File 'lib/grape/middleware/lograge.rb', line 116 def endpoint env[Grape::Env::API_ENDPOINT] end |
#parameters ⇒ Object
92 93 94 95 96 97 98 99 100 |
# 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 [:filter] [:filter].filter(request_params) else request_params end end |
#raw_payload ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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'] } end |