Class: Sensu::Extension::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/extensions/graphite.rb

Overview

EndPoint An endpoint is a backend metric store. This is a compositional object to help keep the rest of the code sane.

Constant Summary collapse

MAX_QUEUE_SIZE =

EM::Connection.send_data batches network connection writes in 16KB We should start out by having all data in the queue flush in the space of a single loop tick.

2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, host, port) ⇒ Endpoint

Returns a new instance of Endpoint.



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sensu/extensions/graphite.rb', line 134

def initialize(name, host, port)
  @queue = []
  @connection = EM.connect(host, port, ConnectionHandler)
  @connection.name = name
  @connection.host = host
  @connection.port = port
  @connection.message_queue = @queue
  EventMachine::PeriodicTimer.new(60) do
    Sensu::Logger.get.info("Graphite: queue size for #{name}: #{queue_length}")
  end
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



132
133
134
# File 'lib/sensu/extensions/graphite.rb', line 132

def connection
  @connection
end

#queueObject

Returns the value of attribute queue.



132
133
134
# File 'lib/sensu/extensions/graphite.rb', line 132

def queue
  @queue
end

Instance Method Details

#flush_to_netObject



152
153
154
155
# File 'lib/sensu/extensions/graphite.rb', line 152

def flush_to_net
  sent = @connection.send_data(@queue.join)
  @queue = [] if sent > 0
end

#queue_lengthObject



146
147
148
149
150
# File 'lib/sensu/extensions/graphite.rb', line 146

def queue_length
  @queue
    .map(&:bytesize)
    .reduce(:+) || 0
end

#relay_event(data) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'lib/sensu/extensions/graphite.rb', line 157

def relay_event(data)
  if @connection.connected
    @queue << data
    if queue_length >= MAX_QUEUE_SIZE
      flush_to_net
      Sensu::Logger.get.debug('Graphite: successfully flushed to network')
    end
  end
end

#stopObject



167
168
169
170
171
172
173
# File 'lib/sensu/extensions/graphite.rb', line 167

def stop
  if @connection.connected
    flush_to_net
    #@connection.close_connection_after_writing
    @connection.close_connection # Force connection closing, do not wait.
  end
end