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'

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/datadog/kit/appsec/events.rb', line 108

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
end

.track_login_failure(trace = nil, span = nil, 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) (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)

    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.



56
57
58
59
60
61
62
63
64
65
# File 'lib/datadog/kit/appsec/events.rb', line 56

def (trace = nil, span = nil, user_id:, user_exists:, **others)
  set_trace_and_span_context('track_login_failure', trace, span) do |active_trace, active_span|
    raise ArgumentError, 'user_id cannot be nil' if user_id.nil?

    track(LOGIN_FAILURE_EVENT, active_trace, active_span, **others)

    active_span.set_tag('appsec.events.users.login.failure.usr.id', 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.



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

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)

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

    track(LOGIN_SUCCESS_EVENT, active_trace, active_span, **others)

    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.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datadog/kit/appsec/events.rb', line 81

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)

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

    track(SIGNUP_EVENT, active_trace, active_span, **others)

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