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 LoggerHelper

#log_error, #log_info, #logger

Constructor Details

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

Initialize statsmeter client.

Parameters:

  • url (+String+)

    statsmeter url.

  • token (+String+)

    authorization token.



58
59
60
61
62
63
64
# 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

Instance Attribute Details

#clientObject

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_bytesObject

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_mapObject

Metric names.

Type: Hash



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

def names_map
  @names_map
end

#tagsObject (readonly)

Statscloud client tags.

Type: String



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

def tags
  @tags
end

#urlObject (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

#closeObject

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.

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/statscloud/statsmeter_client.rb', line 126

def connected?
  return false unless @client

  @client.state == :connect
end

#flush_eventsObject

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)
  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.



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.

Parameters:

  • events (+Array+)

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



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.

Parameters:

  • events (+Array+)

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



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