factorylabs-newrelic_rpm 2.10.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/CHANGELOG +354 -0
  2. data/LICENSE +37 -0
  3. data/README-2.10.2.2 +10 -0
  4. data/README.md +138 -0
  5. data/bin/mongrel_rpm +33 -0
  6. data/bin/newrelic_cmd +4 -0
  7. data/cert/cacert.pem +34 -0
  8. data/install.rb +45 -0
  9. data/lib/new_relic/agent.rb +315 -0
  10. data/lib/new_relic/agent/agent.rb +647 -0
  11. data/lib/new_relic/agent/busy_calculator.rb +86 -0
  12. data/lib/new_relic/agent/chained_call.rb +13 -0
  13. data/lib/new_relic/agent/collection_helper.rb +66 -0
  14. data/lib/new_relic/agent/error_collector.rb +117 -0
  15. data/lib/new_relic/agent/instrumentation/active_merchant.rb +18 -0
  16. data/lib/new_relic/agent/instrumentation/active_record_instrumentation.rb +91 -0
  17. data/lib/new_relic/agent/instrumentation/authlogic.rb +8 -0
  18. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +389 -0
  19. data/lib/new_relic/agent/instrumentation/data_mapper.rb +90 -0
  20. data/lib/new_relic/agent/instrumentation/delayed_job_instrumentation.rb +12 -0
  21. data/lib/new_relic/agent/instrumentation/memcache.rb +24 -0
  22. data/lib/new_relic/agent/instrumentation/merb/controller.rb +26 -0
  23. data/lib/new_relic/agent/instrumentation/merb/errors.rb +8 -0
  24. data/lib/new_relic/agent/instrumentation/metric_frame.rb +199 -0
  25. data/lib/new_relic/agent/instrumentation/net.rb +21 -0
  26. data/lib/new_relic/agent/instrumentation/passenger_instrumentation.rb +20 -0
  27. data/lib/new_relic/agent/instrumentation/rack.rb +109 -0
  28. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +59 -0
  29. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +27 -0
  30. data/lib/new_relic/agent/instrumentation/rails/errors.rb +24 -0
  31. data/lib/new_relic/agent/instrumentation/sinatra.rb +39 -0
  32. data/lib/new_relic/agent/method_tracer.rb +348 -0
  33. data/lib/new_relic/agent/patch_const_missing.rb +125 -0
  34. data/lib/new_relic/agent/sampler.rb +46 -0
  35. data/lib/new_relic/agent/samplers/cpu_sampler.rb +50 -0
  36. data/lib/new_relic/agent/samplers/memory_sampler.rb +141 -0
  37. data/lib/new_relic/agent/samplers/mongrel_sampler.rb +23 -0
  38. data/lib/new_relic/agent/samplers/object_sampler.rb +24 -0
  39. data/lib/new_relic/agent/shim_agent.rb +21 -0
  40. data/lib/new_relic/agent/stats_engine.rb +22 -0
  41. data/lib/new_relic/agent/stats_engine/metric_stats.rb +116 -0
  42. data/lib/new_relic/agent/stats_engine/samplers.rb +74 -0
  43. data/lib/new_relic/agent/stats_engine/transactions.rb +154 -0
  44. data/lib/new_relic/agent/transaction_sampler.rb +315 -0
  45. data/lib/new_relic/agent/worker_loop.rb +118 -0
  46. data/lib/new_relic/commands/deployments.rb +145 -0
  47. data/lib/new_relic/commands/new_relic_commands.rb +30 -0
  48. data/lib/new_relic/control.rb +484 -0
  49. data/lib/new_relic/control/external.rb +13 -0
  50. data/lib/new_relic/control/merb.rb +24 -0
  51. data/lib/new_relic/control/rails.rb +151 -0
  52. data/lib/new_relic/control/ruby.rb +36 -0
  53. data/lib/new_relic/control/sinatra.rb +14 -0
  54. data/lib/new_relic/histogram.rb +89 -0
  55. data/lib/new_relic/local_environment.rb +299 -0
  56. data/lib/new_relic/merbtasks.rb +6 -0
  57. data/lib/new_relic/metric_data.rb +42 -0
  58. data/lib/new_relic/metric_parser.rb +124 -0
  59. data/lib/new_relic/metric_parser/action_mailer.rb +9 -0
  60. data/lib/new_relic/metric_parser/active_merchant.rb +26 -0
  61. data/lib/new_relic/metric_parser/active_record.rb +25 -0
  62. data/lib/new_relic/metric_parser/controller.rb +54 -0
  63. data/lib/new_relic/metric_parser/controller_cpu.rb +38 -0
  64. data/lib/new_relic/metric_parser/errors.rb +6 -0
  65. data/lib/new_relic/metric_parser/external.rb +50 -0
  66. data/lib/new_relic/metric_parser/mem_cache.rb +12 -0
  67. data/lib/new_relic/metric_parser/other_transaction.rb +15 -0
  68. data/lib/new_relic/metric_parser/view.rb +61 -0
  69. data/lib/new_relic/metric_parser/web_frontend.rb +14 -0
  70. data/lib/new_relic/metric_parser/web_service.rb +9 -0
  71. data/lib/new_relic/metric_spec.rb +67 -0
  72. data/lib/new_relic/metrics.rb +7 -0
  73. data/lib/new_relic/noticed_error.rb +23 -0
  74. data/lib/new_relic/rack/metric_app.rb +56 -0
  75. data/lib/new_relic/rack/mongrel_rpm.ru +25 -0
  76. data/lib/new_relic/rack/newrelic.yml +26 -0
  77. data/lib/new_relic/rack_app.rb +5 -0
  78. data/lib/new_relic/recipes.rb +82 -0
  79. data/lib/new_relic/stats.rb +362 -0
  80. data/lib/new_relic/transaction_analysis.rb +121 -0
  81. data/lib/new_relic/transaction_sample.rb +671 -0
  82. data/lib/new_relic/version.rb +54 -0
  83. data/lib/new_relic_api.rb +276 -0
  84. data/lib/newrelic_rpm.rb +45 -0
  85. data/lib/tasks/all.rb +4 -0
  86. data/lib/tasks/install.rake +7 -0
  87. data/lib/tasks/tests.rake +15 -0
  88. data/newrelic.yml +227 -0
  89. data/newrelic_rpm.gemspec +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 +38 -0
  94. data/test/new_relic/agent/active_record_instrumentation_test.rb +268 -0
  95. data/test/new_relic/agent/agent_controller_test.rb +254 -0
  96. data/test/new_relic/agent/agent_test_controller.rb +78 -0
  97. data/test/new_relic/agent/busy_calculator_test.rb +79 -0
  98. data/test/new_relic/agent/classloader_patch_test.rb +56 -0
  99. data/test/new_relic/agent/collection_helper_test.rb +125 -0
  100. data/test/new_relic/agent/error_collector_test.rb +173 -0
  101. data/test/new_relic/agent/method_tracer_test.rb +340 -0
  102. data/test/new_relic/agent/metric_data_test.rb +56 -0
  103. data/test/new_relic/agent/mock_ar_connection.rb +40 -0
  104. data/test/new_relic/agent/mock_scope_listener.rb +23 -0
  105. data/test/new_relic/agent/net_instrumentation_test.rb +63 -0
  106. data/test/new_relic/agent/rpm_agent_test.rb +125 -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 +88 -0
  109. data/test/new_relic/agent/stats_engine/stats_engine_test.rb +184 -0
  110. data/test/new_relic/agent/task_instrumentation_test.rb +177 -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 +186 -0
  114. data/test/new_relic/agent/transaction_sampler_test.rb +404 -0
  115. data/test/new_relic/agent/worker_loop_test.rb +103 -0
  116. data/test/new_relic/control_test.rb +110 -0
  117. data/test/new_relic/delayed_job_test.rb +108 -0
  118. data/test/new_relic/deployments_api_test.rb +68 -0
  119. data/test/new_relic/environment_test.rb +75 -0
  120. data/test/new_relic/metric_parser_test.rb +172 -0
  121. data/test/new_relic/metric_spec_test.rb +177 -0
  122. data/test/new_relic/shim_agent_test.rb +9 -0
  123. data/test/new_relic/stats_test.rb +291 -0
  124. data/test/new_relic/version_number_test.rb +76 -0
  125. data/test/test_helper.rb +53 -0
  126. data/test/ui/newrelic_controller_test.rb +14 -0
  127. data/test/ui/newrelic_helper_test.rb +53 -0
  128. data/ui/controllers/newrelic_controller.rb +220 -0
  129. data/ui/helpers/google_pie_chart.rb +49 -0
  130. data/ui/helpers/newrelic_helper.rb +320 -0
  131. data/ui/views/layouts/newrelic_default.rhtml +47 -0
  132. data/ui/views/newrelic/_explain_plans.rhtml +27 -0
  133. data/ui/views/newrelic/_sample.rhtml +19 -0
  134. data/ui/views/newrelic/_segment.rhtml +28 -0
  135. data/ui/views/newrelic/_segment_limit_message.rhtml +1 -0
  136. data/ui/views/newrelic/_segment_row.rhtml +14 -0
  137. data/ui/views/newrelic/_show_sample_detail.rhtml +24 -0
  138. data/ui/views/newrelic/_show_sample_sql.rhtml +20 -0
  139. data/ui/views/newrelic/_show_sample_summary.rhtml +3 -0
  140. data/ui/views/newrelic/_sql_row.rhtml +11 -0
  141. data/ui/views/newrelic/_stack_trace.rhtml +30 -0
  142. data/ui/views/newrelic/_table.rhtml +12 -0
  143. data/ui/views/newrelic/explain_sql.rhtml +42 -0
  144. data/ui/views/newrelic/images/arrow-close.png +0 -0
  145. data/ui/views/newrelic/images/arrow-open.png +0 -0
  146. data/ui/views/newrelic/images/blue_bar.gif +0 -0
  147. data/ui/views/newrelic/images/file_icon.png +0 -0
  148. data/ui/views/newrelic/images/gray_bar.gif +0 -0
  149. data/ui/views/newrelic/images/new-relic-rpm-desktop.gif +0 -0
  150. data/ui/views/newrelic/images/new_relic_rpm_desktop.gif +0 -0
  151. data/ui/views/newrelic/images/textmate.png +0 -0
  152. data/ui/views/newrelic/index.rhtml +57 -0
  153. data/ui/views/newrelic/javascript/prototype-scriptaculous.js +7288 -0
  154. data/ui/views/newrelic/javascript/transaction_sample.js +107 -0
  155. data/ui/views/newrelic/sample_not_found.rhtml +2 -0
  156. data/ui/views/newrelic/show_sample.rhtml +80 -0
  157. data/ui/views/newrelic/show_source.rhtml +3 -0
  158. data/ui/views/newrelic/stylesheets/style.css +484 -0
  159. data/ui/views/newrelic/threads.rhtml +52 -0
  160. metadata +238 -0
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..', 'test_helper'))
2
+ class NewRelic::VersionNumberTest < Test::Unit::TestCase
3
+
4
+ def test_comparison__first
5
+ versions = %w[1.0.0 0.1.0 0.0.1 10.0.1 1.10.0].map {|s| NewRelic::VersionNumber.new s }
6
+ assert_equal %w[0.0.1 0.1.0 1.0.0 1.10.0 10.0.1], versions.sort.map(&:to_s)
7
+ v0 = NewRelic::VersionNumber.new '1.2.3'
8
+ v1 = NewRelic::VersionNumber.new '1.2.2'
9
+ v3 = NewRelic::VersionNumber.new '1.2.2'
10
+ assert v0 > v1
11
+ assert v1 == v1
12
+ assert v1 == v3
13
+ end
14
+ def test_comparison__second
15
+ v0 = NewRelic::VersionNumber.new '1.2.0'
16
+ v1 = NewRelic::VersionNumber.new '2.2.2'
17
+ v3 = NewRelic::VersionNumber.new '1.1.2'
18
+ assert v0 < v1
19
+ assert v1 > v3
20
+ assert v3 < v0
21
+ end
22
+ def test_bug
23
+ v0 = NewRelic::VersionNumber.new '2.8.999'
24
+ v1 = NewRelic::VersionNumber.new '2.9.10'
25
+ assert v1 > v0
26
+ assert v0 <= v1
27
+ end
28
+ def test_long_version
29
+ v0 = NewRelic::VersionNumber.new '1.2.3.4'
30
+ v1 = NewRelic::VersionNumber.new '1.2.3.3'
31
+ v3 = NewRelic::VersionNumber.new '1.3'
32
+ assert v0 > v1
33
+ assert v0 < '1.2.3.5'
34
+ assert ! (v0 < '1.2.3.4')
35
+ assert v3 > v0
36
+ end
37
+ def test_sort
38
+ values = %w[1.1.1
39
+ 1.1.99
40
+ 1.1.999
41
+ 2.0.6
42
+ 2.6.5
43
+ 2.7
44
+ 2.7.1
45
+ 2.7.2
46
+ 2.7.2.0
47
+ 3
48
+ 999]
49
+ assert_equal values, values.map{|v| NewRelic::VersionNumber.new v}.sort.map(&:to_s)
50
+ end
51
+ def test_compare_string
52
+ v0 = NewRelic::VersionNumber.new '1.2.0'
53
+ v1 = NewRelic::VersionNumber.new '2.2.2'
54
+ v3 = NewRelic::VersionNumber.new '1.1.2'
55
+ assert v0 < '2.2.2'
56
+ assert v1 > '1.1.2'
57
+ assert v3 < '1.2.0'
58
+ assert v0 == '1.2.0'
59
+ end
60
+ def test_4_numbers
61
+ v0 = NewRelic::VersionNumber.new '1.2.0'
62
+ v1 = NewRelic::VersionNumber.new '1.2.0.1'
63
+ v2 = NewRelic::VersionNumber.new '1.2.1.0'
64
+ v3 = NewRelic::VersionNumber.new '1.2.1.1'
65
+ assert v0 < v1
66
+ assert v1 < v2
67
+ assert v2 < v3
68
+ assert v0 < v3
69
+ assert v0 < '1.2.0.1'
70
+ assert v0 > '1.1.0.1'
71
+ end
72
+ def test_string
73
+ assert_equal '1.2.0', NewRelic::VersionNumber.new('1.2.0').to_s
74
+ assert_equal '1.2', NewRelic::VersionNumber.new('1.2').to_s
75
+ end
76
+ end
@@ -0,0 +1,53 @@
1
+ module NewRelic; TEST = true; end unless defined? NewRelic::TEST
2
+ #ENV['NEWRELIC_ENABLE'] = 'true'
3
+ ENV['RAILS_ENV'] = 'test'
4
+ NEWRELIC_PLUGIN_DIR = File.expand_path(File.join(File.dirname(__FILE__),".."))
5
+ $LOAD_PATH << File.join(NEWRELIC_PLUGIN_DIR,"test")
6
+ $LOAD_PATH << File.join(NEWRELIC_PLUGIN_DIR,"ui/helpers")
7
+ $LOAD_PATH.uniq!
8
+
9
+ require File.expand_path(File.join(NEWRELIC_PLUGIN_DIR, "..","..","..","config","environment"))
10
+
11
+ require 'test_help'
12
+ require 'mocha'
13
+ require 'test/unit'
14
+
15
+ def assert_between(floor, ceiling, value, message = nil)
16
+ assert floor <= value && value <= ceiling,
17
+ message || "expected #{floor} <= #{value} <= #{ceiling}"
18
+ end
19
+
20
+ def compare_metrics expected_list, actual_list
21
+ actual = Set.new actual_list
22
+ actual.delete('GC/cumulative') # in case we are in REE
23
+ expected = Set.new expected_list
24
+ assert_equal expected.to_a.sort, actual.to_a.sort, "extra: #{(actual - expected).to_a.join(", ")}; missing: #{(expected - actual).to_a.join(", ")}"
25
+ end
26
+ =begin Enable this to see test names as they run
27
+ Test::Unit::TestCase.class_eval do
28
+ def run_with_info *args, &block
29
+ puts "#{self.class.name.underscore}/#{@method_name}"
30
+ run_without_info *args, &block
31
+ end
32
+ alias_method_chain :run, :info
33
+ end
34
+ =end
35
+ module TransactionSampleTestHelper
36
+ def make_sql_transaction(*sql)
37
+ sampler = NewRelic::Agent::TransactionSampler.new
38
+ sampler.notice_first_scope_push Time.now.to_f
39
+ sampler.notice_transaction '/path', nil, :jim => "cool"
40
+ sampler.notice_push_scope "a"
41
+
42
+ sampler.notice_transaction '/path/2', nil, :jim => "cool"
43
+
44
+ sql.each {|sql_statement| sampler.notice_sql(sql_statement, {:adapter => "test"}, 0 ) }
45
+
46
+ sleep 1.0
47
+ yield if block_given?
48
+ sampler.notice_pop_scope "a"
49
+ sampler.notice_scope_empty
50
+
51
+ sampler.samples[0]
52
+ end
53
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
2
+ require 'newrelic_helper'
3
+ require 'active_record_fixtures'
4
+
5
+ class NewRelic::Agent::NewrelicControllerTest < Test::Unit::TestCase
6
+
7
+ # Clearly we need a functional test for the controller. For now
8
+ # I want to at least make sure the class loads in all versions of rails.
9
+ def test_controller_loading
10
+ NewrelicController
11
+ rescue
12
+ fail "Controller would not load:#{$!}"
13
+ end
14
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))
2
+ require 'newrelic_helper'
3
+ require 'active_record_fixtures'
4
+
5
+ class NewRelic::Agent::NewrelicHelperTest < Test::Unit::TestCase
6
+ include NewrelicHelper
7
+
8
+ def params
9
+ {}
10
+ end
11
+
12
+ def setup
13
+ super
14
+ ActiveRecordFixtures.setup
15
+ # setup instrumentation
16
+ NewRelic::Agent.manual_start
17
+ # let's get a real stack trace
18
+ begin
19
+ ActiveRecordFixtures::Order.find 0
20
+ rescue => e
21
+ @exception = e
22
+ return
23
+ end
24
+ flunk "should throw"
25
+ end
26
+ def teardown
27
+ ActiveRecordFixtures.teardown
28
+ NewRelic::Agent.instance.shutdown
29
+ super
30
+ end
31
+ def test_application_caller
32
+ assert_match /setup/, application_caller(@exception.backtrace)
33
+ end
34
+
35
+ def test_application_stack_trace__rails
36
+ assert_clean(application_stack_trace(@exception.backtrace, true), true)
37
+ end
38
+ def test_application_stack_trace__no_rails
39
+ assert_clean(application_stack_trace(@exception.backtrace, false), false)
40
+ end
41
+ def test_with_delimiter
42
+ assert_equal "123,456.123456", with_delimiter(123456.123456)
43
+ end
44
+
45
+ private
46
+ def assert_clean(backtrace, rails=false)
47
+ if !rails
48
+ assert_equal 0, backtrace.grep('/rails/').size, backtrace.join("\n")
49
+ end
50
+ assert_equal 0, backtrace.grep(/trace/).size, backtrace.join("\n")
51
+ assert_equal 0, backtrace.grep(/newrelic_rpm\/agent/).size, backtrace.join("\n")
52
+ end
53
+ end
@@ -0,0 +1,220 @@
1
+ class NewrelicController < ActionController::Base
2
+ include NewrelicHelper
3
+ helper NewrelicHelper
4
+
5
+ # See http://wiki.rubyonrails.org/rails/pages/Safe+ERB:
6
+ # We don't need to worry about checking taintedness
7
+ def initialize(*args)
8
+ @skip_checking_tainted = true
9
+ super *args
10
+ end
11
+
12
+ # do not include any filters inside the application since there might be a conflict
13
+ if respond_to? :filter_chain
14
+ filters = filter_chain.collect do |f|
15
+ if f.respond_to? :filter
16
+ # rails 2.0
17
+ f.filter
18
+ elsif f.respond_to? :method
19
+ # rails 2.1
20
+ f.method
21
+ else
22
+ fail "Unknown filter class. Please send this exception to support@newrelic.com"
23
+ end
24
+ end
25
+ skip_filter filters
26
+ end
27
+
28
+ # for this controller, the views are located in a different directory from
29
+ # the application's views.
30
+ view_path = File.join(File.dirname(__FILE__), '..', 'views')
31
+ if respond_to? :append_view_path # rails 2.1+
32
+ self.append_view_path view_path
33
+ elsif respond_to? :view_paths # rails 2.0+
34
+ self.view_paths << view_path
35
+ else # rails <2.0
36
+ self.template_root = view_path
37
+ end
38
+
39
+ layout "newrelic_default"
40
+
41
+ write_inheritable_attribute('do_not_trace', true)
42
+
43
+ def profile
44
+ NewRelic::Control.instance.profiling = params['start'] == 'true'
45
+ get_samples
46
+ redirect_to :action => 'index'
47
+ end
48
+
49
+ def file
50
+ file_name=params[:file].to_s
51
+ file_name=~/^.*[.]([^.]*)$/
52
+ ext=$1
53
+ case ext
54
+ when 'css' then
55
+ forward_to_file '/newrelic/stylesheets/', 'text/css'
56
+ when 'gif','jpg','png' then
57
+ forward_to_file '/newrelic/images/', "image/#{ext}"
58
+ when 'js' then
59
+ forward_to_file '/newrelic/javascript/', 'text/javascript'
60
+ else
61
+ raise "Unknown type '#{ext}' (#{file_name})"
62
+ end
63
+ end
64
+
65
+ def index
66
+ get_samples
67
+ end
68
+
69
+ def threads
70
+
71
+ end
72
+
73
+ def reset
74
+ NewRelic::Agent.instance.transaction_sampler.reset!
75
+ redirect_to :action => 'index'
76
+ end
77
+
78
+ def show_sample_detail
79
+ show_sample_data
80
+ end
81
+
82
+ def show_sample_summary
83
+ show_sample_data
84
+ end
85
+
86
+ def show_sample_sql
87
+ show_sample_data
88
+ end
89
+
90
+
91
+ def explain_sql
92
+ get_segment
93
+
94
+ render :action => "sample_not_found" and return unless @sample
95
+
96
+ @sql = @segment[:sql]
97
+ @trace = @segment[:backtrace]
98
+
99
+ if NewRelic::Agent.agent.record_sql == :obfuscated
100
+ @obfuscated_sql = @segment.obfuscated_sql
101
+ end
102
+
103
+ explanations = @segment.explain_sql
104
+ if explanations
105
+ @explanation = explanations.first
106
+ if !@explanation.blank?
107
+ first_row = @explanation.first
108
+ # Show the standard headers if it looks like a mysql explain plan
109
+ # Otherwise show blank headers
110
+ if first_row.length < NewRelic::MYSQL_EXPLAIN_COLUMNS.length
111
+ @row_headers = nil
112
+ else
113
+ @row_headers = NewRelic::MYSQL_EXPLAIN_COLUMNS
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ # show the selected source file with the highlighted selected line
120
+ def show_source
121
+ @filename = params[:file]
122
+ line_number = params[:line].to_i
123
+
124
+ if !File.readable?(@filename)
125
+ @source="<p>Unable to read #{@filename}.</p>"
126
+ return
127
+ end
128
+ begin
129
+ file = File.new(@filename, 'r')
130
+ rescue => e
131
+ @source="<p>Unable to access the source file #{@filename} (#{e.message}).</p>"
132
+ return
133
+ end
134
+ @source = ""
135
+
136
+ @source << "<pre>"
137
+ file.each_line do |line|
138
+ # place an anchor 6 lines above the selected line (if the line # < 6)
139
+ if file.lineno == line_number - 6
140
+ @source << "</pre><pre id = 'selected_line'>"
141
+ @source << line.rstrip
142
+ @source << "</pre><pre>"
143
+
144
+ # highlight the selected line
145
+ elsif file.lineno == line_number
146
+ @source << "</pre><pre class = 'selected_source_line'>"
147
+ @source << line.rstrip
148
+ @source << "</pre><pre>"
149
+ else
150
+ @source << line
151
+ end
152
+ end
153
+ end
154
+
155
+ private
156
+
157
+ # root path is relative to plugin newrelic_rpm/ui/views directory.
158
+ def forward_to_file(root_path, content_type='ignored anyway')
159
+ file = File.expand_path(File.join(__FILE__,"../../views", root_path, params[:file]))
160
+ last_modified = File.mtime(file)
161
+ date_check = request.respond_to?(:headers) ? request.headers['if-modified-since'] : request.env['HTTP_IF_MODIFIED_SINCE']
162
+ if date_check && Time.parse(date_check) >= last_modified
163
+ expires_in 24.hours
164
+ head :not_modified,
165
+ :last_modified => last_modified,
166
+ :type => 'text/plain'
167
+ else
168
+ response.headers['Last-Modified'] = last_modified.to_formatted_s(:rfc822)
169
+ expires_in 24.hours
170
+ send_file file, :content_type => mime_type_from_extension(file), :disposition => 'inline' #, :filename => File.basename(file)
171
+ end
172
+ end
173
+
174
+ def show_sample_data
175
+ get_sample
176
+
177
+ render :action => "sample_not_found" and return unless @sample
178
+
179
+ @request_params = @sample.params[:request_params] || {}
180
+ @custom_params = @sample.params[:custom_params] || {}
181
+
182
+ controller_metric = @sample.root_segment.called_segments.first.metric_name
183
+
184
+ controller_segments = controller_metric.split('/')
185
+ @sample_controller_name = controller_segments[1..-2].join('/').camelize+"Controller"
186
+ @sample_action_name = controller_segments[-1].underscore
187
+
188
+ render :action => :show_sample
189
+ end
190
+
191
+ def get_samples
192
+ @samples = NewRelic::Agent.instance.transaction_sampler.samples.select do |sample|
193
+ sample.params[:path] != nil
194
+ end
195
+
196
+ return @samples = @samples.sort{|x,y| y.omit_segments_with('(Rails/Application Code Loading)|(Database/.*/.+ Columns)').duration <=>
197
+ x.omit_segments_with('(Rails/Application Code Loading)|(Database/.*/.+ Columns)').duration} if params[:h]
198
+ return @samples = @samples.sort{|x,y| x.params[:uri] <=> y.params[:uri]} if params[:u]
199
+ @samples = @samples.reverse
200
+ end
201
+
202
+ def get_sample
203
+ get_samples
204
+ sample_id = params[:id].to_i
205
+ @samples.each do |s|
206
+ if s.sample_id == sample_id
207
+ @sample = stripped_sample(s)
208
+ return
209
+ end
210
+ end
211
+ end
212
+
213
+ def get_segment
214
+ get_sample
215
+ return unless @sample
216
+
217
+ segment_id = params[:segment].to_i
218
+ @segment = @sample.find_segment(segment_id)
219
+ end
220
+ end
@@ -0,0 +1,49 @@
1
+
2
+ # A wrapper around the google charts service.
3
+ # TODO consider making generic and open sourcing
4
+ class GooglePieChart
5
+ attr_accessor :width, :height, :color
6
+
7
+ def initialize
8
+ # an array of [label, value]
9
+ @data = []
10
+ self.width = 300
11
+ self.height = 200
12
+ end
13
+
14
+ def add_data_point(label, value)
15
+ @data << [label, value]
16
+ @max = (@max.nil? || @max < value ? value : @max)
17
+ end
18
+
19
+ # render the chart to html by creating an image object and
20
+ # placing the correct URL to the google charts api
21
+ def render
22
+ labels = []
23
+ values = []
24
+ @data.each do |label, value|
25
+ labels << CGI::escape(label)
26
+ values << (value > 0 ? value * 100 / @max : value).round.to_s
27
+ end
28
+ params = {:cht => 'p', :chs => "#{width}x#{height}", :chd => "t:#{values.join(',')}", :chl => labels.join('|') }
29
+ params['chco'] = color if color
30
+
31
+ url = "http://chart.apis.google.com/chart?#{to_query(params)}"
32
+
33
+ alt_msg = "This pie chart is generated by Google Charts. You must be connected to the Internet to view this chart."
34
+ html = "<img id=\"pie_chart_image\" src=\"#{url}\" alt=\"#{alt_msg}\"/>"
35
+ return html
36
+ end
37
+
38
+ private
39
+ # Hash#to_query is not present in all supported rails platforms, so implement
40
+ # its equivalent here.
41
+ def to_query(params)
42
+ p = []
43
+ params.each do |k,v|
44
+ p << "#{k}=#{v}"
45
+ end
46
+
47
+ p.join "&"
48
+ end
49
+ end