elastic_record 0.9.3 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/elastic_record.gemspec +1 -1
- data/lib/elastic_record/log_subscriber.rb +16 -1
- data/lib/elastic_record/railtie.rb +9 -1
- data/lib/elastic_record/railties/controller_runtime.rb +45 -0
- data/test/elastic_record/log_subscriber_test.rb +4 -0
- data/test/elastic_record/railties/controller_runtime_test.rb +49 -0
- metadata +4 -2
data/elastic_record.gemspec
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
module ElasticRecord
|
2
2
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
3
|
+
def self.runtime=(value)
|
4
|
+
Thread.current["elastic_record_request_runtime"] = value
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.runtime
|
8
|
+
Thread.current["elastic_record_request_runtime"] ||= 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.reset_runtime
|
12
|
+
rt, self.runtime = runtime, 0
|
13
|
+
rt
|
14
|
+
end
|
15
|
+
|
3
16
|
def request(event)
|
17
|
+
self.class.runtime += event.duration
|
18
|
+
|
4
19
|
payload = event.payload
|
5
20
|
request_log = "#{payload[:request].method} #{payload[:http].address}:#{payload[:http].port}#{payload[:request].path}"
|
6
21
|
|
@@ -17,4 +32,4 @@ module ElasticRecord
|
|
17
32
|
end
|
18
33
|
end
|
19
34
|
|
20
|
-
ElasticRecord::LogSubscriber.attach_to :elastic_record
|
35
|
+
ElasticRecord::LogSubscriber.attach_to :elastic_record
|
@@ -7,5 +7,13 @@ module ElasticRecord
|
|
7
7
|
rake_tasks do
|
8
8
|
load "elastic_record/tasks/index.rake"
|
9
9
|
end
|
10
|
+
|
11
|
+
# Expose database runtime to controller for logging.
|
12
|
+
initializer "elastic_record.log_runtime" do |app|
|
13
|
+
require "elastic_record/railties/controller_runtime"
|
14
|
+
ActiveSupport.on_load(:action_controller) do
|
15
|
+
include ElasticRecord::Railties::ControllerRuntime
|
16
|
+
end
|
17
|
+
end
|
10
18
|
end
|
11
|
-
end
|
19
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'active_support/core_ext/module/attr_internal'
|
2
|
+
require 'elastic_record/log_subscriber'
|
3
|
+
|
4
|
+
module ElasticRecord
|
5
|
+
module Railties # :nodoc:
|
6
|
+
module ControllerRuntime #:nodoc:
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods # :nodoc:
|
10
|
+
def log_process_action(payload)
|
11
|
+
messages, elastic_record_runtime = super, payload[:elastic_record_runtime]
|
12
|
+
if elastic_record_runtime.to_i > 0
|
13
|
+
messages << ("ElasticRecord: %.1fms" % elastic_record_runtime.to_f)
|
14
|
+
end
|
15
|
+
messages
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
#private
|
20
|
+
|
21
|
+
attr_internal :elastic_record_runtime
|
22
|
+
|
23
|
+
def process_action(action, *args)
|
24
|
+
# We also need to reset the runtime before each action
|
25
|
+
# because of queries in middleware or in cases we are streaming
|
26
|
+
# and it won't be cleaned up by the method below.
|
27
|
+
ElasticRecord::LogSubscriber.reset_runtime
|
28
|
+
super
|
29
|
+
end
|
30
|
+
|
31
|
+
def cleanup_view_runtime
|
32
|
+
runtime_before_render = ElasticRecord::LogSubscriber.reset_runtime
|
33
|
+
runtime = super
|
34
|
+
runtime_after_render = ElasticRecord::LogSubscriber.reset_runtime
|
35
|
+
self.elastic_record_runtime = runtime_before_render + runtime_after_render
|
36
|
+
runtime - runtime_after_render
|
37
|
+
end
|
38
|
+
|
39
|
+
def append_info_to_payload(payload)
|
40
|
+
super
|
41
|
+
payload[:elastic_record_runtime] = (elastic_record_runtime || 0) + ElasticRecord::LogSubscriber.reset_runtime
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -25,4 +25,8 @@ class ElasticRecord::LogSubscriberTest < ActiveSupport::TestCase
|
|
25
25
|
assert_match /GET (.*)test/, @logger.logged(:debug)[0]
|
26
26
|
assert_match %r['#{ActiveSupport::JSON.encode('foo' => 'bar')}'], @logger.logged(:debug)[0]
|
27
27
|
end
|
28
|
+
|
29
|
+
def test_initializes_runtime
|
30
|
+
Thread.new { assert_equal 0, ElasticRecord::LogSubscriber.runtime }.join
|
31
|
+
end
|
28
32
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require "elastic_record/railties/controller_runtime"
|
3
|
+
|
4
|
+
class ElasticRecord::Railties::ControllerRuntimeTest < MiniTest::Spec
|
5
|
+
class TestRuntime
|
6
|
+
def self.log_process_action(payload)
|
7
|
+
['sweet']
|
8
|
+
end
|
9
|
+
|
10
|
+
def cleanup_view_runtime
|
11
|
+
12
|
12
|
+
end
|
13
|
+
|
14
|
+
def append_info_to_payload(payload)
|
15
|
+
payload[:foo] = 42
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
class ElasticRuntime < TestRuntime
|
21
|
+
include ElasticRecord::Railties::ControllerRuntime
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_cleanup_view_runtime
|
25
|
+
runtime = ElasticRuntime.new
|
26
|
+
ElasticRecord::LogSubscriber.runtime = 10
|
27
|
+
|
28
|
+
runtime.cleanup_view_runtime
|
29
|
+
|
30
|
+
assert_equal 0, ElasticRecord::LogSubscriber.runtime
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_append_info_to_payload
|
34
|
+
runtime = ElasticRuntime.new
|
35
|
+
payload = {}
|
36
|
+
runtime.append_info_to_payload(payload)
|
37
|
+
|
38
|
+
assert_equal 42, payload[:foo]
|
39
|
+
assert payload.key?(:elastic_record_runtime)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_log_process_action
|
43
|
+
payload = {elastic_record_runtime: 12.3}
|
44
|
+
messages = ElasticRuntime.log_process_action(payload)
|
45
|
+
|
46
|
+
assert_equal 2, messages.size
|
47
|
+
assert_equal "ElasticRecord: 12.3ms", messages.last
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastic_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.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-11-
|
12
|
+
date: 2012-11-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: arelastic
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- lib/elastic_record/model.rb
|
73
73
|
- lib/elastic_record/orm/active_record.rb
|
74
74
|
- lib/elastic_record/railtie.rb
|
75
|
+
- lib/elastic_record/railties/controller_runtime.rb
|
75
76
|
- lib/elastic_record/relation.rb
|
76
77
|
- lib/elastic_record/relation/batches.rb
|
77
78
|
- lib/elastic_record/relation/delegation.rb
|
@@ -99,6 +100,7 @@ files:
|
|
99
100
|
- test/elastic_record/log_subscriber_test.rb
|
100
101
|
- test/elastic_record/lucene_test.rb
|
101
102
|
- test/elastic_record/model_test.rb
|
103
|
+
- test/elastic_record/railties/controller_runtime_test.rb
|
102
104
|
- test/elastic_record/relation/batches_test.rb
|
103
105
|
- test/elastic_record/relation/delegation_test.rb
|
104
106
|
- test/elastic_record/relation/finder_methods_test.rb
|