Class: Bosh::Monitor::TsdbConnection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/bosh/monitor/protocols/tsdb.rb

Constant Summary collapse

BACKOFF_CEILING =
9
MAX_RETRIES =
35

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ TsdbConnection

Returns a new instance of TsdbConnection.



9
10
11
12
13
14
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 9

def initialize(host, port)
  @host = host
  @port = port
  @logger = Bhm.logger
  reset_retries
end

Instance Attribute Details

#retriesObject (readonly)

Returns the value of attribute retries.



7
8
9
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 7

def retries
  @retries
end

Instance Method Details

#connection_completedObject



31
32
33
34
35
36
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 31

def connection_completed
  reset_retries
  @reconnecting = false
  @connected = true
  @logger.info("Connected to TSDB server at #{@host}:#{@port}")
end

#increment_retriesObject



20
21
22
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 20

def increment_retries
  @retries += 1
end

#receive_data(data) ⇒ Object



63
64
65
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 63

def receive_data(data)
  @logger.info("[TSDB] << #{data.chomp}")
end

#reset_retriesObject



16
17
18
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 16

def reset_retries
  @retries = 0
end

#send_metric(name, timestamp, value, tags = {}) ⇒ Object



24
25
26
27
28
29
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 24

def send_metric(name, timestamp, value, tags = {})
  formatted_tags = tags.map { |tag| tag.join("=") }.sort.join(" ")
  command = "put #{name} #{timestamp} #{value} #{formatted_tags}\n"
  @logger.debug("[TSDB] >> #{command.chomp}")
  send_data(command)
end

#tsdb_reconnectObject



58
59
60
61
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 58

def tsdb_reconnect
  @logger.info("Trying to reconnect to TSDB server at #{@host}:#{@port} (#{retries})...")
  reconnect(@host, @port)
end

#unbindObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bosh/monitor/protocols/tsdb.rb', line 38

def unbind
  if @connected
    @logger.warn("Lost connection to TSDB server at #{@host}:#{@port}")
  end
  @connected = false

  retry_in = 2**[retries, BACKOFF_CEILING].min - 1
  increment_retries

  if retries > MAX_RETRIES
    raise "Failed to reconnect to TSDB after #{MAX_RETRIES} retries"
  end

  if retries > 1
    @logger.info("Failed to reconnect to TSDB, will try again in #{retry_in} seconds...")
  end

  EM.add_timer(retry_in) { tsdb_reconnect }
end