# encoding: utf-8 module ::Excon module Middleware class OneApmCrossAppTracing TRACE_DATA_IVAR = :@oneapm_trace_data def initialize(stack) @stack = stack end def request_call(datum) #THREAD_LOCAL_ACCESS begin # Only instrument this request if we haven't already done so, because # we can get request_call multiple times for requests marked as # :idempotent in the options, but there will be only a single # accompanying response_call/error_call. if datum[:connection] && !datum[:connection].instance_variable_get(TRACE_DATA_IVAR) wrapped_request = ::OneApm::Support::HTTPClients::ExconHTTPRequest.new(datum) state = ::OneApm::TransactionState.tl_get t0 = Time.now segment = ::OneApm::Agent::CrossAppTracing.start_trace(state, t0, wrapped_request) datum[:connection].instance_variable_set(TRACE_DATA_IVAR, [t0, segment, wrapped_request]) end rescue => e OneApm::Agent.logger.debug(e) end @stack.request_call(datum) end def response_call(datum) finish_trace(datum) @stack.response_call(datum) end def error_call(datum) finish_trace(datum) @stack.error_call(datum) end def finish_trace(datum) #THREAD_LOCAL_ACCESS trace_data = datum[:connection] && datum[:connection].instance_variable_get(TRACE_DATA_IVAR) if trace_data datum[:connection].instance_variable_set(TRACE_DATA_IVAR, nil) t0, segment, wrapped_request = trace_data if datum[:response] wrapped_response = ::OneApm::Support::HTTPClients::ExconHTTPResponse.new(datum[:response]) end state = ::OneApm::TransactionState.tl_get ::OneApm::Agent::CrossAppTracing.finish_trace(state, t0, segment, wrapped_request, wrapped_response) end end end end end