Skip to content

Commit

Permalink
rails/net_http: monkey-patch with prepend instead of method chaining
Browse files Browse the repository at this point in the history
Fixes #1077 (Airbrake's new Performance metrics instrumentation leads to
infinite loop with Datadog)

Since we don't care about Ruby 1.9 support, we can safely rely on `prepend` to
monkey-patch methods safely. This is more robust because nobody will overwrite
our implementation (unless they forget to call `super`) and we will also respect
other libraries that monkey-patch `Net::HTTP#request`.
  • Loading branch information
kyrylo committed Mar 31, 2020
1 parent 18ff165 commit d5276f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Airbrake Changelog

* ActiveJob: fix error reporting
([#1074](https://github.com/airbrake/airbrake/issues/1074))
* Fixed `Net::HTTP` performance breakdown when 3rd party code monkey-patches
`Net::HTTP#request` ([#1078](https://github.com/airbrake/airbrake/issues/1078))

### [v10.0.1][v10.0.1] (January 29, 2020)

Expand Down
20 changes: 13 additions & 7 deletions lib/airbrake/rails/net_http.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
# frozen_string_literal: true

# Monkey-patch Net::HTTP to benchmark it.
Net::HTTP.class_eval do
alias_method :request_without_airbrake, :request

def request(request, *args, &block)
Airbrake::Rack.capture_timing(:http) do
request_without_airbrake(request, *args, &block)
module Airbrake
module Rails
# Monkey-patch Net::HTTP to benchmark it.
# @api private
# @since v10.0.2
module NetHttp
def request(request, *args, &block)
Airbrake::Rack.capture_timing(:http) do
super(request, *args, &block)
end
end
end
end
end

Net::HTTP.prepend(Airbrake::Rails::NetHttp)

0 comments on commit d5276f4

Please sign in to comment.