module NewRelic::Agent::Instrumentation::RackBuilder

Constants

INSTRUMENTATION_NAME

Public Class Methods

track_deferred_detection(builder_class) click to toggle source
# File lib/new_relic/agent/instrumentation/rack/instrumentation.rb, line 11
def self.track_deferred_detection(builder_class)
  class << builder_class
    attr_accessor :_nr_deferred_detection_ran
  end
  builder_class._nr_deferred_detection_ran = false
end

Public Instance Methods

check_for_late_instrumentation(app) click to toggle source
# File lib/new_relic/agent/instrumentation/rack/instrumentation.rb, line 26
def check_for_late_instrumentation(app)
  return if defined?(@checked_for_late_instrumentation) && @checked_for_late_instrumentation

  @checked_for_late_instrumentation = true
  if middleware_instrumentation_enabled?
    if ::NewRelic::Agent::Instrumentation::MiddlewareProxy.needs_wrapping?(app)
      ::NewRelic::Agent.logger.info("We weren't able to instrument all of your Rack middlewares.",
        "To correct this, ensure you 'require \"newrelic_rpm\"' before setting up your middleware stack.")
    end
  end
end
deferred_dependency_check() click to toggle source
# File lib/new_relic/agent/instrumentation/rack/instrumentation.rb, line 18
def deferred_dependency_check
  return if self.class._nr_deferred_detection_ran

  NewRelic::Agent.logger.info('Doing deferred dependency-detection before Rack startup')
  DependencyDetection.detect!
  self.class._nr_deferred_detection_ran = true
end
middleware_instrumentation_enabled?() click to toggle source
# File lib/new_relic/agent/instrumentation/rack/instrumentation.rb, line 49
def middleware_instrumentation_enabled?
  ::NewRelic::Agent::Instrumentation::RackHelpers.middleware_instrumentation_enabled?
end
run_with_tracing(app) { |app| ... } click to toggle source
# File lib/new_relic/agent/instrumentation/rack/instrumentation.rb, line 53
def run_with_tracing(app)
  return yield(app) unless middleware_instrumentation_enabled?

  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  yield(::NewRelic::Agent::Instrumentation::MiddlewareProxy.wrap(app, true))
end
use_with_tracing(middleware_class) { |middleware_class| ... } click to toggle source
# File lib/new_relic/agent/instrumentation/rack/instrumentation.rb, line 61
def use_with_tracing(middleware_class)
  return if middleware_class.nil?
  return yield(middleware_class) unless middleware_instrumentation_enabled?

  NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

  yield(::NewRelic::Agent::Instrumentation::MiddlewareProxy.for_class(middleware_class))
end
with_deferred_dependency_detection() { || ... } click to toggle source

We patch the to_app method for a reason that actually has nothing to do with instrumenting rack itself. It happens to be a convenient and easy-to-hook point that happens late in the startup sequence of almost every application, making it a good place to do a final call to DependencyDetection.detect!, since all libraries are likely loaded at this point.

# File lib/new_relic/agent/instrumentation/rack/instrumentation.rb, line 44
def with_deferred_dependency_detection
  deferred_dependency_check
  yield.tap { |result| check_for_late_instrumentation(result) }
end