class StatsCloud::StatsmeterClient

Client for Statsmeter.

Constants

BINARY_BUFFER_SIZE

Maximum size of pending binary events buffer.

PLAIN_BUFFER_SIZE

Maximum size of pending binary events buffer.

Attributes

client[RW]

Socket connection with statscloud cluster which is used to record events.

Type: SocketIO::Client::Simple

event_name_size_in_bytes[RW]

Binary size for metric names.

Type: Integer

names_map[RW]

Metric names.

Type: Hash

tags[R]

Statscloud client tags.

Type: String

url[R]

Statsmeter cluster url is used to connect to cluster.

Type: String

Public Class Methods

new(url, token, plugins, tags = []) click to toggle source

Initialize statsmeter client.

@param [String] url

statsmeter url.

@param [String] token

authorization token.
# File lib/statscloud/statsmeter_client.rb, line 58
def initialize(url, token, plugins, tags = [])
  initialize_plugin_threads(plugins)
  set_config(url, token, tags)
  set_client_to_nil
  set_pending_values
  set_socket_values
end

Public Instance Methods

close() click to toggle source

Stops socket.io connection.

# File lib/statscloud/statsmeter_client.rb, line 133
def close
  stop_eventmachine
  client&.auto_reconnection = false
  client&.disconnect
  set_client_to_nil
end
connect(register_connection_job = nil) click to toggle source

Connects to the server and starts periodic sending events.

# File lib/statscloud/statsmeter_client.rb, line 67
def connect(register_connection_job = nil)
  @register_connection_job ||= register_connection_job
  Thread.new do
    connect_client
    start_plugins
    flush_events_loop
    @register_connection_job&.start
  end
end
connected?() click to toggle source

Shows statsmeter state.

# File lib/statscloud/statsmeter_client.rb, line 126
def connected?
  return false unless @client

  @client.state == :connect
end
flush_events() click to toggle source

Sends all pending events to statscloud.

# File lib/statscloud/statsmeter_client.rb, line 114
def flush_events
  return if @pending_binary_offset.zero?

  checksum = crc32.calculate(@pending_plain_events.buffer, @pending_plain_offset, 0)
  return if checksum.zero?

  @pending_binary_events.writeInt32BE(checksum, @pending_binary_offset)
  send_message @pending_binary_events
  set_pending_values
end
record_event(name, measurement = 0) click to toggle source

Records a single event.

@param [String] name

name of the event to record.

@param [Integer] measurement

optional measurement, depending on metrics type.

Save event in pending events.

# File lib/statscloud/statsmeter_client.rb, line 85
def record_event(name, measurement = 0)
  record_events(name: name, measurement: measurement)
end
record_events(*events) click to toggle source

Records several events at once.

@param [Array] events

events to send (each should have name and optional measurement fields).

Save events in pending events.

# File lib/statscloud/statsmeter_client.rb, line 95
def record_events(*events)
  record_events_array(events)
end
record_events_array(events) click to toggle source

Records an array of events at once.

@param [Array] events

array of events to send (each shoud have name and optional measurement fields).

Save events in pending binary and plain arrays. Check flush condition.

# File lib/statscloud/statsmeter_client.rb, line 105
def record_events_array(events)
  events.each do |event|
    process_single_event(event)
  end
rescue StandardError => error
  log_error error
end

Private Instance Methods

binary_packet(byte_array) click to toggle source
# File lib/statscloud/statsmeter_client.rb, line 162
def binary_packet(byte_array)
  socketio_client.as_byte_buffer(byte_array)
end
crc32() click to toggle source
# File lib/statscloud/statsmeter_client.rb, line 174
def crc32
  Crc32
end
flush_condition(binary, plain) click to toggle source
# File lib/statscloud/statsmeter_client.rb, line 166
def flush_condition(binary, plain)
  @pending_binary_offset + binary > BINARY_BUFFER_SIZE || @pending_plain_offset + plain > PLAIN_BUFFER_SIZE
end
flush_events_loop() click to toggle source
# File lib/statscloud/statsmeter_client.rb, line 142
def flush_events_loop
  Thread.new do
    eventmachine.run do
      eventmachine.add_periodic_timer(0.5) do
        @mutex.synchronize do
          connected? ? flush_events : connect.join
        end
      end
    end
  end
end
send_message(string_buffer) click to toggle source
# File lib/statscloud/statsmeter_client.rb, line 158
def send_message(string_buffer)
  @client.emit :metric, binary_packet(string_buffer.buffer.bytes)
end
start_plugins() click to toggle source
# File lib/statscloud/statsmeter_client.rb, line 154
def start_plugins
  start_plugins_job(@plugins, @mutex)
end
stop_eventmachine() click to toggle source
# File lib/statscloud/statsmeter_client.rb, line 170
def stop_eventmachine
  eventmachine.stop_event_loop if eventmachine.reactor_running?
end