2021-02-03 16:09:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
Add structured logging for Rails processes
This introduces JSON logging for Rails views saved to a file called
`development_json.log`, `production_json.log`, etc.
For example, instead of this unparsable log:
```
Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
Rendered text template within layouts/application (0.0ms)
Rendered layouts/_assets.html.erb (2.0ms)
Rendered layouts/_top.html.erb (2.6ms)
Rendered layouts/_about.html.erb (0.3ms)
Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)
```
We get a single line with this:
```
{"method":"GET","path":"/,"format":"html","controller":"HomeController","action":"index","status":200,"duration":79,"view":78.8,"db":0.0,"location":"http://localhost/","time":"2017-07-18 09:35:17 -0700"}
```
Part of #20060
2017-07-17 18:54:13 -04:00
|
|
|
# Only use Lograge for Rails
|
2019-12-22 04:07:51 -05:00
|
|
|
unless Gitlab::Runtime.sidekiq?
|
2020-10-05 11:08:56 -04:00
|
|
|
Rails.application.reloader.to_prepare do
|
|
|
|
filename = File.join(Rails.root, 'log', "#{Rails.env}_json.log")
|
Add structured logging for Rails processes
This introduces JSON logging for Rails views saved to a file called
`development_json.log`, `production_json.log`, etc.
For example, instead of this unparsable log:
```
Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
Rendered text template within layouts/application (0.0ms)
Rendered layouts/_assets.html.erb (2.0ms)
Rendered layouts/_top.html.erb (2.6ms)
Rendered layouts/_about.html.erb (0.3ms)
Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)
```
We get a single line with this:
```
{"method":"GET","path":"/,"format":"html","controller":"HomeController","action":"index","status":200,"duration":79,"view":78.8,"db":0.0,"location":"http://localhost/","time":"2017-07-18 09:35:17 -0700"}
```
Part of #20060
2017-07-17 18:54:13 -04:00
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
Rails.application.configure do
|
|
|
|
config.lograge.enabled = true
|
|
|
|
# Store the lograge JSON files in a separate file
|
|
|
|
config.lograge.keep_original_rails_log = Gitlab::Utils.to_boolean(ENV.fetch('UNSTRUCTURED_RAILS_LOG', 'true'))
|
|
|
|
# Don't use the Logstash formatter since this requires logstash-event, an
|
|
|
|
# unmaintained gem that monkey patches `Time`
|
|
|
|
config.lograge.formatter = Lograge::Formatters::Json.new
|
|
|
|
config.lograge.logger = ActiveSupport::Logger.new(filename)
|
|
|
|
config.lograge.before_format = lambda do |data, payload|
|
|
|
|
data.delete(:error)
|
|
|
|
data[:db_duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:db)) if data[:db]
|
|
|
|
data[:view_duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:view)) if data[:view]
|
|
|
|
data[:duration_s] = Gitlab::Utils.ms_to_round_sec(data.delete(:duration)) if data[:duration]
|
2021-06-01 17:10:06 -04:00
|
|
|
data[:location] = Gitlab::Utils.removes_sensitive_data_from_url(data[:location]) if data[:location]
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
# Remove empty hashes to prevent type mismatches
|
|
|
|
# These are set to empty hashes in Lograge's ActionCable subscriber
|
|
|
|
# https://github.com/roidrage/lograge/blob/v0.11.2/lib/lograge/log_subscribers/action_cable.rb#L14-L16
|
|
|
|
%i(method path format).each do |key|
|
|
|
|
data[key] = nil if data[key] == {}
|
|
|
|
end
|
2020-09-21 08:09:34 -04:00
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
data
|
|
|
|
end
|
2019-11-18 13:06:53 -05:00
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
# This isn't a user-reachable controller; we use it to check for a
|
|
|
|
# valid CSRF token in the API
|
|
|
|
config.lograge.ignore_actions = ['Gitlab::RequestForgeryProtection::Controller#index']
|
2020-01-24 13:09:00 -05:00
|
|
|
|
2020-10-05 11:08:56 -04:00
|
|
|
# Add request parameters to log output
|
|
|
|
config.lograge.custom_options = Gitlab::Lograge::CustomOptions
|
|
|
|
end
|
Add structured logging for Rails processes
This introduces JSON logging for Rails views saved to a file called
`development_json.log`, `production_json.log`, etc.
For example, instead of this unparsable log:
```
Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
Rendered text template within layouts/application (0.0ms)
Rendered layouts/_assets.html.erb (2.0ms)
Rendered layouts/_top.html.erb (2.6ms)
Rendered layouts/_about.html.erb (0.3ms)
Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)
```
We get a single line with this:
```
{"method":"GET","path":"/,"format":"html","controller":"HomeController","action":"index","status":200,"duration":79,"view":78.8,"db":0.0,"location":"http://localhost/","time":"2017-07-18 09:35:17 -0700"}
```
Part of #20060
2017-07-17 18:54:13 -04:00
|
|
|
end
|
|
|
|
end
|