Class: StatsCloud::StatsmeterClient
- Inherits:
-
Object
- Object
- StatsCloud::StatsmeterClient
- Includes:
- EventHelper, LoggerHelper, 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.
-
#url ⇒ Object
readonly
Statsmeter cluster url is used to connect to cluster.
Instance Method Summary collapse
-
#close ⇒ Object
Stops socket.io connection.
-
#connect ⇒ 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, 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
Constructor Details
#initialize(url, token, tags = []) ⇒ StatsmeterClient
Initialize statsmeter client.
52 53 54 55 56 57 |
# File 'lib/statscloud/statsmeter_client.rb', line 52 def initialize(url, token, = []) 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
34 35 36 |
# File 'lib/statscloud/statsmeter_client.rb', line 34 def client @client end |
#event_name_size_in_bytes ⇒ Object
Binary size for metric names.
Type: Integer
44 45 46 |
# File 'lib/statscloud/statsmeter_client.rb', line 44 def event_name_size_in_bytes @event_name_size_in_bytes end |
#names_map ⇒ Object
Metric names.
Type: Hash
39 40 41 |
# File 'lib/statscloud/statsmeter_client.rb', line 39 def names_map @names_map end |
#url ⇒ Object (readonly)
Statsmeter cluster url is used to connect to cluster.
Type: String
29 30 31 |
# File 'lib/statscloud/statsmeter_client.rb', line 29 def url @url end |
Instance Method Details
#close ⇒ Object
Stops socket.io connection.
130 131 132 133 |
# File 'lib/statscloud/statsmeter_client.rb', line 130 def close client.disconnect if open? set_client_to_nil end |
#connect ⇒ Object
Connects to the server and starts periodic sending events.
60 61 62 63 64 65 |
# File 'lib/statscloud/statsmeter_client.rb', line 60 def connect Thread.new do connect_client(@url, @token) flush_events_loop.join if @client end end |
#connected? ⇒ Boolean
Shows statsmeter state.
125 126 127 |
# File 'lib/statscloud/statsmeter_client.rb', line 125 def connected? @client&.state == :connect end |
#flush_events ⇒ Object
Sends all pending events to statscloud.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/statscloud/statsmeter_client.rb', line 111 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 rescue StandardError => error logger.error error close connect.join end |
#record_event(name, measurement = 0) ⇒ Object
Records a single event.
Save event in pending events.
75 76 77 |
# File 'lib/statscloud/statsmeter_client.rb', line 75 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.
85 86 87 |
# File 'lib/statscloud/statsmeter_client.rb', line 85 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/statscloud/statsmeter_client.rb', line 95 def record_events_array(events) events.each do |event| name = get_event_name(event) measurement = get_event_measurement(event)&.to_f next unless @names_map && @names_map[name] binary_length = get_binary_length(@event_name_size_in_bytes) plain_length = get_plain_length(name, measurement) flush_events if flush_condition(binary_length, plain_length) record_binary_event(name, measurement, binary_length) record_plain_event(name, measurement, plain_length) end end |