Module: Contrast::Utils::Assess::EventLimitUtils

Overview

EventLimitUtils is used to check and validate the number of source, propagation, or trigger events collected during the reporting time frame

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Instance Method Details

#event_limit?(policy) ⇒ Boolean

Checks to see if the event limit for the policy type has been met or exceeded



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/contrast/utils/assess/event_limit_utils.rb', line 17

def event_limit? policy
  return false unless (context = Contrast::Agent::REQUEST_TRACKER.current)

  if policy.source_node
    max =  ::Contrast::ASSESS.max_context_source_events
    return at_limit?(policy, context.source_event_count, max, context)
  end

  if policy.propagation_node
    max = ::Contrast::ASSESS.max_propagation_events
    return at_limit?(policy, context.propagation_event_count, max, context)
  end

  false # policy does not have limit
end

#event_limit_for_rule?(rule_id) ⇒ Boolean

rubocop:disable Metrics/AbcSize



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/contrast/utils/assess/event_limit_utils.rb', line 33

def event_limit_for_rule? rule_id # rubocop:disable Metrics/AbcSize
  return false unless (context = Contrast::Agent::REQUEST_TRACKER.current)

  saved_request_ids = rule_counts.keys.map { |k| k.to_s.split('_')[1].to_i }

  # if we passed the threshold and we actually have records for that request - wipe them
  if saved_request_ids.uniq.include?(context.request.__id__)
    restore_defaults
    threshold_time_limit
  end

  # if we have recorded rule counts, but none of them are for the current request_id
  # eventually we can try and play with the time_limit_threshold -> DEFAULT_MAX_RULE_TIME_THRESHOLD
  if !rule_counts.empty? && !saved_request_ids.include?(context.request.__id__)
    restore_defaults
    threshold_time_limit
  end

  rule_key = "#{ rule_id }_#{ context.request.__id__ }"
  rule_counts[rule_key] += 1
  rule_counts[rule_key] >= ::Contrast::ASSESS.max_rule_reported
end

#increment_event_count(node) ⇒ Object

Increments the event count for the type of event that is being tracked



59
60
61
62
63
64
# File 'lib/contrast/utils/assess/event_limit_utils.rb', line 59

def increment_event_count node
  return unless (context = Contrast::Agent::REQUEST_TRACKER.current)

  context.source_event_count += 1 if node.cs__is_a?(Contrast::Agent::Assess::Policy::SourceNode)
  context.propagation_event_count += 1 if node.cs__is_a?(Contrast::Agent::Assess::Policy::PropagationNode)
end