Skip to content

Commit

Permalink
Merge pull request #2587 from DataDog/move-events-to-appsec-specific-…
Browse files Browse the repository at this point in the history
…namespace

Move events to appsec specific namespace
  • Loading branch information
lloeki authored Jan 27, 2023
2 parents b8f4cec + a2ba515 commit 4fd258b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 103 deletions.
75 changes: 75 additions & 0 deletions lib/datadog/kit/appsec/events.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# typed: false
# frozen_string_literal: true

require_relative '../identity'

module Datadog
module Kit
module AppSec
# Tracking events
module Events
LOGIN_SUCCESS_EVENT = 'users.login.success'
LOGIN_FAILURE_EVENT = 'users.login.failure'

# Attach login success event information to the trace
#
# This method is experimental and may change in the future.
#
# @param trace [TraceOperation] Trace to attach data to.
# @param user [Hash<Symbol, String>] User information to pass to
# Datadog::Kit::Identity.set_user. Must contain at least :id as key.
# @param others [Hash<String || Symbol, String>] Additional free-form
# event information to attach to the trace.
def self.track_login_success(trace, user:, **others)
track(LOGIN_SUCCESS_EVENT, trace, **others)

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

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

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

# Attach login failure event information to the trace
#
# This method is experimental and may change in the future.
#
# @param trace [TraceOperation] Trace to attach data to.
# @param user_id [String] User id that attempted login
# @param user_exists [bool] Whether the user id that did a login attempt exists.
# @param others [Hash<String || Symbol, String>] Additional free-form
# event information to attach to the trace.
def self.track_login_failure(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

# Attach custom event information to the trace
#
# This method is experimental and may change in the future.
#
# @param event [String] Mandatory. Event code.
# @param trace [TraceOperation] Trace to attach data to.
# @param others [Hash<Symbol, String>] Additional free-form
# event information to attach to the trace. Key must not
# be :track.
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
end
end
end
end
78 changes: 0 additions & 78 deletions lib/datadog/kit/events.rb

This file was deleted.

16 changes: 16 additions & 0 deletions sig/datadog/kit/appsec/events.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Datadog
module Kit
module AppSec
module Events
LOGIN_SUCCESS_EVENT: ::String
LOGIN_FAILURE_EVENT: ::String

def self.track_login_success: (Datadog::Tracing::TraceOperation trace, user: Hash[::Symbol, ::String | nil], **::Hash[::Symbol, ::String | nil] others) -> void

def self.track_login_failure: (Datadog::Tracing::TraceOperation trace, user_id: ::String, user_exists: bool, **::Hash[::Symbol, ::String | nil] others) -> void

def self.track: (::String | ::Symbol event, Datadog::Tracing::TraceOperation trace, **::Hash[::Symbol, ::String | nil] others) -> void
end
end
end
end
14 changes: 0 additions & 14 deletions sig/datadog/kit/events.rbs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
require 'time'

require 'datadog/tracing/trace_operation'
require 'datadog/kit/events'
require 'datadog/kit/appsec/events'

RSpec.describe Datadog::Kit::Events do
RSpec.describe Datadog::Kit::AppSec::Events do
subject(:trace_op) { Datadog::Tracing::TraceOperation.new }

let(:trace) { trace_op.flush! }
Expand Down Expand Up @@ -74,23 +74,16 @@
end

describe '#track' do
it 'rejects unexpected namespaces' do
trace_op.measure('root') do
expect { described_class.track(:foo, 'bar', trace_op) }.to raise_error ArgumentError, /namespace cannot be/
end
expect(meta).to_not include('foo.events.bar.track' => 'true')
end

it 'sets event tracking key on trace' do
trace_op.measure('root') do
described_class.track(:appsec, 'foo', trace_op)
described_class.track('foo', trace_op)
end
expect(meta).to include('appsec.events.foo.track' => 'true')
end

it 'sets other keys on trace' do
trace_op.measure('root') do
described_class.track(:appsec, 'foo', trace_op, bar: 'baz')
described_class.track('foo', trace_op, bar: 'baz')
end
expect(meta).to include('appsec.events.foo.bar' => 'baz')
end
Expand Down

0 comments on commit 4fd258b

Please sign in to comment.