cql-instrumentation 1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1fbcc065324776bdf9ce523c916a1f12ba1084cb
4
+ data.tar.gz: 0eddb84b1b1941a6de1c2e09b337d7d07d45216e
5
+ SHA512:
6
+ metadata.gz: 2006513b5f6797723cb5f1ed3c79f3deed9c450ef365ffa9fa64ec567e68e099d73e17422bb67a8f5b9664e52d2b6b9e61d427f9e4738d83e08a9a1fa59114e1
7
+ data.tar.gz: 6abd308b4470b7c5e0ed35caa90612c60301ec5c60680a96513fdfb5456d907722ca35d1c82c0faa5b11b80f81dafe23c57a17e34b02688b125425d9df2a4361
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cql-instrumentation.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nikita Afanasenko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # Cassandra CQL Instrumentations
2
+
3
+ Performs logging for [cql-rb](https://github.com/iconara/cql-rb) queries and durations similar to ActiveRecord logging for SQL queries.
4
+
5
+ Example logs:
6
+
7
+ ```
8
+ Cassandra Query (34.1ms) SELECT * FROM timeseries WHERE name = ? ["counts"]
9
+ Completed 200 OK in 141ms (Views: 7.8ms | ActiveRecord: 0.0ms | Cassandra: 117.3ms)
10
+ ```
11
+
12
+ ## Usage With Rails
13
+
14
+ All you need to do is to add this line to your application's Gemfile:
15
+
16
+ ```
17
+ gem 'cql-instrumentation'
18
+ ```
19
+
20
+ ## Usage Without Rails
21
+
22
+ If you want to use this logging without Rails, setup it manually:
23
+
24
+ ```ruby
25
+ ActiveSupport::LogSubscriber.logger = mylogger
26
+ CqlInstrumentation.setup_instrumentation
27
+ CqlInstrumentation.attach_log_subscriber
28
+ ```
29
+
30
+ Full example:
31
+
32
+ ```ruby
33
+ require 'cql'
34
+ require 'cql/instrumentation'
35
+ require 'logger'
36
+
37
+ logger = Logger.new(STDOUT)
38
+ ActiveSupport::LogSubscriber.logger = logger
39
+
40
+ CqlInstrumentation.setup_instrumentation
41
+ CqlInstrumentation.attach_log_subscriber
42
+
43
+ Cql::Client.connect.execute('SELECT * FROM timeseries WHERE name = ?', 'counts')
44
+ ```
45
+
46
+ And you'll get:
47
+
48
+ ```
49
+ Cassandra Query (1.7ms) SELECT * FROM timeseries WHERE name = ? ["counts"]
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/[my-github-username]/cql-instrumentation/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,22 @@
1
+ require_relative 'lib/cql_instrumentation/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "cql-instrumentation"
5
+ spec.version = CqlInstrumentation::VERSION
6
+ spec.authors = ["Nikita Afanasenko"]
7
+ spec.email = ["nikita@afanasenko.name"]
8
+ spec.summary = %q{CQL queries and durations logging for cql-rb and Rails}
9
+ spec.homepage = "https://github.com/spbtv/cql-instrumentation"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = `git ls-files -z`.split("\x0")
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
18
+ spec.add_runtime_dependency "cql-rb", "~> 2.0"
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ end
@@ -0,0 +1 @@
1
+ require 'cql_instrumentation'
@@ -0,0 +1,35 @@
1
+ require 'active_support/notifications'
2
+ require 'active_support/core_ext/module/aliasing'
3
+
4
+ module CqlInstrumentation
5
+ SERVICE_ID = :cassandra
6
+
7
+ class << self
8
+ def setup_instrumentation
9
+ instrument(Cql::Client::SynchronousClient)
10
+ end
11
+
12
+ def attach_log_subscriber
13
+ LogSubscriber.attach_to(SERVICE_ID)
14
+ end
15
+
16
+ private
17
+
18
+ def instrument(klass)
19
+ klass.class_eval do
20
+ def execute_with_instrumentation(cql, *binds)
21
+ ActiveSupport::Notifications.instrument("query.#{SERVICE_ID}", cql: cql, binds: binds) do
22
+ execute_without_instrumentation(cql, *binds)
23
+ end
24
+ end
25
+
26
+ alias_method_chain :execute, :instrumentation unless method_defined?(:execute_without_instrumentation)
27
+ end
28
+ end
29
+ end
30
+
31
+ require_relative 'cql_instrumentation/version'
32
+ require_relative 'cql_instrumentation/log_subscriber'
33
+ require_relative 'cql_instrumentation/controller_runtime'
34
+ require_relative 'cql_instrumentation/railtie' if defined?(Rails)
35
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_support/concern'
2
+
3
+ module CqlInstrumentation
4
+ module ControllerRuntime
5
+ extend ActiveSupport::Concern
6
+
7
+ protected
8
+
9
+ def append_info_to_payload(payload)
10
+ super
11
+ payload[:cassandra_runtime] = LogSubscriber.reset_runtime
12
+ end
13
+
14
+ module ClassMethods
15
+ def log_process_action(payload)
16
+ messages, cassandra_runtime = super, payload[:cassandra_runtime]
17
+ messages << "Cassandra: #{cassandra_runtime.round(1)}ms" if cassandra_runtime
18
+ messages
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,45 @@
1
+ require 'active_support/per_thread_registry'
2
+ require 'active_support/log_subscriber'
3
+
4
+ module CqlInstrumentation
5
+ class LogSubscriber < ActiveSupport::LogSubscriber
6
+ cattr_accessor :calculate_runtime
7
+
8
+ def query(event)
9
+ self.class.runtime += event.duration if self.class.calculate_runtime
10
+ return unless logger.debug?
11
+
12
+ cql = event.payload[:cql]
13
+ binds = event.payload[:binds]
14
+ name = "Cassandra Query (#{event.duration.round(1)}ms)"
15
+ debug " #{color(name, YELLOW, true)} #{color(cql, nil, true)} #{binds.any? ? binds.inspect : nil}"
16
+ end
17
+
18
+ def self.runtime=(value)
19
+ RuntimeRegistry.cql_runtime = value
20
+ end
21
+
22
+ def self.runtime
23
+ RuntimeRegistry.cql_runtime ||= 0
24
+ end
25
+
26
+ def self.reset_runtime
27
+ rt, self.runtime = runtime, 0
28
+ rt
29
+ end
30
+ end
31
+
32
+ class RuntimeRegistry
33
+ extend ActiveSupport::PerThreadRegistry
34
+
35
+ attr_accessor :cql_runtime
36
+
37
+ def self.cql_runtime
38
+ instance.cql_runtime
39
+ end
40
+
41
+ def self.cql_runtime=(val)
42
+ instance.cql_runtime = val
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module CqlInstrumentation
2
+ class Railtie < Rails::Railtie
3
+ initializer "cql_instrumentation" do
4
+ CqlInstrumentation.setup_instrumentation
5
+ CqlInstrumentation.attach_log_subscriber
6
+
7
+ ActiveSupport.on_load(:action_controller) do
8
+ include ControllerRuntime
9
+ LogSubscriber.calculate_runtime = true
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module CqlInstrumentation
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,39 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/cql_instrumentation'
3
+ require 'logger'
4
+
5
+ class CqlInstrumentationTest < Minitest::Test
6
+ class FakeLogger < Logger
7
+ attr_accessor :debug_called
8
+
9
+ def debug(*args)
10
+ self.debug_called = true
11
+ super
12
+ end
13
+ end
14
+
15
+ class FakeClient
16
+ def execute(cql, *binds)
17
+ end
18
+ end
19
+
20
+ CqlInstrumentation.send(:instrument, FakeClient)
21
+ CqlInstrumentation.attach_log_subscriber
22
+ CqlInstrumentation::LogSubscriber.calculate_runtime = true
23
+
24
+ def setup
25
+ @client = FakeClient.new
26
+ @logger = FakeLogger.new('/dev/null')
27
+ ActiveSupport::LogSubscriber.logger = @logger
28
+ end
29
+
30
+ def test_query_logging
31
+ @client.execute('SELECT * FROM timeseries WHERE value = ?', 1)
32
+ assert @logger.debug_called, 'Query was not logged'
33
+ end
34
+
35
+ def test_runtime_calculation
36
+ @client.execute('SELECT * FROM timeseries WHERE value = ?', 1)
37
+ assert CqlInstrumentation::LogSubscriber.reset_runtime > 0, 'Execution time was not calculated'
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cql-instrumentation
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Nikita Afanasenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cql-rb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - nikita@afanasenko.name
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - cql-instrumentation.gemspec
82
+ - lib/cql/instrumentation.rb
83
+ - lib/cql_instrumentation.rb
84
+ - lib/cql_instrumentation/controller_runtime.rb
85
+ - lib/cql_instrumentation/log_subscriber.rb
86
+ - lib/cql_instrumentation/railtie.rb
87
+ - lib/cql_instrumentation/version.rb
88
+ - test/cql_instrumentation_test.rb
89
+ homepage: https://github.com/spbtv/cql-instrumentation
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: CQL queries and durations logging for cql-rb and Rails
113
+ test_files:
114
+ - test/cql_instrumentation_test.rb
115
+ has_rdoc: