genki-newrelic_rpm 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/CHANGELOG +316 -0
  2. data/LICENSE +37 -0
  3. data/Manifest +156 -0
  4. data/README.md +138 -0
  5. data/Rakefile +22 -0
  6. data/bin/mongrel_rpm +33 -0
  7. data/bin/newrelic_cmd +4 -0
  8. data/cert/cacert.pem +34 -0
  9. data/genki-newrelic_rpm.gemspec +32 -0
  10. data/init.rb +38 -0
  11. data/install.rb +37 -0
  12. data/lib/new_relic/agent.rb +280 -0
  13. data/lib/new_relic/agent/agent.rb +627 -0
  14. data/lib/new_relic/agent/chained_call.rb +13 -0
  15. data/lib/new_relic/agent/collection_helper.rb +61 -0
  16. data/lib/new_relic/agent/error_collector.rb +125 -0
  17. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  18. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +83 -0
  19. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  20. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +368 -0
  21. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  22. data/lib/new_relic/agent/instrumentation/dispatcher_instrumentation.rb +132 -0
  23. data/lib/new_relic/agent/instrumentation/memcache.rb +21 -0
  24. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  25. data/lib/new_relic/agent/instrumentation/merb/dispatcher.rb +13 -0
  26. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  27. data/lib/new_relic/agent/instrumentation/net.rb +12 -0
  28. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  29. data/lib/new_relic/agent/instrumentation/rack.rb +77 -0
  30. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  31. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  32. data/lib/new_relic/agent/instrumentation/rails/dispatcher.rb +38 -0
  33. data/lib/new_relic/agent/instrumentation/rails/errors.rb +27 -0
  34. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  35. data/lib/new_relic/agent/method_tracer.rb +277 -0
  36. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  37. data/lib/new_relic/agent/sampler.rb +12 -0
  38. data/lib/new_relic/agent/samplers/cpu_sampler.rb +49 -0
  39. data/lib/new_relic/agent/samplers/memory_sampler.rb +137 -0
  40. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +22 -0
  41. data/lib/new_relic/agent/shim_agent.rb +21 -0
  42. data/lib/new_relic/agent/stats_engine.rb +24 -0
  43. data/lib/new_relic/agent/stats_engine/metric_stats.rb +111 -0
  44. data/lib/new_relic/agent/stats_engine/samplers.rb +71 -0
  45. data/lib/new_relic/agent/stats_engine/transactions.rb +155 -0
  46. data/lib/new_relic/agent/transaction_sampler.rb +319 -0
  47. data/lib/new_relic/agent/worker_loop.rb +118 -0
  48. data/lib/new_relic/commands/deployments.rb +145 -0
  49. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  50. data/lib/new_relic/control.rb +436 -0
  51. data/lib/new_relic/control/external.rb +13 -0
  52. data/lib/new_relic/control/merb.rb +22 -0
  53. data/lib/new_relic/control/rails.rb +143 -0
  54. data/lib/new_relic/control/ruby.rb +34 -0
  55. data/lib/new_relic/control/sinatra.rb +14 -0
  56. data/lib/new_relic/histogram.rb +89 -0
  57. data/lib/new_relic/local_environment.rb +285 -0
  58. data/lib/new_relic/merbtasks.rb +6 -0
  59. data/lib/new_relic/metric_data.rb +44 -0
  60. data/lib/new_relic/metric_parser.rb +120 -0
  61. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  62. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  63. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  64. data/lib/new_relic/metric_parser/controller.rb +54 -0
  65. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  66. data/lib/new_relic/metric_parser/errors.rb +6 -0
  67. data/lib/new_relic/metric_parser/external.rb +50 -0
  68. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  69. data/lib/new_relic/metric_parser/view.rb +61 -0
  70. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  71. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  72. data/lib/new_relic/metric_spec.rb +52 -0
  73. data/lib/new_relic/metrics.rb +7 -0
  74. data/lib/new_relic/noticed_error.rb +25 -0
  75. data/lib/new_relic/rack/metric_app.rb +56 -0
  76. data/lib/new_relic/rack/newrelic.ru +25 -0
  77. data/lib/new_relic/rack/newrelic.yml +25 -0
  78. data/lib/new_relic/rack_app.rb +5 -0
  79. data/lib/new_relic/recipes.rb +82 -0
  80. data/lib/new_relic/stats.rb +360 -0
  81. data/lib/new_relic/transaction_analysis.rb +121 -0
  82. data/lib/new_relic/transaction_sample.rb +583 -0
  83. data/lib/new_relic/version.rb +54 -0
  84. data/lib/new_relic_api.rb +315 -0
  85. data/lib/newrelic_rpm.rb +40 -0
  86. data/lib/tasks/all.rb +4 -0
  87. data/lib/tasks/install.rake +7 -0
  88. data/lib/tasks/tests.rake +13 -0
  89. data/newrelic.yml +214 -0
  90. data/recipes/newrelic.rb +6 -0
  91. data/test/active_record_fixtures.rb +55 -0
  92. data/test/config/newrelic.yml +46 -0
  93. data/test/config/test_control.rb +39 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +234 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +107 -0
  96. data/test/new_relic/agent/agent_test.rb +117 -0
  97. data/test/new_relic/agent/agent_test_controller.rb +44 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +118 -0
  100. data/test/new_relic/agent/dispatcher_instrumentation_test.rb +76 -0
  101. data/test/new_relic/agent/error_collector_test.rb +155 -0
  102. data/test/new_relic/agent/method_tracer_test.rb +335 -0
  103. data/test/new_relic/agent/metric_data_test.rb +56 -0
  104. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  105. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  106. data/test/new_relic/agent/net_instrumentation_test.rb +51 -0
  107. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +79 -0
  108. data/test/new_relic/agent/stats_engine/samplers_test.rb +78 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +177 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +67 -0
  111. data/test/new_relic/agent/testable_agent.rb +13 -0
  112. data/test/new_relic/agent/transaction_sample_builder_test.rb +195 -0
  113. data/test/new_relic/agent/transaction_sample_test.rb +146 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +387 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +94 -0
  117. data/test/new_relic/deployments_api_test.rb +68 -0
  118. data/test/new_relic/environment_test.rb +75 -0
  119. data/test/new_relic/metric_parser_test.rb +172 -0
  120. data/test/new_relic/metric_spec_test.rb +177 -0
  121. data/test/new_relic/shim_agent_test.rb +9 -0
  122. data/test/new_relic/stats_test.rb +291 -0
  123. data/test/new_relic/version_number_test.rb +74 -0
  124. data/test/test_helper.rb +38 -0
  125. data/test/ui/newrelic_controller_test.rb +14 -0
  126. data/test/ui/newrelic_helper_test.rb +53 -0
  127. data/ui/controllers/newrelic_controller.rb +214 -0
  128. data/ui/helpers/google_pie_chart.rb +55 -0
  129. data/ui/helpers/newrelic_helper.rb +314 -0
  130. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  131. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  132. data/ui/views/newrelic/_sample.rhtml +15 -0
  133. data/ui/views/newrelic/_segment.rhtml +28 -0
  134. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  135. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  136. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  137. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  138. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  139. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  140. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  141. data/ui/views/newrelic/_table.rhtml +12 -0
  142. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  143. data/ui/views/newrelic/images/arrow-close.png +0 -0
  144. data/ui/views/newrelic/images/arrow-open.png +0 -0
  145. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  146. data/ui/views/newrelic/images/file_icon.png +0 -0
  147. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  148. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  149. data/ui/views/newrelic/images/textmate.png +0 -0
  150. data/ui/views/newrelic/index.rhtml +45 -0
  151. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  152. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  153. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  154. data/ui/views/newrelic/show_sample.rhtml +77 -0
  155. data/ui/views/newrelic/show_source.rhtml +3 -0
  156. data/ui/views/newrelic/stylesheets/style.css +433 -0
  157. data/ui/views/newrelic/threads.rhtml +52 -0
  158. metadata +327 -0
@@ -0,0 +1,155 @@
1
+ module NewRelic::Agent
2
+ class StatsEngine
3
+
4
+ # Defines methods that stub out the stats engine methods
5
+ # when the agent is disabled
6
+
7
+ class ScopeStackElement
8
+ attr_reader :name, :deduct_call_time_from_parent
9
+ attr_accessor :children_time
10
+ def initialize(name, deduct_call_time)
11
+ @name = name
12
+ @deduct_call_time_from_parent = deduct_call_time
13
+ @children_time = 0
14
+ end
15
+ end
16
+
17
+ module Transactions
18
+ module Shim # :nodoc:
19
+ def start_transaction; end
20
+ def end_transaction; end
21
+ def push_scope(*args); end
22
+ def pop_scope(*args); end
23
+ end
24
+
25
+ def transaction_sampler= sampler
26
+ fail "Can't add a scope listener midflight in a transaction" if scope_stack.any?
27
+ @transaction_sampler = sampler
28
+ end
29
+
30
+ def remove_transaction_sampler(l)
31
+ @transaction_sampler = nil
32
+ end
33
+
34
+ def push_scope(metric, time = Time.now.to_f, deduct_call_time_from_parent = true)
35
+
36
+ stack = scope_stack
37
+ if collecting_gc?
38
+ if stack.empty?
39
+ # reset the gc time so we only include gc time spent during this call
40
+ @last_gc_timestamp = GC.time
41
+ @last_gc_count = GC.collections
42
+ else
43
+ capture_gc_time
44
+ end
45
+ end
46
+ if @transaction_sampler
47
+ @transaction_sampler.notice_first_scope_push(time) if stack.empty?
48
+ @transaction_sampler.notice_push_scope metric, time
49
+ end
50
+ scope = ScopeStackElement.new(metric, deduct_call_time_from_parent)
51
+ stack.push scope
52
+ scope
53
+ end
54
+
55
+ def pop_scope(expected_scope, duration, time=Time.now.to_f)
56
+ capture_gc_time if collecting_gc?
57
+ stack = scope_stack
58
+ scope = stack.pop
59
+
60
+ fail "unbalanced pop from blame stack: #{scope.name} != #{expected_scope.name}" if scope != expected_scope
61
+
62
+ if !stack.empty?
63
+ if scope.deduct_call_time_from_parent
64
+ stack.last.children_time += duration
65
+ else
66
+ stack.last.children_time += scope.children_time
67
+ end
68
+ end
69
+
70
+ if @transaction_sampler
71
+ @transaction_sampler.notice_pop_scope(scope.name, time)
72
+ @transaction_sampler.notice_scope_empty(time) if stack.empty?
73
+ end
74
+ scope
75
+ end
76
+
77
+ def peek_scope
78
+ scope_stack.last
79
+ end
80
+
81
+ # set the name of the transaction for the current thread, which will be used
82
+ # to define the scope of all traced methods called on this thread until the
83
+ # scope stack is empty.
84
+ #
85
+ # currently the transaction name is the name of the controller action that
86
+ # is invoked via the dispatcher, but conceivably we could use other transaction
87
+ # names in the future if the traced application does more than service http request
88
+ # via controller actions
89
+ def transaction_name=(transaction)
90
+ Thread::current[:newrelic_transaction_name] = transaction
91
+ end
92
+
93
+ def transaction_name
94
+ Thread::current[:newrelic_transaction_name]
95
+ end
96
+
97
+ def start_transaction
98
+ Thread::current[:newrelic_scope_stack] = []
99
+ end
100
+
101
+ # Try to clean up gracefully, otherwise we leave things hanging around on thread locals
102
+ #
103
+ def end_transaction
104
+ stack = scope_stack
105
+
106
+ if stack
107
+ @transaction_sampler.notice_scope_empty(Time.now) if @transaction_sampler && !stack.empty?
108
+ Thread::current[:newrelic_scope_stack] = nil
109
+ end
110
+
111
+ Thread::current[:newrelic_transaction_name] = nil
112
+ end
113
+
114
+ private
115
+
116
+ @@collecting_gc = GC.respond_to?(:time) && GC.respond_to?(:collections)
117
+ # Make sure we don't do this in a multi-threaded environment
118
+ def collecting_gc?
119
+ @@collecting_gc
120
+ end
121
+
122
+ # smallest recordable amount of GC time. We ignore anything
123
+ # smaller than this since it's under our reporting thresholds
124
+ EPSILON = 100 # µs
125
+
126
+ # Assumes collecting_gc?
127
+ def capture_gc_time
128
+ # Skip this if we are already in this segment
129
+ return if !scope_stack.empty? && scope_stack.last.name == "GC/cumulative"
130
+ num_calls = GC.collections - @last_gc_count
131
+ elapsed = (GC.time - @last_gc_timestamp)
132
+ if num_calls > 0 && elapsed >= EPSILON
133
+ @last_gc_timestamp += elapsed
134
+ @last_gc_count += num_calls
135
+ # µs to seconds
136
+ elapsed = elapsed / 1000000.0
137
+ # Allocate the GC time to a scope as if the GC just ended
138
+ # right now.
139
+ time = Time.now.to_f
140
+ gc_scope = push_scope("GC/cumulative", time - elapsed)
141
+ # GC stats are collected into a blamed metric which allows
142
+ # us to show the stats controller by controller
143
+ gc_stats = NewRelic::Agent.get_stats(gc_scope.name, true)
144
+ gc_stats.record_multiple_data_points(elapsed, num_calls)
145
+ pop_scope(gc_scope, time)
146
+ end
147
+ end
148
+
149
+ def scope_stack
150
+ Thread::current[:newrelic_scope_stack] ||= []
151
+ end
152
+
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,319 @@
1
+
2
+ module NewRelic::Agent
3
+
4
+ class TransactionSampler
5
+
6
+ # Module defining methods stubbed out when the agent is disabled
7
+ module Shim #:nodoc:
8
+ def notice_first_scope_push(*args); end
9
+ def notice_push_scope(*args); end
10
+ def notice_pop_scope(*args); end
11
+ def notice_scope_empty(*args); end
12
+ end
13
+
14
+ BUILDER_KEY = :transaction_sample_builder
15
+
16
+ attr_accessor :stack_trace_threshold, :random_sampling, :sampling_rate, :last_sample, :samples
17
+
18
+ def initialize(agent)
19
+ @samples = []
20
+
21
+ @harvest_count = 0
22
+ @max_samples = 100
23
+ @random_sample = nil
24
+ config = NewRelic::Control.instance
25
+ sampler_config = config.fetch('transaction_tracer', {})
26
+ @stack_trace_threshold = sampler_config.fetch('stack_trace_threshold', 0.500).to_f
27
+ @stats_engine = agent.stats_engine
28
+ @stats_engine.transaction_sampler = self
29
+
30
+ agent.set_sql_obfuscator(:replace) do |sql|
31
+ default_sql_obfuscator(sql)
32
+ end
33
+ @samples_lock = Mutex.new
34
+ end
35
+
36
+ def current_sample_id
37
+ b=builder
38
+ b and b.sample_id
39
+ end
40
+
41
+ def disable
42
+ NewRelic::Agent.instance.stats_engine.remove_transaction_sampler self
43
+ end
44
+
45
+ def sampling_rate=(val)
46
+ @sampling_rate = val
47
+ @harvest_count = rand(val)
48
+ end
49
+
50
+ def default_sql_obfuscator(sql)
51
+ sql = sql.dup
52
+ # This is hardly readable. Use the unit tests.
53
+ # remove single quoted strings:
54
+ sql.gsub!(/'(.*?[^\\'])??'(?!')/, '?')
55
+ # remove double quoted strings:
56
+ sql.gsub!(/"(.*?[^\\"])??"(?!")/, '?')
57
+ # replace all number literals
58
+ sql.gsub!(/\d+/, "?")
59
+ sql
60
+ end
61
+
62
+
63
+ def notice_first_scope_push(time)
64
+ if Thread::current[:record_tt] == false || !NewRelic::Agent.is_execution_traced?
65
+ Thread::current[BUILDER_KEY] = nil
66
+ else
67
+ Thread::current[BUILDER_KEY] = TransactionSampleBuilder.new(time)
68
+ end
69
+ end
70
+
71
+ def notice_push_scope(scope, time=Time.now.to_f)
72
+
73
+ return unless builder
74
+
75
+ builder.trace_entry(scope, time)
76
+
77
+ # in developer mode, capture the stack trace with the segment.
78
+ # this is cpu and memory expensive and therefore should not be
79
+ # turned on in production mode
80
+ if NewRelic::Control.instance.developer_mode?
81
+ segment = builder.current_segment
82
+ if segment
83
+ # Strip stack frames off the top that match /new_relic/agent/
84
+ trace = caller
85
+ while trace.first =~/\/lib\/new_relic\/agent\//
86
+ trace.shift
87
+ end
88
+
89
+ trace = trace[0..39] if trace.length > 40
90
+ segment[:backtrace] = trace
91
+ end
92
+ end
93
+ end
94
+
95
+ def scope_depth
96
+ return 0 unless builder
97
+
98
+ builder.scope_depth
99
+ end
100
+
101
+ def notice_pop_scope(scope, time = Time.now.to_f)
102
+ return unless builder
103
+ builder.trace_exit(scope, time)
104
+ end
105
+
106
+ # This is called when we are done with the transaction. We've
107
+ # unwound the stack to the top level.
108
+ def notice_scope_empty(time=Time.now.to_f)
109
+
110
+ last_builder = builder
111
+ return unless last_builder
112
+
113
+ last_builder.finish_trace(time)
114
+ reset_builder
115
+
116
+
117
+ @samples_lock.synchronize do
118
+ @last_sample = last_builder.sample
119
+
120
+ # We sometimes see "unanchored" transaction traces
121
+ if @last_sample.params[:path]
122
+ @random_sample = @last_sample if @random_sampling
123
+
124
+ # ensure we don't collect more than a specified number of samples in memory
125
+ @samples << @last_sample if NewRelic::Control.instance.developer_mode?
126
+ @samples.shift while @samples.length > @max_samples
127
+
128
+ if @slowest_sample.nil? || @slowest_sample.duration < @last_sample.duration
129
+ @slowest_sample = @last_sample
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ def notice_transaction(path, request, params)
136
+ return unless builder
137
+
138
+ builder.set_transaction_info(path, request, params)
139
+ end
140
+
141
+ def notice_transaction_cpu_time(cpu_time)
142
+ return unless builder
143
+
144
+ builder.set_transaction_cpu_time(cpu_time)
145
+ end
146
+
147
+
148
+ # some statements (particularly INSERTS with large BLOBS
149
+ # may be very large; we should trim them to a maximum usable length
150
+ # config is the driver configuration for the connection
151
+ MAX_SQL_LENGTH = 16384
152
+ def notice_sql(sql, config, duration)
153
+ return unless builder
154
+ if Thread::current[:record_sql].nil? || Thread::current[:record_sql]
155
+ segment = builder.current_segment
156
+ if segment
157
+ current_sql = segment[:sql]
158
+ sql = current_sql + ";\n" + sql if current_sql
159
+
160
+ if sql.length > (MAX_SQL_LENGTH - 4)
161
+ sql = sql[0..MAX_SQL_LENGTH-4] + '...'
162
+ end
163
+
164
+ segment[:sql] = sql
165
+ segment[:connection_config] = config
166
+ segment[:backtrace] = caller.join("\n") if duration >= @stack_trace_threshold
167
+ end
168
+ end
169
+ end
170
+
171
+
172
+ # get the set of collected samples, merging into previous samples,
173
+ # and clear the collected sample list.
174
+
175
+ def harvest(previous = nil, slow_threshold = 2.0)
176
+ result = []
177
+ previous ||= []
178
+
179
+ previous = [previous] unless previous.is_a?(Array)
180
+
181
+ previous_slowest = previous.inject(nil) {|a,ts| (a) ? ((a.duration > ts.duration) ? a : ts) : ts}
182
+
183
+ @samples_lock.synchronize do
184
+
185
+ if @random_sampling
186
+ @harvest_count += 1
187
+
188
+ if (@harvest_count % @sampling_rate) == 0
189
+ result << @random_sample if @random_sample
190
+ else
191
+ @random_sample = nil # if we don't nil this out, then we won't send the slowest if slowest == @random_sample
192
+ end
193
+ end
194
+
195
+ slowest = @slowest_sample
196
+ @slowest_sample = nil
197
+
198
+ if slowest && slowest != @random_sample && slowest.duration >= slow_threshold
199
+ if previous_slowest.nil? || previous_slowest.duration < slowest.duration
200
+ result << slowest
201
+ else
202
+ result << previous_slowest
203
+ end
204
+ end
205
+
206
+ @random_sample = nil
207
+ @last_sample = nil
208
+ end
209
+ result
210
+ end
211
+
212
+ # reset samples without rebooting the web server
213
+ def reset!
214
+ @samples = []
215
+ end
216
+
217
+ private
218
+
219
+ def builder
220
+ Thread::current[BUILDER_KEY]
221
+ end
222
+ def reset_builder
223
+ Thread::current[BUILDER_KEY] = nil
224
+ end
225
+
226
+ end
227
+
228
+ # a builder is created with every sampled transaction, to dynamically
229
+ # generate the sampled data. It is a thread-local object, and is not
230
+ # accessed by any other thread so no need for synchronization.
231
+ class TransactionSampleBuilder
232
+ attr_reader :current_segment
233
+
234
+ include CollectionHelper
235
+
236
+ def initialize(time=Time.now.to_f)
237
+ @sample = NewRelic::TransactionSample.new(time)
238
+ @sample_start = time
239
+ @current_segment = @sample.root_segment
240
+ end
241
+
242
+ def sample_id
243
+ @sample.sample_id
244
+ end
245
+
246
+ def trace_entry(metric_name, time)
247
+ segment = @sample.create_segment(time - @sample_start, metric_name)
248
+ @current_segment.add_called_segment(segment)
249
+ @current_segment = segment
250
+ end
251
+
252
+ def trace_exit(metric_name, time)
253
+ if metric_name != @current_segment.metric_name
254
+ fail "unbalanced entry/exit: #{metric_name} != #{@current_segment.metric_name}"
255
+ end
256
+ @current_segment.end_trace(time - @sample_start)
257
+ @current_segment = @current_segment.parent_segment
258
+ end
259
+
260
+ def finish_trace(time)
261
+ # This should never get called twice, but in a rare case that we can't reproduce in house it does.
262
+ # log forensics and return gracefully
263
+ if @sample.frozen?
264
+ log = NewRelic::Control.instance.log
265
+
266
+ log.warn "Unexpected double-freeze of Transaction Trace Object."
267
+ log.info "Please send this diagnostic data to New Relic"
268
+ log.info @sample.to_s
269
+ return
270
+ end
271
+ @sample.root_segment.end_trace(time - @sample_start)
272
+ @sample.params[:custom_params] = normalize_params(NewRelic::Agent.instance.custom_params)
273
+ @sample.freeze
274
+ @current_segment = nil
275
+ end
276
+
277
+ def scope_depth
278
+ depth = -1 # have to account for the root
279
+ current = @current_segment
280
+
281
+ while(current)
282
+ depth += 1
283
+ current = current.parent_segment
284
+ end
285
+
286
+ depth
287
+ end
288
+
289
+ def freeze
290
+ @sample.freeze unless sample.frozen?
291
+ end
292
+
293
+
294
+ def set_transaction_info(path, request, params)
295
+ @sample.params[:path] = path
296
+
297
+ if NewRelic::Control.instance.capture_params
298
+ params = normalize_params params
299
+
300
+ @sample.params[:request_params].merge!(params)
301
+ @sample.params[:request_params].delete :controller
302
+ @sample.params[:request_params].delete :action
303
+ end
304
+
305
+ @sample.params[:uri] = request.path if request
306
+ end
307
+
308
+ def set_transaction_cpu_time(cpu_time)
309
+ @sample.params[:cpu_time] = cpu_time
310
+ end
311
+
312
+
313
+ def sample
314
+ fail "Not finished building" unless @sample.frozen?
315
+ @sample
316
+ end
317
+
318
+ end
319
+ end