module Datadog::Quantization::Hash

Quantization for HTTP resources

Constants

DEFAULT_OPTIONS
EXCLUDE_KEYS
PLACEHOLDER
SHOW_KEYS

Public Instance Methods

convert_value(value) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 98
def convert_value(value)
  value.is_a?(Symbol) ? value.to_s : value
end
format(hash_obj, options = {}) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 16
def format(hash_obj, options = {})
  options ||= {}
  format!(hash_obj, options)
rescue StandardError
  options[:placeholder] || PLACEHOLDER
end
format!(hash_obj, options = {}) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 23
def format!(hash_obj, options = {})
  options ||= {}
  options = merge_options(DEFAULT_OPTIONS, options)
  format_hash(hash_obj, options)
end
format_array(value, options) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 61
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 = {}) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 29
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 = {}) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 47
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) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 93
def indifferent_equals(value)
  value = convert_value(value)
  ->(compared_value) { value == convert_value(compared_value) }
end
merge_options(original, additional) click to toggle source
# File lib/ddtrace/quantization/hash.rb, line 71
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