module Datadog::Contrib::Elasticsearch::Quantize

Quantize contains ES-specific resource quantization tools.

Constants

DEFAULT_OPTIONS
EXCLUDE_KEYS
ID_PLACEHOLDER
PLACEHOLDER
SHOW_KEYS

Public Instance Methods

format_body(body, options = {}) click to toggle source
# File lib/ddtrace/contrib/elasticsearch/quantize.rb, line 23
def format_body(body, options = {})
  format_body!(body, options)
rescue StandardError
  options[:placeholder] || PLACEHOLDER
end
format_body!(body, options = {}) click to toggle source
# File lib/ddtrace/contrib/elasticsearch/quantize.rb, line 29
def format_body!(body, options = {})
  options = merge_options(DEFAULT_OPTIONS, options)

  # Determine if bulk query or not, based on content
  statements = body.end_with?("\n") ? body.split("\n") : [body]

  # Parse each statement and quantize them.
  statements.collect do |string|
    reserialize_json(string, options[:placeholder]) do |obj|
      Datadog::Quantization::Hash.format(obj, options)
    end
  end.join("\n")
end
format_url(url) click to toggle source
# File lib/ddtrace/contrib/elasticsearch/quantize.rb, line 18
def format_url(url)
  sanitize_fragment_with_id(url)
    .gsub(/(?:[\d]+)/, PLACEHOLDER)
end
merge_options(original, additional) click to toggle source
# File lib/ddtrace/contrib/elasticsearch/quantize.rb, line 43
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
    options[:exclude] = (original[:exclude] || []).dup.concat(additional[:exclude] || []).uniq
  end
end
reserialize_json(string, fail_value = PLACEHOLDER) { |parse| ... } click to toggle source

Parses a JSON object from a string, passes its value to the block provided, and dumps its result back to JSON. If JSON parsing fails, it prints fail_value.

# File lib/ddtrace/contrib/elasticsearch/quantize.rb, line 61
def reserialize_json(string, fail_value = PLACEHOLDER)
  return string unless block_given?
  begin
    JSON.dump(yield(JSON.parse(string)))
  rescue JSON::ParserError
    # If it can't parse/dump, don't raise an error.
    fail_value
  end
end
sanitize_fragment_with_id(url) click to toggle source

Sanitizes URL fragment by changing it to ? whenever a number is detected This is meant as simple heuristic that attempts to detect if particular fragment represents document Id. This is meant to reduce the cardinality in most frequent cases.

# File lib/ddtrace/contrib/elasticsearch/quantize.rb, line 74
def sanitize_fragment_with_id(url)
  url.gsub(%r{^(/?[^/]*/[^/]*/)(?:[^\?/\d]*[\d]+[^\?/]*)}, ID_PLACEHOLDER)
end