wd_newrelic_rpm 3.5.6 → 3.5.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/CHANGELOG +43 -3
  2. data/Gemfile +6 -2
  3. data/LICENSE +23 -0
  4. data/lib/new_relic/agent.rb +50 -3
  5. data/lib/new_relic/agent/agent.rb +40 -60
  6. data/lib/new_relic/agent/configuration/defaults.rb +9 -3
  7. data/lib/new_relic/agent/configuration/server_source.rb +4 -0
  8. data/lib/new_relic/agent/cross_app_monitor.rb +239 -0
  9. data/lib/new_relic/agent/cross_app_tracing.rb +281 -0
  10. data/lib/new_relic/agent/database.rb +28 -10
  11. data/lib/new_relic/agent/error_collector.rb +5 -0
  12. data/lib/new_relic/agent/event_listener.rb +4 -0
  13. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +58 -39
  14. data/lib/new_relic/agent/instrumentation/metric_frame.rb +16 -3
  15. data/lib/new_relic/agent/instrumentation/net.rb +13 -11
  16. data/lib/new_relic/agent/instrumentation/queue_time.rb +50 -192
  17. data/lib/new_relic/agent/instrumentation/rails4/action_controller.rb +145 -0
  18. data/lib/new_relic/agent/instrumentation/rails4/errors.rb +45 -0
  19. data/lib/new_relic/agent/instrumentation/resque.rb +10 -10
  20. data/lib/new_relic/agent/instrumentation/sinatra.rb +19 -9
  21. data/lib/new_relic/agent/new_relic_service.rb +63 -9
  22. data/lib/new_relic/agent/pipe_service.rb +8 -12
  23. data/lib/new_relic/agent/rules_engine.rb +72 -0
  24. data/lib/new_relic/agent/shim_agent.rb +0 -1
  25. data/lib/new_relic/agent/sql_sampler.rb +3 -2
  26. data/lib/new_relic/agent/stats.rb +149 -0
  27. data/lib/new_relic/agent/stats_engine.rb +9 -0
  28. data/lib/new_relic/agent/stats_engine/gc_profiler.rb +1 -24
  29. data/lib/new_relic/agent/stats_engine/metric_stats.rb +84 -185
  30. data/lib/new_relic/agent/stats_engine/stats_hash.rb +58 -0
  31. data/lib/new_relic/agent/stats_engine/transactions.rb +10 -2
  32. data/lib/new_relic/agent/transaction_info.rb +31 -6
  33. data/lib/new_relic/agent/transaction_sample_builder.rb +19 -8
  34. data/lib/new_relic/agent/transaction_sampler.rb +17 -10
  35. data/lib/new_relic/helper.rb +32 -0
  36. data/lib/new_relic/local_environment.rb +24 -32
  37. data/lib/new_relic/okjson.rb +599 -0
  38. data/lib/new_relic/transaction_sample.rb +2 -1
  39. data/lib/new_relic/transaction_sample/segment.rb +2 -1
  40. data/lib/new_relic/version.rb +1 -1
  41. data/newrelic.yml +27 -41
  42. data/test/multiverse/suites/agent_only/Envfile +5 -1
  43. data/test/multiverse/suites/agent_only/audit_log_test.rb +2 -4
  44. data/test/multiverse/suites/agent_only/config/newrelic.yml +1 -2
  45. data/test/multiverse/suites/agent_only/{cross_process_test.rb → cross_application_tracing_test.rb} +3 -3
  46. data/test/multiverse/suites/agent_only/key_transactions_test.rb +66 -0
  47. data/test/multiverse/suites/agent_only/marshaling_test.rb +9 -22
  48. data/test/multiverse/suites/agent_only/rename_rule_test.rb +57 -0
  49. data/test/multiverse/suites/agent_only/start_up_test.rb +1 -1
  50. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +17 -6
  51. data/test/multiverse/suites/rails/error_tracing_test.rb +20 -8
  52. data/test/multiverse/suites/rails/queue_time_test.rb +2 -2
  53. data/test/multiverse/suites/resque/instrumentation_test.rb +4 -3
  54. data/test/multiverse/suites/sinatra/Envfile +2 -0
  55. data/test/multiverse/suites/sinatra/config/newrelic.yml +1 -0
  56. data/test/multiverse/suites/sinatra/sinatra_metric_explosion_test.rb +5 -5
  57. data/test/multiverse/suites/sinatra/sinatra_test.rb +77 -10
  58. data/test/new_relic/agent/agent/connect_test.rb +45 -1
  59. data/test/new_relic/agent/agent/start_worker_thread_test.rb +0 -3
  60. data/test/new_relic/agent/agent_test.rb +20 -40
  61. data/test/new_relic/agent/agent_test_controller_test.rb +27 -60
  62. data/test/new_relic/agent/busy_calculator_test.rb +1 -1
  63. data/test/new_relic/agent/configuration/server_source_test.rb +8 -3
  64. data/test/new_relic/agent/cross_app_monitor_test.rb +237 -0
  65. data/test/new_relic/agent/database_test.rb +60 -16
  66. data/test/new_relic/agent/error_collector_test.rb +28 -4
  67. data/test/new_relic/agent/event_listener_test.rb +23 -2
  68. data/test/new_relic/agent/instrumentation/browser_monitoring_timings_test.rb +1 -1
  69. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +85 -12
  70. data/test/new_relic/agent/instrumentation/metric_frame_test.rb +95 -0
  71. data/test/new_relic/agent/instrumentation/net_instrumentation_test.rb +436 -59
  72. data/test/new_relic/agent/instrumentation/queue_time_test.rb +58 -357
  73. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +2 -5
  74. data/test/new_relic/agent/method_tracer_test.rb +4 -2
  75. data/test/new_relic/agent/new_relic_service_test.rb +108 -6
  76. data/test/new_relic/agent/pipe_channel_manager_test.rb +1 -1
  77. data/test/new_relic/agent/pipe_service_test.rb +9 -9
  78. data/test/new_relic/agent/rpm_agent_test.rb +0 -11
  79. data/test/new_relic/agent/rules_engine_test.rb +82 -0
  80. data/test/new_relic/agent/shim_agent_test.rb +0 -4
  81. data/test/new_relic/agent/sql_sampler_test.rb +7 -0
  82. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +85 -0
  83. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +110 -23
  84. data/test/new_relic/agent/stats_engine_test.rb +1 -46
  85. data/test/new_relic/agent/stats_hash_test.rb +93 -0
  86. data/test/new_relic/agent/stats_test.rb +197 -0
  87. data/test/new_relic/agent/transaction_info_test.rb +63 -11
  88. data/test/new_relic/agent/transaction_sample_builder_test.rb +10 -3
  89. data/test/new_relic/agent/transaction_sampler_test.rb +92 -80
  90. data/test/new_relic/agent/worker_loop_test.rb +1 -1
  91. data/test/new_relic/agent_test.rb +35 -5
  92. data/test/new_relic/control_test.rb +1 -1
  93. data/test/new_relic/fake_collector.rb +87 -9
  94. data/test/new_relic/helper_test.rb +24 -0
  95. data/test/new_relic/metric_data_test.rb +11 -11
  96. data/test/new_relic/metric_spec_test.rb +1 -1
  97. data/test/script/ci.sh +1 -1
  98. data/test/test_contexts.rb +0 -1
  99. data/test/test_helper.rb +21 -3
  100. metadata +34 -41
  101. data/lib/new_relic/agent/cross_process_monitoring.rb +0 -187
  102. data/lib/new_relic/stats.rb +0 -337
  103. data/test/new_relic/agent/cross_process_monitoring_test.rb +0 -190
  104. data/test/new_relic/agent/stats_engine/metric_stats/harvest_test.rb +0 -133
  105. data/test/new_relic/fakes_sending_data.rb +0 -30
  106. data/test/new_relic/stats_test.rb +0 -421
@@ -1,396 +1,97 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper'))
2
2
  class NewRelic::Agent::Instrumentation::QueueTimeTest < Test::Unit::TestCase
3
- require 'new_relic/agent/instrumentation/queue_time'
4
- include NewRelic::Agent::Instrumentation::QueueTime
3
+ include NewRelic::Agent::Instrumentation
5
4
 
6
- def setup
7
- NewRelic::Agent.instance.stats_engine.clear_stats
5
+ def test_parse_frontend_timestamp_given_queue_start_header
6
+ header = { 'HTTP_X_QUEUE_START' => format_header_time(Time.now - 60) }
7
+ assert_in_delta(seconds_ago(60), QueueTime.parse_frontend_timestamp(header), 0.1)
8
8
  end
9
9
 
10
- def create_test_start_time(env)
11
- env[APP_HEADER] = "t=#{convert_to_microseconds(Time.at(1002))}"
10
+ def test_parse_frontend_timestamp_given_request_start_header
11
+ header = { 'HTTP_X_REQUEST_START' => format_header_time(Time.now - 60) }
12
+ assert_in_delta(seconds_ago(60), QueueTime.parse_frontend_timestamp(header), 0.1)
12
13
  end
13
14
 
14
- def test_parse_frontend_headers
15
- middleware_start = Time.at(1002)
16
- queue_start = Time.at(1001)
17
- server_start = Time.at(1000)
18
- Time.stubs(:now).returns(Time.at(1003)) # whee!
19
- self.expects(:add_end_time_header).with(Time.at(1003), {:env => 'hash'})
20
- # ordering is important here, unfortunately, the mocks don't
21
- # support that kind of checking.
22
- self.expects(:parse_middleware_time_from).with({:env => 'hash'}).returns(middleware_start)
23
- self.expects(:parse_queue_time_from).with({:env => 'hash'}).returns(queue_start)
24
- self.expects(:parse_server_time_from).with({:env => 'hash'}).returns(server_start)
25
- assert_equal(server_start, parse_frontend_headers({:env => 'hash'}), "should return the oldest start time")
15
+ def test_parse_frontend_timestamp_given_middleware_start_header
16
+ header = { 'HTTP_X_MIDDLEWARE_START' => format_header_time(Time.now - 60) }
17
+ assert_in_delta(seconds_ago(60), QueueTime.parse_frontend_timestamp(header), 0.1)
26
18
  end
27
19
 
28
- def test_parse_frontend_headers_should_return_earliest_start
29
- middleware_start = Time.at(1002)
30
- queue_start = Time.at(1000)
31
- server_start = Time.at(1001)
32
- Time.stubs(:now).returns(Time.at(1003)) # whee!
33
- self.expects(:add_end_time_header).with(Time.at(1003), {:env => 'hash'})
34
- # ordering is important here, unfortunately, the mocks don't
35
- # support that kind of checking.
36
- self.expects(:parse_middleware_time_from).with({:env => 'hash'}).returns(middleware_start)
37
- self.expects(:parse_queue_time_from).with({:env => 'hash'}).returns(queue_start)
38
- self.expects(:parse_server_time_from).with({:env => 'hash'}).returns(server_start)
39
- assert_equal(queue_start, parse_frontend_headers({:env => 'hash'}), "should return the oldest start time")
40
- end
41
-
42
- def test_all_combined_frontend_headers
43
- env = {}
44
- env[MAIN_HEADER] = "t=#{convert_to_microseconds(Time.at(1000))}"
45
- env[QUEUE_HEADER] = "t=#{convert_to_microseconds(Time.at(1001))}"
46
- env[MIDDLEWARE_HEADER] = "t=#{convert_to_microseconds(Time.at(1002))}"
47
-
48
- env[APP_HEADER] = "t=#{convert_to_microseconds(Time.at(1003))}"
49
-
50
- assert_calls_metrics('WebFrontend/WebServer/all', 'WebFrontend/QueueTime', 'Middleware/all') do
51
- assert_equal(Time.at(1002), parse_middleware_time_from(env))
52
- assert_equal(Time.at(1001), parse_queue_time_from(env))
53
- assert_equal(Time.at(1000), parse_server_time_from(env))
54
- end
55
-
56
- check_metric_time('WebFrontend/WebServer/all', 1.0, 0.001)
57
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.001)
58
- check_metric_time('Middleware/all', 1.0, 0.001)
59
- end
60
-
61
- def test_combined_middleware_and_queue
62
- env = {}
63
- env[QUEUE_HEADER] = "t=#{convert_to_microseconds(Time.at(1000))}"
64
- env[MIDDLEWARE_HEADER] = "t=#{convert_to_microseconds(Time.at(1001))}"
65
- create_test_start_time(env)
66
-
67
- assert_calls_metrics('Middleware/all', 'WebFrontend/QueueTime') do
68
- parse_middleware_time_from(env)
69
- assert_equal(Time.at(1000), parse_queue_time_from(env))
70
- end
71
-
72
- check_metric_time('Middleware/all', 1.0, 0.001)
73
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.001)
74
- end
75
-
76
- def test_combined_queue_and_server
77
- env = {}
78
- env[MAIN_HEADER] = "t=#{convert_to_microseconds(Time.at(1000))}"
79
- env[QUEUE_HEADER] = "t=#{convert_to_microseconds(Time.at(1001))}"
80
- create_test_start_time(env)
81
-
82
- assert_calls_metrics('WebFrontend/WebServer/all', 'WebFrontend/QueueTime') do
83
- assert_equal(Time.at(1001), parse_queue_time_from(env))
84
- parse_server_time_from(env)
85
- end
86
-
87
- check_metric_time('WebFrontend/WebServer/all', 1.0, 0.001)
88
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.001)
89
- end
90
-
91
- def test_combined_middleware_and_server
92
- env = {}
93
- env[MAIN_HEADER] = "t=#{convert_to_microseconds(Time.at(1000))}"
94
- env[MIDDLEWARE_HEADER] = "t=#{convert_to_microseconds(Time.at(1001))}"
95
- create_test_start_time(env)
96
-
97
- assert_calls_metrics('WebFrontend/WebServer/all', 'Middleware/all') do
98
- parse_middleware_time_from(env)
99
- parse_server_time_from(env)
100
- end
101
-
102
- check_metric_time('WebFrontend/WebServer/all', 1.0, 0.001)
103
- check_metric_time('Middleware/all', 1.0, 0.001)
104
- end
105
-
106
- # initial base case, a router and a static content server
107
- def test_parse_server_time_from_initial
108
- env = {}
109
- create_test_start_time(env)
110
- time1 = convert_to_microseconds(Time.at(1000))
111
- time2 = convert_to_microseconds(Time.at(1001))
112
- env['HTTP_X_REQUEST_START'] = "servera t=#{time1}, serverb t=#{time2}"
113
- assert_calls_metrics('WebFrontend/WebServer/all', 'WebFrontend/WebServer/servera', 'WebFrontend/WebServer/serverb') do
114
- parse_server_time_from(env)
115
- end
116
- check_metric_time('WebFrontend/WebServer/all', 2.0, 0.1)
117
- check_metric_time('WebFrontend/WebServer/servera', 1.0, 0.1)
118
- check_metric_time('WebFrontend/WebServer/serverb', 1.0, 0.1)
119
- end
120
-
121
- def test_parse_server_time_accepting_milliseconds_resolution_separator_char
122
- env = {'HTTP_X_REQUEST_START' => "t=1000.000000"}
123
- create_test_start_time(env)
124
- env['HTTP_X_QUEUE_START'] = "t=1001.000000"
125
- assert_calls_metrics('WebFrontend/WebServer/all') do
126
- assert_equal(Time.at(1000), parse_server_time_from(env))
127
- end
128
- assert_calls_metrics('WebFrontend/QueueTime') do
129
- assert_equal(Time.at(1001), parse_queue_time_from(env))
130
- end
131
- check_metric_time('WebFrontend/WebServer/all', 2.0, 0.1)
132
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.1)
133
- end
134
-
135
- # test for backwards compatibility with old header
136
- def test_parse_server_time_from_with_no_server_name
137
- env = {'HTTP_X_REQUEST_START' => "t=#{convert_to_microseconds(Time.at(1001))}"}
138
- create_test_start_time(env)
139
- assert_calls_metrics('WebFrontend/WebServer/all') do
140
- parse_server_time_from(env)
141
- end
142
- check_metric_time('WebFrontend/WebServer/all', 1.0, 0.1)
143
- end
144
-
145
- def test_parse_server_time_from_with_bad_header
146
- env = {'HTTP_X_REQUEST_START' => 't=t=t=t='}
147
- create_test_start_time(env)
148
- assert_calls_metrics('WebFrontend/WebServer/all') do
149
- parse_server_time_from(env)
150
- end
151
- end
152
-
153
- def test_parse_server_time_from_with_no_header
154
- assert_calls_metrics('WebFrontend/WebServer/all') do
155
- parse_server_time_from({})
156
- end
157
- end
158
-
159
- def test_parse_middleware_time
160
- env = {}
161
- create_test_start_time(env)
162
- time1 = convert_to_microseconds(Time.at(1000))
163
- time2 = convert_to_microseconds(Time.at(1001))
164
-
165
- env['HTTP_X_MIDDLEWARE_START'] = "base t=#{time1}, second t=#{time2}"
166
- assert_calls_metrics('Middleware/all', 'Middleware/base', 'Middleware/second') do
167
- parse_middleware_time_from(env)
168
- end
169
- check_metric_time('Middleware/all', 2.0, 0.1)
170
- check_metric_time('Middleware/base', 1.0, 0.1)
171
- check_metric_time('Middleware/second', 1.0, 0.1)
172
- end
173
-
174
- def test_parse_queue_time
175
- env = {}
176
- create_test_start_time(env)
177
- time1 = convert_to_microseconds(Time.at(1000))
178
-
179
- env['HTTP_X_QUEUE_START'] = "t=#{time1}"
180
- assert_calls_metrics('WebFrontend/QueueTime') do
181
- assert_equal(Time.at(1000), parse_queue_time_from(env))
182
- end
183
-
184
- check_metric_time('WebFrontend/QueueTime', 2.0, 0.1)
185
- end
186
-
187
- def test_check_for_alternate_queue_length
188
- env = {}
189
- create_test_start_time(env)
190
- env['HTTP_X_QUEUE_TIME'] = '1000000'
191
- assert_calls_metrics('WebFrontend/QueueTime') do
192
- assert_equal(Time.at(1001), parse_queue_time_from(env))
193
- end
194
-
195
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.001)
196
- end
197
-
198
- def test_check_for_alternate_queue_length_override
199
- env = {}
200
- create_test_start_time(env)
201
- env['HTTP_X_QUEUE_START'] = 't=1' # obviously incorrect
202
- env['HTTP_X_QUEUE_TIME'] = '1000000'
203
- assert_calls_metrics('WebFrontend/QueueTime') do
204
- assert_equal(Time.at(1001), parse_queue_time_from(env))
205
- end
206
-
207
- # alternate queue should override normal header
208
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.001)
209
- end
210
-
211
- def test_check_for_heroku_queue_length
212
- env = {}
213
- create_test_start_time(env)
214
- env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'] = '1000'
215
- assert_calls_metrics('WebFrontend/QueueTime') do
216
- assert_equal(Time.at(1001), parse_queue_time_from(env))
217
- end
218
-
219
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.001)
220
- end
221
-
222
- def test_check_for_heroku_queue_length_override
223
- env = {}
224
- create_test_start_time(env)
225
- env['HTTP_X_QUEUE_TIME'] = '10000000' # ten MEEELION useconds
226
- env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'] = '1000'
227
- assert_calls_metrics('WebFrontend/QueueTime') do
228
- assert_equal(Time.at(1001), parse_queue_time_from(env))
229
- end
230
-
231
- # heroku queue should override alternate queue
232
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.001)
233
- end
234
-
235
- # each server should be one second, and the total would be 2 seconds
236
- def test_record_individual_server_stats
237
- matches = [['foo', Time.at(1000)], ['bar', Time.at(1001)]]
238
- assert_calls_metrics('WebFrontend/WebServer/foo', 'WebFrontend/WebServer/bar') do
239
- record_individual_server_stats(Time.at(1002), matches)
240
- end
241
- check_metric_time('WebFrontend/WebServer/foo', 1.0, 0.1)
242
- check_metric_time('WebFrontend/WebServer/bar', 1.0, 0.1)
243
- end
20
+ def test_parse_frontend_timestamp_from_earliest_header
21
+ headers = { 'HTTP_X_REQUEST_START' => format_header_time(Time.now - 63),
22
+ 'HTTP_X_QUEUE_START' => format_header_time(Time.now - 62),
23
+ 'HTTP_X_MIDDLEWARE_START' => format_header_time(Time.now - 61)}
244
24
 
245
- def test_record_rollup_server_stat
246
- assert_calls_metrics('WebFrontend/WebServer/all') do
247
- record_rollup_server_stat(Time.at(1001), [['a', Time.at(1000)]])
248
- end
249
- check_metric_time('WebFrontend/WebServer/all', 1.0, 0.1)
250
- end
251
-
252
- def test_record_rollup_server_stat_no_data
253
- assert_calls_metrics('WebFrontend/WebServer/all') do
254
- record_rollup_server_stat(Time.at(1001), [])
255
- end
256
- check_metric_time('WebFrontend/WebServer/all', 0.0, 0.001)
25
+ assert_in_delta(seconds_ago(63), QueueTime.parse_frontend_timestamp(headers), 0.1)
257
26
  end
258
27
 
259
- def test_record_rollup_middleware_stat
260
- assert_calls_metrics('Middleware/all') do
261
- record_rollup_middleware_stat(Time.at(1001), [['a', Time.at(1000)]])
262
- end
263
- check_metric_time('Middleware/all', 1.0, 0.1)
264
- end
265
-
266
- def test_record_rollup_middleware_stat_no_data
267
- assert_calls_metrics('Middleware/all') do
268
- record_rollup_middleware_stat(Time.at(1001), [])
269
- end
270
- check_metric_time('Middleware/all', 0.0, 0.001)
271
- end
272
-
273
- def test_record_rollup_queue_stat
274
- assert_calls_metrics('WebFrontend/QueueTime') do
275
- record_rollup_queue_stat(Time.at(1001), [[nil, Time.at(1000)]])
276
- end
277
- check_metric_time('WebFrontend/QueueTime', 1.0, 0.1)
278
- end
28
+ def test_parse_frontend_timestamp_from_earliest_header_out_of_order
29
+ headers = { 'HTTP_X_MIDDLEWARE_START' => format_header_time(Time.now - 63),
30
+ 'HTTP_X_REQUEST_START' => format_header_time(Time.now - 62),
31
+ 'HTTP_X_QUEUE_START' => format_header_time(Time.now - 61) }
279
32
 
280
- def test_record_rollup_queue_stat_no_data
281
- assert_calls_metrics('WebFrontend/QueueTime') do
282
- record_rollup_queue_stat(Time.at(1001), [])
283
- end
284
- check_metric_time('WebFrontend/QueueTime', 0.0, 0.001)
33
+ assert_in_delta(seconds_ago(63), QueueTime.parse_frontend_timestamp(headers), 0.1)
285
34
  end
286
35
 
287
- def test_find_oldest_time
288
- test_array = [
289
- ['c', Time.at(1002)],
290
- ['a', Time.at(1000)],
291
- ['b', Time.at(1001)],
292
- ['d', Time.at(1000)],
293
- ]
294
- assert_equal(find_oldest_time(test_array), Time.at(1000),
295
- "Should be the oldest time in the array")
36
+ def test_parse_frontend_timestamp_from_header_in_seconds
37
+ header = { 'HTTP_X_QUEUE_START' => "t=#{Time.now.to_f - 60}" }
38
+ assert_in_delta(seconds_ago(60), QueueTime.parse_frontend_timestamp(header), 0.1)
296
39
  end
297
40
 
298
- # trivial test but the method doesn't do much
299
- def test_record_server_time_for
300
- name = 'foo'
301
- time = Time.at(1000)
302
- start_time = Time.at(1001)
303
- self.expects(:record_time_stat).with('WebFrontend/WebServer/foo', time, start_time)
304
- record_server_time_for(name, time, start_time)
41
+ def test_parse_frontend_timestamp_from_header_in_milliseconds
42
+ header = { 'HTTP_X_QUEUE_START' => "t=#{(Time.now.to_f - 60) * 1_000}" }
43
+ assert_in_delta(seconds_ago(60), QueueTime.parse_frontend_timestamp(header), 0.1)
305
44
  end
306
45
 
307
- def test_record_time_stat
308
- assert_calls_metrics('WebFrontend/WebServer/foo') do
309
- record_time_stat('WebFrontend/WebServer/foo', Time.at(1000), Time.at(1001))
310
- end
311
- check_metric_time('WebFrontend/WebServer/foo', 1.0, 0.1)
312
- assert_raises(RuntimeError) do
313
- record_time_stat('foo', Time.at(1001), Time.at(1000))
314
- end
46
+ def test_parse_frontend_timestamp_from_header_with_multiple_servers
47
+ now = Time.now.to_f
48
+ header = { 'HTTP_X_QUEUE_START' => "servera t=#{now - 60}, serverb t=#{now - 30}" }
49
+ assert_in_delta(seconds_ago(60), QueueTime.parse_frontend_timestamp(header), 0.1)
315
50
  end
316
51
 
317
- def test_record_time_stat_with_end_after_start
318
- record_time_stat('WebFrontend/WebServer/foo', Time.at(1.0001), Time.at(1))
319
- rescue RuntimeError => e
320
- assert_match(/should not provide an end time less than start time/, e.message)
52
+ def test_parse_frontend_timestamp_from_header_missing_t_equals
53
+ header = { 'HTTP_X_REQUEST_START' => (Time.now.to_f - 60).to_s }
54
+ assert_in_delta(seconds_ago(60), QueueTime.parse_frontend_timestamp(header), 0.1)
321
55
  end
322
56
 
323
- def test_convert_to_microseconds
324
- assert_equal((1_000_000_000), convert_to_microseconds(Time.at(1000)), 'time at 1000 seconds past epoch should be 1,000,000,000 usec')
325
- assert_equal 1_000_000_000, convert_to_microseconds(1_000_000_000), 'should not mess with a number if passed in'
326
- assert_raises(TypeError) do
327
- convert_to_microseconds('whoo yeah buddy')
328
- end
57
+ def test_parse_frontend_timestamp_from_header_negative
58
+ now = Time.now
59
+ the_future = Time.at(now.to_f + 60)
60
+ header = { 'HTTP_X_REQUEST_START' => the_future.to_f.to_s }
61
+ assert_in_delta(now, QueueTime.parse_frontend_timestamp(header, now), 0.1)
329
62
  end
330
63
 
331
- def test_convert_from_microseconds
332
- assert_equal Time.at(1000), convert_from_microseconds(1_000_000_000), 'time at 1,000,000,000 usec should be 1000 seconds after epoch'
333
- assert_equal Time.at(1000), convert_from_microseconds(Time.at(1000)), 'should not mess with a time passed in'
334
- assert_raises(TypeError) do
335
- convert_from_microseconds('10000000000')
64
+ def test_recording_queue_time_metric
65
+ assert_metric_value_in_delta(60, 'WebFrontend/QueueTime', 0.1) do
66
+ QueueTime.record_frontend_metrics(Time.at(Time.now.to_f - 60))
336
67
  end
337
68
  end
338
69
 
339
- def test_add_end_time_header
340
- env = {}
341
- start_time = Time.at(1)
342
- add_end_time_header(start_time, env)
343
- assert_equal({'HTTP_X_APPLICATION_START' => "t=#{convert_to_microseconds(Time.at(1))}"}, env, "should add the header to the env hash")
344
- end
70
+ def test_parsing_malformed_header
71
+ header = { 'HTTP_X_REQUEST_START' => 'gobledy gook' }
345
72
 
346
- def test_parse_end_time_base
347
- env = {}
348
- env['HTTP_X_APPLICATION_START'] = "t=#{convert_to_microseconds(Time.at(1))}"
349
- start_time = parse_end_time(env)
350
- assert_equal(Time.at(1), start_time, "should pull the correct start time from the app header")
73
+ assert_nil QueueTime.parse_frontend_timestamp(header)
351
74
  end
352
75
 
353
- def test_get_matches_from_header
354
- env = {'A HEADER' => 't=1000000'}
355
- self.expects(:convert_from_microseconds).with(1000000).returns(Time.at(1))
356
- matches = get_matches_from_header('A HEADER', env)
357
- assert_equal [[nil, Time.at(1)]], matches, "should pull the correct time from the string"
76
+ def test_parse_timestamp_can_identify_unit
77
+ now = Time.now.to_f
78
+ assert_in_delta(now, QueueTime.parse_timestamp(now.to_f.to_s).to_f, 0.001)
79
+ assert_in_delta(now, QueueTime.parse_timestamp((now.to_f * 1_000).to_s).to_f, 0.001)
80
+ assert_in_delta(now, QueueTime.parse_timestamp((now.to_f * 1_000_000).to_s).to_f, 0.001)
358
81
  end
359
82
 
360
- def test_convert_to_name_time_pair
361
- name = :foo
362
- time = "1000000"
363
-
364
- pair = convert_to_name_time_pair(name, time)
365
- assert_equal [:foo, Time.at(1)], pair
83
+ def format_header_time(time=Time.now)
84
+ "t=#{(time.to_f * 1_000_000).to_i}"
366
85
  end
367
86
 
368
- def test_get_matches
369
- str = "servera t=1000000, serverb t=1000000"
370
- matches = get_matches(str) # start a fire
371
- assert_equal [['servera', '1000000'], ['serverb', '1000000']], matches
87
+ def seconds_ago(seconds)
88
+ Time.at(Time.now.to_f - seconds)
372
89
  end
373
90
 
374
- def test_matches_with_bad_data
375
- str = "stephan is a dumb lol"
376
- matches = get_matches(str)
377
- assert_equal [], matches
378
-
379
- str = "t=100"
380
- matches = get_matches(str)
381
- assert_equal [[nil, '100']], matches
382
-
383
- str = nil
384
- matches = get_matches(str)
385
- assert_equal [], matches
386
- end
387
- # each server should be one second, and the total would be 2 seconds
388
- def test_record_individual_middleware_stats
389
- matches = [['foo', Time.at(1000)], ['bar', Time.at(1001)]]
390
- assert_calls_metrics('Middleware/foo', 'Middleware/bar') do
391
- record_individual_middleware_stats(Time.at(1002), matches)
392
- end
393
- check_metric_time('Middleware/foo', 1.0, 0.1)
394
- check_metric_time('Middleware/bar', 1.0, 0.1)
91
+ def assert_metric_value_in_delta(expected, metric_name, delta)
92
+ stats_engine = NewRelic::Agent.instance.stats_engine
93
+ stats_engine.clear_stats
94
+ yield
95
+ assert_in_delta(expected, stats_engine.get_stats(metric_name).total_call_time, delta)
395
96
  end
396
97
  end