ghazel-newrelic_rpm 3.1.0.1 → 3.4.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. data/CHANGELOG +120 -35
  2. data/LICENSE +29 -2
  3. data/README.rdoc +2 -2
  4. data/bin/mongrel_rpm +0 -0
  5. data/bin/newrelic +0 -0
  6. data/bin/newrelic_cmd +0 -0
  7. data/lib/new_relic/agent.rb +50 -38
  8. data/lib/new_relic/agent/agent.rb +459 -337
  9. data/lib/new_relic/agent/beacon_configuration.rb +71 -11
  10. data/lib/new_relic/agent/browser_monitoring.rb +73 -14
  11. data/lib/new_relic/agent/busy_calculator.rb +11 -3
  12. data/lib/new_relic/agent/chained_call.rb +2 -2
  13. data/lib/new_relic/agent/database.rb +223 -0
  14. data/lib/new_relic/agent/error_collector.rb +231 -183
  15. data/lib/new_relic/agent/instrumentation.rb +2 -2
  16. data/lib/new_relic/agent/instrumentation/active_merchant.rb +10 -2
  17. data/lib/new_relic/agent/instrumentation/active_record.rb +138 -0
  18. data/lib/new_relic/agent/instrumentation/acts_as_solr.rb +7 -1
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +6 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +46 -14
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +8 -2
  22. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +11 -3
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +49 -25
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +7 -2
  25. data/lib/new_relic/agent/instrumentation/merb/errors.rb +7 -1
  26. data/lib/new_relic/agent/instrumentation/metric_frame.rb +31 -4
  27. data/lib/new_relic/agent/instrumentation/metric_frame/pop.rb +1 -5
  28. data/lib/new_relic/agent/instrumentation/net.rb +8 -2
  29. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +5 -2
  30. data/lib/new_relic/agent/instrumentation/queue_time.rb +1 -1
  31. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +66 -35
  32. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +7 -1
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +7 -1
  34. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +121 -1
  35. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +7 -1
  36. data/lib/new_relic/agent/instrumentation/rainbows_instrumentation.rb +21 -0
  37. data/lib/new_relic/agent/instrumentation/resque.rb +80 -0
  38. data/lib/new_relic/agent/instrumentation/sinatra.rb +46 -20
  39. data/lib/new_relic/agent/instrumentation/sunspot.rb +6 -0
  40. data/lib/new_relic/agent/instrumentation/unicorn_instrumentation.rb +7 -2
  41. data/lib/new_relic/agent/method_tracer.rb +205 -99
  42. data/lib/new_relic/agent/new_relic_service.rb +221 -0
  43. data/lib/new_relic/agent/pipe_channel_manager.rb +161 -0
  44. data/lib/new_relic/agent/pipe_service.rb +54 -0
  45. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +89 -0
  46. data/lib/new_relic/agent/samplers/memory_sampler.rb +6 -7
  47. data/lib/new_relic/agent/shim_agent.rb +5 -5
  48. data/lib/new_relic/agent/sql_sampler.rb +282 -0
  49. data/lib/new_relic/agent/stats_engine.rb +2 -0
  50. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +123 -0
  51. data/lib/new_relic/agent/stats_engine/metric_stats.rb +35 -30
  52. data/lib/new_relic/agent/stats_engine/samplers.rb +10 -4
  53. data/lib/new_relic/agent/stats_engine/transactions.rb +28 -87
  54. data/lib/new_relic/agent/transaction_info.rb +74 -0
  55. data/lib/new_relic/agent/transaction_sample_builder.rb +18 -3
  56. data/lib/new_relic/agent/transaction_sampler.rb +108 -20
  57. data/lib/new_relic/agent/worker_loop.rb +14 -6
  58. data/lib/new_relic/collection_helper.rb +19 -11
  59. data/lib/new_relic/command.rb +1 -1
  60. data/lib/new_relic/commands/deployments.rb +2 -2
  61. data/lib/new_relic/commands/install.rb +2 -13
  62. data/lib/new_relic/control.rb +2 -3
  63. data/lib/new_relic/control/class_methods.rb +12 -6
  64. data/lib/new_relic/control/configuration.rb +57 -8
  65. data/lib/new_relic/control/frameworks.rb +10 -0
  66. data/lib/new_relic/control/frameworks/external.rb +4 -4
  67. data/lib/new_relic/control/frameworks/merb.rb +2 -1
  68. data/lib/new_relic/control/frameworks/rails.rb +35 -22
  69. data/lib/new_relic/control/frameworks/rails3.rb +12 -7
  70. data/lib/new_relic/control/frameworks/ruby.rb +5 -5
  71. data/lib/new_relic/control/frameworks/sinatra.rb +1 -4
  72. data/lib/new_relic/control/instance_methods.rb +38 -12
  73. data/lib/new_relic/control/instrumentation.rb +23 -4
  74. data/lib/new_relic/control/logging_methods.rb +70 -15
  75. data/lib/new_relic/control/server_methods.rb +22 -9
  76. data/lib/new_relic/delayed_job_injection.rb +16 -3
  77. data/lib/new_relic/helper.rb +21 -0
  78. data/lib/new_relic/language_support.rb +95 -0
  79. data/lib/new_relic/local_environment.rb +92 -48
  80. data/lib/new_relic/metric_data.rb +7 -2
  81. data/lib/new_relic/metric_spec.rb +12 -9
  82. data/lib/new_relic/noticed_error.rb +6 -1
  83. data/lib/new_relic/rack/browser_monitoring.rb +18 -19
  84. data/lib/new_relic/rack/developer_mode.rb +3 -2
  85. data/lib/new_relic/recipes.rb +8 -4
  86. data/lib/new_relic/stats.rb +17 -60
  87. data/lib/new_relic/transaction_analysis.rb +2 -1
  88. data/lib/new_relic/transaction_analysis/segment_summary.rb +4 -2
  89. data/lib/new_relic/transaction_sample.rb +60 -75
  90. data/lib/new_relic/transaction_sample/segment.rb +31 -79
  91. data/lib/new_relic/version.rb +2 -2
  92. data/lib/newrelic_rpm.rb +1 -1
  93. data/newrelic.yml +2 -2
  94. data/newrelic_rpm.gemspec +46 -54
  95. data/test/active_record_fixtures.rb +3 -3
  96. data/test/config/newrelic.yml +1 -1
  97. data/test/fixtures/proc_cpuinfo.txt +575 -0
  98. data/test/new_relic/agent/agent/connect_test.rb +128 -25
  99. data/test/new_relic/agent/agent/start_test.rb +9 -94
  100. data/test/new_relic/agent/agent/start_worker_thread_test.rb +2 -4
  101. data/test/new_relic/agent/agent_test.rb +51 -78
  102. data/test/new_relic/agent/agent_test_controller.rb +1 -1
  103. data/test/new_relic/agent/agent_test_controller_test.rb +49 -33
  104. data/test/new_relic/agent/beacon_configuration_test.rb +12 -5
  105. data/test/new_relic/agent/browser_monitoring_test.rb +99 -50
  106. data/test/new_relic/agent/database_test.rb +161 -0
  107. data/test/new_relic/agent/error_collector_test.rb +47 -23
  108. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +96 -42
  109. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +0 -2
  110. data/test/new_relic/agent/instrumentation/instrumentation_test.rb +1 -1
  111. data/test/new_relic/agent/instrumentation/metric_frame/pop_test.rb +3 -11
  112. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +9 -9
  113. data/test/new_relic/agent/instrumentation/queue_time_test.rb +6 -11
  114. data/test/new_relic/agent/memcache_instrumentation_test.rb +54 -18
  115. data/test/new_relic/agent/method_tracer/class_methods/add_method_tracer_test.rb +1 -1
  116. data/test/new_relic/agent/method_tracer/instance_methods/trace_execution_scoped_test.rb +1 -1
  117. data/test/new_relic/agent/method_tracer_test.rb +3 -2
  118. data/test/new_relic/agent/new_relic_service_test.rb +151 -0
  119. data/test/new_relic/agent/pipe_channel_manager_test.rb +114 -0
  120. data/test/new_relic/agent/pipe_service_test.rb +113 -0
  121. data/test/new_relic/agent/rpm_agent_test.rb +4 -31
  122. data/test/new_relic/agent/sql_sampler_test.rb +192 -0
  123. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +19 -18
  124. data/test/new_relic/agent/stats_engine_test.rb +41 -6
  125. data/test/new_relic/agent/transaction_info_test.rb +13 -0
  126. data/test/new_relic/agent/transaction_sample_builder_test.rb +27 -4
  127. data/test/new_relic/agent/transaction_sampler_test.rb +68 -46
  128. data/test/new_relic/agent/worker_loop_test.rb +3 -3
  129. data/test/new_relic/agent_test.rb +242 -0
  130. data/test/new_relic/collection_helper_test.rb +50 -28
  131. data/test/new_relic/control/configuration_test.rb +77 -0
  132. data/test/new_relic/control/logging_methods_test.rb +49 -21
  133. data/test/new_relic/control_test.rb +115 -54
  134. data/test/new_relic/delayed_job_injection_test.rb +21 -0
  135. data/test/new_relic/fake_collector.rb +210 -0
  136. data/test/new_relic/fake_service.rb +44 -0
  137. data/test/new_relic/local_environment_test.rb +14 -1
  138. data/test/new_relic/metric_parser/metric_parser_test.rb +11 -0
  139. data/test/new_relic/rack/browser_monitoring_test.rb +84 -23
  140. data/test/new_relic/rack/developer_mode_helper_test.rb +141 -0
  141. data/test/new_relic/rack/developer_mode_test.rb +31 -0
  142. data/test/new_relic/stats_test.rb +3 -18
  143. data/test/new_relic/transaction_analysis/segment_summary_test.rb +14 -0
  144. data/test/new_relic/transaction_analysis_test.rb +3 -3
  145. data/test/new_relic/transaction_sample/segment_test.rb +15 -80
  146. data/test/new_relic/transaction_sample_test.rb +25 -18
  147. data/test/script/build_test_gem.sh +51 -0
  148. data/test/script/ci.sh +140 -0
  149. data/test/script/ci_agent-tests_runner.sh +82 -0
  150. data/test/script/ci_bench.sh +52 -0
  151. data/test/script/ci_multiverse_runner.sh +63 -0
  152. data/test/test_contexts.rb +1 -0
  153. data/test/test_helper.rb +18 -5
  154. data/ui/helpers/developer_mode_helper.rb +14 -8
  155. data/ui/helpers/google_pie_chart.rb +0 -1
  156. data/ui/views/newrelic/index.rhtml +2 -2
  157. data/vendor/gems/dependency_detection-0.0.1.build/LICENSE +4 -18
  158. data/vendor/gems/dependency_detection-0.0.1.build/lib/dependency_detection.rb +10 -0
  159. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/mem_cache.rb +11 -11
  160. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/metric_parser.rb +17 -4
  161. data/vendor/gems/metric_parser-0.1.0.pre1/lib/new_relic/metric_parser/view.rb +4 -0
  162. metadata +50 -36
  163. data/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb +0 -108
  164. data/lib/new_relic/agent/instrumentation/rails3/active_record_instrumentation.rb +0 -112
  165. data/lib/new_relic/agent/samplers/delayed_job_lock_sampler.rb +0 -40
  166. data/lib/new_relic/data_serialization.rb +0 -84
  167. data/lib/new_relic/histogram.rb +0 -91
  168. data/lib/new_relic/rack/metric_app.rb +0 -65
  169. data/lib/new_relic/rack/mongrel_rpm.ru +0 -28
  170. data/lib/new_relic/rack/newrelic.yml +0 -27
  171. data/lib/new_relic/rack_app.rb +0 -6
  172. data/test/new_relic/data_serialization_test.rb +0 -70
  173. data/vendor/gems/dependency_detection-0.0.1.build/README +0 -0
  174. data/vendor/gems/metric_parser-0.1.0.pre1/LICENSE +0 -0
  175. data/vendor/gems/metric_parser-0.1.0.pre1/README +0 -0
@@ -1,12 +1,23 @@
1
1
  module NewRelic
2
2
  class Control
3
+ # Contains methods that relate to adding and executing files that
4
+ # contain instrumentation for the Ruby Agent
3
5
  module Instrumentation
6
+
7
+ # Adds a list of files in Dir.glob format
8
+ # (e.g. '/app/foo/**/*_instrumentation.rb')
9
+ # This requires the files within a rescue block, so that any
10
+ # errors within instrumentation files do not affect the overall
11
+ # agent or application in which it runs.
12
+ #
13
+ # Logs at debug level for each file loaded, and logs errors in
14
+ # file loading at error level
4
15
  def load_instrumentation_files pattern
5
16
  Dir.glob(pattern) do |file|
6
17
  begin
7
18
  log.debug "Processing instrumentation file '#{file}'"
8
19
  require file.to_s
9
- rescue Exception => e
20
+ rescue => e
10
21
  log.error "Error loading instrumentation file '#{file}': #{e}"
11
22
  log.debug e.backtrace.join("\n")
12
23
  end
@@ -23,8 +34,11 @@ module NewRelic
23
34
 
24
35
  # Add instrumentation. Don't call this directly. Use NewRelic::Agent#add_instrumentation.
25
36
  # This will load the file synchronously if we've already loaded the default
26
- # instrumentation.
37
+ # instrumentation, otherwise instrumentation files specified
38
+ # here will be deferred until all instrumentation is run
27
39
  #
40
+ # This happens after the agent has loaded and all dependencies
41
+ # are ready to be instrumented
28
42
  def add_instrumentation pattern
29
43
  if @instrumented
30
44
  load_instrumentation_files pattern
@@ -32,11 +46,16 @@ module NewRelic
32
46
  @instrumentation_files << pattern
33
47
  end
34
48
  end
35
-
49
+
50
+ # Signals the agent that it's time to actually load the
51
+ # instrumentation files. May be overridden by subclasses
36
52
  def install_instrumentation
37
53
  _install_instrumentation
38
54
  end
39
-
55
+
56
+ # adds samplers to the stats engine so that they run every
57
+ # minute. This is dynamically recognized by any class that
58
+ # subclasses NewRelic::Agent::Sampler
40
59
  def load_samplers
41
60
  agent = NewRelic::Agent.instance
42
61
  NewRelic::Agent::Sampler.sampler_classes.each do | subclass |
@@ -1,15 +1,19 @@
1
1
 
2
2
  module NewRelic
3
3
  class Control
4
+ # Contains methods that relate to locating, creating, and writing
5
+ # to the log file and/or standard out
4
6
  module LoggingMethods
5
7
 
6
8
  attr_accessor :log_file
7
-
9
+
10
+ # returns either the log set up with setup_log or else a new
11
+ # logger pointing to standard out, if we're trying to log before
12
+ # a log exists
8
13
  def log
9
- # If we try to get a log before one has been set up, return a stdout log
10
- unless @log
14
+ if !@log
11
15
  l = Logger.new(STDOUT)
12
- l.level = Logger::INFO
16
+ l.level = Logger::INFO
13
17
  return l
14
18
  end
15
19
  @log
@@ -24,12 +28,18 @@ module NewRelic
24
28
  return unless should_log?
25
29
  log.send level, msg if @log
26
30
  end
27
-
31
+
32
+ # true if the agent has settings, and the agent is enabled,
33
+ # otherwise false
28
34
  def should_log?
29
35
  @settings && agent_enabled?
30
36
  end
31
37
 
32
38
  # set the log level as specified in the config file
39
+ #
40
+ # Possible values are from the Logger class: debug, info, warn,
41
+ #error, and fatal
42
+ # Defaults to info
33
43
  def set_log_level!(logger)
34
44
  case fetch("log_level","info").downcase
35
45
  when "debug" then logger.level = Logger::DEBUG
@@ -42,10 +52,11 @@ module NewRelic
42
52
  logger
43
53
  end
44
54
 
45
- # change the format just for our logger
55
+ # patches the logger's format_message method to change the format just for our logger
46
56
  def set_log_format!(logger)
47
57
  def logger.format_message(severity, timestamp, progname, msg)
48
- "[#{timestamp.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n"
58
+ prefix = @logdev.dev == STDOUT ? '** [NewRelic]' : ''
59
+ prefix + "[#{timestamp.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n"
49
60
  end
50
61
  logger
51
62
  end
@@ -54,8 +65,14 @@ module NewRelic
54
65
  #
55
66
  # Control subclasses may override this, but it can be called multiple times.
56
67
  def setup_log
57
- @log_file = "#{log_path}/#{log_file_name}"
58
- @log = Logger.new(@log_file) rescue nil
68
+ if log_to_stdout?
69
+ @log = Logger.new(STDOUT)
70
+ else
71
+ @log_file = "#{log_path}/#{log_file_name}"
72
+ @log = Logger.new(@log_file) rescue nil
73
+ @log ||= Logger.new(STDOUT) # failsafe to STDOUT
74
+ end
75
+
59
76
  if @log
60
77
  set_log_format!(@log)
61
78
  set_log_level!(@log)
@@ -65,22 +82,60 @@ module NewRelic
65
82
  # above
66
83
  log
67
84
  end
68
-
85
+
86
+ # simply puts a message to standard out, prepended with the
87
+ # '** [NewRelic]' sigil to make sure people know where the message
88
+ # comes from. This should be used sparingly
69
89
  def to_stdout(msg)
70
90
  STDOUT.puts "** [NewRelic] " + msg
71
91
  end
72
-
92
+
93
+ # Sets up and caches the log path, attempting to create it if it
94
+ # does not exist. If it does not succeed, it prints an
95
+ # error and returns nil.
96
+ # This comes from the configuration variable 'log_file_path' in the configuration file.
73
97
  def log_path
74
98
  return @log_path if @log_path
75
- @log_path = File.expand_path(fetch('log_file_path', 'log/'))
76
- if !File.directory?(@log_path) && ! (Dir.mkdir(@log_path) rescue nil)
77
- log!("Error creating New Relic log directory '#{@log_path}'", :error)
99
+ if log_to_stdout?
100
+ @log_path = nil
101
+ else
102
+ if ENV['NEW_RELIC_LOG']
103
+ log_path_setting = File.dirname(ENV['NEW_RELIC_LOG'])
104
+ else
105
+ log_path_setting = fetch('log_file_path', 'log')
106
+ end
107
+ @log_path = find_or_create_file_path(log_path_setting)
108
+ log!("Error creating log directory #{log_path_setting}, using standard out for logging.", :warn) unless @log_path
78
109
  end
79
110
  @log_path
80
111
  end
112
+
113
+ def find_or_create_file_path(path_setting)
114
+ for abs_path in [ File.expand_path(path_setting),
115
+ File.expand_path(File.join(root, path_setting)) ] do
116
+ if File.directory?(abs_path) || (Dir.mkdir(abs_path) rescue nil)
117
+ return abs_path[%r{^(.*?)/?$}]
118
+ end
119
+ end
120
+ nil
121
+ end
81
122
 
123
+ def log_to_stdout?
124
+ return true if @stdout
125
+ destination = ENV['NEW_RELIC_LOG'] || fetch('log_file_path', 'log')
126
+ if destination.upcase == 'STDOUT'
127
+ @stdout = true
128
+ end
129
+ end
130
+
131
+ # Retrieves the log file's name from the config file option
132
+ #'log_file_name', defaulting to 'newrelic_agent.log'
82
133
  def log_file_name
83
- fetch('log_file_name', 'newrelic_agent.log')
134
+ if ENV['NEW_RELIC_LOG']
135
+ File.basename(ENV['NEW_RELIC_LOG'])
136
+ else
137
+ fetch('log_file_name', 'newrelic_agent.log')
138
+ end
84
139
  end
85
140
  end
86
141
  include LoggingMethods
@@ -7,13 +7,16 @@ module NewRelic
7
7
  end
8
8
 
9
9
  ProxyServer = Struct.new :name, :port, :user, :password #:nodoc:
10
-
10
+
11
+ # Contains methods that deal with connecting to the server
11
12
  module ServerMethods
12
-
13
+
13
14
  def server
14
15
  @remote_server ||= server_from_host(nil)
15
16
  end
16
-
17
+
18
+ # the server we should contact for api requests, like uploading
19
+ # deployments and the like
17
20
  def api_server
18
21
  api_host = self['api_host'] || 'rpm.newrelic.com'
19
22
  @api_server ||=
@@ -22,12 +25,17 @@ module NewRelic
22
25
  (self['api_port'] || self['port'] || (use_ssl? ? 443 : 80)).to_i,
23
26
  nil
24
27
  end
25
-
28
+
29
+ # a new instances of the proxy server - this passes through if
30
+ # there is no proxy, otherwise it has proxy configuration
31
+ # information pulled from the config file
26
32
  def proxy_server
27
33
  @proxy_server ||=
28
34
  NewRelic::Control::ProxyServer.new self['proxy_host'], self['proxy_port'], self['proxy_user'], self['proxy_pass']
29
35
  end
30
-
36
+
37
+ # turns a hostname into an ip address and returns a
38
+ # NewRelic::Control::Server that contains the configuration info
31
39
  def server_from_host(hostname=nil)
32
40
  host = hostname || self['host'] || 'collector.newrelic.com'
33
41
 
@@ -45,7 +53,10 @@ module NewRelic
45
53
  return host if verify_certificate?
46
54
  return nil if host.nil? || host.downcase == "localhost"
47
55
  ip = resolve_ip_address(host)
48
- log.info "Resolved #{host} to #{ip}"
56
+
57
+ # FIXME: commented out to squelch STDOUT output for RUBY-839
58
+ # should bring it back when logging / startup is fixed
59
+ # log.info "Resolved #{host} to #{ip}"
49
60
  ip
50
61
  end
51
62
 
@@ -55,18 +66,20 @@ module NewRelic
55
66
  # address and don't have a good default.
56
67
  def resolve_ip_address(host)
57
68
  Resolv.getaddress(host)
58
- rescue Exception => e
69
+ rescue => e
59
70
  log.warn("DNS Error caching IP address: #{e}")
60
71
  log.debug(e.backtrace.join("\n "))
61
72
  begin
62
73
  log.info("Trying native DNS lookup since Resolv failed")
63
74
  IPSocket.getaddress(host)
64
- rescue Exception => e
75
+ rescue => e
65
76
  log.error("Could not look up server address: #{e}")
66
77
  nil
67
78
  end
68
79
  end
69
-
80
+
81
+ # The path to the certificate file used to verify the SSL
82
+ # connection if verify_peer is enabled
70
83
  def cert_file_path
71
84
  File.expand_path(File.join(newrelic_root, 'cert', 'cacert.pem'))
72
85
  end
@@ -12,10 +12,18 @@ module NewRelic
12
12
  end
13
13
 
14
14
  DependencyDetection.defer do
15
+ @name = :delayed_job_injection
16
+
15
17
  depends_on do
16
- defined?(::Delayed) && defined?(::Delayed::Worker)
18
+ defined?(::Delayed) && defined?(::Delayed::Worker) && !NewRelic::Control.instance['disable_dj']
17
19
  end
18
-
20
+
21
+ executes do
22
+ if NewRelic::Agent.respond_to?(:logger)
23
+ NewRelic::Agent.logger.debug 'Installing DelayedJob instrumentation hooks'
24
+ end
25
+ end
26
+
19
27
  executes do
20
28
  Delayed::Worker.class_eval do
21
29
  def initialize_with_new_relic(*args)
@@ -35,4 +43,9 @@ DependencyDetection.defer do
35
43
  end
36
44
  end
37
45
  end
38
- DependencyDetection.detect!
46
+
47
+ # If Rails is defined, this gets called in an after_initialize hook
48
+ # see NewRelic::Control::Frameworks::Rails#init_config
49
+ unless defined?(Rails)
50
+ DependencyDetection.detect!
51
+ end
@@ -0,0 +1,21 @@
1
+ module NewRelic
2
+ # A singleton for shared generic helper methods
3
+ module Helper
4
+ extend self
5
+
6
+ # confirm a string is correctly encoded (in >= 1.9)
7
+ # If not force the encoding to ASCII-8BIT (binary)
8
+ if RUBY_VERSION >= '1.9'
9
+ def correctly_encoded(string)
10
+ return string unless string.is_a? String
11
+ string.valid_encoding? ? string : string.force_encoding("ASCII-8BIT")
12
+ end
13
+ else
14
+ #noop
15
+ def correctly_encoded(string)
16
+ string
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,95 @@
1
+ module NewRelic::LanguageSupport
2
+ extend self
3
+
4
+ module Control
5
+ def self.included(base)
6
+ # need to use syck rather than psych when possible
7
+ if defined?(::YAML::ENGINE)
8
+ if !NewRelic::LanguageSupport.using_engine?('jruby') &&
9
+ (NewRelic::LanguageSupport.using_version?('1.9.1') ||
10
+ NewRelic::LanguageSupport.using_version?('1.9.2'))
11
+ base.class_eval do
12
+ def load_newrelic_yml(*args)
13
+ yamler = ::YAML::ENGINE.yamler
14
+ ::YAML::ENGINE.yamler = 'syck'
15
+ val = super
16
+ ::YAML::ENGINE.yamler = yamler
17
+ val
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ module SynchronizedHash
26
+ def self.included(base)
27
+ # need to lock iteration of stats hash in 1.9.x
28
+ if NewRelic::LanguageSupport.using_version?('1.9') ||
29
+ NewRelic::LanguageSupport.using_engine?('jruby')
30
+ base.class_eval do
31
+ def each(*args, &block)
32
+ @lock.synchronize { super }
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ @@forkable = nil
40
+
41
+ def can_fork?
42
+ # this is expensive to check, so we should only check once
43
+ return @@forkable if @@forkable != nil
44
+
45
+ if Process.respond_to?(:fork)
46
+ # if this is not 1.9.2 or higher, we have to make sure
47
+ @@forkable = ::RUBY_VERSION < '1.9.2' ? test_forkability : true
48
+ else
49
+ @@forkable = false
50
+ end
51
+
52
+ @@forkable
53
+ end
54
+
55
+ def using_engine?(engine)
56
+ if defined?(::RUBY_ENGINE)
57
+ ::RUBY_ENGINE == engine
58
+ else
59
+ engine == 'ruby'
60
+ end
61
+ end
62
+
63
+ def broken_gc?
64
+ NewRelic::LanguageSupport.using_version?('1.8.7') &&
65
+ RUBY_PATCHLEVEL < 348 &&
66
+ !NewRelic::LanguageSupport.using_engine?('jruby') &&
67
+ !NewRelic::LanguageSupport.using_engine?('rbx')
68
+ end
69
+
70
+ def with_disabled_gc
71
+ if defined?(::GC) && ::GC.respond_to?(:disable)
72
+ ::GC.disable
73
+ val = yield
74
+ ::GC.enable
75
+ val
76
+ else
77
+ yield
78
+ end
79
+ end
80
+
81
+ def using_version?(version)
82
+ numbers = version.split('.')
83
+ numbers == ::RUBY_VERSION.split('.')[0, numbers.size]
84
+ end
85
+
86
+ def test_forkability
87
+ child = Process.fork { exit! }
88
+ # calling wait here doesn't seem like it should necessary, but it seems to
89
+ # resolve some weird edge cases with resque forking.
90
+ Process.wait child
91
+ true
92
+ rescue NotImplementedError
93
+ false
94
+ end
95
+ end
@@ -14,12 +14,16 @@ module NewRelic
14
14
  #
15
15
  # NewRelic::LocalEnvironment should be accessed through NewRelic::Control#env (via the NewRelic::Control singleton).
16
16
  class LocalEnvironment
17
-
18
- attr_accessor :dispatcher # mongrel, thin, webrick, or possibly nil
19
- attr_accessor :dispatcher_instance_id # used to distinguish instances of a dispatcher from each other, may be nil
20
- attr_accessor :framework # rails, rails3, merb, external, ruby, test
21
- attr_reader :mongrel # The mongrel instance, if there is one, captured as a convenience
22
- attr_reader :processors # The number of cpus, if detected, or nil
17
+
18
+ # mongrel, thin, webrick, or possibly nil
19
+ attr_accessor :dispatcher
20
+ # used to distinguish instances of a dispatcher from each other, may be nil
21
+ attr_writer :dispatcher_instance_id
22
+ # rails, rails3, merb, external, ruby, test, etc
23
+ attr_accessor :framework
24
+ # The number of cpus, if detected, or nil - many platforms do not
25
+ # support this :(
26
+ attr_reader :processors
23
27
  alias environment dispatcher
24
28
 
25
29
  def initialize
@@ -45,25 +49,31 @@ module NewRelic
45
49
  def append_environment_value(name, value = nil)
46
50
  value = yield if block_given?
47
51
  @config[name] = value if value
48
- rescue Exception
52
+ rescue
49
53
  # puts "#{e}\n #{e.backtrace.join("\n ")}"
50
54
  raise if @framework == :test
51
55
  end
52
-
56
+
57
+ # yields to the block and appends the returned value to the list
58
+ # of gems - this catches errors that might be raised in the block
53
59
  def append_gem_list
54
60
  @gems += yield
55
- rescue Exception => e
61
+ rescue => e
56
62
  # puts "#{e}\n #{e.backtrace.join("\n ")}"
57
63
  raise if @framework == :test
58
64
  end
59
-
65
+
66
+ # yields to the block and appends the returned value to the list
67
+ # of plugins - this catches errors that might be raised in the block
60
68
  def append_plugin_list
61
69
  @plugins += yield
62
- rescue Exception
70
+ rescue
63
71
  # puts "#{e}\n #{e.backtrace.join("\n ")}"
64
72
  raise if @framework == :test
65
73
  end
66
-
74
+
75
+ # An instance id pulled from either @dispatcher_instance_id or by
76
+ # splitting out the first part of the running file
67
77
  def dispatcher_instance_id
68
78
  if @dispatcher_instance_id.nil?
69
79
  if @dispatcher.nil?
@@ -72,7 +82,9 @@ module NewRelic
72
82
  end
73
83
  @dispatcher_instance_id
74
84
  end
75
-
85
+
86
+ # Interrogates some common ruby constants for useful information
87
+ # about what kind of ruby environment the agent is running in
76
88
  def gather_ruby_info
77
89
  append_environment_value('Ruby version'){ RUBY_VERSION }
78
90
  append_environment_value('Ruby description'){ RUBY_DESCRIPTION } if defined? ::RUBY_DESCRIPTION
@@ -83,40 +95,63 @@ module NewRelic
83
95
  gather_jruby_info
84
96
  end
85
97
  end
86
-
98
+
99
+ # like gather_ruby_info but for the special case of JRuby
87
100
  def gather_jruby_info
88
101
  append_environment_value('JRuby version') { JRUBY_VERSION }
89
102
  append_environment_value('Java VM version') { ENV_JAVA['java.vm.version']}
90
103
  end
91
104
 
92
- # See what the number of cpus is, works only on linux.
93
- def gather_cpu_info
94
- return unless File.readable? '/proc/cpuinfo'
105
+ # See what the number of cpus is, works only on some linux variants
106
+ def gather_cpu_info(proc_file='/proc/cpuinfo')
107
+ return unless File.readable? proc_file
95
108
  @processors = append_environment_value('Processors') do
96
- processors = File.readlines('/proc/cpuinfo').select { |line| line =~ /^processor\s*:/ }.size
97
- raise "Cannot determine the number of processors in /proc/cpuinfo" unless processors > 0
109
+ cpuinfo = ''
110
+ File.open(proc_file) do |f|
111
+ loop do
112
+ begin
113
+ cpuinfo << f.read_nonblock(4096).strip
114
+ rescue EOFError
115
+ break
116
+ rescue Errno::EWOULDBLOCK, Errno::EAGAIN
117
+ cpuinfo = ''
118
+ break # don't select file handle, just give up
119
+ end
120
+ end
121
+ end
122
+ processors = cpuinfo.split("\n").select {|line| line =~ /^processor\s*:/ }.size
123
+
124
+ if processors == 0
125
+ processors = 1 # assume there is at least one processor
126
+ NewRelic::Agent.logger.warn("Cannot determine the number of processors in #{proc_file}")
127
+ end
98
128
  processors
99
129
  end
100
130
  end
101
-
131
+
132
+ # Grabs the architecture string from either `uname -p` or the env
133
+ # variable PROCESSOR_ARCHITECTURE
102
134
  def gather_architecture_info
103
135
  append_environment_value('Arch') { `uname -p` } ||
104
136
  append_environment_value('Arch') { ENV['PROCESSOR_ARCHITECTURE'] }
105
137
  end
106
-
138
+
139
+ # gathers OS info from either `uname -v`, `uname -s`, or the OS
140
+ # env variable
107
141
  def gather_os_info
108
142
  append_environment_value('OS version') { `uname -v` }
109
143
  append_environment_value('OS') { `uname -s` } ||
110
144
  append_environment_value('OS') { ENV['OS'] }
111
145
  end
112
-
146
+
147
+ # Gathers the architecture and cpu info
113
148
  def gather_system_info
114
149
  gather_architecture_info
115
150
  gather_cpu_info
116
151
  end
117
-
152
+
153
+ # Looks for a capistrano file indicating the current revision
118
154
  def gather_revision_info
119
- # Look for a capistrano file indicating the current revision:
120
155
  rev_file = File.join(NewRelic::Control.instance.root, "REVISION")
121
156
  if File.readable?(rev_file) && File.size(rev_file) < 64
122
157
  append_environment_value('Revision') do
@@ -124,9 +159,11 @@ module NewRelic
124
159
  end
125
160
  end
126
161
  end
127
-
162
+
163
+ # The name of the AR database adapter for the current environment and
164
+ # the current schema version
128
165
  def gather_ar_adapter_info
129
- # The name of the database adapter for the current environment.
166
+
130
167
  append_environment_value 'Database adapter' do
131
168
  if defined?(ActiveRecord) && defined?(ActiveRecord::Base) &&
132
169
  ActiveRecord::Base.respond_to?(:configurations)
@@ -136,18 +173,18 @@ module NewRelic
136
173
  end
137
174
  end
138
175
  end
139
- append_environment_value 'Database schema version' do
140
- ActiveRecord::Migrator.current_version
141
- end
142
176
  end
143
-
177
+
178
+ # Datamapper version
144
179
  def gather_dm_adapter_info
145
180
  append_environment_value 'DataMapper version' do
146
181
  require 'dm-core/version'
147
182
  DataMapper::VERSION
148
183
  end
149
184
  end
150
-
185
+
186
+ # sensing for which adapter is defined, then appends the relevant
187
+ # config information
151
188
  def gather_db_info
152
189
  # room here for more database adapters, when.
153
190
  if defined? ::ActiveRecord
@@ -181,17 +218,23 @@ module NewRelic
181
218
  i << [ 'Gems', @gems.to_a] if not @gems.empty?
182
219
  i
183
220
  end
184
-
221
+
222
+ # it's a working jruby if it has the runtime method, and object
223
+ # space is enabled
185
224
  def working_jruby?
186
225
  !(defined?(::JRuby) && JRuby.respond_to?(:runtime) && !JRuby.runtime.is_object_space_enabled)
187
226
  end
188
-
227
+
228
+ # Runs through all the objects in ObjectSpace to find the first one that
229
+ # match the provided class
189
230
  def find_class_in_object_space(klass)
190
231
  ObjectSpace.each_object(klass) do |x|
191
232
  return x
192
233
  end
234
+ return nil
193
235
  end
194
-
236
+
237
+ # Sets the @mongrel instance variable if we can find a Mongrel::HttpServer
195
238
  def mongrel
196
239
  return @mongrel if @mongrel
197
240
  if defined?(::Mongrel) && defined?(::Mongrel::HttpServer) && working_jruby?
@@ -199,22 +242,15 @@ module NewRelic
199
242
  end
200
243
  @mongrel
201
244
  end
202
-
203
- def unicorn
204
- return @unicorn if @unicorn
205
- if (defined?(::Unicorn) && defined?(::Unicorn::HttpServer)) && working_jruby?
206
- @unicorn = find_class_in_object_space(::Unicorn::HttpServer)
207
- end
208
- @unicorn
209
- end
210
-
245
+
211
246
  private
212
247
 
213
248
  # Although you can override the framework with NEWRELIC_DISPATCHER this
214
249
  # is not advisable since it implies certain api's being available.
215
250
  def discover_dispatcher
216
251
  @dispatcher ||= ENV['NEWRELIC_DISPATCHER'] && ENV['NEWRELIC_DISPATCHER'].to_sym
217
- dispatchers = %w[passenger torquebox glassfish thin mongrel litespeed webrick fastcgi unicorn sinatra]
252
+ @dispatcher ||= ENV['NEW_RELIC_DISPATCHER'] && ENV['NEW_RELIC_DISPATCHER'].to_sym
253
+ dispatchers = %w[passenger torquebox glassfish thin mongrel litespeed webrick fastcgi rainbows unicorn sinatra]
218
254
  while dispatchers.any? && @dispatcher.nil?
219
255
  send 'check_for_'+(dispatchers.shift)
220
256
  end
@@ -228,6 +264,7 @@ module NewRelic
228
264
  # of JRuby.
229
265
  @framework ||= case
230
266
  when ENV['NEWRELIC_FRAMEWORK'] then ENV['NEWRELIC_FRAMEWORK'].to_sym
267
+ when ENV['NEW_RELIC_FRAMEWORK'] then ENV['NEW_RELIC_FRAMEWORK'].to_sym
231
268
  when defined?(::NewRelic::TEST) then :test
232
269
  when defined?(::Merb) && defined?(::Merb::Plugins) then :merb
233
270
  when defined?(::Rails) then check_rails_version
@@ -307,11 +344,17 @@ module NewRelic
307
344
  end
308
345
 
309
346
  def check_for_unicorn
310
- return unless defined?(::Unicorn) && defined?(::Unicorn::HttpServer)
347
+ if (defined?(::Unicorn) && defined?(::Unicorn::HttpServer)) && working_jruby?
348
+ v = find_class_in_object_space(::Unicorn::HttpServer)
349
+ @dispatcher = :unicorn if v
350
+ end
351
+ end
311
352
 
312
- # unlike mongrel, unicorn manages muliple threads and ports, so we
313
- # have to map multiple processes into one instance, as we do with passenger
314
- @dispatcher = :unicorn
353
+ def check_for_rainbows
354
+ if (defined?(::Rainbows) && defined?(::Rainbows::HttpServer)) && working_jruby?
355
+ v = find_class_in_object_space(::Rainbows::HttpServer)
356
+ @dispatcher = :rainbows if v
357
+ end
315
358
  end
316
359
 
317
360
  def check_for_sinatra
@@ -379,6 +422,7 @@ module NewRelic
379
422
  end
380
423
 
381
424
  public
425
+ # outputs a human-readable description
382
426
  def to_s
383
427
  s = "LocalEnvironment["
384
428
  s << @framework.to_s