From a0979b1d57e7e28c0f8babbc47c8239518614022 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 7 Aug 2018 16:13:40 +0200 Subject: [PATCH] Fix logstasher custom fields --- app/controllers/application_controller.rb | 35 ++++++++++--------- config/initializers/logstasher.rb | 14 ++------ .../application_controller_spec.rb | 15 +++----- 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5f13b18e2..2ee12840f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -75,38 +75,37 @@ class ApplicationController < ActionController::Base ].compact end + def logged_user + logged_users.first + end + def logged_user_roles roles = logged_users.map { |logged_user| logged_user.class.name } roles.any? ? roles.join(', ') : 'Guest' end - def logged_user_info - logged_user = logged_users.first - - if logged_user - { - id: logged_user.id, - email: logged_user.email - } - end - end - def set_raven_context + user = logged_user + context = { ip_address: request.ip, + id: user&.id, + email: user&.email, roles: logged_user_roles - } - context.merge!(logged_user_info || {}) + }.compact Raven.user_context(context) end - def append_info_to_payload(payload) - payload.merge!({ + def session_info_payload + user = logged_user + + payload = { user_agent: request.user_agent, - current_user: logged_user_info, + current_user_id: user&.id, + current_user_email: user&.email, current_user_roles: logged_user_roles - }.compact) + }.compact if browser.known? payload.merge!({ @@ -115,6 +114,8 @@ class ApplicationController < ActionController::Base platform: browser.platform.name, }) end + + payload end def reject diff --git a/config/initializers/logstasher.rb b/config/initializers/logstasher.rb index d85bb8d6a..0b14a8ede 100644 --- a/config/initializers/logstasher.rb +++ b/config/initializers/logstasher.rb @@ -3,17 +3,7 @@ if LogStasher.enabled fields[:type] = "tps" end - LogStasher.watch('process_action.action_controller') do |name, start, finish, id, payload, store| - store[:user_agent] = payload[:user_agent] - store[:browser] = payload[:browser] - store[:browser_version] = payload[:browser_version] - store[:platform] = payload[:platform] - - store[:current_user_roles] = payload[:current_user_roles] - - if payload[:current_user].present? - store[:current_user_id] = payload[:current_user][:id] - store[:current_user_email] = payload[:current_user][:email] - end + LogStasher.add_custom_fields_to_request_context do |fields| + fields.merge!(session_info_payload) end end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index ce2dbc1c1..c264994bb 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -17,7 +17,7 @@ describe ApplicationController, type: :controller do let(:current_gestionnaire) { nil } let(:current_administrateur) { nil } let(:current_administration) { nil } - let(:payload) { {} } + let(:payload) { @controller.send(:session_info_payload) } before do expect(@controller).to receive(:current_user).and_return(current_user) @@ -27,7 +27,6 @@ describe ApplicationController, type: :controller do allow(Raven).to receive(:user_context) @controller.send(:set_raven_context) - @controller.send(:append_info_to_payload, payload) end context 'when no one is logged in' do @@ -50,10 +49,8 @@ describe ApplicationController, type: :controller do it do expect(payload).to eq({ user_agent: 'Rails Testing', - current_user: { - id: current_user.id, - email: current_user.email - }, + current_user_id: current_user.id, + current_user_email: current_user.email, current_user_roles: 'User' }) end @@ -73,10 +70,8 @@ describe ApplicationController, type: :controller do it do expect(payload).to eq({ user_agent: 'Rails Testing', - current_user: { - id: current_user.id, - email: current_user.email - }, + current_user_id: current_user.id, + current_user_email: current_user.email, current_user_roles: 'User, Gestionnaire, Administrateur, Administration' }) end