Merge branch 'main' from workbench2.git
[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       ClusterID: Rails.configuration.ClusterID,
13       request_id: event.payload[:request_id],
14       client_ipaddr: event.payload[:client_ipaddr],
15       client_auth: event.payload[:client_auth],
16     }
17
18     # Lograge adds exceptions not being rescued to event.payload, but we're
19     # catching all errors on ApplicationController so we look for backtraces
20     # elsewhere.
21     if !Thread.current[:backtrace].nil?
22       payload.merge!(
23         {
24           exception: Thread.current[:exception],
25           exception_backtrace: Thread.current[:backtrace],
26         }
27       )
28       Thread.current[:exception] = nil
29       Thread.current[:backtrace] = nil
30     end
31
32     exceptions = %w(controller action format id)
33     params = event.payload[:params].except(*exceptions)
34
35     # Omit secret_mounts field if supplied in create/update request
36     # body.
37     [
38       ['container', 'secret_mounts'],
39       ['container_request', 'secret_mounts'],
40     ].each do |resource, field|
41       if params[resource].is_a? Hash
42         params[resource] = params[resource].except(field)
43       end
44     end
45
46     # Redact new_user_token param in /arvados/v1/users/merge
47     # request. Log the auth UUID instead, if the token exists.
48     if params['new_user_token'].is_a? String
49       params['new_user_token_uuid'] =
50         ApiClientAuthorization.
51           where('api_token = ?', params['new_user_token']).
52           first.andand.uuid
53       params['new_user_token'] = '[...]'
54     end
55
56     params_s = SafeJSON.dump(params)
57     if params_s.length > Rails.configuration.SystemLogs["MaxRequestLogParamsSize"]
58       payload[:params_truncated] = params_s[0..Rails.configuration.SystemLogs["MaxRequestLogParamsSize"]] + "[...]"
59     else
60       payload[:params] = params
61     end
62     payload
63   end
64 end