Module: Datadog::Tracing::Contrib::Utils::Quantization::HTTP
- Defined in:
- lib/datadog/tracing/contrib/utils/quantization/http.rb
Overview
Quantization for HTTP resources
Constant Summary collapse
- PLACEHOLDER =
"?"- RFC3986_URL_BASE =
taken from Ruby https://github.com/ruby/uri/blob/eaf89cc31619d49e67c64d0b58ea9dc38892d175/lib/uri/rfc3986_parser.rb but adjusted to parse only
:// : / components and stop there, since we don't care about the path, query string, and fragment components /\A(?<URI>(?<scheme>[A-Za-z][+\-.0-9A-Za-z]*+):(?<hier-part>\/\/(?<authority>(?:(?<userinfo>(?:%\h\h|[!$&-.0-;=A-Z_a-z~])*+)@)?(?<host>(?<IP-literal>\[(?:(?<IPv6address>(?:\h{1,4}:){6}(?<ls32>\h{1,4}:\h{1,4}|(?<IPv4address>(?<dec-octet>[1-9]\d|1\d{2}|2[0-4]\d|25[0-5]|\d)\.\g<dec-octet>\.\g<dec-octet>\.\g<dec-octet>))|::(?:\h{1,4}:){5}\g<ls32>|\h{1,4}?::(?:\h{1,4}:){4}\g<ls32>|(?:(?:\h{1,4}:)?\h{1,4})?::(?:\h{1,4}:){3}\g<ls32>|(?:(?:\h{1,4}:){,2}\h{1,4})?::(?:\h{1,4}:){2}\g<ls32>|(?:(?:\h{1,4}:){,3}\h{1,4})?::\h{1,4}:\g<ls32>|(?:(?:\h{1,4}:){,4}\h{1,4})?::\g<ls32>|(?:(?:\h{1,4}:){,5}\h{1,4})?::\h{1,4}|(?:(?:\h{1,4}:){,6}\h{1,4})?::)|(?<IPvFuture>v\h++\.[!$&-.0-;=A-Z_a-z~]++))\])|\g<IPv4address>|(?<reg-name>(?:%\h\h|[!$&-.0-9;=A-Z_a-z~])*+))(?::(?<port>\d*+))?)))(?:\/|\z)/.freeze
- OBFUSCATOR_WITH =
"<redacted>"- OBFUSCATOR_REGEX =
rubocop:disable Layout/LineLength
%r{ (?: # JSON-ish leading quote (?:"|%22)? ) (?: # common keys (?:old[-_]?|new[-_]?)?p(?:ass)?w(?:or)?d(?:1|2)? # pw, password variants |pass(?:[-_]?phrase)? # pass, passphrase variants |secret |(?: # key, key_id variants api[-_]? |private[-_]? |public[-_]? |access[-_]? |secret[-_]? |app(?:lication)?[-_]? )key(?:[-_]?id)? |token |consumer[-_]?(?:id|key|secret) |sign(?:ed|ature)? |auth(?:entication|orization)? ) (?: # '=' query string separator, plus value til next '&' separator (?:\s|%20)*(?:=|%3D)[^&]+ # JSON-ish '": "somevalue"', key being handled with case above, without the opening '"' |(?:"|%22) # closing '"' at end of key (?:\s|%20)*(?::|%3A)(?:\s|%20)* # ':' key-value spearator, with surrounding spaces (?:"|%22) # opening '"' at start of value (?:%2[^2]|%[^2]|[^"%])+ # value (?:"|%22) # closing '"' at end of value ) |(?: # other common secret values bearer(?:\s|%20)+[a-z0-9._-]+ |token(?::|%3A)[a-z0-9]{13} |gh[opsu]_[0-9a-zA-Z]{36} |(?:^|[^\w%-]|%[0-9a-f]{2})ey[I-L][\w-]+(?:=|%3D)*\.ey[I-L][\w-]+(?:=|%3D)*(?:\.(?:[\w.+/=-]|%3D|%2F|%2B)+)? |-{5}BEGIN(?:[a-z\s]|%20)+PRIVATE(?:\s|%20)KEY-{5}[^-]+-{5}END(?:[a-z\s]|%20)+PRIVATE(?:\s|%20)KEY(?:-{5})?(?:\n|%0A)? |(?:ssh-(?:rsa|dss)|ecdsa-[a-z0-9]+-[a-z0-9]+)(?:\s|%20|%09)+(?:[a-z0-9/.+]|%2F|%5C|%2B){100,}(?:=|%3D)*(?:(?:\s|%20|%09)+[a-z0-9._-]+)? ) }ix.freeze
Class Method Summary collapse
- .base_url(url, options = {}) ⇒ Object
- .client_resource(method, path, enabled:) ⇒ Object
- .decode_path(path) ⇒ Object
- .quantize_path(path) ⇒ Object
- .query(query, options = {}) ⇒ Object
- .query!(query, options = {}) ⇒ Object
- .url(url, options = {}) ⇒ Object
- .url!(url, options = {}) ⇒ Object
Class Method Details
.base_url(url, options = {}) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 119 def self.base_url(url, = {}) if (m = RFC3986_URL_BASE.match(url)) m[1] else "" end end |
.client_resource(method, path, enabled:) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 21 def self.client_resource(method, path, enabled:) return method unless enabled && path resource = method.dup resource << (+" ").force_encoding(resource.encoding) quantized_path = quantize_path(decode_path(path)) resource << quantized_path.force_encoding(resource.encoding) end |
.decode_path(path) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 30 def self.decode_path(path) decoded = String.new(encoding: Encoding::BINARY) index = 0 while index < path.bytesize byte = path.getbyte(index) break unless byte if byte == 37 && index + 2 < path.bytesize high_byte = path.getbyte(index + 1) low_byte = path.getbyte(index + 2) if high_byte && low_byte high = decode_hex_byte(high_byte) low = decode_hex_byte(low_byte) if high && low decoded << high * 16 + low index += 3 next end end end decoded << byte index += 1 end decoded.force_encoding(path.encoding) end |
.quantize_path(path) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 59 def self.quantize_path(path) slash = (+"/").force_encoding(path.encoding) normalized = if path.getbyte(0) == 47 path else String.new(encoding: path.encoding) << slash << path end output = String.new(encoding: path.encoding) index = 1 replacements = 0 while index < normalized.bytesize segment_end = index allowed = 0 digits = 0 specials = 0 while segment_end < normalized.bytesize && normalized.getbyte(segment_end) != 47 byte = normalized.getbyte(segment_end) break unless byte if byte.between?(48, 57) digits += 1 elsif byte.between?(65, 90) || byte.between?(97, 122) || byte == 45 || byte == 95 allowed += 1 else specials += 1 end segment_end += 1 end output << slash segment = normalized.byteslice(index, segment_end - index) || String.new(encoding: path.encoding) if normalized.getbyte(index) == 118 && allowed == 1 && digits > 0 && specials.zero? output << segment elsif specials > 0 || digits > 0 output << (+"*").force_encoding(path.encoding) replacements += 1 else output << segment end index = segment_end + 1 end return normalized.dup if replacements.zero? output << slash if normalized.getbyte(normalized.bytesize - 1) == 47 output end |
.query(query, options = {}) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 148 def self.query(query, = {}) query!(query, ) rescue return "" if && [:obfuscate] [:placeholder] || PLACEHOLDER end |
.query!(query, options = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 156 def self.query!(query, = {}) ||= {} [:obfuscate] = {} if [:obfuscate] == :internal [:show] = [:show] || ([:obfuscate] ? :all : []) [:exclude] = [:exclude] || [] # Short circuit if query string is meant to exclude everything # or if the query string is meant to include everything return "" if [:exclude] == :all unless [:show] == :all && !([:obfuscate] && [:exclude]) query = collect_query(query, uniq: true) do |key, value| if [:exclude].include?(key) [nil, nil] else value = ([:show] == :all || [:show].include?(key)) ? value : nil [key, value] end end end [:obfuscate] ? obfuscate_query(query, [:obfuscate]) : query end |
.url(url, options = {}) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 111 def self.url(url, = {}) url!(url, ) rescue placeholder = [:placeholder] || PLACEHOLDER ([:base] == :exclude) ? placeholder : "#{base_url(url)}/#{placeholder}" end |
.url!(url, options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/datadog/tracing/contrib/utils/quantization/http.rb', line 127 def self.url!(url, = {}) ||= {} URI.parse(url).tap do |uri| # Format the query string if uri.query query = query(uri.query, [:query]) uri.query = ((!query.nil? && query.empty?) ? nil : query) end # Remove any URI fragments uri.fragment = nil unless [:fragment] == :show if [:base] == :exclude uri.host = nil uri.port = nil uri.scheme = nil end end.to_s end |