statscloud 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 975399b879c7b5d75bce48b4318eb6d2c8a15a84e12b3b88a91ba11780a5fba7
4
- data.tar.gz: 9a89d250087c614d37c8ce2b6e25fff3b7bbc9d4fecbf52b0c2839211a75ba58
3
+ metadata.gz: eb4636c428d2bdf3e787989de374e1419343363ee91b1abc3dd767dc4f4644a8
4
+ data.tar.gz: 07666f855bad5d68797f24a2342b17875cfa5e99627ab10fe6f51e8995e1805a
5
5
  SHA512:
6
- metadata.gz: cf9e39a86f33e9fc6fa960470303eefc15a5f9e89ff16648fe104a5ff7eb734367e6e319d1c0c3b62fa0c69e01c4f473c27d8b617fd7bb78a9b992bd4625e82b
7
- data.tar.gz: e244c81a7dd520fbec1c3f2c3614fcd26e8a4656b27bfe45bdf6b6d0ffe51e4f7020c88d919c6182495c96ed5d448b27cc3ad64d1a5a7164e9466f84c3f3f7d7
6
+ metadata.gz: 750eb1480dd06068bccfcd0bd54a783094c87e212dc04c957faa5b8f8c6cc6e2547628b3d6ea99203a7e629d709751d08b853e555f2402b82c676119b1318690
7
+ data.tar.gz: 4f1f670bff40b26dd8c906afba20cd041ec9fba6bf53b77f5a2b769d6c086b7c849f15baf75cfa30c0bccc29b7823f9a0dcba3f63def7d7c075f3e7478a93d34
data/.rubocop.yml CHANGED
@@ -1,22 +1,6 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.3
3
3
 
4
- Layout/ExtraSpacing:
5
- Exclude:
6
- - 'statscloud.gemspec'
7
-
8
- Layout/LeadingBlankLines:
9
- Exclude:
10
- - 'statscloud.gemspec'
11
-
12
- Layout/SpaceAroundOperators:
13
- Exclude:
14
- - 'statscloud.gemspec'
15
-
16
- Layout/SpaceInsideBlockBraces:
17
- Exclude:
18
- - 'Gemfile'
19
-
20
4
  Metrics/BlockLength:
21
5
  Exclude:
22
6
  - 'spec/**/*'
@@ -36,37 +20,12 @@ Style/Documentation:
36
20
  - 'spec/**/*'
37
21
  - 'test/**/*'
38
22
 
39
- Style/ExpandPathArguments:
40
- Exclude:
41
- - 'statscloud.gemspec'
42
-
43
- Style/HashSyntax:
44
- EnforcedStyle: ruby19
45
-
46
23
  Style/MutableConstant:
47
24
  Exclude:
48
25
  - 'lib/statscloud/version.rb'
49
26
 
50
- Style/PercentLiteralDelimiters:
51
- Exclude:
52
- - 'statscloud.gemspec'
53
-
54
27
  Style/StringLiterals:
55
- Exclude:
56
- - 'Gemfile'
57
- - 'Rakefile'
58
- - 'bin/console'
59
- - 'lib/statscloud/version.rb'
60
- - 'spec/spec_helper.rb'
61
- - 'spec/statscloud_client_spec.rb'
62
- - 'statscloud.gemspec'
63
-
64
- Style/FrozenStringLiteralComment:
65
- Exclude:
66
- - 'lib/templates/statscloud/statscloud.rb'
67
- Style/UnneededPercentQ:
68
- Exclude:
69
- - 'statscloud.gemspec'
28
+ EnforcedStyle: double_quotes
70
29
 
71
30
  Metrics/LineLength:
72
31
  Max: 120
data/Gemfile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  # Specify your gem's dependencies in statscloud.gemspec
8
8
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- statscloud (1.0.2)
4
+ statscloud (1.0.4)
5
5
  activesupport (>= 5.0.0.1)
6
6
  crc32 (~> 1.0.1)
7
7
  eventmachine (~> 1.2)
@@ -9,17 +9,17 @@ PATH
9
9
  leon (~> 1.1)
10
10
  logger (~> 1.2)
11
11
  rest-client (~> 2.0.2)
12
- statscloud.io-ruby-socket.io-client-simple (~> 1.2.1.pre.2)
12
+ statscloud.io-ruby-socket.io-client-simple (~> 1.2.1.pre.3)
13
13
 
14
14
  GEM
15
15
  remote: https://rubygems.org/
16
16
  specs:
17
- activesupport (5.2.1)
17
+ activesupport (5.2.1.1)
18
18
  concurrent-ruby (~> 1.0, >= 1.0.2)
19
19
  i18n (>= 0.7, < 2)
20
20
  minitest (~> 5.1)
21
21
  tzinfo (~> 1.1)
22
- concurrent-ruby (1.0.5)
22
+ concurrent-ruby (1.1.3)
23
23
  crc32 (1.0.1)
24
24
  diff-lcs (1.3)
25
25
  domain_name (0.5.20180417)
@@ -29,7 +29,8 @@ GEM
29
29
  fileutils (1.1.0)
30
30
  http-cookie (1.0.3)
31
31
  domain_name (~> 0.5)
32
- httparty (0.16.2)
32
+ httparty (0.16.3)
33
+ mime-types (~> 3.0)
33
34
  multi_xml (>= 0.5.2)
34
35
  i18n (1.1.1)
35
36
  concurrent-ruby (~> 1.0)
@@ -60,7 +61,7 @@ GEM
60
61
  diff-lcs (>= 1.2.0, < 2.0)
61
62
  rspec-support (~> 3.8.0)
62
63
  rspec-support (3.8.0)
63
- statscloud.io-ruby-socket.io-client-simple (1.2.1.pre.2)
64
+ statscloud.io-ruby-socket.io-client-simple (1.2.1.pre.3)
64
65
  event_emitter
65
66
  httparty
66
67
  json
@@ -1,25 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails/generators/base'
3
+ require "rails/generators/base"
4
4
 
5
5
  module StatsCloud
6
6
  # Rails generator for statscloud configuration.
7
7
  class InstallGenerator < Rails::Generators::Base
8
- source_root File.expand_path('../../templates/statscloud', __dir__)
8
+ source_root File.expand_path("../../templates/statscloud", __dir__)
9
9
 
10
10
  # Creates rails initializer for simultaneous start with rails server.
11
11
  def create_initializer_file
12
- copy_file 'statscloud_template.rb', File.join('config', 'initializers', 'statscloud.rb')
12
+ copy_file "statscloud_template.rb", File.join("config", "initializers", "statscloud.rb")
13
13
  end
14
14
 
15
15
  # Creates directory for saving metrics config files.
16
16
  def create_config_statscloud_directory
17
- empty_directory File.join('config', 'statscloud')
17
+ empty_directory File.join("config", "statscloud")
18
18
  end
19
19
 
20
20
  # Creates statscloud configuration file.
21
21
  def create_config_file
22
- copy_file '.statscloud_template.yml', '.statscloud.yml'
22
+ copy_file ".statscloud_template.yml", ".statscloud.yml"
23
23
  end
24
24
  end
25
25
  end
data/lib/statscloud.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'statscloud/statscloud_client'
4
- require 'statscloud/helpers/logger_helper'
3
+ require "statscloud/statscloud_client"
4
+ require "statscloud/helpers/logger_helper"
5
5
 
6
6
  # StatsCloud.io ruby client
7
7
  module StatsCloud
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './helpers/parsed_response_helper'
4
- require_relative './helpers/logger_helper'
5
- require 'rest-client'
3
+ require_relative "./helpers/parsed_response_helper"
4
+ require_relative "./helpers/logger_helper"
5
+ require "rest-client"
6
6
 
7
7
  module StatsCloud
8
8
  # statscloud.io cluster-api service client.
@@ -18,7 +18,7 @@ module StatsCloud
18
18
  # cluster api host.
19
19
  #
20
20
  # @api public
21
- def initialize(env, host = 'https://cluster-api-v1.statscloud.statscloud.io')
21
+ def initialize(env, host = "https://cluster-api-v1.statscloud.statscloud.io")
22
22
  @environment = env
23
23
  @host = host
24
24
  end
@@ -90,7 +90,7 @@ module StatsCloud
90
90
 
91
91
  def auth_headers(token)
92
92
  {
93
- 'auth-token' => token
93
+ "auth-token" => token
94
94
  }
95
95
  end
96
96
 
@@ -107,9 +107,5 @@ module StatsCloud
107
107
  def http_client
108
108
  RestClient
109
109
  end
110
-
111
- def logger
112
- @logger ||= ::StatsCloud.logger
113
- end
114
110
  end
115
111
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StatsCloud
4
+ ClientError = Class.new(StandardError)
5
+ end
@@ -11,7 +11,7 @@ module StatsCloud
11
11
  process_match(includes, filename) && !process_match(excludes, filename)
12
12
  end
13
13
 
14
- def collect_statscloud_assets(config, source_mappings, dir = '.')
14
+ def collect_statscloud_assets(config, source_mappings, dir = ".")
15
15
  asset_selector = get_checked_asset_selector(config)
16
16
  process_files(dir, config, source_mappings, asset_selector)
17
17
  end
@@ -120,7 +120,7 @@ module StatsCloud
120
120
 
121
121
  def value_fields
122
122
  %w[token application environment endpoint assetSelector propagateErrors
123
- passwordProtect retention flushIntervalInSeconds diskSize]
123
+ passwordProtect retention flushIntervalInSeconds diskSize plugins]
124
124
  end
125
125
  end
126
126
  end
@@ -1,15 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'fileutils'
3
+ require "fileutils"
4
4
 
5
5
  module StatsCloud
6
6
  # This helper works to help log errors and info about StatsCloud service work.
7
7
  module LoggerHelper
8
8
  # Log information about work to standart ouput
9
9
  def logger
10
- file_utils.mkdir_p 'log'
11
- @logger ||= logger_servise.new(File.join('log', 'statscloud.log')).tap do |log|
12
- log.progname = 'StatsCloud'
10
+ file_utils.mkdir_p "log"
11
+ @logger ||= logger_servise.new(File.join("log", "statscloud.log")).tap do |log|
12
+ log.progname = "StatsCloud"
13
13
  end
14
14
  end
15
15
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
- require 'active_support/all'
3
+ require "json"
4
+ require "active_support/all"
5
5
 
6
6
  module StatsCloud
7
7
  # This helper works to parse response from the server to convenient object.
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../errors/client_error"
4
+ require_relative "logger_helper"
5
+
6
+ module StatsCloud
7
+ # This helper configures StatsCloud plugins.
8
+ module PluginsHelper
9
+ def check_plugins(plugins)
10
+ plugins&.each do |plugin|
11
+ plugin[:class_name] = check_plugin_gem_by_name(plugin_class_name_from_plugin(plugin))
12
+ end
13
+ end
14
+
15
+ def start_plugins_job(plugins, mutex)
16
+ plugins&.each do |plugin|
17
+ start_plugin(plugin, mutex)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def start_plugin(plugin, mutex)
24
+ plugin[:class_name].start(self, mutex, plugin["settings"])
25
+ end
26
+
27
+ def check_plugin_gem_by_name(plugin_name)
28
+ Object.const_get "StatsCloud::Plugin::#{plugin_name}"
29
+ rescue NameError
30
+ raise statscloud_error(no_plugin_error_message(plugin_name))
31
+ end
32
+
33
+ def no_plugin_error_message(plugin_name)
34
+ "Can not find StatsCloud::Plugin::#{plugin_name}. Please require gem to load this plugin"
35
+ end
36
+
37
+ def plugin_class_name_from_plugin(plugin)
38
+ plugin_name = get_plugin_name(plugin)
39
+ plugin_name.split("-").map(&:capitalize).join
40
+ end
41
+
42
+ def get_plugin_name(plugin)
43
+ plugin.is_a?(Hash) ? plugin["name"] : plugin
44
+ end
45
+
46
+ def statscloud_error(message)
47
+ logger.error message
48
+ StatsCloud::ClientError.new(message)
49
+ end
50
+ end
51
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './logger_helper'
3
+ require_relative "logger_helper"
4
4
 
5
5
  module StatsCloud
6
6
  # This helper works to create socket connection.
@@ -13,19 +13,19 @@ module StatsCloud
13
13
  !@client.nil? && @client.open?
14
14
  end
15
15
 
16
- def connect_client(url, token)
16
+ def connect_client
17
17
  eventmachine.run do
18
18
  eventmachine.add_periodic_timer(5) do
19
- try_connect(url, token)
20
- eventmachine.add_timer(3) { send_tags && eventmachine.stop if connected? }
19
+ try_connect
20
+ eventmachine.add_timer(3) { eventmachine.stop if connected? }
21
21
  end
22
- eventmachine.add_timer(300.01) { stop_machine unless @client }
23
22
  end
24
23
  end
25
24
 
26
- def try_connect(url, token)
27
- @client = socketio_client.connect url, path: '/ws', 'auth-token' => token
25
+ def try_connect
26
+ @client = socketio_client.connect @url, path: "/ws", "auth-token" => @token
28
27
  listen_connect
28
+ listen_disconnect
29
29
  listen_events
30
30
  listen_errors
31
31
  rescue StandardError => error
@@ -37,7 +37,16 @@ module StatsCloud
37
37
  this = self
38
38
 
39
39
  @client.on :connect do
40
- this.logger.info 'StatsCloud client has connected to StatsCloud server'
40
+ emit :tags, this.tags
41
+ this.logger.info "StatsCloud client has connected to StatsCloud server"
42
+ end
43
+ end
44
+
45
+ def listen_disconnect
46
+ this = self
47
+
48
+ @client.on :disconnect do
49
+ this.logger.error "StatsCloud client disconnected from the StatsCloud server"
41
50
  end
42
51
  end
43
52
 
@@ -59,15 +68,6 @@ module StatsCloud
59
68
  end
60
69
  end
61
70
 
62
- def send_tags
63
- @client.emit :tags, @tags
64
- end
65
-
66
- def stop_machine
67
- eventmachine.stop
68
- Thread.stop
69
- end
70
-
71
71
  def eventmachine
72
72
  EM
73
73
  end
@@ -1,12 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'leon'
3
+ require "leon"
4
+ require "active_support/core_ext"
5
+ require_relative "../errors/client_error"
4
6
 
5
7
  module StatsCloud
6
8
  # This helper configures StatsCloud::Client.
7
9
  module StatsCloudHelper
8
10
  private
9
11
 
12
+ def validate_configuration
13
+ @app = @config["application"]
14
+ raise error(app_name_missing_error_message) if @app.blank?
15
+ end
16
+
10
17
  def config_environment(env)
11
18
  @env = env
12
19
  end
@@ -16,25 +23,45 @@ module StatsCloud
16
23
  end
17
24
 
18
25
  def config_token
19
- @token = @config['token'] || ENV['STATSCLOUD_AUTH_TOKEN']
26
+ @token = @config["token"] || ENV["STATSCLOUD_AUTH_TOKEN"]
20
27
  end
21
28
 
22
29
  def env
23
- @env ||= @config['environment'] || ENV['RAILS_ENV'] || 'default'
30
+ @env ||= @config["environment"] || ENV["RAILS_ENV"] || "default"
24
31
  end
25
32
 
26
33
  def tags
27
- @tags ||= @config['tags'] || [os.gethostname]
34
+ @tags ||= @config["tags"] || [os.gethostname]
35
+ end
36
+
37
+ def error(message = error_message)
38
+ stop
39
+ statscloud_error(message)
40
+ rescue StandardError
41
+ statscloud_error(message)
42
+ end
43
+
44
+ def cluster_error_message
45
+ return "Unable to get information about cluster." unless @cluster
46
+ @cluster[:status][:error]
47
+ end
48
+
49
+ def app_name_missing_error_message
50
+ "Unable to launch StatsCloud. Please configure application_name in path/to/config/file.yml."
28
51
  end
29
52
 
30
53
  def error_message
31
- "statscloud.io #{@app} cluster have failed to deploy. #{@cluster[:status][:error]}"
54
+ "statscloud.io #{@app} cluster have failed to deploy. #{cluster_error_message}"
32
55
  end
33
56
 
34
57
  def successful_message
35
58
  "statscloud.io support configured, dashboard URLs are \n #{(@cluster[:grafanaDashboardsUrls] || []).join("\n")}"
36
59
  end
37
60
 
61
+ def statscloud_error(message)
62
+ StatsCloud::ClientError.new(message)
63
+ end
64
+
38
65
  def os
39
66
  Socket
40
67
  end
@@ -1,16 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'leon'
3
+ require "leon"
4
4
 
5
5
  module StatsCloud
6
6
  # This helper configures StatsmeterClient.
7
7
  module StatsmeterHelper
8
8
  private
9
9
 
10
+ def initialize_plugin_threads(plugins)
11
+ @plugins = plugins
12
+ @mutex = mutex_service
13
+ end
14
+
10
15
  def set_config(url, token, tags)
11
16
  @url = url
12
17
  @token = token
13
- @tags = tags.map { |t| t.gsub(/[^a-z0-9_-]/, '_') }
18
+ @tags = tags.map { |t| t.gsub(/[^a-z0-9_-]/, "_") }
14
19
  end
15
20
 
16
21
  def set_client_to_nil
@@ -30,7 +35,8 @@ module StatsCloud
30
35
  end
31
36
 
32
37
  def record_binary_event(name, measurement, binary_length)
33
- @pending_binary_events.writeInt8(@names_map[name], @pending_binary_offset)
38
+ @pending_binary_events.writeInt32LE(@names_map[name], @pending_binary_offset)
39
+ @pending_binary_events = @pending_binary_events.slice(0, @pending_binary_offset + @event_name_size_in_bytes)
34
40
  measurement.zero? ? record_binary_zero : record_binary_measurement(measurement)
35
41
  @pending_binary_offset += binary_length
36
42
  end
@@ -58,13 +64,17 @@ module StatsCloud
58
64
  def record_plain_event(name, measurement, plain_length)
59
65
  @pending_plain_events.write(name, @pending_plain_offset)
60
66
  if measurement
61
- @pending_plain_events.write(',', @pending_plain_offset + name.length)
67
+ @pending_plain_events.write(",", @pending_plain_offset + name.length)
62
68
  measurement.zero? ? record_plain_zero(name) : record_plain_measurement(measurement, name)
63
69
  end
64
- @pending_plain_events.write(';', @pending_plain_offset + plain_length - 1)
70
+ @pending_plain_events.write(";", @pending_plain_offset + plain_length - 1)
65
71
  @pending_plain_offset += plain_length
66
72
  end
67
73
 
74
+ def mutex_service
75
+ Mutex.new
76
+ end
77
+
68
78
  def buffer
69
79
  ::LEON::StringBuffer
70
80
  end
@@ -1,23 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'yaml'
4
- require 'json'
5
- require 'eventmachine'
6
- require 'singleton'
7
- require 'socket'
8
- require_relative 'version'
9
- require_relative 'statsmeter_client'
10
- require_relative 'cluster_client'
11
- require_relative 'helpers/assets_helper'
12
- require_relative 'helpers/logger_helper'
13
- require_relative 'helpers/statscloud_helper'
3
+ require "yaml"
4
+ require "json"
5
+ require "eventmachine"
6
+ require "singleton"
7
+ require "socket"
8
+ require_relative "version"
9
+ require_relative "statsmeter_client"
10
+ require_relative "cluster_client"
11
+ require_relative "helpers/assets_helper"
12
+ require_relative "helpers/logger_helper"
13
+ require_relative "helpers/statscloud_helper"
14
14
 
15
15
  module StatsCloud
16
16
  # Module which implements StatsCloud framework support.
17
17
  class Client
18
18
  include Singleton
19
- include AssetsHelper
20
- include LoggerHelper
19
+ include StatsCloud::AssetsHelper
20
+ include StatsCloud::LoggerHelper
21
+ include StatsCloud::PluginsHelper
21
22
  include StatsCloud::StatsCloudHelper
22
23
 
23
24
  # Configures statsmeter.io support for application and initializes a statscloud.io client.
@@ -37,6 +38,7 @@ module StatsCloud
37
38
  collect_statscloud_assets(@config, @source_mappings)
38
39
  config_values
39
40
  clear_data
41
+ configure_statscloud_plugins
40
42
  configure_cluster
41
43
  connect_to_cluster
42
44
  end
@@ -74,6 +76,7 @@ module StatsCloud
74
76
  def cluster_status
75
77
  return unless @cluster_client
76
78
  cluster = @cluster_client.get_cluster(@token, @app)&.body
79
+ check_cluster_status(cluster)
77
80
  cluster[:status][:status] if cluster
78
81
  end
79
82
 
@@ -94,28 +97,33 @@ module StatsCloud
94
97
  attr_reader :config, :source_mappings, :statsmeter_client, :cluster, :token, :app, :graphite_url
95
98
 
96
99
  def initialize_values
100
+ stop
97
101
  @config = {}
98
102
  @source_mappings = { metrics: [], admins: [], dashboards: [], alerts: [] }
99
103
  end
100
104
 
101
105
  def generate_configuration(base_config)
102
106
  if base_config
103
- join_configs(@config, base_config, @source_mappings, '')
107
+ join_configs(@config, base_config, @source_mappings, "")
104
108
  else
105
- base_config = get_config_from_file(File.join(__dir__, '.statscloud.yml'))
106
- join_configs(@config, base_config, @source_mappings, '.statscloud.yml')
109
+ base_config = get_config_from_file(File.join(__dir__, ".statscloud.yml"))
110
+ join_configs(@config, base_config, @source_mappings, ".statscloud.yml")
107
111
  end
108
112
  end
109
113
 
110
114
  def config_values
111
- @config['sourceMaps'] = source_mappings
115
+ @config["sourceMaps"] = source_mappings
112
116
  config_token
113
- @app = @config['application']
117
+ validate_configuration
114
118
  end
115
119
 
116
120
  def clear_data
117
- @config.delete('propagateErrors')
118
- @config.delete('token')
121
+ @config.delete("propagateErrors")
122
+ @config.delete("token")
123
+ end
124
+
125
+ def configure_statscloud_plugins
126
+ @plugins = check_plugins(@config["plugins"])
119
127
  end
120
128
 
121
129
  def configure_cluster
@@ -123,21 +131,16 @@ module StatsCloud
123
131
  @cluster_client.deploy_cluster(@token, @app, @config)
124
132
  @cluster = @cluster_client.get_cluster(@token, @app).body
125
133
  @graphite_url = @cluster[:graphiteUrl]
126
- check_cluster_status
134
+ check_cluster_status(@cluster)
127
135
  logger.info successful_message
128
136
  end
129
137
 
130
- def check_cluster_status
131
- raise error if @cluster[:status][:status] == 'ERROR'
132
- end
133
-
134
- def error
135
- logger.error error_message
136
- StandardError.new(error_message)
138
+ def check_cluster_status(cluster)
139
+ raise error(cluster_error_message) if cluster.nil? || cluster[:status][:status] == "ERROR"
137
140
  end
138
141
 
139
142
  def connect_to_cluster
140
- @statsmeter_client = create_statsmeter_client(@cluster, @token, tags)
143
+ @statsmeter_client = create_statsmeter_client(@cluster, @token, @plugins, tags)
141
144
  @statsmeter_client.connect
142
145
  end
143
146
 
@@ -145,9 +148,9 @@ module StatsCloud
145
148
  StatsCloud::ClusterClient.new(env)
146
149
  end
147
150
 
148
- def create_statsmeter_client(cluster, token, tags)
151
+ def create_statsmeter_client(cluster, token, plugins, tags)
149
152
  host = cluster[:statsmeterUrl]
150
- StatsCloud::StatsmeterClient.new(host, token, tags)
153
+ StatsCloud::StatsmeterClient.new(host, token, plugins, tags)
151
154
  rescue StandardError => e
152
155
  logger.error e
153
156
  end
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './helpers/socketio_helper'
4
- require_relative './helpers/statsmeter_helper'
5
- require_relative './helpers/logger_helper'
6
- require_relative './helpers/event_helper'
7
- require 'leon'
8
- require 'eventmachine'
9
- require 'statscloud.io-ruby-socket.io-client-simple'
10
- require 'crc32'
3
+ require_relative "helpers/socketio_helper"
4
+ require_relative "helpers/statsmeter_helper"
5
+ require_relative "helpers/logger_helper"
6
+ require_relative "helpers/event_helper"
7
+ require_relative "helpers/plugins_helper"
8
+ require "leon"
9
+ require "eventmachine"
10
+ require "statscloud.io-ruby-socket.io-client-simple"
11
+ require "crc32"
11
12
 
12
13
  module StatsCloud
13
14
  # Client for Statsmeter.
@@ -16,6 +17,7 @@ module StatsCloud
16
17
  include StatsCloud::SocketIOHelper
17
18
  include StatsCloud::StatsmeterHelper
18
19
  include StatsCloud::EventHelper
20
+ include StatsCloud::PluginsHelper
19
21
 
20
22
  # Maximum size of pending binary events buffer.
21
23
  BINARY_BUFFER_SIZE = 1024
@@ -28,6 +30,11 @@ module StatsCloud
28
30
  # Type: *String*
29
31
  attr_reader :url
30
32
 
33
+ # Statscloud client tags.
34
+ #
35
+ # Type: *String*
36
+ attr_reader :tags
37
+
31
38
  # Socket connection with statscloud cluster which is used to record events.
32
39
  #
33
40
  # Type: *SocketIO::Client::Simple*
@@ -49,7 +56,8 @@ module StatsCloud
49
56
  # statsmeter url.
50
57
  # @param [+String+] token
51
58
  # authorization token.
52
- def initialize(url, token, tags = [])
59
+ def initialize(url, token, plugins, tags = [])
60
+ initialize_plugin_threads(plugins)
53
61
  set_config(url, token, tags)
54
62
  set_client_to_nil
55
63
  set_pending_values
@@ -59,8 +67,9 @@ module StatsCloud
59
67
  # Connects to the server and starts periodic sending events.
60
68
  def connect
61
69
  Thread.new do
62
- connect_client(@url, @token)
63
- flush_events_loop.join if @client
70
+ connect_client
71
+ start_plugins
72
+ flush_events_loop.join
64
73
  end
65
74
  end
66
75
 
@@ -115,20 +124,19 @@ module StatsCloud
115
124
  @pending_binary_events.writeInt32BE(checksum, @pending_binary_offset)
116
125
  send_message @pending_binary_events
117
126
  set_pending_values
118
- rescue StandardError => error
119
- logger.error error
120
- close
121
- connect.join
122
127
  end
123
128
 
124
129
  # Shows statsmeter state.
125
130
  def connected?
126
- @client&.state == :connect
131
+ return false unless @client
132
+ @client.state == :connect
127
133
  end
128
134
 
129
135
  # Stops socket.io connection.
130
136
  def close
131
- client.disconnect if open?
137
+ stop_eventmachine
138
+ client.auto_reconnection = false
139
+ client.disconnect
132
140
  set_client_to_nil
133
141
  end
134
142
 
@@ -138,13 +146,18 @@ module StatsCloud
138
146
  Thread.new do
139
147
  eventmachine.run do
140
148
  eventmachine.add_periodic_timer(0.5) do
141
- stop_machine unless @client
142
- flush_events
149
+ @mutex.synchronize do
150
+ flush_events if connected?
151
+ end
143
152
  end
144
153
  end
145
154
  end
146
155
  end
147
156
 
157
+ def start_plugins
158
+ start_plugins_job(@plugins, @mutex)
159
+ end
160
+
148
161
  def send_message(string_buffer)
149
162
  @client.emit :metric, binary_packet(string_buffer.buffer.bytes)
150
163
  end
@@ -157,6 +170,10 @@ module StatsCloud
157
170
  @pending_binary_offset + binary > BINARY_BUFFER_SIZE || @pending_plain_offset + plain > PLAIN_BUFFER_SIZE
158
171
  end
159
172
 
173
+ def stop_eventmachine
174
+ eventmachine.stop_event_loop
175
+ end
176
+
160
177
  def crc32
161
178
  Crc32
162
179
  end
@@ -4,5 +4,5 @@ module StatsCloud
4
4
  # version of statscloud-ruby-client.
5
5
  #
6
6
  # Type: *String*
7
- VERSION = '1.0.2'
7
+ VERSION = "1.0.4"
8
8
  end
data/statscloud.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path("lib", __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require "statscloud/version"
6
6
 
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ["Roman Ovcharov"]
11
11
  spec.email = ["roman.o.as@agiliumlabs.com"]
12
12
 
13
- spec.summary = %q{StatsCloud service.}
14
- spec.description = %q{PaaS application monitoring system.}
13
+ spec.summary = "StatsCloud service."
14
+ spec.description = "PaaS application monitoring system."
15
15
  spec.homepage = "https://statscloud.io"
16
16
  spec.license = "MIT"
17
17
 
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  # Specify which files should be added to the gem when it is released.
28
28
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
30
30
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
31
  end
32
32
  spec.bindir = "exe"
@@ -43,5 +43,5 @@ Gem::Specification.new do |spec|
43
43
  spec.add_development_dependency "rake", "~> 10.0"
44
44
  spec.add_dependency "rest-client", "~> 2.0.2"
45
45
  spec.add_development_dependency "rspec", "~> 3.0"
46
- spec.add_dependency "statscloud.io-ruby-socket.io-client-simple", "~> 1.2.1.pre.2"
46
+ spec.add_dependency "statscloud.io-ruby-socket.io-client-simple", "~> 1.2.1.pre.3"
47
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statscloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Ovcharov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-31 00:00:00.000000000 Z
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -156,14 +156,14 @@ dependencies:
156
156
  requirements:
157
157
  - - "~>"
158
158
  - !ruby/object:Gem::Version
159
- version: 1.2.1.pre.2
159
+ version: 1.2.1.pre.3
160
160
  type: :runtime
161
161
  prerelease: false
162
162
  version_requirements: !ruby/object:Gem::Requirement
163
163
  requirements:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
- version: 1.2.1.pre.2
166
+ version: 1.2.1.pre.3
167
167
  description: PaaS application monitoring system.
168
168
  email:
169
169
  - roman.o.as@agiliumlabs.com
@@ -184,10 +184,12 @@ files:
184
184
  - lib/generators/stats_cloud/install_generator.rb
185
185
  - lib/statscloud.rb
186
186
  - lib/statscloud/cluster_client.rb
187
+ - lib/statscloud/errors/client_error.rb
187
188
  - lib/statscloud/helpers/assets_helper.rb
188
189
  - lib/statscloud/helpers/event_helper.rb
189
190
  - lib/statscloud/helpers/logger_helper.rb
190
191
  - lib/statscloud/helpers/parsed_response_helper.rb
192
+ - lib/statscloud/helpers/plugins_helper.rb
191
193
  - lib/statscloud/helpers/socketio_helper.rb
192
194
  - lib/statscloud/helpers/statscloud_helper.rb
193
195
  - lib/statscloud/helpers/statsmeter_helper.rb