Last active
May 26, 2016 18:36
-
-
Save bdevel/c333d5d9c7021aae0d0d2b351fa98639 to your computer and use it in GitHub Desktop.
Rails Database HTTP Logger - Rack Middleware
This file contains hidden or 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 CreateHttpLogsTable < ActiveRecord::Migration | |
def change | |
create_table :http_request_logs do |t| | |
t.datetime :created_at | |
t.string :request_method | |
t.text :path | |
t.jsonb :params | |
t.integer :http_status | |
t.integer :bytes_sent | |
t.integer :bytes_received | |
t.float :response_time | |
t.string :user_id | |
t.string :remote_addr | |
t.string :browser | |
t.string :operating_system | |
end | |
end | |
end |
This file contains hidden or 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
# Middleware | |
# Add `config.middleware.insert_before Rack::Runtime, DbHttpLogger` | |
# to your config/application.rb | |
class DbHttpLogger | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, response = @app.call(env) | |
browser = UserAgentParser.parse env["HTTP_USER_AGENT"].to_s | |
# Filter out any passwords - though they shouldn't be in GET requests. | |
clean_params.each do |k,v| | |
x[k] = '*****' if k.to_s =~ /password/i | |
end | |
# Don't save params for POST requests because we are | |
# storing the data in other tables. | |
if env["REQUEST_METHOD"] == "GET" | |
clean_params = env["action_controller.instance"].params.as_json rescue {} | |
# No point in logging these | |
clean_params.delete "action" | |
clean_params.delete "format" | |
clean_params.delete "controller" | |
clean_params.delete "json_store_path" | |
else | |
clean_params = {} | |
end | |
data = { | |
request_method: env["REQUEST_METHOD"], | |
path: env["REQUEST_PATH"], #"/api/v2/solutions/w6zv-4rgk", | |
params: clean_params, | |
http_status: status, | |
bytes_sent: headers["Content-Length"] || nil, | |
bytes_received: env["CONTENT_LENGTH"] || nil, | |
response_time: headers["X-Runtime"].to_f || nil, | |
user_id: (env["action_controller.instance"].current_user.user_id rescue nil), | |
remote_addr: env["REMOTE_ADDR"], | |
browser: browser.to_s, | |
operating_system: browser.os.to_s | |
} | |
begin | |
Thread.new do | |
ActiveRecord::Base.connection_pool.with_connection do | |
HttpRequestLog.create(data) | |
end | |
end | |
rescue Exception => e | |
Rails.logger.error("Cannot log request to DB: " + e.to_s) | |
end | |
[status, headers, response] | |
end | |
end |
This file contains hidden or 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
# Model | |
class HttpRequestLog < ActiveRecord::Base | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment