# encoding: utf-8 LibraryDetection.defer do named :typhoeus depends_on do defined?(Typhoeus) && defined?(Typhoeus::VERSION) end depends_on do OneApm::Agent::Instrumentation::TyphoeusTracing.is_supported_version? end executes do OneApm::Manager.logger.info 'Installing Typhoeus instrumentation' require 'one_apm/agent/cross_app/cross_app_tracing' require 'one_apm/support/http_clients/typhoeus_wrappers' end # Basic request tracing executes do Typhoeus.before do |request| OneApm::Agent::Instrumentation::TyphoeusTracing.trace(request) # Ensure that we always return a truthy value from the before block, # otherwise Typhoeus will bail out of the instrumentation. true end end # Apply single TT node for Hydra requests until async support executes do class Typhoeus::Hydra include OneApm::Support::MethodTracer def run_with_oneapm(*args) trace_execution_scoped("External/Multiple/Typhoeus::Hydra/run") do run_without_oneapm(*args) end end alias run_without_oneapm run alias run run_with_oneapm end end end module OneApm::Agent::Instrumentation::TyphoeusTracing OA_EARLIEST_VERSION = OneApm::VersionNumber.new("0.5.3") def self.is_supported_version? OneApm::VersionNumber.new(Typhoeus::VERSION) >= OneApm::Agent::Instrumentation::TyphoeusTracing::OA_EARLIEST_VERSION end def self.request_is_hydra_enabled?(request) request.respond_to?(:hydra) && request.hydra end def self.trace(request) state = OneApm::TransactionState.tl_get if state.is_execution_traced? && !request_is_hydra_enabled?(request) wrapped_request = ::OneApm::Support::HTTPClients::TyphoeusHTTPRequest.new(request) t0 = Time.now segment = ::OneApm::Agent::CrossAppTracing.start_trace(state, t0, wrapped_request) callback = Proc.new do wrapped_response = ::OneApm::Support::HTTPClients::TyphoeusHTTPResponse.new(request.response) ::OneApm::Agent::CrossAppTracing.finish_trace(state, t0, segment, wrapped_request, wrapped_response) end request.on_complete.unshift(callback) end rescue => e OneApm::Manager.logger.error("Exception during trace setup for Typhoeus request", e) end end