Class: Contrast::Agent::Telemetry::ExceptionHash

Inherits:
Hash
  • Object
show all
Includes:
Components::Logger::InstanceMethods
Defined in:
lib/contrast/agent/telemetry/exception_hash.rb

Overview

This is the Telemetry::Hash, which will store Contrast::Agent::Telemetry::Exception::Event, so we can push freely, without worrying about validating the event before that. Telemetry::Hash has a max size of events, default is 10 events

Constant Summary collapse

HASH_SIZE_LIMIT =
10

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Instance Method Details

#[]=(key, value) ⇒ Object?

Wrapper to set a value in this Telemetry::Hash only if the provided value is of the data_type for this Telemetry::Hash or the hash has not reached its limit of for unique keys

Parameters:

  • key (Object)

    the key to which to associate the value

  • value (Object)

    the value to store

Returns:

  • (Object, nil)

    echo back out the value as the Hash#[]= method does, or nil if not of the expected data_type



24
25
26
27
28
29
30
31
32
33
# File 'lib/contrast/agent/telemetry/exception_hash.rb', line 24

def []= key, value
  # If telemetry is not running, do not add more as we want to avoid a memory leak.
  return unless Contrast::Agent.telemetry_queue&.running?
  # If the Hash is full, do not add more as we want to avoid consuming all application resources.
  return if exception_limit?
  # If the given value is of unexpected type, do not add it to avoid issues later where type is assumed.
  return unless valid_value?(value)

  super(key, value)
end

#exception_limit?Boolean

Determine if hash has reached exception event limit

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/contrast/agent/telemetry/exception_hash.rb', line 47

def exception_limit?
  unless size < HASH_SIZE_LIMIT
    logger.debug("Number of TelemetryExceptions exceeds limit of #{ HASH_SIZE_LIMIT }")
    return true
  end
  false
end

#increment(key) ⇒ Object

Increment the occurrences for the exception object contained in this Telemetry::Hash

Parameters:

  • key (Object)

    the key to check for the exception stored in this Telemetry::Hash



38
39
40
41
42
43
# File 'lib/contrast/agent/telemetry/exception_hash.rb', line 38

def increment key
  value = self[key]
  return unless value&.exceptions&.any?

  value.exceptions[0].increment_occurrences
end