Module: Datadog::Tracing::Contrib::OpenSearch::Quantize

Defined in:
lib/datadog/tracing/contrib/opensearch/quantize.rb

Overview

Removes sensitive data from OpenSearch strings (i.e. url and body).

Constant Summary collapse

PLACEHOLDER =
'/?'
DEFAULT_PLACEHOLDER =
'?'
EXCLUDE_KEYS =
[].freeze
SHOW_KEYS =
[:_index, :_type, :_id].freeze
DEFAULT_OPTIONS =
{
  exclude: EXCLUDE_KEYS,
  show: SHOW_KEYS,
  placeholder: PLACEHOLDER
}.freeze

Class Method Summary collapse

Class Method Details

.format_body(body, options = {}) ⇒ Object

Use Elasticsearch implementation



29
30
31
32
33
# File 'lib/datadog/tracing/contrib/opensearch/quantize.rb', line 29

def format_body(body, options = {})
  format_body!(body, options)
rescue StandardError
  options[:placeholder] || DEFAULT_PLACEHOLDER
end

.format_body!(body, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/datadog/tracing/contrib/opensearch/quantize.rb', line 35

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|
      Contrib::Utils::Quantization::Hash.format(obj, options)
    end
  end.join("\n")
end

.format_url(url) ⇒ Object



23
24
25
26
# File 'lib/datadog/tracing/contrib/opensearch/quantize.rb', line 23

def format_url(url)
  # Url does not contain path, so just replace last digit(s) with '?' if '/' precedes the digit(s)
  url.to_s.gsub(%r{/\d+$}, PLACEHOLDER)
end

.merge_options(original, additional) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/datadog/tracing/contrib/opensearch/quantize.rb', line 49

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 = DEFAULT_PLACEHOLDER) ⇒ Object

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.



67
68
69
70
71
72
73
74
75
76
# File 'lib/datadog/tracing/contrib/opensearch/quantize.rb', line 67

def reserialize_json(string, fail_value = DEFAULT_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