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,74 @@
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 v3 > v0
34
+ end
35
+ def test_sort
36
+ values = %w[1.1.1
37
+ 1.1.99
38
+ 1.1.999
39
+ 2.0.6
40
+ 2.6.5
41
+ 2.7
42
+ 2.7.1
43
+ 2.7.2
44
+ 2.7.2.0
45
+ 3
46
+ 999]
47
+ assert_equal values, values.map{|v| NewRelic::VersionNumber.new v}.sort.map(&:to_s)
48
+ end
49
+ def test_compare_string
50
+ v0 = NewRelic::VersionNumber.new '1.2.0'
51
+ v1 = NewRelic::VersionNumber.new '2.2.2'
52
+ v3 = NewRelic::VersionNumber.new '1.1.2'
53
+ assert v0 < '2.2.2'
54
+ assert v1 > '1.1.2'
55
+ assert v3 < '1.2.0'
56
+ assert v0 == '1.2.0'
57
+ end
58
+ def test_4_numbers
59
+ v0 = NewRelic::VersionNumber.new '1.2.0'
60
+ v1 = NewRelic::VersionNumber.new '1.2.0.1'
61
+ v2 = NewRelic::VersionNumber.new '1.2.1.0'
62
+ v3 = NewRelic::VersionNumber.new '1.2.1.1'
63
+ assert v0 < v1
64
+ assert v1 < v2
65
+ assert v2 < v3
66
+ assert v0 < v3
67
+ assert v0 < '1.2.0.1'
68
+ assert v0 > '1.1.0.1'
69
+ end
70
+ def test_string
71
+ assert_equal '1.2.0', NewRelic::VersionNumber.new('1.2.0').to_s
72
+ assert_equal '1.2', NewRelic::VersionNumber.new('1.2').to_s
73
+ end
74
+ end
@@ -0,0 +1,38 @@
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
+ module TransactionSampleTestHelper
21
+ def make_sql_transaction(*sql)
22
+ sampler = NewRelic::Agent::TransactionSampler.new(NewRelic::Agent.instance)
23
+ sampler.notice_first_scope_push Time.now.to_f
24
+ sampler.notice_transaction '/path', nil, :jim => "cool"
25
+ sampler.notice_push_scope "a"
26
+
27
+ sampler.notice_transaction '/path/2', nil, :jim => "cool"
28
+
29
+ sql.each {|sql_statement| sampler.notice_sql(sql_statement, {:adapter => "test"}, 0 ) }
30
+
31
+ sleep 1.0
32
+
33
+ sampler.notice_pop_scope "a"
34
+ sampler.notice_scope_empty
35
+
36
+ sampler.samples[0]
37
+ end
38
+ 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,214 @@
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 file
44
+ file_name=params[:file].to_s
45
+ file_name=~/^.*[.]([^.]*)$/
46
+ ext=$1
47
+ case ext
48
+ when 'css':
49
+ forward_to_file '/newrelic/stylesheets/', 'text/css'
50
+ when 'gif','jpg','png':
51
+ forward_to_file '/newrelic/images/', "image/#{ext}"
52
+ when 'js':
53
+ forward_to_file '/newrelic/javascript/', 'text/javascript'
54
+ else
55
+ raise "Unknown type '#{ext}' (#{file_name})"
56
+ end
57
+ end
58
+
59
+ def index
60
+ get_samples
61
+ end
62
+
63
+ def threads
64
+
65
+ end
66
+
67
+ def reset
68
+ NewRelic::Agent.instance.transaction_sampler.reset!
69
+ redirect_to :action => 'index'
70
+ end
71
+
72
+ def show_sample_detail
73
+ show_sample_data
74
+ end
75
+
76
+ def show_sample_summary
77
+ show_sample_data
78
+ end
79
+
80
+ def show_sample_sql
81
+ show_sample_data
82
+ end
83
+
84
+
85
+ def explain_sql
86
+ get_segment
87
+
88
+ render :action => "sample_not_found" and return unless @sample
89
+
90
+ @sql = @segment[:sql]
91
+ @trace = @segment[:backtrace]
92
+
93
+ if NewRelic::Agent.agent.record_sql == :obfuscated
94
+ @obfuscated_sql = @segment.obfuscated_sql
95
+ end
96
+
97
+ explanations = @segment.explain_sql
98
+ if explanations
99
+ @explanation = explanations.first
100
+ if !@explanation.blank?
101
+ first_row = @explanation.first
102
+ # Show the standard headers if it looks like a mysql explain plan
103
+ # Otherwise show blank headers
104
+ if first_row.length < NewRelic::MYSQL_EXPLAIN_COLUMNS.length
105
+ @row_headers = nil
106
+ else
107
+ @row_headers = NewRelic::MYSQL_EXPLAIN_COLUMNS
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ # show the selected source file with the highlighted selected line
114
+ def show_source
115
+ @filename = params[:file]
116
+ line_number = params[:line].to_i
117
+
118
+ if !File.readable?(@filename)
119
+ @source="<p>Unable to read #{@filename}.</p>"
120
+ return
121
+ end
122
+ begin
123
+ file = File.new(@filename, 'r')
124
+ rescue => e
125
+ @source="<p>Unable to access the source file #{@filename} (#{e.message}).</p>"
126
+ return
127
+ end
128
+ @source = ""
129
+
130
+ @source << "<pre>"
131
+ file.each_line do |line|
132
+ # place an anchor 6 lines above the selected line (if the line # < 6)
133
+ if file.lineno == line_number - 6
134
+ @source << "</pre><pre id = 'selected_line'>"
135
+ @source << line.rstrip
136
+ @source << "</pre><pre>"
137
+
138
+ # highlight the selected line
139
+ elsif file.lineno == line_number
140
+ @source << "</pre><pre class = 'selected_source_line'>"
141
+ @source << line.rstrip
142
+ @source << "</pre><pre>"
143
+ else
144
+ @source << line
145
+ end
146
+ end
147
+ end
148
+
149
+ private
150
+
151
+ # root path is relative to plugin newrelic_rpm/ui/views directory.
152
+ def forward_to_file(root_path, content_type='ignored anyway')
153
+ file = File.expand_path(File.join(__FILE__,"../../views", root_path, params[:file]))
154
+ last_modified = File.mtime(file)
155
+ date_check = request.respond_to?(:headers) ? request.headers['if-modified-since'] : request.env['HTTP_IF_MODIFIED_SINCE']
156
+ if date_check && Time.parse(date_check) >= last_modified
157
+ expires_in 24.hours
158
+ head :not_modified,
159
+ :last_modified => last_modified,
160
+ :type => 'text/plain'
161
+ else
162
+ response.headers['Last-Modified'] = last_modified.to_formatted_s(:rfc822)
163
+ expires_in 24.hours
164
+ send_file file, :content_type => mime_type_from_extension(file), :disposition => 'inline' #, :filename => File.basename(file)
165
+ end
166
+ end
167
+
168
+ def show_sample_data
169
+ get_sample
170
+
171
+ render :action => "sample_not_found" and return unless @sample
172
+
173
+ @request_params = @sample.params[:request_params] || {}
174
+ @custom_params = @sample.params[:custom_params] || {}
175
+
176
+ controller_metric = @sample.root_segment.called_segments.first.metric_name
177
+
178
+ controller_segments = controller_metric.split('/')
179
+ @sample_controller_name = controller_segments[1..-2].join('/').camelize+"Controller"
180
+ @sample_action_name = controller_segments[-1].underscore
181
+
182
+ render :action => :show_sample
183
+ end
184
+
185
+ def get_samples
186
+ @samples = NewRelic::Agent.instance.transaction_sampler.samples.select do |sample|
187
+ sample.params[:path] != nil
188
+ end
189
+
190
+ return @samples = @samples.sort{|x,y| y.omit_segments_with('(Rails/Application Code Loading)|(Database/.*/.+ Columns)').duration <=>
191
+ x.omit_segments_with('(Rails/Application Code Loading)|(Database/.*/.+ Columns)').duration} if params[:h]
192
+ return @samples = @samples.sort{|x,y| x.params[:uri] <=> y.params[:uri]} if params[:u]
193
+ @samples = @samples.reverse
194
+ end
195
+
196
+ def get_sample
197
+ get_samples
198
+ sample_id = params[:id].to_i
199
+ @samples.each do |s|
200
+ if s.sample_id == sample_id
201
+ @sample = stripped_sample(s)
202
+ return
203
+ end
204
+ end
205
+ end
206
+
207
+ def get_segment
208
+ get_sample
209
+ return unless @sample
210
+
211
+ segment_id = params[:segment].to_i
212
+ @segment = @sample.find_segment(segment_id)
213
+ end
214
+ end
@@ -0,0 +1,55 @@
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
+
11
+ self.width = 300
12
+ self.height = 200
13
+ end
14
+
15
+ def add_data_point(label, value)
16
+ @data << [label, value]
17
+ @max = (@max.nil? || @max < value ? value : @max)
18
+ end
19
+
20
+ # render the chart to html by creating an image object and
21
+ # placing the correct URL to the google charts api
22
+ def render
23
+ labels = ''
24
+ values = ''
25
+ @data.each do |label, value|
26
+ labels << CGI::escape(label) + '|'
27
+ values << (value * 100 / @max).round.to_s + ","
28
+ end
29
+
30
+ # strip of the last separator char for labels and values
31
+ labels = labels[0..-2]
32
+ values = values[0..-2]
33
+
34
+ params = {:cht => 'p', :chs => "#{width}x#{height}", :chd => "t:#{values}", :chl => labels }
35
+ params['chco'] = color if color
36
+
37
+ url = "http://chart.apis.google.com/chart?#{to_query(params)}"
38
+
39
+ alt_msg = "This pie chart is generated by Google Charts. You must be connected to the Internet to view this chart."
40
+ html = "<img id=\"pie_chart_image\" src=\"#{url}\" alt=\"#{alt_msg}\"/>"
41
+ return html
42
+ end
43
+
44
+ private
45
+ # Hash#to_query is not present in all supported rails platforms, so implement
46
+ # its equivalent here.
47
+ def to_query(params)
48
+ p = []
49
+ params.each do |k,v|
50
+ p << "#{k}=#{v}"
51
+ end
52
+
53
+ p.join "&"
54
+ end
55
+ end