@@ -10,6 +10,7 @@ module Devise
1010 # A Rack middleware capable of tracking currently signed user
1111 class TrackingMiddleware
1212 WARDEN_KEY = 'warden'
13+ SESSION_ID_KEY = 'session_id'
1314
1415 def initialize ( app )
1516 @app = app
@@ -32,16 +33,28 @@ def call(env)
3233 return @app . call ( env )
3334 end
3435
36+ # NOTE: Rails session id will be set for unauthenticated users as well,
37+ # so we need to make sure we are tracking only authenticated users.
3538 id = transform ( extract_id ( env [ WARDEN_KEY ] ) )
39+ session_id = env [ WARDEN_KEY ] . raw_session [ SESSION_ID_KEY ] if id
40+
3641 if id
37- unless context . span . has_tag? ( Ext ::TAG_USR_ID )
38- context . span [ Ext ::TAG_USR_ID ] = id
42+ # NOTE: There is no option to set session id without setting user id via SDK.
43+ unless context . span . has_tag? ( Ext ::TAG_USR_ID ) && context . span . has_tag? ( Ext ::TAG_SESSION_ID )
44+ user_id = context . span [ Ext ::TAG_USR_ID ] || id
45+ user_session_id = context . span [ Ext ::TAG_SESSION_ID ] || session_id
46+
47+ # FIXME: The current implementation of event arguments is forsing us
48+ # to bloat User class, and pass nil-value instead of skip
49+ # passing them at first place.
50+ # This is a temporary situation until we refactor events model.
3951 AppSec ::Instrumentation . gateway . push (
40- 'identity.set_user' , AppSec ::Instrumentation ::Gateway ::User . new ( id , nil )
52+ 'identity.set_user' , AppSec ::Instrumentation ::Gateway ::User . new ( user_id , nil , user_session_id )
4153 )
4254 end
4355
44- context . span [ Ext ::TAG_DD_USR_ID ] = id . to_s
56+ context . span [ Ext ::TAG_USR_ID ] ||= id
57+ context . span [ Ext ::TAG_DD_USR_ID ] = id
4558 context . span [ Ext ::TAG_DD_COLLECTION_MODE ] ||= Configuration . auto_user_instrumentation_mode
4659 end
4760
0 commit comments