Module: Datadog::Tracing::Contrib::Utils::Quantization::Hash

Defined in:
lib/datadog/tracing/contrib/utils/quantization/hash.rb

Overview

Quantization for Hash

Constant Summary collapse

PLACEHOLDER =
'?'
EXCLUDE_KEYS =
[].freeze
SHOW_KEYS =
[].freeze
DEFAULT_OPTIONS =
{ # steep:ignore IncompatibleAssignment
  exclude: EXCLUDE_KEYS,
  show: SHOW_KEYS,
  placeholder: PLACEHOLDER
}.freeze

Class Method Summary collapse

Class Method Details

.convert_value(value) ⇒ Object



105
106
107
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 105

def convert_value(value)
  value.is_a?(Symbol) ? value.to_s : value
end

.format(hash_obj, options = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 23

def format(hash_obj, options = {})
  options ||= {}
  format!(hash_obj, options)
rescue
  options[:placeholder] || PLACEHOLDER
end

.format!(hash_obj, options = {}) ⇒ Object



30
31
32
33
34
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 30

def format!(hash_obj, options = {})
  options ||= {}
  options = merge_options(DEFAULT_OPTIONS, options)
  format_hash(hash_obj, options)
end

.format_array(value, options) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 68

def format_array(value, options)
  if value.any? { |v| v.class <= ::Hash || v.class <= Array }
    first_entry = format_value(value.first, options)
    (value.size > 1) ? [first_entry, options[:placeholder]] : [first_entry]
    # Otherwise short-circuit and return single placeholder
  else
    [options[:placeholder]]
  end
end

.format_hash(hash_obj, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 36

def format_hash(hash_obj, options = {})
  case hash_obj
  when ::Hash
    return {} if options[:exclude] == :all
    return hash_obj if options[:show] == :all

    hash_obj.each_with_object({}) do |(key, value), quantized|
      if options[:show].any?(&indifferent_equals(key))
        quantized[key] = value
      elsif options[:exclude].none?(&indifferent_equals(key))
        quantized[key] = format_value(value, options)
      end
    end
  else
    format_value(hash_obj, options)
  end
end

.format_value(value, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 54

def format_value(value, options = {})
  return value if options[:show] == :all

  case value
  when ::Hash
    format_hash(value, options)
  when Array
    # If any are objects, format them.
    format_array(value, options)
  else
    options[:placeholder]
  end
end

.indifferent_equals(value) ⇒ Object



100
101
102
103
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 100

def indifferent_equals(value)
  value = convert_value(value)
  ->(compared_value) { value == convert_value(compared_value) }
end

.merge_options(original, additional) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/datadog/tracing/contrib/utils/quantization/hash.rb', line 78

def merge_options(original, additional)
  {}.tap do |options|
    # Show
    # If either is :all, value becomes :all
    options[:show] = if original[:show] == :all || additional[:show] == :all
      :all
    else
      (original[:show] || []).dup.concat(additional[:show] || []).uniq
    end

    # Exclude
    # If either is :all, value becomes :all
    options[:exclude] = if original[:exclude] == :all || additional[:exclude] == :all
      :all
    else
      (original[:exclude] || []).dup.concat(additional[:exclude] || []).uniq
    end

    options[:placeholder] = additional[:placeholder] || original[:placeholder]
  end
end