Last active
December 12, 2016 15:31
-
-
Save luxerama/0347d3921a96792a83d9 to your computer and use it in GitHub Desktop.
Grape Log Instrumentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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