garelic 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,14 +1,16 @@
1
- # Garelic: Google Analytics Reports as "New Relic"-like performance monitoring for your Rails app
1
+ # Garelic: Use Google Analytics as "New Relic" performance monitoring for your Rails app
2
2
 
3
3
  This gem uses Google Analytics User Timing API to report application performance statistics directly to Google Analytics, where you can slice & dice your data as you wish.
4
4
 
5
5
  Here are some features with pictures:
6
6
 
7
- - [Showing average response times for each action](http://twitpic.com/b0gwkx/full)
8
- - [Identify slow page loads & drill down to different actions](http://twitpic.com/b0gump/full)
9
- - [Histogram of response times for an actions](http://twitpic.com/b0gv6e/full)
10
7
  - [Performance reports as a nice dashboard](http://twitpic.com/b0gt4j/full)
11
-
8
+ - [Histogram of response times for action](http://twitpic.com/b0gv6e/full)
9
+ - [Identify slow page loads & drill down to different actions](http://twitpic.com/b0gump/full)
10
+ - [Showing average response times for each action](http://twitpic.com/b0gwkx/full)
11
+ - [How much is spent in database & view generation for an action?](http://twitpic.com/b0h062/full)
12
+ - [How has the switch to Ruby 1.9.3 affected response time?](http://twitpic.com/b11mxm/full)
13
+ - [In which action do we spent most time globaly?](http://twitpic.com/b15l7j/full)
12
14
 
13
15
  *This is a proof of concept and will probably break things. Use it at your own risk.*
14
16
 
@@ -31,18 +33,20 @@ It's easy as 1-2-3.
31
33
 
32
34
  <%= Garelic::Timing %>
33
35
 
34
- (function() {
36
+ (function() {
35
37
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
36
38
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
37
39
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
38
- })();
40
+ })();
39
41
  </script>
42
+
43
+ (Please note the `_gaq.push(['_setSiteSpeedSampleRate', 100]);` code for better testing.)
40
44
 
41
- *Step 3.* Go to Google Analytics > Content > Site speed > User Timings
45
+ *Step 3.* Go to Google Analytics > Content > Site Speed > User Timings
42
46
 
43
47
  Enjoy!
44
48
 
45
- ## Know advantages
49
+ ## Known advantages
46
50
 
47
51
  - it's free
48
52
  - shows slow performing pages (not only actions)
data/garelic.gemspec CHANGED
@@ -4,7 +4,7 @@ require File.expand_path('../lib/garelic/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Jan Suchal"]
6
6
  gem.email = ["johno@jsmf.net"]
7
- gem.description = %q{Google Analytics Reports as "New Relic"-like performance monitoring for your Rails app}
7
+ gem.description = %q{Use Google Analytics for Rails App Performance Monitoring}
8
8
  gem.summary = %q{}
9
9
  gem.homepage = "https://github.com/jsuchal/garelic"
10
10
 
@@ -3,9 +3,11 @@ module ActionController
3
3
  alias :dispatch_without_garelic :dispatch
4
4
 
5
5
  def dispatch(*args)
6
+ Garelic::Metrics.reset!
7
+
6
8
  response = dispatch_without_garelic(*args)
7
9
 
8
- timing_data = Garelic.build_user_timing_from_payload(Thread.current[:garelic_payload])
10
+ timing_data = Garelic.report_user_timing_from_metrics(Garelic::Metrics)
9
11
 
10
12
  _, _, chunks = response
11
13
  chunks.each do |chunk|
@@ -1,3 +1,3 @@
1
1
  class Garelic
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/garelic.rb CHANGED
@@ -4,13 +4,27 @@ require "garelic/dispatcher"
4
4
  class Garelic
5
5
  Timing = '<!-- /* GARELIC DATA */ -->'.html_safe
6
6
 
7
- def self.build_user_timing_from_payload(payload)
8
- action_identifier = "#{payload[:controller]}##{payload[:action]}"
7
+ def self.report_user_timing_from_metrics(metrics)
8
+ reports = report_user_timing_for_action(metrics[:action], metrics.action_identifier)
9
+ reports += report_user_timing_for_active_record(metrics[:active_record], metrics.action_identifier)
10
+
11
+ reports.join("\n")
12
+ end
13
+
14
+ def self.report_user_timing_for_action(metrics, action_identifier)
9
15
  [
10
- track_timing('Garelic', 'Response (Total)', payload[:total_runtime], action_identifier),
11
- track_timing('Garelic', 'Response (Views)', payload[:view_runtime], action_identifier),
12
- track_timing('Garelic', 'Response (ActiveRecord)', payload[:db_runtime], action_identifier),
13
- ].join("\n")
16
+ track_timing('Garelic (Controller)', 'Response time (Total)', metrics[:total_runtime], action_identifier),
17
+ track_timing('Garelic (Controller)', 'Response time (Views)', metrics[:view_runtime], action_identifier),
18
+ track_timing('Garelic (Controller)', 'Response time (ActiveRecord)', metrics[:db_runtime], action_identifier),
19
+ ]
20
+ end
21
+
22
+ def self.report_user_timing_for_active_record(metrics, action_identifier)
23
+ timings = []
24
+ metrics.each do |call_type, time|
25
+ timings << track_timing('Garelic (ActiveRecord)', call_type, time, action_identifier)
26
+ end
27
+ timings
14
28
  end
15
29
 
16
30
  def self.track_timing(category, variable, time, opt_label = nil, opt_sample = nil)
@@ -22,10 +36,48 @@ class Garelic
22
36
 
23
37
  class Railtie < Rails::Railtie
24
38
  initializer 'garelic.install_instrumentation' do
39
+ Garelic::Metrics.reset!
40
+
25
41
  ActiveSupport::Notifications.subscribe('process_action.action_controller') do |_, start, finish, _, payload|
26
- payload[:total_runtime] = (finish - start) * 1000
27
- Thread.current[:garelic_payload] = payload
42
+ Garelic::Metrics.report(:action, :db_runtime, payload[:db_runtime] || 0)
43
+ Garelic::Metrics.report(:action, :view_runtime, payload[:view_runtime] || 0)
44
+ Garelic::Metrics.report(:action, :total_runtime, (finish - start) * 1000)
45
+ Garelic::Metrics.action_identifier = "#{payload[:controller]}##{payload[:action]}"
46
+ end
47
+
48
+ ActiveSupport::Notifications.subscribe('sql.active_record') do |_, start, finish, _, payload|
49
+ type = payload[:name] || 'Other SQL'
50
+ Garelic::Metrics.report(:active_record, type, (finish - start) * 1000) if type != 'CACHE'
28
51
  end
29
52
  end
30
53
  end
54
+
55
+ class Metrics
56
+ def self.reset!
57
+ Thread.current[:garelic] = {}
58
+ Thread.current[:garelic_action] = nil
59
+ end
60
+
61
+ def self.action_identifier=(value)
62
+ Thread.current[:garelic_action] = value
63
+ end
64
+
65
+ def self.action_identifier
66
+ Thread.current[:garelic_action]
67
+ end
68
+
69
+ def self.report(category, variable, runtime, payload = nil)
70
+ metrics[category] ||= Hash.new(0)
71
+ metrics[category][variable] += runtime
72
+ end
73
+
74
+ def self.[](category)
75
+ metrics[category]
76
+ end
77
+
78
+ private
79
+ def self.metrics
80
+ Thread.current[:garelic]
81
+ end
82
+ end
31
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garelic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-02 00:00:00.000000000 Z
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Google Analytics Reports as "New Relic"-like performance monitoring for
15
- your Rails app
14
+ description: Use Google Analytics for Rails App Performance Monitoring
16
15
  email:
17
16
  - johno@jsmf.net
18
17
  executables: []