Class: StatsCloud::Client

Inherits:
Object
  • Object
show all
Includes:
AssetsHelper, LoggerHelper, PluginsHelper, StatsCloudHelper
Defined in:
lib/statscloud/statscloud_client.rb

Overview

Module which implements StatsCloud framework support.

Instance Method Summary collapse

Methods included from LoggerHelper

#log_error, #log_info, #logger

Constructor Details

#initialize(environment = nil, client_tags = nil) ⇒ Object

Creates new StatsCloud::Client instance with empty configuration.



26
27
28
29
# File 'lib/statscloud/statscloud_client.rb', line 26

def initialize(environment = nil, client_tags = nil)
  initialize_values(environment, client_tags)
  update_config_file(".statscloud.yml")
end

Instance Method Details

#client_tagsArray[+String+]

Returns statscloud client tags.

Returns:

  • (Array[+String+])


167
168
169
# File 'lib/statscloud/statscloud_client.rb', line 167

def client_tags
  @tags
end

#cluster_status+Hash+

Returns cluster status.

Returns:

  • (+Hash+)


143
144
145
146
147
148
149
150
151
# File 'lib/statscloud/statscloud_client.rb', line 143

def cluster_status
  return unless @cluster_client

  cluster = @cluster_client.get_cluster(@token, @app)&.body
  check_cluster_status(cluster)
  cluster["status"]["status"] if cluster
rescue StandardError => error
  log_error error
end

#environment+String+

Returns statscloud client environemnt.

Returns:

  • (+String+)


158
159
160
# File 'lib/statscloud/statscloud_client.rb', line 158

def environment
  @env
end

#meterObject

Returns statscloud.io client aka Statsmeter client

Returns:

  • instance of StatsCloud::StatsmeterClient



96
97
98
# File 'lib/statscloud/statscloud_client.rb', line 96

def meter
  @statsmeter_client
end

#record_event(name, measurement = 0) ⇒ Object

Records a single event.

Calls statsmeter client record_event method.

Parameters:

  • name (+String+)

    name of the event to record.

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

    optional measurement, depending on metrics type.



110
111
112
# File 'lib/statscloud/statscloud_client.rb', line 110

def record_event(name, measurement = 0)
  @statsmeter_client&.record_event(name, measurement)
end

#record_events(*events) ⇒ Object

Records several events at once.

Calls statsmeter record_events method.

Parameters:

  • events (+Array+)

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



122
123
124
# File 'lib/statscloud/statscloud_client.rb', line 122

def record_events(*events)
  @statsmeter_client&.record_events(*events)
end

#record_events_array(events) ⇒ Object

Records an array of events at once.

Calls statsmeter client record_events_array method.

Parameters:

  • events (+Array+)

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



134
135
136
# File 'lib/statscloud/statscloud_client.rb', line 134

def record_events_array(events)
  @statsmeter_client&.record_events_array(events)
end

#start(base_config = nil) ⇒ Thread

Configures statsmeter.io support for application and initializes a statscloud.io client.

Parameters:

  • env (+String)

    statsmeter.io cluster environment

  • base_config (+Hash+) (defaults to: nil)

    statsmeter.io configuration

Returns:

  • (Thread)


42
43
44
45
46
47
48
49
50
# File 'lib/statscloud/statscloud_client.rb', line 42

def start(base_config = nil)
  initialize_values
  process_configuration(base_config)
  configure_statscloud_plugins
  configure_cluster
  connect_to_cluster
rescue StandardError => error
  log_error error
end

#stopObject

Stops statscloud.io service.

Returns:

  • nil



176
177
178
179
180
181
182
183
# File 'lib/statscloud/statscloud_client.rb', line 176

def stop
  @statsmeter_client&.close
  @statsmeter_client = nil
  @cluster_client&.undeploy_cluster(@token, @app)
  @cluster_client = nil
rescue StandardError => error
  log_error error
end

#with_config_file(file) ⇒ Object

Allows to start several instances of StatsCloud::Client with different confifurations.

Parameters:

  • file (+String+)

    Main configuration file for StatsCloud::Client

Returns:

  • instance of StatsCloud::Client



86
87
88
89
# File 'lib/statscloud/statscloud_client.rb', line 86

def with_config_file(file)
  update_config_file(file)
  self
end

#with_environment(environment) ⇒ Object

Configures Statscloud environment.

Parameters:

  • env (+String+)

    Environment

Returns:

  • instance of StatsCloud::Client



60
61
62
63
# File 'lib/statscloud/statscloud_client.rb', line 60

def with_environment(environment)
  config_environment(environment)
  self
end

#with_tags(tags) ⇒ Object

Configures Statscloud tags.

Parameters:

  • tags (+Array+)

    List of tags

Returns:

  • instance of StatsCloud::Client



73
74
75
76
# File 'lib/statscloud/statscloud_client.rb', line 73

def with_tags(tags)
  config_tags(tags)
  self
end