Class: Metriks::Reporter::Cassandra
- Inherits:
-
Object
- Object
- Metriks::Reporter::Cassandra
- Defined in:
- lib/metriks/reporter/cassandra.rb
Instance Attribute Summary collapse
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #close_connection ⇒ Object
-
#connection ⇒ Object
two weeks.
- #execute_prepared_statement(array) ⇒ Object
-
#initialize(host, options = {}) ⇒ Cassandra
constructor
A new instance of Cassandra.
- #open_connection ⇒ Object
- #restart ⇒ Object
- #send_metric(compound_name, metric, keys, snapshot_keys = []) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #write ⇒ Object
- #write_statement ⇒ Object
Constructor Details
#initialize(host, options = {}) ⇒ Cassandra
Returns a new instance of Cassandra.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/metriks/reporter/cassandra.rb', line 10 def initialize(host, = {}) @host = host @port = [:port] || "9042" @prefix = [:prefix] @source = [:source] @database = [:database] @table = [:table] @interval = [:interval] || 60 @registry = [:registry] || Metriks::Registry.default @on_error = [:on_error] || proc { |ex| } end |
Instance Attribute Details
#prefix ⇒ Object
Returns the value of attribute prefix.
8 9 10 |
# File 'lib/metriks/reporter/cassandra.rb', line 8 def prefix @prefix end |
#source ⇒ Object
Returns the value of attribute source.
8 9 10 |
# File 'lib/metriks/reporter/cassandra.rb', line 8 def source @source end |
Instance Method Details
#close_connection ⇒ Object
106 107 108 |
# File 'lib/metriks/reporter/cassandra.rb', line 106 def close_connection connection.close end |
#connection ⇒ Object
two weeks
29 30 31 |
# File 'lib/metriks/reporter/cassandra.rb', line 29 def connection @connection end |
#execute_prepared_statement(array) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/metriks/reporter/cassandra.rb', line 114 def execute_prepared_statement array future = write_statement.async.execute(*array) future.on_failure do |error| raise error end end |
#open_connection ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/metriks/reporter/cassandra.rb', line 22 def open_connection @connection = Cql::Client.connect(host: @host) @connection.use(@database) @write_statement = @connection.prepare("INSERT INTO #{@table}(server,metric, time, v) VALUES (?, ?, ?, ?) USING TTL 1209600") # two weeks end |
#restart ⇒ Object
52 53 54 55 |
# File 'lib/metriks/reporter/cassandra.rb', line 52 def restart stop start end |
#send_metric(compound_name, metric, keys, snapshot_keys = []) ⇒ Object
109 110 111 112 113 |
# File 'lib/metriks/reporter/cassandra.rb', line 109 def send_metric(compound_name, metric, keys, snapshot_keys = []) keys.each do |key| execute_prepared_statement [@source,"#{compound_name}.#{key}","#{Time.now.to_i}",metric.send(key)] end end |
#start ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/metriks/reporter/cassandra.rb', line 32 def start @thread ||= Thread.new do loop do sleep @interval Thread.new do begin write rescue Exception => ex @on_error[ex] rescue nil end end end end end |
#stop ⇒ Object
47 48 49 50 |
# File 'lib/metriks/reporter/cassandra.rb', line 47 def stop @thread.kill if @thread @thread = nil end |
#write ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/metriks/reporter/cassandra.rb', line 57 def write open_connection @registry.each do |name, metric| case metric when Metriks::Meter send_metric name, metric, [ :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate ] when Metriks::Counter send_metric name, metric, [ :count ] metric.clear if metric.reset_on_submit when Metriks::Gauge send_metric name, metric, [ :value ] when Metriks::UtilizationTimer send_metric name, metric, [ :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate, :min, :max, :mean, :stddev, :one_minute_utilization, :five_minute_utilization, :fifteen_minute_utilization, :mean_utilization, ], [ :median, :get_95th_percentile ] when Metriks::Timer send_metric name, metric, [ :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate, :min, :max, :mean, :stddev ], [ :median, :get_95th_percentile ] metric.clear if metric.reset_on_submit when Metriks::Histogram send_metric name, metric, [ :count, :min, :max, :mean, :stddev ], [ :median, :get_95th_percentile ] end end sleep 2 close_connection end |
#write_statement ⇒ Object
120 121 122 |
# File 'lib/metriks/reporter/cassandra.rb', line 120 def write_statement @write_statement end |