# encoding: utf-8 require 'one_apm/support/method_tracer' require 'one_apm/transaction' require 'one_apm/transaction/transaction_state' require 'one_apm/inst/support/queue_time' require 'one_apm/inst/transaction_base' # This module is intended to be included into both MiddlewareWrapper and our # internal middleware classes. # # Host classes must define two methods: # # * target: returns the original middleware being traced # * category: returns the category for the resulting agent transaction # should be either :middleware or :rack # * transaction_options: returns an options hash to be passed to # Transaction.start when tracing this middleware. # # The target may be self, in which case the host class should define a # #traced_call method, instead of the usual #call. module OneApm::Rack module MiddlewareTracing TXN_STARTED_KEY = 'oneapm.transaction_started'.freeze unless defined?(TXN_STARTED_KEY) def _oa_has_middleware_tracing true end def build_transaction_options(env, first_middleware) opts = transaction_options opts = merge_first_middleware_options(opts, env) if first_middleware opts end def merge_first_middleware_options(opts, env) opts.merge( :request => ::Rack::Request.new(env), :apdex_start_time => OneApm::Agent::Instrumentation::QueueTime.parse_frontend_timestamp(env) ) end def note_transaction_started(env) env[TXN_STARTED_KEY] = true unless env[TXN_STARTED_KEY] end def capture_http_response_code(state, result) if result.is_a?(Array) && state.current_transaction state.current_transaction.http_response_code = result[0] end end def call(env) first_middleware = note_transaction_started(env) state = OneApm::TransactionState.tl_get begin options = build_transaction_options(env, first_middleware) current_transaction = OneApm::Transaction.start(state, category, options) events.notify(:before_call, env) if first_middleware result = (target == self) ? traced_call(env) : target.call(env) current_transaction.ignore_frames << options[:transaction_name] if respond_to?(:middleware_ignore?) && middleware_ignore? capture_http_response_code(state, result) events.notify(:after_call, env, result) if first_middleware result rescue Exception => e OneApm::Manager.notice_error(e) raise e ensure OneApm::Transaction.stop(state) end end def events OneApm::Manager.agent.events end end end