Skip to content

Commit

Permalink
Net::HTTP patch optional by ENV variable
Browse files Browse the repository at this point in the history
  • Loading branch information
nateberkopec committed Apr 7, 2023
1 parent 5d07da7 commit d3fbf79
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -166,6 +166,14 @@ export RACK_MINI_PROFILER_PATCH="false"
# initializers/rack_profiler.rb: SqlPatches.patch %w(mongo)
```

#### Patching Net::HTTP

Other than databases, `rack-mini-profiler` applies a patch to `Net::HTTP`. You may want to disable this patch:

```bash
export RACK_MINI_PROFILER_PATCH_NET_HTTP="false"
```

### Flamegraphs

To generate [flamegraphs](http://samsaffron.com/archive/2013/03/19/flame-graphs-in-ruby-miniprofiler), add the [**stackprof**](https://rubygems.org/gems/stackprof) gem to your Gemfile.
Expand Down
35 changes: 18 additions & 17 deletions lib/patches/net_patches.rb
@@ -1,26 +1,27 @@
# frozen_string_literal: true

if (defined?(Net) && defined?(Net::HTTP))

if defined?(Rack::MINI_PROFILER_PREPEND_NET_HTTP_PATCH)
module NetHTTPWithMiniProfiler
def request(request, *args, &block)
Rack::MiniProfiler.step("Net::HTTP #{request.method} #{request.path}") do
super
if ENV['RACK_MINI_PROFILER_PATCH_NET_HTTP'] != 'false'
if (defined?(Net) && defined?(Net::HTTP))
if defined?(Rack::MINI_PROFILER_PREPEND_NET_HTTP_PATCH)
module NetHTTPWithMiniProfiler
def request(request, *args, &block)
Rack::MiniProfiler.step("Net::HTTP #{request.method} #{request.path}") do
super
end
end
end
end
Net::HTTP.prepend(NetHTTPWithMiniProfiler)
else
Net::HTTP.class_eval do
def request_with_mini_profiler(*args, &block)
request = args[0]
Rack::MiniProfiler.step("Net::HTTP #{request.method} #{request.path}") do
request_without_mini_profiler(*args, &block)
Net::HTTP.prepend(NetHTTPWithMiniProfiler)
else
Net::HTTP.class_eval do
def request_with_mini_profiler(*args, &block)
request = args[0]
Rack::MiniProfiler.step("Net::HTTP #{request.method} #{request.path}") do
request_without_mini_profiler(*args, &block)
end
end
alias request_without_mini_profiler request
alias request request_with_mini_profiler
end
alias request_without_mini_profiler request
alias request request_with_mini_profiler
end
end
end

0 comments on commit d3fbf79

Please sign in to comment.