Merge pull request #1283 from tchak/add-user-info-to-logstasher
Add user info to LogStasher
This commit is contained in:
commit
859de6168b
3 changed files with 84 additions and 12 deletions
|
@ -51,22 +51,54 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_raven_context
|
def logged_users
|
||||||
context = { ip_address: request.ip }
|
@logged_users ||= [
|
||||||
|
|
||||||
logged_models = [
|
|
||||||
current_user,
|
current_user,
|
||||||
current_gestionnaire,
|
current_gestionnaire,
|
||||||
current_administrateur,
|
current_administrateur,
|
||||||
current_administration
|
current_administration
|
||||||
].compact
|
].compact
|
||||||
|
end
|
||||||
|
|
||||||
context[:email] = logged_models.first&.email
|
def logged_user_roles
|
||||||
context[:id] = logged_models.first&.id
|
roles = logged_users.map { |logged_user| logged_user.class.name }
|
||||||
|
roles.any? ? roles.join(', ') : 'Guest'
|
||||||
|
end
|
||||||
|
|
||||||
class_names = logged_models.map { |model| model.class.name }
|
def logged_user_info
|
||||||
context[:classes] = class_names.any? ? class_names.join(', ') : 'Guest'
|
logged_user = logged_users.first
|
||||||
|
|
||||||
|
if logged_user
|
||||||
|
{
|
||||||
|
id: logged_user.id,
|
||||||
|
email: logged_user.email
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_raven_context
|
||||||
|
context = {
|
||||||
|
ip_address: request.ip,
|
||||||
|
roles: logged_user_roles
|
||||||
|
}
|
||||||
|
context.merge!(logged_user_info || {})
|
||||||
|
|
||||||
Raven.user_context(context)
|
Raven.user_context(context)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def append_info_to_payload(payload)
|
||||||
|
payload.merge!({
|
||||||
|
user_agent: request.user_agent,
|
||||||
|
current_user: logged_user_info,
|
||||||
|
current_user_roles: logged_user_roles
|
||||||
|
}.compact)
|
||||||
|
|
||||||
|
if browser.known?
|
||||||
|
payload.merge!({
|
||||||
|
browser: browser.name,
|
||||||
|
browser_version: browser.version.to_s,
|
||||||
|
platform: browser.platform.name,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,4 +2,18 @@ if LogStasher.enabled
|
||||||
LogStasher.add_custom_fields do |fields|
|
LogStasher.add_custom_fields do |fields|
|
||||||
fields[:type] = "tps"
|
fields[:type] = "tps"
|
||||||
end
|
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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,11 +12,12 @@ describe ApplicationController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'set_raven_context' do
|
describe 'set_raven_context and append_info_to_payload' do
|
||||||
let(:current_user) { nil }
|
let(:current_user) { nil }
|
||||||
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) { {} }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
expect(@controller).to receive(:current_user).and_return(current_user)
|
expect(@controller).to receive(:current_user).and_return(current_user)
|
||||||
|
@ -26,13 +27,16 @@ 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
|
||||||
it do
|
it do
|
||||||
expect(Raven).to have_received(:user_context)
|
expect(Raven).to have_received(:user_context)
|
||||||
.with({ ip_address: '0.0.0.0', email: nil, id: nil, classes: 'Guest' })
|
.with({ ip_address: '0.0.0.0', roles: 'Guest' })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it { expect(payload).to eq({ user_agent: 'Rails Testing', current_user_roles: 'Guest' }) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when a user is logged in' do
|
context 'when a user is logged in' do
|
||||||
|
@ -40,7 +44,18 @@ describe ApplicationController, type: :controller do
|
||||||
|
|
||||||
it do
|
it do
|
||||||
expect(Raven).to have_received(:user_context)
|
expect(Raven).to have_received(:user_context)
|
||||||
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, classes: 'User' })
|
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, roles: 'User' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it do
|
||||||
|
expect(payload).to eq({
|
||||||
|
user_agent: 'Rails Testing',
|
||||||
|
current_user: {
|
||||||
|
id: current_user.id,
|
||||||
|
email: current_user.email
|
||||||
|
},
|
||||||
|
current_user_roles: 'User'
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -52,7 +67,18 @@ describe ApplicationController, type: :controller do
|
||||||
|
|
||||||
it do
|
it do
|
||||||
expect(Raven).to have_received(:user_context)
|
expect(Raven).to have_received(:user_context)
|
||||||
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, classes: 'User, Gestionnaire, Administrateur, Administration' })
|
.with({ ip_address: '0.0.0.0', email: current_user.email, id: current_user.id, roles: 'User, Gestionnaire, Administrateur, Administration' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it do
|
||||||
|
expect(payload).to eq({
|
||||||
|
user_agent: 'Rails Testing',
|
||||||
|
current_user: {
|
||||||
|
id: current_user.id,
|
||||||
|
email: current_user.email
|
||||||
|
},
|
||||||
|
current_user_roles: 'User, Gestionnaire, Administrateur, Administration'
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue