Module: Datadog::Kit::AppSec::Events

Defined in:
lib/datadog/kit/appsec/events.rb

Overview

Tracking events

Constant Summary collapse

LOGIN_SUCCESS_EVENT =
'users.login.success'
LOGIN_FAILURE_EVENT =
'users.login.failure'
SIGNUP_EVENT =
'users.signup'
USER_LOGIN_KEYS =
['usr.login', :'usr.login'].freeze

Class Method Summary collapse

Class Method Details

.track(event, trace = nil, span = nil, **others) ⇒ Object

Attach custom event information to the trace

This method is experimental and may change in the future.

Parameters:

  • event (String)

    Mandatory. Event code.

  • trace (TraceOperation) (defaults to: nil)

    Trace to attach data to. Defaults to active trace.

  • span (SpanOperation) (defaults to: nil)

    Span to attach data to. Defaults to active span on trace. Note that this should be a service entry span. When AppSec is enabled, the expected span and trace are automatically used as defaults.

  • others (Hash<Symbol, String>)

    Additional free-form event information to attach to the trace. Key must not be :track.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/datadog/kit/appsec/events.rb', line 116

def track(event, trace = nil, span = nil, **others)
  if trace && span
    check_trace_span_integrity(trace, span)

    span.set_tag("appsec.events.#{event}.track", 'true')
    span.set_tag("_dd.appsec.events.#{event}.sdk", 'true')

    others.each do |k, v|
      raise ArgumentError, 'key cannot be :track' if k.to_sym == :track

      span.set_tag("appsec.events.#{event}.#{k}", v) unless v.nil?
    end

    trace.keep!
  else
    set_trace_and_span_context('track', trace, span) do |active_trace, active_span|
      active_span.set_tag("appsec.events.#{event}.track", 'true')
      active_span.set_tag("_dd.appsec.events.#{event}.sdk", 'true')

      others.each do |k, v|
        raise ArgumentError, 'key cannot be :track' if k.to_sym == :track

        active_span.set_tag("appsec.events.#{event}.#{k}", v) unless v.nil?
      end

      active_trace.keep!
    end
  end

  ::Datadog::AppSec::Instrumentation.gateway.push('appsec.events.user_lifecycle', event)
end

.track_login_failure(trace = nil, span = nil, user_exists:, user_id: nil, **others) ⇒ Object

Attach login failure event information to the trace

This method is experimental and may change in the future.

Parameters:

  • trace (TraceOperation) (defaults to: nil)

    Trace to attach data to. Defaults to active trace.

  • span (SpanOperation) (defaults to: nil)

    Span to attach data to. Defaults to active span on trace. Note that this should be a service entry span. When AppSec is enabled, the expected span and trace are automatically used as defaults.

  • user_id (String) (defaults to: nil)

    User id that attempted login

  • user_exists (bool)

    Whether the user id that did a login attempt exists.

  • others (Hash<String || Symbol, String>)

    Additional free-form event information to attach to the trace.



61
62
63
64
65
66
67
68
69
# File 'lib/datadog/kit/appsec/events.rb', line 61

def (trace = nil, span = nil, user_exists:, user_id: nil, **others)
  set_trace_and_span_context('track_login_failure', trace, span) do |active_trace, active_span|
    others[:'usr.login'] = user_id if user_id && !others.key?(:'usr.login') && !others.key?('usr.login')
    track(LOGIN_FAILURE_EVENT, active_trace, active_span, **others)

    active_span.set_tag('appsec.events.users.login.failure.usr.id', user_id) if user_id
    active_span.set_tag('appsec.events.users.login.failure.usr.exists', user_exists)
  end
end

.track_login_success(trace = nil, span = nil, user:, **others) ⇒ Object

Attach login success event information to the trace

This method is experimental and may change in the future.

Parameters:

  • trace (TraceOperation) (defaults to: nil)

    Trace to attach data to. Defaults to active trace.

  • span (SpanOperation) (defaults to: nil)

    Span to attach data to. Defaults to active span on trace. Note that this should be a service entry span. When AppSec is enabled, the expected span and trace are automatically used as defaults.

  • user (Hash<Symbol, String>)

    User information to pass to Datadog::Kit::Identity.set_user. Must contain at least :id as key.

  • others (Hash<String || Symbol, String>)

    Additional free-form event information to attach to the trace.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datadog/kit/appsec/events.rb', line 30

def (trace = nil, span = nil, user:, **others)
  set_trace_and_span_context('track_login_success', trace, span) do |active_trace, active_span|
    user_options = user.dup
    user_id = user_options.delete(:id)
     = user_options[:login] || others[:'usr.login'] || others['usr.login'] || user_id

    raise ArgumentError, 'missing required key: :user => { :id }' if user_id.nil?

    others = others.reject { |key, _| USER_LOGIN_KEYS.include?(key) }
    others[:'usr.login'] = 
    track(LOGIN_SUCCESS_EVENT, active_trace, active_span, **others)

    user_options[:login] = 
    Kit::Identity.set_user(active_trace, active_span, id: user_id, **user_options)
  end
end

.track_signup(trace = nil, span = nil, user:, **others) ⇒ Object

Attach signup event information to the trace

This method is experimental and may change in the future.

Parameters:

  • trace (TraceOperation) (defaults to: nil)

    Trace to attach data to. Defaults to active trace.

  • span (SpanOperation) (defaults to: nil)

    Span to attach data to. Defaults to active span on trace. Note that this should be a service entry span. When AppSec is enabled, the expected span and trace are automatically used as defaults.

  • user (Hash<Symbol, String>)

    User information to pass to Datadog::Kit::Identity.set_user. Must contain at least :id as key.

  • others (Hash<String || Symbol, String>)

    Additional free-form event information to attach to the trace.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/datadog/kit/appsec/events.rb', line 85

def (trace = nil, span = nil, user:, **others)
  set_trace_and_span_context('track_signup', trace, span) do |active_trace, active_span|
    user_options = user.dup
    user_id = user_options.delete(:id)
     = user_options[:login] || others[:'usr.login'] || others['usr.login'] || user_id

    raise ArgumentError, 'missing required key: :user => { :id }' if user_id.nil?

    others = others.reject { |key, _| USER_LOGIN_KEYS.include?(key) }
    others[:'usr.login'] = 
    track(SIGNUP_EVENT, active_trace, active_span, **others)

    user_options[:login] = 
    Kit::Identity.set_user(trace, id: user_id, **user_options)
  end
end