Class: Datadog::AppSec::Contrib::Devise::TrackingMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/appsec/contrib/devise/tracking_middleware.rb

Overview

A Rack middleware capable of tracking currently signed user

Constant Summary collapse

WARDEN_KEY =
'warden'

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ TrackingMiddleware

Returns a new instance of TrackingMiddleware.



14
15
16
17
# File 'lib/datadog/appsec/contrib/devise/tracking_middleware.rb', line 14

def initialize(app)
  @app = app
  @devise_session_scope_keys = {}
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/datadog/appsec/contrib/devise/tracking_middleware.rb', line 19

def call(env)
  return @app.call(env) unless AppSec.enabled?
  return @app.call(env) unless Configuration.auto_user_instrumentation_enabled?
  return @app.call(env) unless AppSec.active_context

  unless env.key?(WARDEN_KEY)
    Datadog.logger.debug { 'AppSec: unable to track requests, due to missing warden manager' }
    return @app.call(env)
  end

  context = AppSec.active_context
  if context.trace.nil? || context.span.nil?
    Datadog.logger.debug { 'AppSec: unable to track requests, due to missing trace or span' }
    return @app.call(env)
  end

  id = transform(extract_id(env[WARDEN_KEY]))
  if id
    unless context.span.has_tag?(Ext::TAG_USR_ID)
      context.span[Ext::TAG_USR_ID] = id
      AppSec::Instrumentation.gateway.push(
        'identity.set_user', AppSec::Instrumentation::Gateway::User.new(id, nil)
      )
    end

    context.span[Ext::TAG_DD_USR_ID] = id.to_s
    context.span[Ext::TAG_DD_COLLECTION_MODE] ||= Configuration.auto_user_instrumentation_mode
  end

  @app.call(env)
end