Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance optimizations #9

Merged
merged 9 commits into from
Oct 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Fixed
* Make rubocop pass

## Other
* Performance optimizations (<https://github.com/splattael>) #9

## Added
* Add formatter for lines (<https://github.com/zimbatm/lines>) #35
* Rubocop and rake ci task
Expand Down
30 changes: 11 additions & 19 deletions lib/lograge/formatters/key_value.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
module Lograge
module Formatters
class KeyValue
LOGRAGE_FIELDS = [
:method, :path, :format, :controller, :action, :status, :error,
:duration, :view, :db, :location
]

def call(data)
fields = fields_to_display(data)

event = fields.reduce([]) do |message, key|
next message unless data.key?(key)

message << format(key, data[key])
message
end
event = fields.map { |key| format(key, data[key]) }
event.join(' ')
end

def fields_to_display(data)
LOGRAGE_FIELDS + (data.keys - LOGRAGE_FIELDS)
data.keys
end

def format(key, value)
# Exactly preserve the previous output
# Parsing this can be ambigious if the error messages contains
# a single quote
value = "'#{value}'" if key == :error

# Ensure that we always have exactly two decimals
value = Kernel.format('%.2f', value) if value.is_a? Float
if key == :error
# Exactly preserve the previous output
# Parsing this can be ambigious if the error messages contains
# a single quote
value = "'#{value}'"
else
# Ensure that we always have exactly two decimals
value = Kernel.format('%.2f', value) if value.is_a? Float
end

"#{key}=#{value}"
end
Expand Down
65 changes: 32 additions & 33 deletions lib/lograge/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def process_action(event)
payload = event.payload

data = extract_request(payload)
data.merge! extract_status(payload)
data.merge! runtimes(event)
data.merge! location(event)
data.merge! custom_options(event)
extract_status(data, payload)
runtimes(data, event)
location(data)
custom_options(data, event)

data = before_format(data, payload)
formatted_message = Lograge.formatter.call(data)
Expand Down Expand Up @@ -42,56 +42,55 @@ def extract_request(payload)
end

def extract_path(payload)
payload[:path].split('?').first
path = payload[:path]
index = path.index('?')
index ? path[0, index] : path
end

def extract_format(payload)
if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we conditionally defining the same method here? Am I overseeing something? 😕

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this looks like an oversight. @splattael ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, same method but with different impls. See below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 44c26fa for more info

def extract_format(payload)
payload[:formats].first
else
end
else
def extract_format(payload)
payload[:format]
end
end

def extract_status(payload)
if payload[:status]
{ status: payload[:status].to_i }
elsif payload[:exception]
exception, message = payload[:exception]
{ status: 500, error: "#{exception}:#{message}" }
def extract_status(data, payload)
if (status = payload[:status])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a fan of assignments within the if conditions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, alternatives seem to be "uglier" here.

data[:status] = status.to_i
elsif (error = payload[:exception])
exception, message = error
data[:status] = 500
data[:error] = "#{exception}:#{message}"
else
{ status: 0 }
data[:status] = 0
end
end

def custom_options(event)
Lograge.custom_options(event) || {}
def custom_options(data, event)
options = Lograge.custom_options(event)
data.merge! options if options
end

def before_format(data, payload)
Lograge.before_format(data, payload)
end

def runtimes(event)
{
duration: event.duration,
view: event.payload[:view_runtime],
db: event.payload[:db_runtime]
}.reduce({}) do |runtimes, (name, runtime)|
runtimes[name] = runtime.to_f.round(2) if runtime
runtimes
end
def runtimes(data, event)
payload = event.payload
data[:duration] = event.duration.to_f.round(2)
data[:view] = payload[:view_runtime].to_f.round(2) if payload.key?(:view_runtime)
data[:db] = payload[:db_runtime].to_f.round(2) if payload.key?(:db_runtime)
end

def location(_event)
def location(data)
location = Thread.current[:lograge_location]
return unless location
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pxlpnk I had to work around rubocop here. I'd use a simple assignment within condition again.


if location
Thread.current[:lograge_location] = nil
{ location: location }
else
{}
end
Thread.current[:lograge_location] = nil
data[:location] = location
end
end
end