class DependencyDetection::Dependent

Constants

AUTO_CONFIG_VALUE
VALID_CONFIG_VALUES

Attributes

config_name[W]
dependencies[R]
executed[R]
name[RW]
prepend_conflicts[R]

Public Class Methods

new() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 53
def initialize
  @dependencies = []
  @executes = []
  @prepend_conflicts = []
  @name = nil
  @config_name = nil
end

Public Instance Methods

allowed_by_config?() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 143
def allowed_by_config?
  !(disabled_configured? || deprecated_disabled_configured?)
end
chain_instrument(instrumenting_module, supportability_name = nil) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 100
def chain_instrument(instrumenting_module, supportability_name = nil)
  log_and_instrument('MethodChaining', instrumenting_module, supportability_name) do
    instrumenting_module.instrument!
  end
end
chain_instrument_target(target, instrumenting_module, supportability_name = nil) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 106
def chain_instrument_target(target, instrumenting_module, supportability_name = nil)
  NewRelic::Agent.logger.info("Installing deferred #{target} instrumentation")
  log_and_instrument('MethodChaining', instrumenting_module, supportability_name) do
    instrumenting_module.instrument!(target)
  end
end
check_dependencies() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 126
def check_dependencies
  return false unless allowed_by_config? && dependencies

  dependencies.all? do |dep|
    begin
      dep.call
    rescue => err
      NewRelic::Agent.logger.error("Error while detecting #{self.name}:", err)
      false
    end
  end
end
config_key() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 164
def config_key
  return nil if self.config_name.nil?

  @config_key ||= "instrumentation.#{self.config_name}".to_sym
end
config_name() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 49
def config_name
  @config_name || @name
end
config_value() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 202
def config_value
  return AUTO_CONFIG_VALUE unless config_key

  fetch_config_value(config_key)
end
configure_as_unsatisfied() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 65
def configure_as_unsatisfied
  # TODO: currently using :unsatisfied for Padrino will clobber the value
  #       already set for Sinatra, so skip Padrino and circle back with a
  #       new Padrino specific solution in the future.
  #
  #       https://github.com/newrelic/newrelic-ruby-agent/issues/2912
  return if name == :padrino

  NewRelic::Agent.config.instance_variable_get(:@cache)[config_key] = :unsatisfied
end
configure_with(new_config_name) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 212
def configure_with(new_config_name)
  self.config_name = new_config_name
end
conflicts_with_prepend(&block) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 220
def conflicts_with_prepend(&block)
  @prepend_conflicts << block if block
end
dependencies_satisfied?() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 61
def dependencies_satisfied?
  !executed and check_dependencies
end
depends_on(&block) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 139
def depends_on(&block)
  @dependencies << block if block
end
deprecated_disabled_configured?() click to toggle source

TODO: MAJOR VERSION will only return true if a disabled key is found and is truthy

# File lib/new_relic/dependency_detection.rb, line 149
def deprecated_disabled_configured?
  return false if self.name.nil?

  key = "disable_#{self.name}".to_sym
  return false unless ::NewRelic::Agent.config[key] == true

  ::NewRelic::Agent.logger.debug("Not installing #{self.name} instrumentation because of configuration #{key}")
  ::NewRelic::Agent.logger.debug( \
    "[DEPRECATED] configuration #{key} for #{self.name} will be removed in the next major release. " \
    "Use `#{config_key}` with one of `#{VALID_CONFIG_VALUES.map(&:to_s).inspect}`"
  )

  return true
end
execute() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 113
def execute
  @executes.each do |x|
    begin
      x.call
    rescue => err
      NewRelic::Agent.logger.error("Error while installing #{self.name} instrumentation:", err)
      break
    end
  end
ensure
  executed!
end
executed!() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 45
def executed!
  @executed = true
end
executes(&block) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 216
def executes(&block)
  @executes << block if block
end
extract_supportability_name(instrumenting_module) click to toggle source

Extracts the instrumented library name from the instrumenting module’s name Given “NewRelic::Agent::Instrumentation::NetHTTP::Prepend” Will extract “NetHTTP” which is in the 2nd to last spot

# File lib/new_relic/dependency_detection.rb, line 83
def extract_supportability_name(instrumenting_module)
  instrumenting_module.to_s.split('::')[-2]
end
fetch_config_value(key) click to toggle source

fetches and transform potentially invalid value given to one of the valid config values logs the resolved value during debug mode.

# File lib/new_relic/dependency_detection.rb, line 187
def fetch_config_value(key)
  valid_value = valid_config_value(::NewRelic::Agent.config[key].to_s.to_sym)
  ::NewRelic::Agent.logger.debug("Using #{valid_value} configuration value for #{self.name} to configure instrumentation")
  return valid_value
end
log_and_instrument(method, instrumenting_module, supportability_name) { || ... } click to toggle source
# File lib/new_relic/dependency_detection.rb, line 87
def log_and_instrument(method, instrumenting_module, supportability_name)
  supportability_name ||= extract_supportability_name(instrumenting_module)
  NewRelic::Agent.logger.info("Installing New Relic supported #{supportability_name} instrumentation using #{method}")
  NewRelic::Agent.record_metric("Supportability/Instrumentation/#{supportability_name}/#{method}", 0.0)
  yield
end
named(new_name) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 208
def named(new_name)
  self.name = new_name
end
prepend_conflicts?() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 228
def prepend_conflicts?
  @prepend_conflicts.any? do |conflict|
    begin
      conflict.call
    rescue => err
      NewRelic::Agent.logger.error("Error while checking prepend conflicts #{self.name}:", err)
      false # assumes no conflicts exist since `prepend` is preferred method of instrumenting
    end
  end
end
prepend_instrument(target_class, instrumenting_module, supportability_name = nil) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 94
def prepend_instrument(target_class, instrumenting_module, supportability_name = nil)
  log_and_instrument('Prepend', instrumenting_module, supportability_name) do
    target_class.send(:prepend, instrumenting_module)
  end
end
source_location_for(klass, method_name) click to toggle source
# File lib/new_relic/dependency_detection.rb, line 76
def source_location_for(klass, method_name)
  Object.instance_method(:method).bind(klass.allocate).call(method_name).source_location.to_s
end
update_config_value(use_prepend) click to toggle source

update any :auto config value to be either :prepend or :chain after auto determination has selected one of those to use

# File lib/new_relic/dependency_detection.rb, line 195
def update_config_value(use_prepend)
  if config_key && auto_configured?
    NewRelic::Agent.config.instance_variable_get(:@cache)[config_key] = use_prepend ? :prepend : :chain
  end
  use_prepend
end
use_prepend?() click to toggle source
# File lib/new_relic/dependency_detection.rb, line 224
def use_prepend?
  update_config_value(prepend_configured? || (auto_configured? && !prepend_conflicts?))
end
valid_config_value(retrieved_value) click to toggle source

returns only a valid value for instrumentation configuration If user uses “enabled” it’s converted to “auto”

# File lib/new_relic/dependency_detection.rb, line 181
def valid_config_value(retrieved_value)
  VALID_CONFIG_VALUES.include?(retrieved_value) ? retrieved_value : AUTO_CONFIG_VALUE
end