vizsla 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01dd09ce233d8fba9d914505bb601ccbab66eb39
4
- data.tar.gz: 6578bab6b01119fe44cee971d50064eea2b32c6c
3
+ metadata.gz: 022a8c19393e8fec4151136366979fde7d45505d
4
+ data.tar.gz: dcb929f21d33a1dd4f0f0d50a42b770cb7fc6d5b
5
5
  SHA512:
6
- metadata.gz: 853aa45a657bdc6848afebd9e3dd37386be461214241bba81a5d8cbcdbdc55c259d5788ad1daf8aafb63121d0b0679edc8aed3aaa77909d49953fba767ab159b
7
- data.tar.gz: 136724d70d693bfa6c361e8336c497b8f8221d697d58bfcad0c9e143a37c94b1bd82f6750cc3bab1161e4633813e7d5a447beb2cdb0c7977df8fbd2919615ee5
6
+ metadata.gz: d2017c6757a5d87af0a5fd36b3cf43dda5948b21a267844f7602727fb973b5806a6c33095bd4388b00923532888522fa1061445b9eebda1990586ac6e0b6c37b
7
+ data.tar.gz: 8d0dbfc99b947ca32957c249a4bae2f8f470530e4c9810c24ab37bd0e9df310a488ec375d1945637b39555f929a64b620063cec475be5b68f866a4aa29475096
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
1
  # Vizsla
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/vizsla`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
3
  ## Installation
8
4
 
9
5
  Add this line to your application's Gemfile:
@@ -32,7 +28,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
28
 
33
29
  ## Contributing
34
30
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vizsla. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/aastronautss/vizsla. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
32
 
37
33
 
38
34
  ## License
@@ -0,0 +1,7 @@
1
+ module Vizsla
2
+ module Helpers
3
+ def to_milliseconds(time)
4
+ (time.to_f * 1000).round 1
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ module Vizsla
2
+ class RequestLogger
3
+ def request_response_time(query_time)
4
+ Rails.logger.debug "=" * 50
5
+ Rails.logger.debug "Total request/response time: #{query_time} seconds."
6
+ Rails.logger.debug "=" * 50
7
+ end
8
+
9
+ def log_events(events)
10
+ if events.empty?
11
+ Rails.logger.debug "=" * 50
12
+ Rails.logger.debug "No data collected. Are you calling ActiveRecord at all?"
13
+ Rails.logger.debug "=" * 50
14
+ else
15
+ events.keys.each do |event_name|
16
+ Rails.logger.debug "=" * 50
17
+ Rails.logger.debug events[event_name]
18
+ Rails.logger.debug "=" * 50
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ require 'vizsla/timer'
2
+ require 'vizsla/puppet_master'
3
+
4
+ module Vizsla
5
+ class Middleware
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ dup.__call(env)
12
+ end
13
+
14
+ def __call(env)
15
+ timer = Timer.new
16
+ timer.start!
17
+
18
+ status, headers, response = @app.call(env)
19
+
20
+ timer.stop!
21
+
22
+ PuppetMaster.new(timer).process
23
+
24
+ [status, headers, response]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,45 @@
1
+ require 'vizsla/helpers'
2
+
3
+ module Vizsla
4
+ class Patches
5
+ include ::Vizsla::Helpers
6
+
7
+ class << self
8
+ def patch_postgres
9
+ if block_given?
10
+ @postgres_event_hanlder = Proc.new(&block)
11
+ end
12
+
13
+ ::PG::Connection.class_eval do
14
+ alias_method :exec_without_profiling, :exec
15
+
16
+ def exec(*args, &blk)
17
+ return exec_without_profiling(*args, &blk)
18
+
19
+ start_time = Time.now
20
+ result = exec_without_profiling(*args, &blk)
21
+ end_time = Time.now
22
+
23
+ event_data = [
24
+ 'sql.postgres_exec',
25
+ start_time,
26
+ end_time,
27
+ {
28
+ sql: args[0]
29
+ }
30
+ ]
31
+
32
+ ::Vizsla::Patches.handle_event :postgres, event_data
33
+
34
+ result
35
+ end
36
+ end
37
+ end
38
+
39
+ def handle_event(handler_name, event_data)
40
+ handler = self.get_instance_variable "@#{handler_name}_event_handler"
41
+ hanlder.call event_data unless hanlder.nil?
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ require 'vizsla/logger'
2
+
3
+ module Vizsla
4
+ class PuppetMaster
5
+ def initialize(timer)
6
+ @timer = timer
7
+ @logger = RequestLogger.new
8
+ end
9
+
10
+ def process
11
+ @logger.request_response_time @timer.elapsed
12
+ @logger.log_events @timer.events
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ module Vizsla
2
+ class Recorder
3
+ THREAD_LOCAL_KEY = :_vizsla_current
4
+ LOCK = Mutex.new
5
+
6
+ class << self
7
+ def current
8
+ LOCK.synchronize do
9
+ Thread.current[THREAD_LOCAL_KEY]
10
+ end
11
+ end
12
+
13
+ def current=(val)
14
+ Thread.current[THREAD_LOCAL_KEY] = val
15
+ end
16
+
17
+ def start_recording
18
+ self.current = {}
19
+ end
20
+
21
+ def recording?
22
+ !self.current.nil?
23
+ end
24
+
25
+ def add_event(event)
26
+ return unless self.recording?
27
+ self.current[event.recorder_type] ||= []
28
+ self.current[event.recorder_type] << event.prettify_data
29
+ end
30
+ alias_method :<<, :add_event
31
+
32
+ def events
33
+ self.current
34
+ end
35
+
36
+ def stop_recording
37
+ LOCK.synchronize do
38
+ Thread.current[THREAD_LOCAL_KEY] = nil
39
+ end
40
+ end
41
+
42
+ at_exit do
43
+ self.stop_recording
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,124 @@
1
+ require 'vizsla/recorder' unless defined?(::Vizsla::Recorder)
2
+ require 'vizsla/patches' unless defined?(::Vizsla::Patches)
3
+
4
+ module Vizsla
5
+ class Event
6
+ attr_reader :event
7
+
8
+ def initialize(event)
9
+ @event = event
10
+ end
11
+
12
+ def recorder_type
13
+ event[0]
14
+ end
15
+
16
+ def valid?
17
+ true
18
+ end
19
+
20
+ def prettify_data
21
+ {
22
+ event_started: event[1],
23
+ event_ended: event[2],
24
+ event_duration: event[2] - event[1],
25
+ event_payload: prettify_payload
26
+ }
27
+ end
28
+ end
29
+
30
+ class SQLEvent < Event
31
+ def valid?
32
+ event.last[:name] != "SCHEMA"
33
+ end
34
+
35
+ private
36
+
37
+ def prettify_payload
38
+ {
39
+ query: event.last[:sql]
40
+ }
41
+ end
42
+ end
43
+
44
+ class ControllerEvent < Event
45
+ private
46
+
47
+ def prettify_payload
48
+ payload = event.last
49
+ {
50
+ format: payload[:format],
51
+ controller: payload[:controller],
52
+ action: payload[:action],
53
+ path: payload[:path],
54
+ db_runtime: payload[:db_runtime]
55
+ }
56
+ end
57
+ end
58
+
59
+ class ViewEvent < Event
60
+ private
61
+
62
+ def prettify_payload
63
+ {
64
+ layout: event.last[:layout]
65
+ }
66
+ end
67
+ end
68
+
69
+ class Subscribers
70
+ def initialize
71
+ @events_data = Recorder
72
+ # @logger = RequestLogger.new
73
+ collect_events_data
74
+ end
75
+
76
+ def sql_hook
77
+ ActiveSupport::Notifications.subscribe "sql.active_record" do |*args|
78
+ event = SQLEvent.new(args)
79
+ @events_data << event if event.valid?
80
+ end
81
+ end
82
+
83
+ def process_action_hook
84
+ ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
85
+ event = ControllerEvent.new(args)
86
+ @events_data << event
87
+ end
88
+ end
89
+
90
+ def render_template_hook
91
+ ActiveSupport::Notifications.subscribe "render_template.action_view" do |*args|
92
+ event = ViewEvent.new(args)
93
+ @events_data << event
94
+ end
95
+ end
96
+
97
+ # ===---------------------------===
98
+ # Non-Rails Hooks
99
+ # ===---------------------------===
100
+
101
+ def postgres_hook
102
+ if !defined? Rails
103
+ ::Vizsla::Patches.patch_postgres do |event_data|
104
+ event = SQLEvent.new event_data
105
+ @events_data << event
106
+ end
107
+ end
108
+ end
109
+
110
+ # ===---------------------------===
111
+ # Aux
112
+ # ===---------------------------===
113
+
114
+ def collect_events_data
115
+ sql_hook
116
+ process_action_hook
117
+ render_template_hook
118
+ end
119
+
120
+ def report_events_data
121
+ @logger.log_events(@events_data)
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,31 @@
1
+ require 'vizsla/recorder'
2
+
3
+ module Vizsla
4
+ class Timer
5
+ attr_reader :events
6
+
7
+ def initialize
8
+ @start_time = nil
9
+ @stop_time = nil
10
+ end
11
+
12
+ def start!
13
+ @start_time = Time.now
14
+ Recorder.start_recording
15
+ end
16
+
17
+ def stop!
18
+ collect_events
19
+ Recorder.stop_recording
20
+ @stop_time = Time.now
21
+ end
22
+
23
+ def collect_events
24
+ @events = Recorder.events
25
+ end
26
+
27
+ def elapsed
28
+ "#{(@stop_time - @start_time).round 2}s"
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Vizsla
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/vizsla.rb CHANGED
@@ -1,5 +1,9 @@
1
- require "vizsla/version"
1
+ require 'vizsla/version'
2
+ require 'vizsla/middleware'
3
+ require 'vizsla/subscribers'
2
4
 
3
5
  module Vizsla
4
- # Your code goes here...
6
+ class Core
7
+ @subscribers = Subscribers.new
8
+ end
5
9
  end
data/vizsla.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.14"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency 'pry', '~> 0.10'
26
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vizsla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Guillen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-04 00:00:00.000000000 Z
11
+ date: 2017-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
55
69
  description:
56
70
  email:
57
71
  - tyguillen@gmail.com
@@ -70,6 +84,14 @@ files:
70
84
  - bin/console
71
85
  - bin/setup
72
86
  - lib/vizsla.rb
87
+ - lib/vizsla/helpers.rb
88
+ - lib/vizsla/logger.rb
89
+ - lib/vizsla/middleware.rb
90
+ - lib/vizsla/patches.rb
91
+ - lib/vizsla/puppet_master.rb
92
+ - lib/vizsla/recorder.rb
93
+ - lib/vizsla/subscribers.rb
94
+ - lib/vizsla/timer.rb
73
95
  - lib/vizsla/version.rb
74
96
  - vizsla.gemspec
75
97
  homepage: https://google.com