Class: DependencyDetection::Dependent

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/dependency_detection.rb

Constant Summary collapse

VALID_CONFIG_VALUES =
[:auto, :disabled, :prepend, :chain]
AUTO_CONFIG_VALUE =

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDependent

Returns a new instance of Dependent.



55
56
57
58
59
60
61
# File 'lib/new_relic/dependency_detection.rb', line 55

def initialize
  @dependencies = []
  @executes = []
  @prepend_conflicts = []
  @name = nil
  @config_name = nil
end

Instance Attribute Details

#config_nameObject



51
52
53
# File 'lib/new_relic/dependency_detection.rb', line 51

def config_name
  @config_name || @name
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



44
45
46
# File 'lib/new_relic/dependency_detection.rb', line 44

def dependencies
  @dependencies
end

#executedObject (readonly)

Returns the value of attribute executed.



41
42
43
# File 'lib/new_relic/dependency_detection.rb', line 41

def executed
  @executed
end

#nameObject

Returns the value of attribute name.



42
43
44
# File 'lib/new_relic/dependency_detection.rb', line 42

def name
  @name
end

#prepend_conflictsObject (readonly)

Returns the value of attribute prepend_conflicts.



45
46
47
# File 'lib/new_relic/dependency_detection.rb', line 45

def prepend_conflicts
  @prepend_conflicts
end

Instance Method Details

#allowed_by_config?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/new_relic/dependency_detection.rb', line 138

def allowed_by_config?
  !(disabled_configured? || deprecated_disabled_configured?)
end

#chain_instrument(instrumenting_module, supportability_name = nil) ⇒ Object



95
96
97
98
99
# File 'lib/new_relic/dependency_detection.rb', line 95

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) ⇒ Object



101
102
103
104
105
106
# File 'lib/new_relic/dependency_detection.rb', line 101

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_dependenciesObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/new_relic/dependency_detection.rb', line 121

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_keyObject



159
160
161
162
163
# File 'lib/new_relic/dependency_detection.rb', line 159

def config_key
  return nil if self.config_name.nil?

  @config_key ||= "instrumentation.#{self.config_name}".to_sym
end

#config_valueObject



197
198
199
200
201
# File 'lib/new_relic/dependency_detection.rb', line 197

def config_value
  return AUTO_CONFIG_VALUE unless config_key

  fetch_config_value(config_key)
end

#configure_as_unsatisfiedObject



67
68
69
# File 'lib/new_relic/dependency_detection.rb', line 67

def configure_as_unsatisfied
  NewRelic::Agent.config.instance_variable_get(:@cache)[config_key] = :unsatisfied
end

#configure_with(new_config_name) ⇒ Object



207
208
209
# File 'lib/new_relic/dependency_detection.rb', line 207

def configure_with(new_config_name)
  self.config_name = new_config_name
end

#conflicts_with_prepend(&block) ⇒ Object



215
216
217
# File 'lib/new_relic/dependency_detection.rb', line 215

def conflicts_with_prepend(&block)
  @prepend_conflicts << block if block
end

#dependencies_satisfied?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/new_relic/dependency_detection.rb', line 63

def dependencies_satisfied?
  !executed and check_dependencies
end

#depends_on(&block) ⇒ Object



134
135
136
# File 'lib/new_relic/dependency_detection.rb', line 134

def depends_on(&block)
  @dependencies << block if block
end

#deprecated_disabled_configured?Boolean

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

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/new_relic/dependency_detection.rb', line 144

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

#executeObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/new_relic/dependency_detection.rb', line 108

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!Object



47
48
49
# File 'lib/new_relic/dependency_detection.rb', line 47

def executed!
  @executed = true
end

#executes(&block) ⇒ Object



211
212
213
# File 'lib/new_relic/dependency_detection.rb', line 211

def executes(&block)
  @executes << block if block
end

#extract_supportability_name(instrumenting_module) ⇒ Object

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



78
79
80
# File 'lib/new_relic/dependency_detection.rb', line 78

def extract_supportability_name(instrumenting_module)
  instrumenting_module.to_s.split('::')[-2]
end

#fetch_config_value(key) ⇒ Object

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



182
183
184
185
186
# File 'lib/new_relic/dependency_detection.rb', line 182

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) ⇒ Object



82
83
84
85
86
87
# File 'lib/new_relic/dependency_detection.rb', line 82

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) ⇒ Object



203
204
205
# File 'lib/new_relic/dependency_detection.rb', line 203

def named(new_name)
  self.name = new_name
end

#prepend_conflicts?Boolean

Returns:

  • (Boolean)


223
224
225
226
227
228
229
230
231
232
# File 'lib/new_relic/dependency_detection.rb', line 223

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) ⇒ Object



89
90
91
92
93
# File 'lib/new_relic/dependency_detection.rb', line 89

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) ⇒ Object



71
72
73
# File 'lib/new_relic/dependency_detection.rb', line 71

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) ⇒ Object

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



190
191
192
193
194
195
# File 'lib/new_relic/dependency_detection.rb', line 190

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?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/new_relic/dependency_detection.rb', line 219

def use_prepend?
  update_config_value(prepend_configured? || (auto_configured? && !prepend_conflicts?))
end

#valid_config_value(retrieved_value) ⇒ Object

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



176
177
178
# File 'lib/new_relic/dependency_detection.rb', line 176

def valid_config_value(retrieved_value)
  VALID_CONFIG_VALUES.include?(retrieved_value) ? retrieved_value : AUTO_CONFIG_VALUE
end