module NewRelic::Agent::Instrumentation::MiddlewareTracing

Constants

CONTENT_LENGTH
CONTENT_TYPE
TXN_STARTED_KEY

Public Instance Methods

_nr_has_middleware_tracing() click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 33
def _nr_has_middleware_tracing
  true
end
build_transaction_options(env, first_middleware) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 37
def build_transaction_options(env, first_middleware)
  opts = transaction_options
  opts = merge_first_middleware_options(opts, env) if first_middleware
  opts
end
call(env) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 83
def call(env)
  first_middleware = note_transaction_started(env)

  state = NewRelic::Agent::Tracer.state

  begin
    options = build_transaction_options(env, first_middleware)

    finishable = Tracer.start_transaction_or_segment(
      name: options[:transaction_name],
      category: category,
      options: options
    )

    events.notify(:before_call, env) if first_middleware

    result = target == self ? traced_call(env) : target.call(env)

    if first_middleware
      capture_response_attributes(state, result)
      events.notify(:after_call, env, result)
    end

    result
  rescue Exception => e
    NewRelic::Agent.notice_error(e)
    raise e
  ensure
    finishable&.finish
  end
end
capture_http_response_code(state, result) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 55
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
capture_response_attributes(state, result) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 77
def capture_response_attributes(state, result)
  capture_http_response_code(state, result)
  capture_response_content_type(state, result)
  capture_response_content_length(state, result)
end
capture_response_content_length(state, result) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 68
def capture_response_content_length(state, result)
  if result.is_a?(Array) && state.current_transaction
    _, headers, _ = result
    length = headers[CONTENT_LENGTH]
    length = length.reduce(0) { |sum, h| sum + h.to_i } if length.is_a?(Array)
    state.current_transaction.response_content_length = length
  end
end
capture_response_content_type(state, result) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 61
def capture_response_content_type(state, result)
  if result.is_a?(Array) && state.current_transaction
    _, headers, _ = result
    state.current_transaction.response_content_type = headers[CONTENT_TYPE]
  end
end
events() click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 115
def events
  NewRelic::Agent.instance.events
end
merge_first_middleware_options(opts, env) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 43
def merge_first_middleware_options(opts, env)
  opts[:apdex_start_time] = QueueTime.parse_frontend_timestamp(env)
  # this case is for the rare occasion that an app is using Puma::Rack
  # without having ::Rack as a dependency
  opts[:request] = ::Rack::Request.new(env.dup) if defined? ::Rack
  opts
end
note_transaction_started(env) click to toggle source
# File lib/new_relic/agent/instrumentation/middleware_tracing.rb, line 51
def note_transaction_started(env)
  env[TXN_STARTED_KEY] = true unless env[TXN_STARTED_KEY]
end