Module: Datadog::Quantization::Hash
- Defined in:
- lib/ddtrace/quantization/hash.rb
Overview
Quantization for HTTP resources
Constant Summary collapse
- PLACEHOLDER =
'?'.freeze
- EXCLUDE_KEYS =
[].freeze
- SHOW_KEYS =
[].freeze
- DEFAULT_OPTIONS =
{ exclude: EXCLUDE_KEYS, show: SHOW_KEYS, placeholder: PLACEHOLDER }.freeze
Class Method Summary collapse
- .convert_value(value) ⇒ Object
- .format(hash_obj, options = {}) ⇒ Object
- .format!(hash_obj, options = {}) ⇒ Object
- .format_array(value, options) ⇒ Object
- .format_hash(hash_obj, options = {}) ⇒ Object
- .format_value(value, options = {}) ⇒ Object
- .indifferent_equals(value) ⇒ Object
- .merge_options(original, additional) ⇒ Object
Class Method Details
.convert_value(value) ⇒ Object
99 100 101 |
# File 'lib/ddtrace/quantization/hash.rb', line 99 def convert_value(value) value.is_a?(Symbol) ? value.to_s : value end |
.format(hash_obj, options = {}) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/ddtrace/quantization/hash.rb', line 17 def format(hash_obj, = {}) ||= {} format!(hash_obj, ) rescue StandardError [:placeholder] || PLACEHOLDER end |
.format!(hash_obj, options = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/ddtrace/quantization/hash.rb', line 24 def format!(hash_obj, = {}) ||= {} = (DEFAULT_OPTIONS, ) format_hash(hash_obj, ) end |
.format_array(value, options) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/ddtrace/quantization/hash.rb', line 62 def format_array(value, ) if value.any? { |v| v.class <= ::Hash || v.class <= Array } first_entry = format_value(value.first, ) value.size > 1 ? [first_entry, [:placeholder]] : [first_entry] # Otherwise short-circuit and return single placeholder else [[:placeholder]] end end |
.format_hash(hash_obj, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ddtrace/quantization/hash.rb', line 30 def format_hash(hash_obj, = {}) case hash_obj when ::Hash return {} if [:exclude] == :all return hash_obj if [:show] == :all hash_obj.each_with_object({}) do |(key, value), quantized| if [:show].any?(&indifferent_equals(key)) quantized[key] = value elsif [:exclude].none?(&indifferent_equals(key)) quantized[key] = format_value(value, ) end end else format_value(hash_obj, ) end end |
.format_value(value, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ddtrace/quantization/hash.rb', line 48 def format_value(value, = {}) return value if [:show] == :all case value when ::Hash format_hash(value, ) when Array # If any are objects, format them. format_array(value, ) else [:placeholder] end end |
.indifferent_equals(value) ⇒ Object
94 95 96 97 |
# File 'lib/ddtrace/quantization/hash.rb', line 94 def indifferent_equals(value) value = convert_value(value) ->(compared_value) { value == convert_value(compared_value) } end |
.merge_options(original, additional) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ddtrace/quantization/hash.rb', line 72 def (original, additional) {}.tap do || # Show # If either is :all, value becomes :all [: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 [:exclude] = if original[:exclude] == :all || additional[:exclude] == :all :all else (original[:exclude] || []).dup.concat(additional[:exclude] || []).uniq end [:placeholder] = additional[:placeholder] || original[:placeholder] end end |