model_observer 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -12
- data/lib/model_observer/active_record3.rb +12 -0
- data/lib/model_observer/active_record4.rb +12 -0
- data/lib/model_observer/log_subscriber.rb +19 -0
- data/lib/model_observer/querying.rb +14 -0
- data/lib/model_observer/version.rb +1 -1
- data/lib/model_observer.rb +2 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4cefc88cdbb1e00c4b1550cb9f45edcc6b0fd078
|
4
|
+
data.tar.gz: a0798211619936ad40912908f92ba157423c0695
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96c0cf861d126fb6be493c7ee17cc4dc9090775187cb7d49c01bbd34ff77a310b8a81b6038b3af8453b78075ad66685c4a6c4994b6df25d53f6fa99d3e69a6f0
|
7
|
+
data.tar.gz: 3182a0ed8c4cb77aadddeaafb5a971eb2e22f6a39123c6439ea083446de315dee9f5ae44e967e06af73880d63e53a01b8f4888ad63e8add7c3dc54d06312f3da
|
data/README.md
CHANGED
@@ -16,18 +16,29 @@ and bundle
|
|
16
16
|
|
17
17
|
bundle
|
18
18
|
|
19
|
-
## Log
|
19
|
+
## Query Log
|
20
20
|
|
21
|
-
The
|
21
|
+
The time include instantiate models will be logged after each sql log like this:
|
22
|
+
|
23
|
+
Author Load (0.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
|
24
|
+
Author Instantiate (1.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
|
25
|
+
|
26
|
+
1.5ms == 0.5ms(DB query) + 1.0ms(model instantiation)
|
27
|
+
|
28
|
+
## Summary Log
|
29
|
+
|
30
|
+
The summary of each request will be added to the end like this:
|
22
31
|
|
23
32
|
===== Model Observer Start =====
|
24
|
-
Author: 1 sum(
|
25
|
-
Book:
|
26
|
-
id(
|
27
|
-
id(
|
28
|
-
id(
|
29
|
-
id(
|
30
|
-
id(
|
31
|
-
id(
|
32
|
-
id(
|
33
|
-
|
33
|
+
Author: 1 sum(10.6ms) avg(10.6ms)
|
34
|
+
Book: 27 sum(25.7ms) avg(1.0ms)
|
35
|
+
id(319): 3
|
36
|
+
id(377): 3
|
37
|
+
id(487): 3
|
38
|
+
id(489): 3
|
39
|
+
id(493): 3
|
40
|
+
id(496): 3
|
41
|
+
id(499): 3
|
42
|
+
id(507): 3
|
43
|
+
id(536): 3
|
44
|
+
===== Model Observer End =======
|
@@ -2,6 +2,7 @@ module ModelObserver
|
|
2
2
|
module ActiveRecord
|
3
3
|
def self.enable
|
4
4
|
require 'active_record'
|
5
|
+
|
5
6
|
::ActiveRecord::Inheritance::ClassMethods.class_eval do
|
6
7
|
alias_method :origin_instantiate, :instantiate
|
7
8
|
|
@@ -12,6 +13,17 @@ module ModelObserver
|
|
12
13
|
instance
|
13
14
|
end
|
14
15
|
end
|
16
|
+
|
17
|
+
::ActiveRecord::Querying.class_eval do
|
18
|
+
alias_method :origin_find_by_sql, :find_by_sql
|
19
|
+
|
20
|
+
def find_by_sql(sql, binds = [])
|
21
|
+
::ActiveSupport::Notifications.instrumenter.instrument(
|
22
|
+
"instantiate.model_observer",
|
23
|
+
:sql => connection.to_sql(sanitize_sql(sql), binds),
|
24
|
+
:name => "#{name} Instantiate") { origin_find_by_sql(sql, binds) }
|
25
|
+
end
|
26
|
+
end
|
15
27
|
end
|
16
28
|
end
|
17
29
|
end
|
@@ -2,6 +2,7 @@ module ModelObserver
|
|
2
2
|
module ActiveRecord
|
3
3
|
def self.enable
|
4
4
|
require 'active_record'
|
5
|
+
|
5
6
|
::ActiveRecord::Persistence::ClassMethods.class_eval do
|
6
7
|
alias_method :origin_instantiate, :instantiate
|
7
8
|
|
@@ -12,6 +13,17 @@ module ModelObserver
|
|
12
13
|
instance
|
13
14
|
end
|
14
15
|
end
|
16
|
+
|
17
|
+
::ActiveRecord::Querying.class_eval do
|
18
|
+
alias_method :origin_find_by_sql, :find_by_sql
|
19
|
+
|
20
|
+
def find_by_sql(sql, binds = [])
|
21
|
+
::ActiveSupport::Notifications.instrumenter.instrument(
|
22
|
+
"instantiate.model_observer",
|
23
|
+
:sql => connection.to_sql(sanitize_sql(sql), binds),
|
24
|
+
:name => "#{name} Instantiate") { origin_find_by_sql(sql, binds) }
|
25
|
+
end
|
26
|
+
end
|
15
27
|
end
|
16
28
|
end
|
17
29
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_record/log_subscriber'
|
2
|
+
|
3
|
+
module ModelObserver
|
4
|
+
class LogSubscriber < ::ActiveRecord::LogSubscriber
|
5
|
+
def instantiate(event)
|
6
|
+
return unless logger.debug?
|
7
|
+
|
8
|
+
payload = event.payload
|
9
|
+
name = '%s (%.1fms)' % [payload[:name], event.duration]
|
10
|
+
sql = payload[:sql].squeeze(' ')
|
11
|
+
|
12
|
+
name = color(name, YELLOW, true)
|
13
|
+
sql = color(sql, nil, true)
|
14
|
+
debug " #{name} #{sql}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
ModelObserver::LogSubscriber.attach_to :model_observer
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ModelObserver
|
2
|
+
module Querying
|
3
|
+
::ActiveRecord::Querying.class_eval do
|
4
|
+
alias_method :origin_find_by_sql, :find_by_sql
|
5
|
+
|
6
|
+
def find_by_sql(sql, binds = [])
|
7
|
+
::ActiveSupport::Notifications.instrumenter.instrument(
|
8
|
+
"instantiate.active_record",
|
9
|
+
:sql => sql,
|
10
|
+
:name => "#{name} Load") { origin_find_by_sql }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/model_observer.rb
CHANGED
@@ -19,6 +19,7 @@ module ModelObserver
|
|
19
19
|
else
|
20
20
|
raise "Current version of active_record is not supported."
|
21
21
|
end
|
22
|
+
require 'model_observer/log_subscriber'
|
22
23
|
end
|
23
24
|
|
24
25
|
class << self
|
@@ -32,7 +33,7 @@ module ModelObserver
|
|
32
33
|
def write_to_rails_log
|
33
34
|
Rails.logger.warn "===== Model Observer Start ====="
|
34
35
|
Rails.logger.warn Analyser.result
|
35
|
-
Rails.logger.warn "===== Model Observer End
|
36
|
+
Rails.logger.warn "===== Model Observer End ======="
|
36
37
|
end
|
37
38
|
end
|
38
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: model_observer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weihu Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sqlite3
|
@@ -51,7 +51,9 @@ files:
|
|
51
51
|
- lib/model_observer/analyser.rb
|
52
52
|
- lib/model_observer/collector.rb
|
53
53
|
- lib/model_observer/dependency.rb
|
54
|
+
- lib/model_observer/log_subscriber.rb
|
54
55
|
- lib/model_observer/metric.rb
|
56
|
+
- lib/model_observer/querying.rb
|
55
57
|
- lib/model_observer/rack.rb
|
56
58
|
- lib/model_observer/railtie.rb
|
57
59
|
- lib/model_observer/version.rb
|