18001: address review comments.
[arvados.git] / services / api / config / initializers / lograge.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'safe_json'
6
7 Server::Application.configure do
8   config.lograge.enabled = true
9   config.lograge.formatter = Lograge::Formatters::Logstash.new
10   config.lograge.custom_options = lambda do |event|
11     payload = {
12       request_id: event.payload[:request_id],
13       client_ipaddr: event.payload[:client_ipaddr],
14       client_auth: event.payload[:client_auth],
15     }
16
17     # Lograge adds exceptions not being rescued to event.payload, but we're
18     # catching all errors on ApplicationController so we look for backtraces
19     # elsewhere.
20     if !Thread.current[:backtrace].nil?
21       payload.merge!(
22         {
23           exception: Thread.current[:exception],
24           exception_backtrace: Thread.current[:backtrace],
25         }
26       )
27       Thread.current[:exception] = nil
28       Thread.current[:backtrace] = nil
29     end
30
31     exceptions = %w(controller action format id)
32     params = event.payload[:params].except(*exceptions)
33
34     # Omit secret_mounts field if supplied in create/update request
35     # body.
36     [
37       ['container', 'secret_mounts'],
38       ['container_request', 'secret_mounts'],
39     ].each do |resource, field|
40       if params[resource].is_a? Hash
41         params[resource] = params[resource].except(field)
42       end
43     end
44
45     # Redact new_user_token param in /arvados/v1/users/merge
46     # request. Log the auth UUID instead, if the token exists.
47     if params['new_user_token'].is_a? String
48       params['new_user_token_uuid'] =
49         ApiClientAuthorization.
50           where('api_token = ?', params['new_user_token']).
51           first.andand.uuid
52       params['new_user_token'] = '[...]'
53     end
54
55     params_s = SafeJSON.dump(params)
56     if params_s.length > Rails.configuration.SystemLogs["MaxRequestLogParamsSize"]
57       payload[:params_truncated] = params_s[0..Rails.configuration.SystemLogs["MaxRequestLogParamsSize"]] + "[...]"
58     else
59       payload[:params] = params
60     end
61     payload
62   end
63 end