Class: StatsCloud::StatsmeterClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from PluginsHelper

#build_plugins, #start_plugins_job

Methods included from LoggerHelper

#logger

Constructor Details

#initialize(url, token, plugins, tags = []) ⇒ StatsmeterClient

Initialize statsmeter client.



59
60
61
62
63
64
65
# File 'lib/statscloud/statsmeter_client.rb', line 59

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

Instance Attribute Details

#clientObject

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

Type: SocketIO::Client::Simple



41
42
43
# File 'lib/statscloud/statsmeter_client.rb', line 41

def client
  @client
end

#event_name_size_in_bytesObject

Binary size for metric names.

Type: Integer



51
52
53
# File 'lib/statscloud/statsmeter_client.rb', line 51

def event_name_size_in_bytes
  @event_name_size_in_bytes
end

#names_mapObject

Metric names.

Type: Hash



46
47
48
# File 'lib/statscloud/statsmeter_client.rb', line 46

def names_map
  @names_map
end

#tagsObject (readonly)

Statscloud client tags.

Type: String



36
37
38
# File 'lib/statscloud/statsmeter_client.rb', line 36

def tags
  @tags
end

#urlObject (readonly)

Statsmeter cluster url is used to connect to cluster.

Type: String



31
32
33
# File 'lib/statscloud/statsmeter_client.rb', line 31

def url
  @url
end

Instance Method Details

#closeObject

Stops socket.io connection.



141
142
143
144
145
146
# File 'lib/statscloud/statsmeter_client.rb', line 141

def close
  stop_eventmachine
  client.auto_reconnection = false
  client.disconnect
  set_client_to_nil
end

#connect(register_connection_job) ⇒ Object

Connects to the server and starts periodic sending events.



68
69
70
71
72
73
74
75
# File 'lib/statscloud/statsmeter_client.rb', line 68

def connect(register_connection_job)
  Thread.new do
    connect_client
    start_plugins
    flush_events_loop
    register_connection_job.start
  end
end

#connected?Boolean

Shows statsmeter state.



134
135
136
137
138
# File 'lib/statscloud/statsmeter_client.rb', line 134

def connected?
  return false unless @client

  @client.state == :connect
end

#flush_eventsObject

Sends all pending events to statscloud.



122
123
124
125
126
127
128
129
130
131
# File 'lib/statscloud/statsmeter_client.rb', line 122

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) ⇒ 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
112
113
114
115
116
117
118
119
# File 'lib/statscloud/statsmeter_client.rb', line 105

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