-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for async-http http client
- Loading branch information
1 parent
6633f52
commit 04835d9
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
lib/newrelic_security/instrumentation-security/async-http/chain.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module NewRelic::Security | ||
module Instrumentation | ||
module AsyncHttp | ||
module Chain | ||
def self.instrument! | ||
::Async::HTTP::Internet.class_eval do | ||
include NewRelic::Security::Instrumentation::AsyncHttp | ||
|
||
alias_method :call_without_security, :call | ||
|
||
def call(method, url, headers = nil, body = nil) | ||
retval = nil | ||
event = call_on_enter(method, url, headers = nil, body = nil) { retval = call_without_security(method, url, headers, body) } | ||
call_on_exit(event) { return retval } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
lib/newrelic_security/instrumentation-security/async-http/instrumentation.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require_relative 'prepend' | ||
require_relative 'chain' | ||
require 'uri' | ||
|
||
module NewRelic::Security | ||
module Instrumentation | ||
module AsyncHttp | ||
|
||
def call_on_enter(method, url, headers, body) | ||
event = nil | ||
NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}" | ||
ob = {} | ||
ob[:Method] = method | ||
uri = ::URI.parse url | ||
ob[:scheme] = uri.scheme | ||
ob[:host] = uri.host | ||
ob[:port] = uri.port | ||
ob[:URI] = uri.to_s | ||
ob[:path] = uri.path | ||
ob[:query] = uri.query | ||
ob[:Body] = body.respond_to?(:join) ? body.join.to_s : body.to_s | ||
ob[:Headers] = headers.to_h | ||
ob.each { |_, value| value.dup.force_encoding(ISO_8859_1).encode(UTF_8) if value.is_a?(String) } | ||
event = NewRelic::Security::Agent::Control::Collector.collect(HTTP_REQUEST, [ob]) | ||
NewRelic::Security::Instrumentation::InstrumentationUtils.append_tracing_data(headers, event) if event | ||
event | ||
rescue => exception | ||
NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}" | ||
ensure | ||
yield | ||
return event | ||
end | ||
|
||
def call_on_exit(event) | ||
NewRelic::Security::Agent.logger.debug "OnExit : #{self.class}.#{__method__}" | ||
NewRelic::Security::Agent::Utils.create_exit_event(event) | ||
rescue => exception | ||
NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}" | ||
ensure | ||
yield | ||
end | ||
end | ||
end | ||
end | ||
|
||
NewRelic::Security::Instrumentation::InstrumentationLoader.install_instrumentation(:async_http, ::Async::HTTP::Internet, ::NewRelic::Security::Instrumentation::AsyncHttp) |
16 changes: 16 additions & 0 deletions
16
lib/newrelic_security/instrumentation-security/async-http/prepend.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module NewRelic::Security | ||
module Instrumentation | ||
module AsyncHttp | ||
module Prepend | ||
include NewRelic::Security::Instrumentation::AsyncHttp | ||
|
||
def call(method, url, headers = nil, body = nil) | ||
retval = nil | ||
event = call_on_enter(method, url, headers, body) { retval = super } | ||
call_on_exit(event) { return retval } | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains 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