Module: Profiler::Instrumentation::NetHttpInstrumentation
- Defined in:
- lib/profiler/instrumentation/net_http_instrumentation.rb
Defined Under Namespace
Modules: RequestPatch
Constant Summary collapse
- SKIP_HOSTS =
%w[127.0.0.1 localhost ::1].freeze
- TEXT_CONTENT_TYPES =
/\A(text\/|application\/(json|xml|xhtml|javascript|x-www-form-urlencoded)|image\/svg)/i- BINARY_CONTENT_TYPES =
/\A(image\/|application\/pdf|application\/octet-stream|application\/zip|audio\/|video\/)/i
Class Method Summary collapse
- .decompress_body(body, content_encoding) ⇒ Object
- .extract_backtrace ⇒ Object
- .install! ⇒ Object
- .process_body(body, content_type) ⇒ Object
- .skip_host?(host) ⇒ Boolean
Class Method Details
.decompress_body(body, content_encoding) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/profiler/instrumentation/net_http_instrumentation.rb', line 131 def self.decompress_body(body, content_encoding) return body if content_encoding.empty? || body.nil? || body.empty? case content_encoding when "gzip", "x-gzip" Zlib::GzipReader.new(StringIO.new(body)).read when "deflate" Zlib::Inflate.inflate(body) else body end rescue StandardError body end |
.extract_backtrace ⇒ Object
159 160 161 162 163 |
# File 'lib/profiler/instrumentation/net_http_instrumentation.rb', line 159 def self.extract_backtrace caller_locations(5, 40) .reject { |l| l.path.to_s.include?("net/http") || l.path.to_s.include?("profiler/instrumentation") } .map { |l| "#{l.path}:#{l.lineno}:in `#{l.label}`" } end |
.install! ⇒ Object
120 121 122 123 124 |
# File 'lib/profiler/instrumentation/net_http_instrumentation.rb', line 120 def self.install! return if @installed Net::HTTP.prepend(RequestPatch) @installed = true end |
.process_body(body, content_type) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/profiler/instrumentation/net_http_instrumentation.rb', line 146 def self.process_body(body, content_type) return { body: nil, encoding: "text" } if body.nil? || body.empty? mime = content_type.split(";").first.to_s.strip if mime.match?(BINARY_CONTENT_TYPES) { body: Base64.strict_encode64(body.b), encoding: "base64" } else text = body.encode("UTF-8", invalid: :replace, undef: :replace, replace: "?") { body: text, encoding: "text" } end end |
.skip_host?(host) ⇒ Boolean
126 127 128 129 |
# File 'lib/profiler/instrumentation/net_http_instrumentation.rb', line 126 def self.skip_host?(host) SKIP_HOSTS.include?(host) || Profiler.configuration.http_skip_hosts.any? { |p| host.match?(p) } end |