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



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



102
103
104
# File 'lib/metriks/reporter/cassandra.rb', line 102

def close_connection
  connection.close
end

#connectionObject



27
28
29
# File 'lib/metriks/reporter/cassandra.rb', line 27

def connection
  @connection
end

#open_connectionObject



22
23
24
25
26
# File 'lib/metriks/reporter/cassandra.rb', line 22

def open_connection
  @connection = Cql::Client.connect(host: @host, port: @port)
  @connection.use(@database)
  @connection
end

#restartObject



50
51
52
53
# File 'lib/metriks/reporter/cassandra.rb', line 50

def restart
  stop
  start
end

#send_metric(compound_name, metric, keys, snapshot_keys = []) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/metriks/reporter/cassandra.rb', line 105

def send_metric(compound_name, metric, keys, snapshot_keys = [])
  keys.each do |key|
    command = "INSERT INTO #{@table} (server,metric,time,v) VALUES ('#{@source}','#{compound_name}','#{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S+0000")}',#{metric.send(key)})"
    # puts command
    connection.execute command
  end
end

#startObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/metriks/reporter/cassandra.rb', line 30

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



45
46
47
48
# File 'lib/metriks/reporter/cassandra.rb', line 45

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

#writeObject



55
56
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
# File 'lib/metriks/reporter/cassandra.rb', line 55

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
      ]
    when Metriks::Histogram
      send_metric name, metric, [
        :count, :min, :max, :mean, :stddev
      ], [
        :median, :get_95th_percentile
      ]
    end
  end
  close_connection

end