cequel 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9085a953689716b573953d289758730b72c6952
4
- data.tar.gz: 5c96c40d2b735f0075c6685ad5e52e45ba6a7054
3
+ metadata.gz: 32765637991b209fa0eda38cae999ce92e03d9de
4
+ data.tar.gz: 5b0f247d96c436fa9947d14162e91b648cc29929
5
5
  SHA512:
6
- metadata.gz: 213f188002f5b59514955bc2474a22a7427b748dc3e34c8eee7b9e23f1ed539e58e216ff6c9cf425971d57f342365deed664bdd8af42ec381496eb8cb6bdeb01
7
- data.tar.gz: b15c63da92f2231b0ec30b9a6851a0a1bd5ffaa256c24ce4d09b96029ef0dccc00c53670d3bd349c0a52d8448a11cee060e8c7c2e42f54f1420d49e810a9260c
6
+ metadata.gz: 7739409b3a223c117e17673d769b04b8382de1638c40262e72a68527bf75b87fd66c850681401fd652f998e59ab272d430379d611da2069ac316e9c7a0eb00b0
7
+ data.tar.gz: 1dc7c4ff48b96d2b6f4716fce64faad2569580a73c54446499809eb2f8947c3bf304da5a4082ae8d14f65da331f0b9ba8266daee99dcadc783bc3b47130a4c58
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.1.2
2
+
3
+ * Simplify logging implementation
4
+ * Support Cassandra authentication
5
+
1
6
  ## 1.1.1
2
7
 
3
8
  * Specify NewRelicInstrumentation with full namespace
data/Gemfile CHANGED
@@ -4,7 +4,7 @@ gemspec
4
4
 
5
5
  group :debug do
6
6
  gem 'debugger', :platforms => :mri_19
7
- gem 'byebug', :platforms => :mri_20
7
+ gem 'byebug', :platforms => [:mri_20, :mri_21]
8
8
  gem 'pry'
9
9
  end
10
10
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cequel (1.1.1)
4
+ cequel (1.1.2)
5
5
  activemodel (>= 3.1)
6
6
  cql-rb
7
7
 
@@ -24,6 +24,8 @@ module Cequel
24
24
  # @return [Symbol] the default consistency for queries in this keyspace
25
25
  # @since 1.1.0
26
26
  attr_writer :default_consistency
27
+ # @return [Hash] credentials for connect to cassandra
28
+ attr_reader :credentials
27
29
 
28
30
  #
29
31
  # @!method write(statement, *bind_vars)
@@ -92,16 +94,17 @@ module Cequel
92
94
  # Configure this keyspace from a hash of options
93
95
  #
94
96
  # @param configuration [Options] configuration options
95
- # @option configuration [String] :host ('127.0.0.1:9042') host/port of
97
+ # @option configuration [String] :host ('127.0.0.1') hostname of
96
98
  # single Cassandra instance to connect to
99
+ # @option configuration [Integer] :port (9042) port on which to connect
100
+ # to all specified hosts
97
101
  # @option configuration [Array<String>] :hosts list of Cassandra
98
- # instances to connect to
99
- # @option configuration [Hash] :thrift Thrift options to be passed
100
- # directly to Thrift client
102
+ # instances to connect to (hostnames only)
103
+ # @option configuration [String] :username user to auth with (leave blank
104
+ # for no auth)
105
+ # @option configuration [String] :password password to auth with (leave
106
+ # blank for no auth)
101
107
  # @option configuration [String] :keyspace name of keyspace to connect to
102
- # @option configuration [Integer] :pool (1) size of connection pool
103
- # @option configuration [Integer] :pool_timeout (0) timeout when
104
- # attempting to check out connection from pool
105
108
  # @return [void]
106
109
  #
107
110
  def configure(configuration = {})
@@ -112,6 +115,7 @@ module Cequel
112
115
  @configuration = configuration
113
116
 
114
117
  @hosts, @port = extract_hosts_and_port(configuration)
118
+ @credentials = extract_credentials(configuration)
115
119
 
116
120
  @name = configuration[:keyspace]
117
121
  # reset the connections
@@ -202,7 +206,10 @@ module Cequel
202
206
  private :lock
203
207
 
204
208
  def build_client
205
- Cql::Client.connect(hosts: hosts, port: port).tap do |client|
209
+ client_options = {hosts: hosts, port: port}.tap do |options|
210
+ options[:credentials] = credentials if credentials
211
+ end
212
+ Cql::Client.connect(client_options).tap do |client|
206
213
  client.use(name) if name
207
214
  end
208
215
  end
@@ -238,6 +245,10 @@ module Cequel
238
245
 
239
246
  [hosts, ports.first || 9042]
240
247
  end
248
+
249
+ def extract_credentials(configuration)
250
+ configuration.slice(:username, :password).presence
251
+ end
241
252
  end
242
253
  end
243
254
  end
@@ -5,44 +5,27 @@ module Cequel
5
5
  # Methods to handle logging for {Keyspace} instances
6
6
  #
7
7
  module Logging
8
- def logger=(logger)
9
- loggers << Logger.new(logger, ::Logger::DEBUG)
10
- self.exception_logger = ExceptionLogger.new(logger, ::Logger::ERROR)
11
- end
8
+ extend Forwardable
9
+ def_delegators :request_logger, :logger, :logger=, :slowlog_threshold,
10
+ :slowlog_threshold=
12
11
 
12
+ #
13
+ # @deprecated
14
+ #
13
15
  def slowlog=(slowlog)
14
- warn "#slowlog= is deprecated and will be removed from a future " \
15
- "version"
16
- loggers << @slowlog = Logger.new(slowlog, ::Logger::WARN, 2000)
17
- end
18
-
19
- def slowlog_threshold=(threshold)
20
- @slowlog.threshold = threshold
16
+ warn "#slowlog= is deprecated and ignored"
21
17
  end
22
18
 
23
19
  protected
24
20
 
25
- attr_accessor :exception_logger
21
+ attr_writer :request_logger
26
22
 
27
- private
28
-
29
- def log(label, statement, *bind_vars)
30
- response = nil
31
- begin
32
- time = Benchmark.ms { response = yield }
33
- loggers.each do |logger|
34
- logger.log(label, time, statement, bind_vars)
35
- end
36
- rescue Exception => e
37
- exception_logger.log(label, statement, bind_vars) if exception_logger
38
- raise
39
- end
40
- response
23
+ def request_logger
24
+ @request_logger ||= RequestLogger.new
41
25
  end
42
26
 
43
- def loggers
44
- @loggers ||= []
45
- end
27
+ def_delegator :request_logger, :log
28
+ protected :log
46
29
  end
47
30
  end
48
31
  end
@@ -0,0 +1,61 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Cequel
3
+ module Metal
4
+ #
5
+ # The Logger class encapsulates logging functionality for {Keyspace}.
6
+ #
7
+ # @api private
8
+ #
9
+ class RequestLogger
10
+ extend Forwardable
11
+ # @return [::Logger] An instance of Logger that responds to methods for
12
+ # standard severity levels
13
+ attr_accessor :logger
14
+ # @return [Integer] Only log queries that take longer than threshold ms
15
+ attr_accessor :slowlog_threshold
16
+
17
+ def initialize
18
+ self.slowlog_threshold = 2000
19
+ end
20
+
21
+ #
22
+ # Log a CQL statement
23
+ #
24
+ # @param label [String] a logical label for this statement
25
+ # @param statement [String] the CQL statement to log
26
+ # @param bind_vars bind variables for the CQL statement
27
+ # @return [void]
28
+ #
29
+ def log(label, statement, *bind_vars)
30
+ return yield if logger.nil?
31
+
32
+ response = nil
33
+ begin
34
+ time = Benchmark.ms { response = yield }
35
+ generate_message = lambda do
36
+ format_for_log(label, "#{time.round.to_i}ms", statement, bind_vars)
37
+ end
38
+
39
+ if time >= slowlog_threshold
40
+ logger.warn(&generate_message)
41
+ else
42
+ logger.debug(&generate_message)
43
+ end
44
+ rescue Exception => e
45
+ logger.error { format_for_log(label, 'ERROR', statement, bind_vars) }
46
+ raise
47
+ end
48
+ response
49
+ end
50
+
51
+ private
52
+
53
+ def format_for_log(label, timing, statement, bind_vars)
54
+ format('%s (%s) %s', label, timing, sanitize(statement, bind_vars))
55
+ end
56
+
57
+ def_delegator 'Cequel::Metal::Keyspace', :sanitize
58
+ private :sanitize
59
+ end
60
+ end
61
+ end
data/lib/cequel/metal.rb CHANGED
@@ -5,7 +5,7 @@ require 'cequel/metal/cql_row_specification'
5
5
  require 'cequel/metal/data_set'
6
6
  require 'cequel/metal/logging'
7
7
  require 'cequel/metal/keyspace'
8
- require 'cequel/metal/logger'
8
+ require 'cequel/metal/request_logger'
9
9
  require 'cequel/metal/row'
10
10
  require 'cequel/metal/row_specification'
11
11
  require 'cequel/metal/statement'
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Cequel
3
3
  # The current version of the library
4
- VERSION = '1.1.1'
4
+ VERSION = '1.1.2'
5
5
  end
@@ -16,3 +16,5 @@ production:
16
16
  - 'cass3.<%= app_name %>.biz'
17
17
  port: 9042
18
18
  keyspace: <%= app_name %>_production
19
+ username: 'myappuser'
20
+ password: 'password1'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat Brown
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2014-03-17 00:00:00.000000000 Z
18
+ date: 2014-03-18 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activemodel
@@ -188,9 +188,9 @@ files:
188
188
  - lib/cequel/metal/incrementer.rb
189
189
  - lib/cequel/metal/inserter.rb
190
190
  - lib/cequel/metal/keyspace.rb
191
- - lib/cequel/metal/logger.rb
192
191
  - lib/cequel/metal/logging.rb
193
192
  - lib/cequel/metal/new_relic_instrumentation.rb
193
+ - lib/cequel/metal/request_logger.rb
194
194
  - lib/cequel/metal/row.rb
195
195
  - lib/cequel/metal/row_specification.rb
196
196
  - lib/cequel/metal/statement.rb
@@ -1,72 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module Cequel
3
- module Metal
4
- #
5
- # The Logger class encapsulates logging functionality for {Keyspace}.
6
- #
7
- # @api private
8
- #
9
- class Logger
10
- extend Forwardable
11
- # @return [::Logger] An instance of Logger from the standard library
12
- attr_reader :out
13
- # @return [Integer] The severity level for this logger
14
- attr_reader :severity
15
- # @return [Integer] Only log queries that take longer than threshold ms
16
- attr_accessor :threshold
17
-
18
- #
19
- # @param out [::Logger] An instance of Logger from the standard library
20
- # @param severity [Integer] The severity level for this logger
21
- # @param threshold [Integer] Only log queries that take longer than
22
- # `threshold` ms
23
- #
24
- def initialize(out, severity, threshold = 0)
25
- @out, @severity, @threshold = out, severity, threshold
26
- end
27
-
28
- #
29
- # Log a CQL statement
30
- #
31
- # @param label [String] a logical label for this statement
32
- # @param timing [Integer] how long this statement took in ms
33
- # @param statement [String] the CQL statement to log
34
- # @param bind_vars [Array] bind variables for the CQL statement
35
- # @return [void]
36
- #
37
- def log(label, timing, statement, bind_vars)
38
- if timing >= threshold
39
- out.add(severity) do
40
- format(
41
- '%s (%dms) %s',
42
- label, timing, sanitize(statement, bind_vars)
43
- )
44
- end
45
- end
46
- end
47
-
48
- private
49
-
50
- def_delegator 'Cequel::Metal::Keyspace', :sanitize
51
- end
52
-
53
- #
54
- # Logger for queries that resulted in an exception
55
- #
56
- class ExceptionLogger < Logger
57
- #
58
- # Log a CQL statement that resulted in an exception
59
- #
60
- # @param label [String] a logical label for this statement
61
- # @param statement [String] the CQL statement to log
62
- # @param bind_vars [Array] bind variables for the CQL statement
63
- # @return [void]
64
- #
65
- def log(label, statement, bind_vars)
66
- out.add(severity) do
67
- format('%s (ERROR) %s', label, sanitize(statement, bind_vars))
68
- end
69
- end
70
- end
71
- end
72
- end