Module: Datadog::Quantization::HTTP

Defined in:
lib/ddtrace/quantization/http.rb

Overview

Quantization for HTTP resources

Constant Summary collapse

PLACEHOLDER =
'?'.freeze

Class Method Summary collapse

Class Method Details

.query(query, options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/ddtrace/quantization/http.rb', line 33

def query(query, options = {})
  query!(query, options)
rescue StandardError
  options[:placeholder] || PLACEHOLDER
end

.query!(query, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ddtrace/quantization/http.rb', line 39

def query!(query, options = {})
  options ||= {}
  options[:show] = options[:show] || []
  options[:exclude] = options[:exclude] || []

  # Short circuit if query string is meant to exclude everything
  # or if the query string is meant to include everything
  return '' if options[:exclude] == :all
  return query if options[:show] == :all

  collect_query(query, uniq: true) do |key, value|
    if options[:exclude].include?(key)
      [nil, nil]
    else
      value = options[:show].include?(key) ? value : nil
      [key, value]
    end
  end
end

.url(url, options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/ddtrace/quantization/http.rb', line 12

def url(url, options = {})
  url!(url, options)
rescue StandardError
  options[:placeholder] || PLACEHOLDER
end

.url!(url, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ddtrace/quantization/http.rb', line 18

def url!(url, options = {})
  options ||= {}

  URI.parse(url).tap do |uri|
    # Format the query string
    if uri.query
      query = query(uri.query, options[:query])
      uri.query = (!query.nil? && query.empty? ? nil : query)
    end

    # Remove any URI framents
    uri.fragment = nil unless options[:fragment] == :show
  end.to_s
end