query_counter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/query_counter/collector.rb +54 -0
- data/lib/query_counter/global.rb +20 -0
- data/lib/query_counter/stat.rb +16 -0
- data/lib/query_counter.rb +61 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dde4c69db1dfdb26db42ab2dd2d549120db69565
|
4
|
+
data.tar.gz: 46d0d9e9ba5593004d2fb9fe034a8b26a417fc28
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25870e36da7884d02c2b5bcf63011bf88233cf39979684a3684431019b302898e2b6fe90af6fbf9ff13cfc4e14ebc71bce10bcb6046a006e9fe45a6fdc00eb7e
|
7
|
+
data.tar.gz: 476ad9e7953ce4e9012988e026b4fc2b3dbc608cf494a4e7be12d8aa923da95d012806a04814c97f268585848a97ed9e3866f8c30289d6c17cedd989992074da
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module QueryCounter
|
2
|
+
class Collector
|
3
|
+
attr_reader :stats,
|
4
|
+
:notification_events
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
reset
|
8
|
+
end
|
9
|
+
|
10
|
+
def record(resource, duration, by=1)
|
11
|
+
@stats[resource] ||= ::QueryCounter::Stat.new
|
12
|
+
@stats[resource].increment(duration, by)
|
13
|
+
end
|
14
|
+
|
15
|
+
# used for debugging resource events
|
16
|
+
def record_event(resource, event)
|
17
|
+
record(resource, event.duration)
|
18
|
+
|
19
|
+
@notification_events ||= {}
|
20
|
+
@notification_events[resource] ||= []
|
21
|
+
@notification_events[resource] << event
|
22
|
+
end
|
23
|
+
|
24
|
+
def reset
|
25
|
+
if instance_variable_defined?(:@notification_events)
|
26
|
+
remove_instance_variable(:@notification_events)
|
27
|
+
end
|
28
|
+
|
29
|
+
# just away to return the current stats and reset them back to an empty hash
|
30
|
+
@stats.tap { @stats = {} }
|
31
|
+
end
|
32
|
+
|
33
|
+
def count(resource)
|
34
|
+
(@stats[resource] && @stats[resource].count) || 0
|
35
|
+
end
|
36
|
+
|
37
|
+
def events(resource)
|
38
|
+
(@notification_events && @notification_events[resource]) || []
|
39
|
+
end
|
40
|
+
|
41
|
+
def add(collector)
|
42
|
+
if collector.notification_events
|
43
|
+
collector.notification_events.each do |resource, list|
|
44
|
+
list.each { |event| record_event(resource, event) }
|
45
|
+
end
|
46
|
+
else
|
47
|
+
collector.stats.each do |resource, stat|
|
48
|
+
record(record, stat.time, stat.count)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
self
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
# Purpose is to collect stats for the life time of the process
|
4
|
+
module QueryCounter
|
5
|
+
class Global
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@collector = ::QueryCounter::Collector.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def record(resource, duration, by=1)
|
13
|
+
@collector.record(resource, duration, by)
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset
|
17
|
+
@collector.reset
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module QueryCounter
|
2
|
+
autoload :Collector, 'query_counter/collector'
|
3
|
+
autoload :Global, 'query_counter/global'
|
4
|
+
autoload :Stat, 'query_counter/stat'
|
5
|
+
|
6
|
+
def self.global_collector
|
7
|
+
::QueryCounter::Global.instance
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.current_collector
|
11
|
+
Thread.current[:query_counter_collector] ||= ::QueryCounter::Collector.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.temporary_collector
|
15
|
+
Thread.current[:temporary_query_counter_collector]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.record(resource, duration, by=1)
|
19
|
+
global_collector.record(resource, duration, by)
|
20
|
+
current_collector.record(resource, duration, by)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.record_event(resource, event)
|
24
|
+
temporary_collector && temporary_collector.record_event(resource, event)
|
25
|
+
record(resource, event.duration)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.reset
|
29
|
+
current_collector.reset
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.around
|
33
|
+
new_collector = ::QueryCounter::Collector.new
|
34
|
+
old_collector, Thread.current[:temporary_query_counter_collector] = Thread.current[:temporary_query_counter_collector], new_collector
|
35
|
+
|
36
|
+
yield
|
37
|
+
|
38
|
+
if old_collector
|
39
|
+
Thread.current[:temporary_query_counter_collector] = old_collector.add(new_collector)
|
40
|
+
else
|
41
|
+
Thread.current[:temporary_query_counter_collector] = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
new_collector
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.count(resource)
|
48
|
+
current_collector.count(resource)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.global_count(resource)
|
52
|
+
current_collector.count(resource)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.auto_subscribe!(resource, event_name)
|
56
|
+
require 'active_support/notifications'
|
57
|
+
ActiveSupport::Notifications.subscribe(event_name) do |*args|
|
58
|
+
::QueryCounter.record_event(resource, ActiveSupport::Notifications::Event.new(*args))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: query_counter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Doug Youch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Used for monitoring number of external calls
|
14
|
+
email: dougyouch@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/query_counter.rb
|
20
|
+
- lib/query_counter/collector.rb
|
21
|
+
- lib/query_counter/global.rb
|
22
|
+
- lib/query_counter/stat.rb
|
23
|
+
homepage: https://github.com/evertrue/query_counter
|
24
|
+
licenses: []
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.5.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Count requests to external systems
|
46
|
+
test_files: []
|