Sha256: 8f9cd94c9fbe56d1a61c171c3805b5aff35de10a46aef4f8071d7a6ff4dd5f00

Contents?: true

Size: 1.9 KB

Versions: 39

Compression:

Stored size: 1.9 KB

Contents

# 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)
        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::Manager.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)
        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

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
oneapm_rpm-1.2.9.rc1 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.8 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.8.rc1 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.7 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.7.rc1 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.6 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.6.rc1 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.5 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.5.rc1 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.4 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.4.rc1 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.3 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.3.rc2 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.2 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.2.rc lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.1 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.1.rc lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.2.0 lib/one_apm/inst/http_clients/excon/middleware.rb
oneapm_rpm-1.1.3 lib/one_apm/inst/http_clients/excon/middleware.rb