Module: Datadog::Tracing::Contrib::Elasticsearch::Quantize
- Defined in:
- lib/datadog/tracing/contrib/elasticsearch/quantize.rb
Overview
Quantize contains ES-specific resource quantization tools.
Constant Summary collapse
- PLACEHOLDER =
'?'.freeze
- ID_PLACEHOLDER =
'\1?'.freeze
- EXCLUDE_KEYS =
[].freeze
- SHOW_KEYS =
[:_index, :_type, :_id].freeze
- DEFAULT_OPTIONS =
{ exclude: EXCLUDE_KEYS, show: SHOW_KEYS, placeholder: PLACEHOLDER }.freeze
Class Method Summary collapse
- .format_body(body, options = {}) ⇒ Object
- .format_body!(body, options = {}) ⇒ Object
- .format_url(url) ⇒ Object
- .merge_options(original, additional) ⇒ Object
-
.reserialize_json(string, fail_value = PLACEHOLDER) ⇒ Object
Parses a JSON object from a string, passes its value to the block provided, and dumps its result back to JSON.
-
.sanitize_fragment_with_id(url) ⇒ Object
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.
Class Method Details
.format_body(body, options = {}) ⇒ Object
26 27 28 29 30 |
# File 'lib/datadog/tracing/contrib/elasticsearch/quantize.rb', line 26 def format_body(body, = {}) format_body!(body, ) rescue StandardError [:placeholder] || PLACEHOLDER end |
.format_body!(body, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/datadog/tracing/contrib/elasticsearch/quantize.rb', line 32 def format_body!(body, = {}) = (DEFAULT_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, [:placeholder]) do |obj| Contrib::Utils::Quantization::Hash.format(obj, ) end end.join("\n") end |
.format_url(url) ⇒ Object
21 22 23 24 |
# File 'lib/datadog/tracing/contrib/elasticsearch/quantize.rb', line 21 def format_url(url) sanitize_fragment_with_id(url) .gsub(/(?:\d+)/, PLACEHOLDER) end |
.merge_options(original, additional) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/datadog/tracing/contrib/elasticsearch/quantize.rb', line 46 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 [:exclude] = (original[:exclude] || []).dup.concat(additional[:exclude] || []).uniq end end |
.reserialize_json(string, fail_value = 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.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/datadog/tracing/contrib/elasticsearch/quantize.rb', line 64 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) ⇒ Object
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.
78 79 80 |
# File 'lib/datadog/tracing/contrib/elasticsearch/quantize.rb', line 78 def sanitize_fragment_with_id(url) url.gsub(%r{^(/?[^/]*/[^/]*/)(?:[^?/\d]*\d+[^?/]*)}, ID_PLACEHOLDER) end |