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'

Class Method Summary collapse

Class Method Details

.track(event, trace, **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)

    Trace to attach data to.

  • others (Hash<Symbol, String>)

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



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

def self.track(event, trace, **others)
  trace.set_tag("appsec.events.#{event}.track", 'true')

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

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

  trace.keep!
end

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

Attach login failure event information to the trace

This method is experimental and may change in the future.

Parameters:

  • trace (TraceOperation)

    Trace to attach data to.

  • user_id (String)

    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.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
# File 'lib/datadog/kit/appsec/events.rb', line 42

def self.(trace, user_id:, user_exists:, **others)
  track(LOGIN_FAILURE_EVENT, trace, **others)

  raise ArgumentError, 'user_id cannot be nil' if user_id.nil?

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

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

Attach login success event information to the trace

This method is experimental and may change in the future.

Parameters:

  • trace (TraceOperation)

    Trace to attach data to.

  • 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.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
# File 'lib/datadog/kit/appsec/events.rb', line 22

def self.(trace, user:, **others)
  track(LOGIN_SUCCESS_EVENT, trace, **others)

  user_options = user.dup
  user_id = user_options.delete(:id)

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

  Kit::Identity.set_user(trace, id: user_id, **user_options)
end