class AccessWatch::RackLogger
Constants
- MEMORY_CONVERSIONS
Attributes
app[R]
client[R]
Public Class Methods
new(app, config)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 5 def initialize(app, config) @app, @client = app, AccessWatch::Client.new(config) end
Public Instance Methods
call(env)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 9 def call(env) started_at = Time.now.utc status, headers, body = app.call(env) record(env, status, started_at, Time.now.utc) [status, headers, body] end
record(env, status, started_at, finished_at)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 16 def record(env, status, started_at, finished_at) post_request( time: started_at.iso8601(3), address: env["REMOTE_ADDR"], request: { protocol: env["HTTP_VERSION"], method: env["REQUEST_METHOD"], scheme: env["rack.url_scheme"], host: env["HTTP_HOST"], port: env["SERVER_PORT"], url: env["ORIGINAL_FULLPATH"], headers: extract_http_headers(env), }, response: {status: status}, context: { execution_time: finished_at - started_at, memory_usage: memory_usage_in_bytes, }, ) end
Private Instance Methods
extract_http_headers(headers)
click to toggle source
Private methods ###
# File lib/access_watch/rack_logger.rb, line 43 def extract_http_headers(headers) headers.reduce({}) do |hash, (name, value)| if name.index("HTTP_") == 0 && name != "HTTP_COOKIE" hash[format_header_name(name)] = value end hash end end
format_header_name(name)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 52 def format_header_name(name) name.sub(/^HTTP_/, '').gsub("_", " ").titleize.gsub(" ", "-") end
linux_process_memory_usage_in_bytes(pid)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 80 def linux_process_memory_usage_in_bytes(pid) return unless status = linux_process_status(pid) value, unit = status["VmRSS"].split value.to_i * MEMORY_CONVERSIONS[unit.downcase] end
linux_process_status(pid)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 68 def linux_process_status(pid) path = "/proc/#{pid}/status" return unless File.readable?(path) File.read(path).split("\n").reduce({}) do |hash, line| name, value = line.split(":") hash[name] = value.strip hash end end
memory_usage_in_bytes()
click to toggle source
# File lib/access_watch/rack_logger.rb, line 64 def memory_usage_in_bytes linux_process_memory_usage_in_bytes(Process.pid) end
post_async(path, data)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 60 def post_async(path, data) Thread.new { client.post(path, data) } end
post_request(data)
click to toggle source
# File lib/access_watch/rack_logger.rb, line 56 def post_request(data) post_async("log".freeze, data) end