-
-
Notifications
You must be signed in to change notification settings - Fork 297
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
Changes from all commits
0f31064
44c26fa
63f61f7
caf7ffe
4f093d5
27083c0
481301e
2f4d44e
2433800
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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 | ||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not a fan of assignments within the if conditions. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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? 😕
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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