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.

Parameters:

  • url (+String+)

    statsmeter url.

  • token (+String+)

    authorization token.



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.



136
137
138
139
140
141
# File 'lib/statscloud/statsmeter_client.rb', line 136

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

#connectObject

Connects to the server and starts periodic sending events.



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

def connect
  Thread.new do
    connect_client
    start_plugins
    flush_events_loop.join
  end
end

#connected?Boolean

Shows statsmeter state.

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/statscloud/statsmeter_client.rb', line 130

def connected?
  return false unless @client
  @client.state == :connect
end

#flush_eventsObject

Sends all pending events to statscloud.



120
121
122
123
124
125
126
127
# File 'lib/statscloud/statsmeter_client.rb', line 120

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.

Parameters:

  • name (+String+)

    name of the event to record.

  • measurement (+Integer+) (defaults to: 0)

    optional measurement, depending on metrics type.



84
85
86
# File 'lib/statscloud/statsmeter_client.rb', line 84

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.

Parameters:

  • events (+Array+)

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



94
95
96
# File 'lib/statscloud/statsmeter_client.rb', line 94

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.

Parameters:

  • events (+Array+)

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/statscloud/statsmeter_client.rb', line 104

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