Class: Metriks::Reporter::Cassandra

Inherits:
Object
  • Object
show all
Defined in:
lib/metriks/reporter/cassandra.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @host = host
  @port = options[:port] || "9042"
  @prefix = options[:prefix]
  @source = options[:source]
  @database = options[:database]
  @table = options[:table]
  @interval = options[:interval] || 60
  @registry  = options[:registry] || Metriks::Registry.default
  @on_error  = options[:on_error] || proc { |ex| }

end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



8
9
10
# File 'lib/metriks/reporter/cassandra.rb', line 8

def prefix
  @prefix
end

#sourceObject

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_connectionObject



106
107
108
# File 'lib/metriks/reporter/cassandra.rb', line 106

def close_connection
  connection.close
end

#connectionObject

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_connectionObject



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

#restartObject



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

#startObject



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

#stopObject



47
48
49
50
# File 'lib/metriks/reporter/cassandra.rb', line 47

def stop
  @thread.kill if @thread
  @thread = nil
end

#writeObject



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_statementObject



120
121
122
# File 'lib/metriks/reporter/cassandra.rb', line 120

def write_statement
  @write_statement
end