newrelic_rpm 3.7.2.190.beta → 3.7.2.192

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data.tar.gz.sig +0 -0
  2. data/CHANGELOG +48 -0
  3. data/LICENSE +1 -1
  4. data/lib/new_relic/agent/configuration/default_source.rb +0 -6
  5. data/lib/new_relic/agent/cross_app_monitor.rb +7 -6
  6. data/lib/new_relic/agent/datastores/mongo/metric_generator.rb +8 -0
  7. data/lib/new_relic/agent/datastores/mongo/metric_translator.rb +27 -36
  8. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +21 -11
  9. data/lib/new_relic/agent/instrumentation/mongo.rb +56 -40
  10. data/lib/new_relic/agent/method_tracer.rb +10 -3
  11. data/lib/new_relic/agent/transaction_sampler.rb +0 -3
  12. data/lib/sequel/extensions/newrelic_instrumentation.rb +12 -5
  13. data/test/helpers/mongo_metric_builder.rb +1 -1
  14. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +2 -2
  15. data/test/multiverse/suites/mongo/Envfile +19 -28
  16. data/test/multiverse/suites/mongo/helpers/mongo_operation_tests.rb +437 -0
  17. data/test/multiverse/suites/mongo/helpers/mongo_replica_set.rb +97 -0
  18. data/test/multiverse/suites/mongo/helpers/mongo_replica_set_test.rb +82 -0
  19. data/test/multiverse/suites/mongo/helpers/mongo_server.rb +239 -0
  20. data/test/multiverse/suites/mongo/helpers/mongo_server_test.rb +176 -0
  21. data/test/multiverse/suites/mongo/mongo_connection_test.rb +40 -0
  22. data/test/multiverse/suites/mongo/mongo_instrumentation_test.rb +8 -393
  23. data/test/multiverse/suites/mongo/mongo_unsupported_version_test.rb +6 -4
  24. data/test/multiverse/suites/rails/ignore_test.rb +7 -2
  25. data/test/multiverse/suites/sequel/database.rb +24 -20
  26. data/test/multiverse/suites/sequel/sequel_instrumentation_test.rb +16 -0
  27. data/test/new_relic/agent/cross_app_monitor_test.rb +4 -2
  28. data/test/new_relic/agent/datastores/mongo/metric_generator_test.rb +27 -1
  29. data/test/new_relic/agent/datastores/mongo/metric_translator_test.rb +19 -9
  30. data/test/new_relic/agent/instrumentation/active_record_instrumentation_test.rb +497 -493
  31. data/test/new_relic/agent/method_tracer_test.rb +23 -0
  32. data/test/new_relic/agent/transaction_sampler_test.rb +5 -16
  33. data/test/new_relic/json_wrapper_test.rb +5 -6
  34. data/test/performance/suites/trace_execution_scoped.rb +32 -0
  35. metadata +34 -26
  36. metadata.gz.sig +0 -0
@@ -0,0 +1,97 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'fileutils'
6
+ require 'timeout'
7
+ require 'mongo'
8
+ require File.expand_path(File.join(File.dirname(__FILE__), 'mongo_server'))
9
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'helpers', 'file_searching'))
10
+
11
+ class MongoReplicaSet
12
+ include Mongo
13
+
14
+ attr_accessor :servers, :client
15
+
16
+ def initialize
17
+ create_servers
18
+ end
19
+
20
+ def create_servers
21
+ self.servers = Array.new(3) { MongoServer.new(:replica) }
22
+ end
23
+
24
+ def start
25
+ self.servers.each { |server| server.start }
26
+ initiate
27
+
28
+ retry_on_exception(:exception => Mongo::ConnectionFailure, :tries => 100) do
29
+ self.client = MongoReplicaSetClient.new(server_connections, :read => :secondary)
30
+ end
31
+
32
+ self
33
+ end
34
+
35
+ def retry_on_exception(options)
36
+ exception = options.fetch(:exception, StandardError)
37
+ message = options[:message]
38
+ maximum_tries = options.fetch(:tries, 3)
39
+
40
+ tries = 0
41
+
42
+ begin
43
+ result = yield
44
+ rescue exception => e
45
+ if message
46
+ raise e unless e.message.include? message
47
+ end
48
+
49
+ sleep 0.1
50
+ tries += 1
51
+ retry unless tries > maximum_tries
52
+ raise e
53
+ end
54
+ end
55
+
56
+ def server_connections
57
+ self.servers.map do |server|
58
+ "localhost:#{server.port}"
59
+ end
60
+ end
61
+
62
+ def stop
63
+ self.servers.each { |server| server.stop }
64
+ self.client = nil
65
+ end
66
+
67
+ def running?
68
+ server_running_statuses = self.servers.map(&:running?).uniq
69
+ server_running_statuses.length == 1 && server_running_statuses.first
70
+ end
71
+
72
+ def status
73
+ return nil unless running?
74
+ self.servers.first.client['admin'].command( { 'replSetGetStatus' => 1 } )
75
+ rescue Mongo::OperationFailure => e
76
+ raise e unless e.message.include? 'EMPTYCONFIG'
77
+ end
78
+
79
+ def config
80
+ return unless running?
81
+
82
+ config = { :_id => 'multiverse', :members => [] }
83
+
84
+ self.servers.each_with_index do |server, index|
85
+ config[:members] << { :_id => index, :host => "localhost:#{server.port}" }
86
+ end
87
+
88
+ config
89
+ end
90
+
91
+ def initiate
92
+ return nil unless running?
93
+ self.servers.first.client['admin'].command( { 'replSetInitiate' => config } )
94
+ rescue Mongo::OperationFailure => e
95
+ raise e unless e.message.match(/already initialized/)
96
+ end
97
+ end
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__), 'mongo_replica_set'))
6
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'helpers', 'file_searching'))
7
+ require 'test/unit'
8
+ require 'mocha/setup'
9
+
10
+ class MongoReplicaSetTest < Test::Unit::TestCase
11
+ def setup
12
+ @replica = MongoReplicaSet.new
13
+ end
14
+
15
+ def teardown
16
+ @replica.stop
17
+ end
18
+
19
+ def test_replica_is_created_with_three_servers
20
+ assert_equal 3, @replica.servers.length
21
+ end
22
+
23
+ def test_start_starts_all_servers
24
+ @replica.start
25
+ assert_equal [true], @replica.servers.map(&:running?).uniq
26
+ end
27
+
28
+ def test_stop_stops_all_servers
29
+ @replica.start
30
+ @replica.stop
31
+ assert_equal [false], @replica.servers.map(&:running?).uniq
32
+ end
33
+
34
+ def test_running_returns_true_for_started_replica_set
35
+ @replica.start
36
+ assert @replica.running?
37
+ end
38
+
39
+ def test_running_returns_false_for_stopped_replica_set
40
+ @replica.start
41
+ @replica.stop
42
+ refute @replica.running?
43
+ end
44
+
45
+ def test_status_does_not_raise_an_error_for_uninitiated_replica_set
46
+ @replica.start
47
+ assert_nothing_raised Mongo::OperationFailure do
48
+ @replica.status
49
+ end
50
+ end
51
+
52
+ def test_config_returns_nil_unless_servers_are_running
53
+ @replica.start
54
+ @replica.stop
55
+ assert_nil @replica.config
56
+ end
57
+
58
+ def test_started_replica_set_servers_have_unique_ports
59
+ @replica.start
60
+
61
+ assert_equal 3, @replica.servers.map(&:port).uniq.length
62
+ end
63
+
64
+ def test_config_includes_replica_set_member_strings_with_correct_ports
65
+ @replica.start
66
+
67
+ result = @replica.config[:members].map { |member| member[:host].split(':').last.to_i }
68
+ expected = @replica.servers.map(&:port)
69
+
70
+ assert_equal expected, result
71
+ end
72
+
73
+ def test_server_connections_returns_the_hosts_and_ports_for_the_servers
74
+ expected = ['localhost:27017', 'localhost:27018', 'localhost:27019']
75
+ assert_equal expected, @replica.server_connections
76
+ end
77
+
78
+ def test_start_creates_a_replica_set_client_connection
79
+ @replica.start
80
+ assert @replica.client
81
+ end
82
+ end
@@ -0,0 +1,239 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'fileutils'
6
+ require 'timeout'
7
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'helpers', 'file_searching'))
8
+
9
+ class MongoServer
10
+ extend NewRelic::TestHelpers::FileSearching
11
+
12
+ attr_reader :type
13
+ attr_accessor :port, :client
14
+
15
+ def initialize(type = :single)
16
+ @type = type
17
+ lock_port
18
+
19
+ make_directories
20
+ end
21
+
22
+ def self.count(type = :all)
23
+ count = `ps aux | grep mongo[d]`.split("\n").length
24
+ count -= Dir.glob(File.join(MongoServer.pid_directory, '*.pid')).length if type == :children
25
+ count
26
+ end
27
+
28
+ def self.tmp_directory
29
+ ENV["MULTIVERSE_TMP"] || File.join(gem_root, 'tmp')
30
+ end
31
+
32
+ def self.port_lock_directory
33
+ File.join(tmp_directory, 'ports')
34
+ end
35
+
36
+ def self.pid_directory
37
+ File.join(tmp_directory, 'pids')
38
+ end
39
+
40
+ def self.db_directory
41
+ File.join(tmp_directory, 'db')
42
+ end
43
+
44
+ def self.log_directory
45
+ File.join(tmp_directory, 'log')
46
+ end
47
+
48
+ def ping
49
+ return unless self.client
50
+ self.client['admin'].command( { 'ping' => 1 } )
51
+ end
52
+
53
+ def pingable?
54
+ ping == { "ok" => 1.0 }
55
+ end
56
+
57
+ def make_directories
58
+ directories = [
59
+ MongoServer.pid_directory,
60
+ MongoServer.db_directory,
61
+ MongoServer.log_directory,
62
+ db_path
63
+ ]
64
+
65
+ FileUtils.mkdir_p(directories)
66
+ end
67
+
68
+ def make_port_lock_directory
69
+ FileUtils.mkdir_p(MongoServer.port_lock_directory)
70
+ end
71
+
72
+ def port_lock_path
73
+ File.join(MongoServer.port_lock_directory, "#{self.port}.lock")
74
+ end
75
+
76
+ def pid_path
77
+ File.join(MongoServer.pid_directory, "#{self.port}-#{self.object_id}-#{Process.pid}.pid")
78
+ end
79
+
80
+ def db_path
81
+ File.join(MongoServer.db_directory, "data_#{self.port}")
82
+ end
83
+
84
+ def log_path
85
+ File.join(MongoServer.log_directory, "#{self.port}.log")
86
+ end
87
+
88
+ def start(wait_for_startup = true)
89
+ lock_port
90
+
91
+ unless running?
92
+ `#{startup_command}`
93
+
94
+ wait_until do
95
+ running?
96
+ end
97
+
98
+ if wait_for_startup
99
+ create_client
100
+ wait_until do
101
+ pingable?
102
+ end
103
+ end
104
+ end
105
+
106
+ self
107
+ end
108
+
109
+ def wait_until(seconds = 10)
110
+ Timeout.timeout(seconds) do
111
+ sleep 0.1 until yield
112
+ end
113
+ end
114
+
115
+ def startup_command
116
+ pid_file = "--pidfilepath #{pid_path}"
117
+ log_file = "--logpath #{log_path} "
118
+
119
+ dbpath = "--dbpath #{db_path}"
120
+ port_flag = "--port #{self.port}"
121
+ small_mongo = "--oplogSize 128 --smallfiles"
122
+ repl_set = "--fork --replSet multiverse"
123
+
124
+ base = "#{port_flag} #{pid_file} #{log_file} #{small_mongo} #{dbpath}"
125
+
126
+ if self.type == :single
127
+ "mongod #{base} &"
128
+ elsif self.type == :replica
129
+ "mongod #{repl_set} #{base} &"
130
+ end
131
+ end
132
+
133
+ def create_client(client_class = nil)
134
+ require 'mongo'
135
+ if defined? MongoClient
136
+ client_class ||= MongoClient
137
+ else
138
+ client_class ||= Mongo::Connection
139
+ end
140
+
141
+ begin
142
+ self.client = client_class.new('localhost', self.port, :connect_timeout => 10)
143
+ rescue Mongo::ConnectionFailure => e
144
+ raise e unless message = "Failed to connect to a master node at localhost:#{port}"
145
+ retry
146
+ end
147
+ end
148
+
149
+ def stop
150
+ if pid
151
+ begin
152
+ Process.kill('TERM', pid)
153
+ rescue Errno::ESRCH => e
154
+ # fine if we're already gone...
155
+ end
156
+
157
+ wait_until do
158
+ !running?
159
+ end
160
+
161
+ cleanup_files
162
+ self.client = nil
163
+ end
164
+
165
+ release_port
166
+ self
167
+ end
168
+
169
+ def running?
170
+ return false unless pid
171
+ Process.kill(0, pid) == 1
172
+ rescue Errno::ESRCH => e
173
+ false
174
+ end
175
+
176
+ def pid
177
+ File.read(pid_path).to_i if File.exists? pid_path
178
+ end
179
+
180
+ def next_available_port
181
+ used_ports = all_port_lock_files.map do |filename|
182
+ File.basename(filename, '.lock').to_i
183
+ end
184
+
185
+ if used_ports.empty?
186
+ 27017
187
+ else
188
+ used_ports.sort.last + 1
189
+ end
190
+ end
191
+
192
+ def all_port_lock_files
193
+ Dir.glob(File.join(MongoServer.port_lock_directory, '*.lock'))
194
+ end
195
+
196
+ def lock_port
197
+ return if self.port
198
+
199
+ make_port_lock_directory
200
+
201
+ retry_on_exception(:exception => Errno::EEXIST, :tries => 10) do
202
+ self.port = next_available_port
203
+ File.new(port_lock_path, File::CREAT|File::EXCL)
204
+ end
205
+ end
206
+
207
+ def retry_on_exception(options)
208
+ exception = options.fetch(:exception, StandardError)
209
+ message = options[:message]
210
+ maximum_tries = options.fetch(:tries, 3)
211
+
212
+ tries = 0
213
+
214
+ begin
215
+ result = yield
216
+ rescue exception => e
217
+ if message
218
+ raise e unless e.message.include? message
219
+ end
220
+
221
+ sleep 0.1
222
+ tries += 1
223
+ retry unless tries > maximum_tries
224
+ raise e
225
+ end
226
+ end
227
+
228
+ def release_port
229
+ FileUtils.rm port_lock_path, :force => true
230
+ self.port = nil
231
+ end
232
+
233
+ # PID file needs to be cleaned up for our process checking logic
234
+ # DB needs to get cleaned up because it's massive
235
+ def cleanup_files
236
+ FileUtils.rm(pid_path)
237
+ FileUtils.rm_rf(db_path)
238
+ end
239
+ end
@@ -0,0 +1,176 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require File.expand_path(File.join(File.dirname(__FILE__), 'mongo_server'))
6
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'helpers', 'file_searching'))
7
+ require 'test/unit'
8
+ require 'mocha/setup'
9
+
10
+ class MongoServerTest < Test::Unit::TestCase
11
+ include NewRelic::TestHelpers::FileSearching
12
+
13
+ def setup
14
+ @server = MongoServer.new
15
+ end
16
+
17
+ def teardown
18
+ @server.stop
19
+ end
20
+
21
+ def test_new_server_has_a_locked_port
22
+ assert File.exists?(@server.port_lock_path)
23
+ end
24
+
25
+ def test_creating_a_new_server_after_locking_port_uses_the_next_port
26
+ new_server = MongoServer.new
27
+ new_server_port = new_server.port
28
+ assert_equal @server.port + 1, new_server_port
29
+ ensure
30
+ new_server.stop
31
+ end
32
+
33
+ def test_pid_path_is_unique_for_two_servers
34
+ new_server = MongoServer.new
35
+ pid_path1 = @server.start.pid_path
36
+ pid_path2 = new_server.start.pid_path
37
+
38
+ refute_equal pid_path1, pid_path2
39
+ ensure
40
+ new_server.stop
41
+ end
42
+
43
+ def test_release_port_deletes_the_port_lock_file
44
+ path = @server.port_lock_path
45
+ assert File.exists?(path)
46
+ @server.release_port
47
+ refute File.exists?(path)
48
+ end
49
+
50
+ def test_all_port_lock_files_returns_all_file_names
51
+ File.write(@server.port_lock_path, @server.port)
52
+ result = @server.all_port_lock_files
53
+ assert_equal [@server.port_lock_path], result
54
+ end
55
+
56
+ def test_start_creates_a_mongod_process
57
+ @server.start
58
+ assert_equal 1, MongoServer.count
59
+ end
60
+
61
+ def test_starting_twice_only_creates_one_process
62
+ @server.start
63
+ @server.start
64
+ assert_equal 1, MongoServer.count
65
+ end
66
+
67
+ def test_server_is_running_after_start
68
+ @server.start
69
+ assert @server.running?
70
+ end
71
+
72
+ def test_stop_kills_a_mongod_process
73
+ @server.start
74
+ @server.stop
75
+ assert_equal 0, MongoServer.count
76
+ end
77
+
78
+ def test_stop_releases_port
79
+ @server.start
80
+ assert File.exists?(@server.port_lock_path)
81
+ @server.stop
82
+ refute File.exists?(@server.port_lock_path)
83
+ end
84
+
85
+ def test_stop_deletes_pid_file
86
+ @server.start
87
+ assert File.exists?(@server.pid_path)
88
+ @server.stop
89
+ refute File.exists?(@server.pid_path)
90
+ end
91
+
92
+ def test_pingable_returns_true_if_ping_is_ok
93
+ ok_status = { "ok" => 1.0 }
94
+ @server.stubs(:ping).returns ok_status
95
+ assert @server.pingable?
96
+ end
97
+
98
+ def test_server_start_times_out_if_it_isnt_pingable
99
+ @server.stubs(:pingable?).returns false
100
+
101
+ assert_raise Timeout::Error do
102
+ @server.start
103
+ end
104
+ end
105
+
106
+ def test_server_count_returns_the_number_of_mongo_processes
107
+ previous_server_count = `ps aux | grep mongo[d]`.split("\n").length
108
+ @server.start
109
+ assert_equal previous_server_count + 1, MongoServer.count
110
+ end
111
+
112
+ def test_server_count_children_returns_number_of_mongo_processes_started_by_mongo_server
113
+ test_pid_path = File.join(MongoServer.tmp_directory, 'test.pid')
114
+ `mongod --pidfilepath #{test_pid_path} &`
115
+ pid = File.read(test_pid_path).to_i
116
+ @server.start
117
+ result = MongoServer.count(:children)
118
+
119
+ assert_equal 1, result
120
+ ensure
121
+ Process.kill('TERM', pid)
122
+ FileUtils.rm(test_pid_path)
123
+ end
124
+
125
+ def test_replica_set_servers_include_fork_flag
126
+ replica = MongoServer.new(:replica)
127
+ assert_includes replica.startup_command, '--fork'
128
+ replica.stop
129
+ end
130
+
131
+ def test_replica_set_servers_include_repl_set
132
+ replica = MongoServer.new(:replica)
133
+ assert_includes replica.startup_command, '--replSet'
134
+ replica.stop
135
+ end
136
+
137
+ def test_starting_a_server_creates_a_client
138
+ @server.start
139
+ assert @server.client
140
+ end
141
+
142
+ def test_ping_returns_ok_for_started_server
143
+ @server.start
144
+ ok_status = { "ok" => 1.0 }
145
+ assert_equal ok_status, @server.ping
146
+ end
147
+
148
+ def test_stop_sets_client_to_nil
149
+ @server.start
150
+ @server.stop
151
+ assert_nil @server.client
152
+ end
153
+
154
+ def test_ping_returns_nil_for_stopped_server
155
+ @server.start
156
+ @server.stop
157
+ assert_nil @server.ping
158
+ end
159
+
160
+ def test_servers_in_different_threads_use_unique_ports
161
+ threads = []
162
+
163
+ 20.times do
164
+ threads << Thread.new do
165
+ MongoServer.new
166
+ end
167
+ end
168
+
169
+ servers = threads.map(&:value)
170
+
171
+ assert_equal 20, servers.map(&:port).uniq.length
172
+ ensure
173
+ servers.each(&:stop)
174
+ end
175
+ end
176
+