Fix logstasher custom fields

This commit is contained in:
Paul Chavard 2018-08-07 16:13:40 +02:00
parent 63866a47b5
commit a0979b1d57
3 changed files with 25 additions and 39 deletions

View file

@ -75,38 +75,37 @@ class ApplicationController < ActionController::Base
].compact ].compact
end end
def logged_user
logged_users.first
end
def logged_user_roles def logged_user_roles
roles = logged_users.map { |logged_user| logged_user.class.name } roles = logged_users.map { |logged_user| logged_user.class.name }
roles.any? ? roles.join(', ') : 'Guest' roles.any? ? roles.join(', ') : 'Guest'
end 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 def set_raven_context
user = logged_user
context = { context = {
ip_address: request.ip, ip_address: request.ip,
id: user&.id,
email: user&.email,
roles: logged_user_roles roles: logged_user_roles
} }.compact
context.merge!(logged_user_info || {})
Raven.user_context(context) Raven.user_context(context)
end end
def append_info_to_payload(payload) def session_info_payload
payload.merge!({ user = logged_user
payload = {
user_agent: request.user_agent, 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 current_user_roles: logged_user_roles
}.compact) }.compact
if browser.known? if browser.known?
payload.merge!({ payload.merge!({
@ -115,6 +114,8 @@ class ApplicationController < ActionController::Base
platform: browser.platform.name, platform: browser.platform.name,
}) })
end end
payload
end end
def reject def reject

View file

@ -3,17 +3,7 @@ if LogStasher.enabled
fields[:type] = "tps" fields[:type] = "tps"
end end
LogStasher.watch('process_action.action_controller') do |name, start, finish, id, payload, store| LogStasher.add_custom_fields_to_request_context do |fields|
store[:user_agent] = payload[:user_agent] fields.merge!(session_info_payload)
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
end end
end end

View file

@ -17,7 +17,7 @@ describe ApplicationController, type: :controller do
let(:current_gestionnaire) { nil } let(:current_gestionnaire) { nil }
let(:current_administrateur) { nil } let(:current_administrateur) { nil }
let(:current_administration) { nil } let(:current_administration) { nil }
let(:payload) { {} } let(:payload) { @controller.send(:session_info_payload) }
before do before do
expect(@controller).to receive(:current_user).and_return(current_user) 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) allow(Raven).to receive(:user_context)
@controller.send(:set_raven_context) @controller.send(:set_raven_context)
@controller.send(:append_info_to_payload, payload)
end end
context 'when no one is logged in' do context 'when no one is logged in' do
@ -50,10 +49,8 @@ describe ApplicationController, type: :controller do
it do it do
expect(payload).to eq({ expect(payload).to eq({
user_agent: 'Rails Testing', user_agent: 'Rails Testing',
current_user: { current_user_id: current_user.id,
id: current_user.id, current_user_email: current_user.email,
email: current_user.email
},
current_user_roles: 'User' current_user_roles: 'User'
}) })
end end
@ -73,10 +70,8 @@ describe ApplicationController, type: :controller do
it do it do
expect(payload).to eq({ expect(payload).to eq({
user_agent: 'Rails Testing', user_agent: 'Rails Testing',
current_user: { current_user_id: current_user.id,
id: current_user.id, current_user_email: current_user.email,
email: current_user.email
},
current_user_roles: 'User, Gestionnaire, Administrateur, Administration' current_user_roles: 'User, Gestionnaire, Administrateur, Administration'
}) })
end end