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
Socket connection with statscloud cluster which is used to record events.
Type: SocketIO::Client::Simple
Binary size for metric names.
Type: Integer
Metric names.
Type: Hash
Statsmeter cluster url is used to connect to cluster.
Type: String
Public Class Methods
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
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
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
Shows statsmeter state.
# File lib/statscloud/statsmeter_client.rb, line 126 def connected? return false unless @client @client.state == :connect end
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
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
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
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
# File lib/statscloud/statsmeter_client.rb, line 162 def binary_packet(byte_array) socketio_client.as_byte_buffer(byte_array) end
# File lib/statscloud/statsmeter_client.rb, line 174 def crc32 Crc32 end
# 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
# 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
# File lib/statscloud/statsmeter_client.rb, line 158 def send_message(string_buffer) @client.emit :metric, binary_packet(string_buffer.buffer.bytes) end
# File lib/statscloud/statsmeter_client.rb, line 154 def start_plugins start_plugins_job(@plugins, @mutex) end
# File lib/statscloud/statsmeter_client.rb, line 170 def stop_eventmachine eventmachine.stop_event_loop if eventmachine.reactor_running? end