class StatsCloud::Client

Module which implements StatsCloud framework support.

Attributes

app[R]
cluster[R]
config[R]
graphite_url[R]
source_mappings[R]
statsmeter_client[R]
token[R]

Public Class Methods

new(environment = nil, client_tags = nil) click to toggle source

Creates new StatsCloud::Client instance with empty configuration.

@return instance of StatsCloud::Client

@api public

# 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

Public Instance Methods

client_tags() click to toggle source

Returns statscloud client tags.

@return [Array[String]]

@api public

# File lib/statscloud/statscloud_client.rb, line 167
def client_tags
  @tags
end
cluster_status() click to toggle source

Returns cluster status.

@return [Hash]

@api public

# 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() click to toggle source

Returns statscloud client environemnt.

@return [String]

@api public

# File lib/statscloud/statscloud_client.rb, line 158
def environment
  @env
end
meter() click to toggle source

Returns statscloud.io client aka Statsmeter client

@return instance of StatsCloud::StatsmeterClient

@api public

# File lib/statscloud/statscloud_client.rb, line 96
def meter
  @statsmeter_client
end
record_event(name, measurement = 0) click to toggle source

Records a single event.

@param [String] name

name of the event to record.

@param [Integer] measurement

optional measurement, depending on metrics type.

Calls statsmeter client record_event method.

@api public

# File lib/statscloud/statscloud_client.rb, line 110
def record_event(name, measurement = 0)
  @statsmeter_client&.record_event(name, measurement)
end
record_events(*events) click to toggle source

Records several events at once.

@param [Array] events

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

Calls statsmeter record_events method.

@api public

# File lib/statscloud/statscloud_client.rb, line 122
def record_events(*events)
  @statsmeter_client&.record_events(*events)
end
record_events_array(events) click to toggle source

Records an array of events at once.

@param [Array] events

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

Calls statsmeter client record_events_array method.

@api public

# File lib/statscloud/statscloud_client.rb, line 134
def record_events_array(events)
  @statsmeter_client&.record_events_array(events)
end
start(base_config = nil) click to toggle source

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

@param [+String] env

statsmeter.io cluster environment

@param [Hash] base_config

statsmeter.io configuration

@return [Thread]

@api public

# 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
stop() click to toggle source

Stops statscloud.io service.

@return nil

@api public

# 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) click to toggle source

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

@param [String] file

Main configuration file for StatsCloud::Client

@return instance of StatsCloud::Client

@api public

# File lib/statscloud/statscloud_client.rb, line 86
def with_config_file(file)
  update_config_file(file)
  self
end
with_environment(environment) click to toggle source

Configures Statscloud environment.

@param [String] env

Environment

@return instance of StatsCloud::Client

@api public

# File lib/statscloud/statscloud_client.rb, line 60
def with_environment(environment)
  config_environment(environment)
  self
end
with_tags(tags) click to toggle source

Configures Statscloud tags.

@param [Array] tags

List of tags

@return instance of StatsCloud::Client

@api public

# File lib/statscloud/statscloud_client.rb, line 73
def with_tags(tags)
  config_tags(tags)
  self
end

Private Instance Methods

check_cluster_status(cluster) click to toggle source
# File lib/statscloud/statscloud_client.rb, line 209
def check_cluster_status(cluster)
  raise error(cluster_error_message) if cluster.nil? || cluster["status"]["status"] == "ERROR"
end
configure_cluster() click to toggle source
# File lib/statscloud/statscloud_client.rb, line 200
def configure_cluster
  @cluster_client = create_cluster_client
  @cluster_client.deploy_cluster(@token, @app, @config)
  @cluster = @cluster_client.get_cluster(@token, @app).body
  @graphite_url = @cluster["graphiteUrl"]
  check_cluster_status(@cluster)
  log_successful_cluster_deploy
end
configure_statscloud_plugins() click to toggle source
# File lib/statscloud/statscloud_client.rb, line 196
def configure_statscloud_plugins
  @plugins = build_plugins(@config["plugins"])
end
connect_to_cluster() click to toggle source
# File lib/statscloud/statscloud_client.rb, line 213
def connect_to_cluster
  @statsmeter_client = create_statsmeter_client(@cluster, @token, @plugins, tags)
  @statsmeter_client.connect(register_connection_job)
end
create_cluster_client() click to toggle source
# File lib/statscloud/statscloud_client.rb, line 218
def create_cluster_client
  StatsCloud::ClusterClient.new(env, @config["endpoint"])
end
create_statsmeter_client(cluster, token, plugins, tags) click to toggle source
# File lib/statscloud/statscloud_client.rb, line 226
def create_statsmeter_client(cluster, token, plugins, tags)
  host = cluster["statsmeterUrl"]
  StatsCloud::StatsmeterClient.new(host, token, plugins, tags)
end
process_configuration(base_config) click to toggle source
# File lib/statscloud/statscloud_client.rb, line 189
def process_configuration(base_config)
  generate_configuration(base_config)
  collect_statscloud_assets(@config, @source_mappings)
  config_values
  clear_data
end
register_connection_job() click to toggle source
# File lib/statscloud/statscloud_client.rb, line 222
def register_connection_job
  StatsCloud::RegisterConnectionJob.new(@cluster_client, @token, @app, tags)
end