Class: StatsCloud::StatsmeterClient
- Inherits:
-
Object
- Object
- StatsCloud::StatsmeterClient
- Includes:
- EventHelper, LoggerHelper, PluginsHelper, SocketIOHelper, StatsmeterHelper
- Defined in:
- lib/statscloud/statsmeter_client.rb
Overview
Client for Statsmeter.
Constant Summary collapse
- BINARY_BUFFER_SIZE =
Maximum size of pending binary events buffer.
1024
- PLAIN_BUFFER_SIZE =
Maximum size of pending binary events buffer.
1024 * 100
Instance Attribute Summary collapse
-
#client ⇒ Object
Socket connection with statscloud cluster which is used to record events.
-
#event_name_size_in_bytes ⇒ Object
Binary size for metric names.
-
#names_map ⇒ Object
Metric names.
-
#tags ⇒ Object
readonly
Statscloud client tags.
-
#url ⇒ Object
readonly
Statsmeter cluster url is used to connect to cluster.
Instance Method Summary collapse
-
#close ⇒ Object
Stops socket.io connection.
-
#connect(register_connection_job = nil) ⇒ Object
Connects to the server and starts periodic sending events.
-
#connected? ⇒ Boolean
Shows statsmeter state.
-
#flush_events ⇒ Object
Sends all pending events to statscloud.
-
#initialize(url, token, plugins, tags = []) ⇒ StatsmeterClient
constructor
Initialize statsmeter client.
-
#record_event(name, measurement = 0) ⇒ Object
Records a single event.
-
#record_events(*events) ⇒ Object
Records several events at once.
-
#record_events_array(events) ⇒ Object
Records an array of events at once.
Methods included from LoggerHelper
#log_error, #log_info, #logger
Constructor Details
#initialize(url, token, plugins, tags = []) ⇒ StatsmeterClient
Initialize statsmeter client.
58 59 60 61 62 63 64 |
# File 'lib/statscloud/statsmeter_client.rb', line 58 def initialize(url, token, plugins, = []) initialize_plugin_threads(plugins) set_config(url, token, ) set_client_to_nil set_pending_values set_socket_values end |
Instance Attribute Details
#client ⇒ Object
Socket connection with statscloud cluster which is used to record events.
Type: SocketIO::Client::Simple
40 41 42 |
# File 'lib/statscloud/statsmeter_client.rb', line 40 def client @client end |
#event_name_size_in_bytes ⇒ Object
Binary size for metric names.
Type: Integer
50 51 52 |
# File 'lib/statscloud/statsmeter_client.rb', line 50 def event_name_size_in_bytes @event_name_size_in_bytes end |
#names_map ⇒ Object
Metric names.
Type: Hash
45 46 47 |
# File 'lib/statscloud/statsmeter_client.rb', line 45 def names_map @names_map end |
#tags ⇒ Object (readonly)
Statscloud client tags.
Type: String
35 36 37 |
# File 'lib/statscloud/statsmeter_client.rb', line 35 def @tags end |
#url ⇒ Object (readonly)
Statsmeter cluster url is used to connect to cluster.
Type: String
30 31 32 |
# File 'lib/statscloud/statsmeter_client.rb', line 30 def url @url end |
Instance Method Details
#close ⇒ Object
Stops socket.io connection.
133 134 135 136 137 138 |
# 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) ⇒ Object
Connects to the server and starts periodic sending events.
67 68 69 70 71 72 73 74 75 |
# 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? ⇒ Boolean
Shows statsmeter state.
126 127 128 129 130 |
# File 'lib/statscloud/statsmeter_client.rb', line 126 def connected? return false unless @client @client.state == :connect end |
#flush_events ⇒ Object
Sends all pending events to statscloud.
114 115 116 117 118 119 120 121 122 123 |
# 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) @pending_binary_events set_pending_values end |
#record_event(name, measurement = 0) ⇒ Object
Records a single event.
Save event in pending events.
85 86 87 |
# File 'lib/statscloud/statsmeter_client.rb', line 85 def record_event(name, measurement = 0) record_events(name: name, measurement: measurement) end |
#record_events(*events) ⇒ Object
Records several events at once.
Save events in pending events.
95 96 97 |
# File 'lib/statscloud/statsmeter_client.rb', line 95 def record_events(*events) record_events_array(events) end |
#record_events_array(events) ⇒ Object
Records an array of events at once.
Save events in pending binary and plain arrays. Check flush condition.
105 106 107 108 109 110 111 |
# 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 |