Last active
October 7, 2022 01:46
-
-
Save qhwa/eaab071c5a49880eb713 to your computer and use it in GitHub Desktop.
log and record HTTParty request time in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: config/initialize/httparty.rb | |
require 'httparty' | |
module HTTParty | |
class Request | |
alias_method :_original_perform, :perform | |
def perform(&block) | |
payload = { | |
method: http_method.const_get(:METHOD), | |
url: uri | |
} | |
ActiveSupport::Notifications.instrument 'request.httparty', payload do | |
_original_perform(&block) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: config/initialize/rpm.rb | |
ActiveSupport::Notifications.subscribe('request.httparty') do |name, start, ending, transaction_id, payload| | |
event = ActiveSupport::Notifications::Event.new(name, start, ending, transaction_id, payload) | |
Rails.logger.info " HTTParty ".bold << "#{event.payload[:method]} #{event.payload[:url]} (#{event.duration}ms)" | |
Thread.current[:http_runtime] ||= 0 | |
Thread.current[:http_runtime] += event.duration | |
payload[:http_runtime] = event.duration | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🙏