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

Add Rails 6 memory allocations to default log (with fixed tests!) #355

Merged
merged 3 commits into from
Jul 1, 2022
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
9 changes: 9 additions & 0 deletions lib/lograge/log_subscribers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def process_main_event(event)
def extract_request(event, payload)
data = initial_data(payload)
data.merge!(extract_status(payload))
data.merge!(extract_allocations(event))
data.merge!(extract_runtimes(event, payload))
data.merge!(extract_location)
data.merge!(extract_unpermitted_params)
Expand Down Expand Up @@ -59,6 +60,14 @@ def get_error_status_code(exception_class_name)
ActionDispatch::ExceptionWrapper.status_code_for_exception(exception_class_name)
end

def extract_allocations(event)
if (allocations = (event.respond_to?(:allocations) && event.allocations))
{ allocations: allocations }
else
{}
end
end

def custom_options(event)
options = Lograge.custom_options(event) || {}
options.merge event.payload[:custom_payload] || {}
Expand Down
22 changes: 22 additions & 0 deletions spec/log_subscribers/action_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,28 @@
subscriber.process_action(event)
expect(log_output.string).to_not include('unpermitted_params=')
end

context 'with memory allocations' do
it 'includes allocations when available' do
event_with_allocations = event.dup
event_with_allocations.define_singleton_method :allocations do
231
end

subscriber.process_action(event_with_allocations)
expect(log_output.string).to match(/allocations=231/)
end

it 'fails gracefully when allocations are unavailable' do
event_without_allocations = event.dup
if event_without_allocations.respond_to? :allocations
event_without_allocations.instance_eval('undef :allocations', __FILE__, __LINE__)
end

subscriber.process_action(event_without_allocations)
expect(log_output.string).to_not match(/allocations=/)
end
end
end

context 'with custom_options configured for lograge output' do
Expand Down