Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Net::HTTP patch optional by ENV variable #568

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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