Skip to content

Instantly share code, notes, and snippets.

@luxerama
Last active December 12, 2016 15:31
Show Gist options
  • Save luxerama/0347d3921a96792a83d9 to your computer and use it in GitHub Desktop.
Save luxerama/0347d3921a96792a83d9 to your computer and use it in GitHub Desktop.
Grape Log Instrumentation
class GrapeRequestLogSubscriber < ActiveSupport::LogSubscriber
def grape_controller(event)
request = Rack::Request.new(event.payload).env
response = Rack::Response.new(event.payload)
data = extract_request(request)
data.merge! extract_status(response)
logger.send(:warning, data.to_json)
end
private
def extract_request(payload)
request = payload[:request]
{
:method => request['REQUEST_METHOD'],
:path => request['PATH_INFO'],
:format => extract_format(request)
}
end
def extract_format(request)
request['PATH_INFO'].split('.').last
end
def extract_status(payload)
{ status: payload.status.to_i }
end
end
require 'grape'
module Lograge
module Instrumentation
class Grape < ::Grape::Middleware::Base
def call!(env)
ActiveSupport::Notifications.instrument('grape_controller', request: env) do
@env = env
before
@app_response = @app.call(@env)
after || @app_response
end
end
end
end
end
ActiveSupport::Notifications.subscribe('grape_controller', GrapeRequestLogSubscriber.new)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment