garelic 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/garelic.rb CHANGED
@@ -1,9 +1,38 @@
1
1
  require "garelic/version"
2
- require "garelic/dispatcher"
2
+ require "garelic/middleware"
3
3
 
4
4
  class Garelic
5
5
  Timing = '<!-- /* GARELIC DATA */ -->'.html_safe
6
6
 
7
+ @@deployed_version_slot = 5
8
+
9
+ cattr_accessor :deployed_version, :deployed_version_slot, instance_accessor: false
10
+
11
+ def self.monitoring(profile_id)
12
+ buffer = <<-HTML
13
+ <script type="text/javascript">
14
+ var _gaq = _gaq || [];
15
+ _gaq.push(['_setAccount', '#{profile_id}']);
16
+ _gaq.push(['_setSiteSpeedSampleRate', 100]);
17
+ #{report_deployed_version}
18
+ _gaq.push(['_trackPageview']);
19
+
20
+ #{Garelic::Timing}
21
+
22
+ (function() {
23
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
24
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
25
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
26
+ })();
27
+ </script>
28
+ HTML
29
+ buffer.html_safe
30
+ end
31
+
32
+ def self.report_deployed_version
33
+ "_gaq.push(['_setCustomVar', #{deployed_version_slot}, 'Garelic (Deployed version)', '#{deployed_version}', 3])" unless deployed_version.blank?
34
+ end
35
+
7
36
  def self.report_user_timing_from_metrics(metrics)
8
37
  reports = report_user_timing_for_action(metrics[:action], metrics.action_identifier)
9
38
  reports += report_user_timing_for_active_record(metrics[:active_record], metrics.action_identifier)
@@ -35,8 +64,10 @@ class Garelic
35
64
  end
36
65
 
37
66
  class Railtie < Rails::Railtie
38
- initializer 'garelic.install_instrumentation' do
39
- Garelic::Metrics.reset!
67
+ initializer 'garelic.install_instrumentation' do |app|
68
+ app.middleware.use Garelic::Middleware
69
+
70
+ Garelic::deployed_version = deployed_version_from_git || deployed_version_from_revision_file
40
71
 
41
72
  ActiveSupport::Notifications.subscribe('process_action.action_controller') do |_, start, finish, _, payload|
42
73
  Garelic::Metrics.report(:action, :db_runtime, payload[:db_runtime] || 0)
@@ -50,6 +81,14 @@ class Garelic
50
81
  Garelic::Metrics.report(:active_record, type, (finish - start) * 1000) if type != 'CACHE'
51
82
  end
52
83
  end
84
+
85
+ def deployed_version_from_git
86
+ `git log --pretty=format:"%cd %h" --date=iso -1 2>/dev/null`.strip.presence
87
+ end
88
+
89
+ def deployed_version_from_revision_file
90
+ `cat #{Rails.root}/REVISION 2>/dev/null`.strip.presence
91
+ end
53
92
  end
54
93
 
55
94
  class Metrics
@@ -77,6 +116,7 @@ class Garelic
77
116
 
78
117
  private
79
118
  def self.metrics
119
+ Thread.current[:garelic] ||= {}
80
120
  Thread.current[:garelic]
81
121
  end
82
122
  end
@@ -0,0 +1,19 @@
1
+ class Garelic::Middleware
2
+ def initialize(app)
3
+ @app = app
4
+ Garelic::Metrics.reset!
5
+ end
6
+
7
+ def call(env)
8
+ status, headers, response = @app.call(env)
9
+
10
+ if headers["Content-Type"] =~ /text\/html|application\/xhtml\+xml/
11
+ body = response.body.gsub(Garelic::Timing, Garelic.report_user_timing_from_metrics(Garelic::Metrics))
12
+ response = [body]
13
+ end
14
+
15
+ Garelic::Metrics.reset!
16
+
17
+ [status, headers, response]
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  class Garelic
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  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.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2012-10-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Use Google Analytics for Rails App Performance Monitoring
15
15
  email:
@@ -25,7 +25,7 @@ files:
25
25
  - Rakefile
26
26
  - garelic.gemspec
27
27
  - lib/garelic.rb
28
- - lib/garelic/dispatcher.rb
28
+ - lib/garelic/middleware.rb
29
29
  - lib/garelic/version.rb
30
30
  homepage: https://github.com/jsuchal/garelic
31
31
  licenses: []
@@ -1,20 +0,0 @@
1
- module ActionController
2
- class Metal < AbstractController::Base
3
- alias :dispatch_without_garelic :dispatch
4
-
5
- def dispatch(*args)
6
- Garelic::Metrics.reset!
7
-
8
- response = dispatch_without_garelic(*args)
9
-
10
- timing_data = Garelic.report_user_timing_from_metrics(Garelic::Metrics)
11
-
12
- _, _, chunks = response
13
- chunks.each do |chunk|
14
- chunk.gsub!(Garelic::Timing, timing_data)
15
- end
16
-
17
- response
18
- end
19
- end
20
- end