Class: Contrast::Utils::MetricsHash

Inherits:
Hash show all
Includes:
Components::Logger::InstanceMethods
Defined in:
lib/contrast/utils/metrics_hash.rb

Overview

This is the MetricsHash, which will take data type, so we now what is introduced/included in the TelemetryEvent

Constant Summary collapse

ERROR_MESSAGES =
[
  'The key is not string or does not meet the requirements.',
  'The key extends the allowed length.',
  'The provided value is not the right data type'
].cs__freeze
KEY_REGEXP =
/[a-zA-Z0-9._-]{1,63}/.cs__freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Constructor Details

#initialize(data_type, *several_variants) ⇒ MetricsHash

Returns a new instance of MetricsHash.



22
23
24
25
# File 'lib/contrast/utils/metrics_hash.rb', line 22

def initialize data_type, *several_variants
  super
  @data_type = data_type
end

Instance Attribute Details

#data_typeObject (readonly)

Returns the value of attribute data_type.



13
14
15
# File 'lib/contrast/utils/metrics_hash.rb', line 13

def data_type
  @data_type
end

Instance Method Details

#[]=(key, value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/contrast/utils/metrics_hash.rb', line 27

def []= key, value
  key_val = key.dup
  value_val = value.dup
  key_val.strip! if key_val.cs__is_a?(String)
  value_val.strip! if value_val.cs__is_a?(String)
  return unless valid_pair?(key_val, value_val)

  key_val.downcase!
  key_val.strip!
  super(key_val, value_val)
end

#valid_pair?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/contrast/utils/metrics_hash.rb', line 39

def valid_pair? key, value
  if !key.cs__is_a?(String) || (KEY_REGEXP =~ key).nil?
    logger.warn('The following key will be omitted', key: key, error: ERROR_MESSAGES[0])
    return false
  end
  unless key.length <= 28
    logger.warn('The following key will be omitted', key: key, error: ERROR_MESSAGES[1])
    return false
  end
  unless value.cs__is_a?(data_type)
    logger.warn('The following key will be omitted', value: value, error: ERROR_MESSAGES[2])
    return false
  end
  return false if value.cs__is_a?(String) && value.empty?
  return false if value.cs__is_a?(String) && value.length > 200

  true
end