Skip to content

Commit 732463d

Browse files
committed
Include more DB stats in logs
[Reducing the number of DB queries has made page response times faster](#2274) [In Rails 7.2, the default logger started including the number of database queries in the log](rails/rails#51457). We already have DB time in the logs. I think it's useful to include the number of queries as another simple metric. This is useful for checking performance in local development and I think probably for production as well. It would enable us to see if the number of queries is increasing or decreasing over time for specific pages. Example log entry: { "method": "GET", "path": "/forms/1", "format": "html", "controller": "FormsController", "action": "show", "status": 200, "allocations": 67752, "duration": 238.2, "view": 23.53, "db": 156.87, "request_host": "localhost", "request_id": "01d5ce3c-7a6c-4d32-9288-cf4350850af3", "trace_id": null, "form_id": "1", "user_ip": null, "params": { "form_id": "1" }, "queries_count": 26, "cached_queries_count": 15 }
1 parent 8cc614c commit 732463d

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

app/models/current_logging_attributes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ class CurrentLoggingAttributes < ActiveSupport::CurrentAttributes
1515
:answer_type,
1616
:auth0_session_id,
1717
:validation_errors,
18-
:params
18+
:params,
19+
:queries_count,
20+
:cached_queries_count
1921
end

config/application.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ class Application < Rails::Application
5757
config.lograge.keep_original_rails_log = false
5858

5959
config.lograge.custom_options = lambda do |event|
60-
CurrentLoggingAttributes.attributes.merge(exception: event.payload[:exception]).compact
60+
CurrentLoggingAttributes.attributes.merge(
61+
exception: event.payload[:exception],
62+
queries_count: event.payload[:queries_count],
63+
cached_queries_count: event.payload[:cached_queries_count],
64+
).compact
6165
end
6266

6367
# Use custom logger and formatter to log in JSON with request context fields. To use conventional

0 commit comments

Comments
 (0)