Class: Metriks::Reporter::Opentsdb

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options = {}) ⇒ Opentsdb

Returns a new instance of Opentsdb.



9
10
11
12
13
14
15
16
17
18
# File 'lib/metriks/reporter/opentsdb.rb', line 9

def initialize(host, port, options = {})
  @host = host
  @port = port
  @prefix = options[:prefix]
  @source = options[:source]
  @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.



7
8
9
# File 'lib/metriks/reporter/opentsdb.rb', line 7

def prefix
  @prefix
end

#sourceObject

Returns the value of attribute source.



7
8
9
# File 'lib/metriks/reporter/opentsdb.rb', line 7

def source
  @source
end

Instance Method Details

#close_connectionObject



97
98
99
100
101
102
# File 'lib/metriks/reporter/opentsdb.rb', line 97

def close_connection
  if connection.ready?
   raise "Error while pushing data: #{connection.gets}"
  end
  connection.close
end

#connectionObject



22
23
24
# File 'lib/metriks/reporter/opentsdb.rb', line 22

def connection
  @connection
end

#open_connectionObject



19
20
21
# File 'lib/metriks/reporter/opentsdb.rb', line 19

def open_connection
  @connection = TCPSocket.new(@host, @port)
end

#restartObject



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

def restart
  stop
  start
end

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



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/metriks/reporter/opentsdb.rb', line 103

def send_metric(compound_name, metric, keys, snapshot_keys = [])
  name, tags = compound_name.split("#")
  if keys.size == 1
    # puts "put #{name} #{Time.now.to_i} #{metric.send(keys.first)} #{tags}"
    connection.puts("put #{name} #{Time.now.to_i} #{metric.send(keys.first)} #{tags}")
  else
    keys.each do |key|
      #puts "put #{name}.#{key} #{Time.now.to_i} #{metric.send(key)} #{tags}"
      connection.puts("put #{name}.#{key} #{Time.now.to_i} #{metric.send(key)} #{tags}")
    end
  end
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/metriks/reporter/opentsdb.rb', line 25

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



40
41
42
43
# File 'lib/metriks/reporter/opentsdb.rb', line 40

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

#writeObject



50
51
52
53
54
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
# File 'lib/metriks/reporter/opentsdb.rb', line 50

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