statscloud 1.0.9 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00ef058f3553e52eec23bd98630e643ab43a323f67684681994263010249d202
4
- data.tar.gz: f56e77cd1509110497f847e69cb0180890325a567a09415c1b0a8fc3f6d052e2
3
+ metadata.gz: 46395a60038b7f3886bc4e79275fe98156940690f73acee7ab8061a28164e82e
4
+ data.tar.gz: dc1d83819866769fb18f48f732f5e8db2ed2d2c732c2929d949cbdc1f21de96d
5
5
  SHA512:
6
- metadata.gz: debc25c241c6b738f9638c04ea2fedad7ea4c8948978ef830acb4c28d141c7b50fc41e8046d07277072110adc7c6c35e44acf078dcd3b203b1750871fe06de77
7
- data.tar.gz: e531513f320a1785c27cbaabfc7847b2ae91562c6a2d12bb60b11f979b74e81fd00a90e2d6438d5fedd5d44924b0acbc2504175e89c451c3334688d23294df88
6
+ metadata.gz: 016af1b77dd8c472abea59ec7737735581ae9e6b3ffdd7e9f1cd32bf6cc36d59ffd54219cd163a460fcbce5771433c333afb7847ebcc20ad11d518d420c80edd
7
+ data.tar.gz: 1c79fd2b558d124c645dc4c3f9e1f29082f2d83fd696ce5966a9c9e641d797f6f1a557cfac66c0c1a5518d20f8c44a4316fa68939d3fb58c61a1fba022bc8d1f
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,8 @@
1
+ image: ruby:2.3
2
+
3
+ test:
4
+ script:
5
+ - apt-get update -qy
6
+ - bundle install
7
+ - bundle exec rspec
8
+ - rubocop
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- statscloud (1.0.9)
4
+ statscloud (1.1.0)
5
5
  crc32 (~> 1.0.1)
6
6
  eventmachine (~> 1.2)
7
7
  fileutils
@@ -13,6 +13,13 @@ PATH
13
13
  GEM
14
14
  remote: https://rubygems.org/
15
15
  specs:
16
+ activesupport (5.2.2)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ ast (2.4.0)
22
+ concurrent-ruby (1.1.4)
16
23
  crc32 (1.0.1)
17
24
  diff-lcs (1.3)
18
25
  domain_name (0.5.20180417)
@@ -25,14 +32,23 @@ GEM
25
32
  httparty (0.16.3)
26
33
  mime-types (~> 3.0)
27
34
  multi_xml (>= 0.5.2)
35
+ i18n (1.2.0)
36
+ concurrent-ruby (~> 1.0)
37
+ jaro_winkler (1.5.1)
28
38
  json (2.1.0)
29
39
  leon (1.1.2)
30
40
  logger (1.3.0)
31
41
  mime-types (3.2.2)
32
42
  mime-types-data (~> 3.2015)
33
43
  mime-types-data (3.2018.0812)
44
+ minitest (5.11.3)
34
45
  multi_xml (0.6.0)
35
46
  netrc (0.11.0)
47
+ parallel (1.12.1)
48
+ parser (2.5.3.0)
49
+ ast (~> 2.4.0)
50
+ powerpack (0.1.2)
51
+ rainbow (3.0.0)
36
52
  rake (10.5.0)
37
53
  rest-client (2.0.2)
38
54
  http-cookie (>= 1.0.2, < 2.0)
@@ -51,14 +67,27 @@ GEM
51
67
  diff-lcs (>= 1.2.0, < 2.0)
52
68
  rspec-support (~> 3.8.0)
53
69
  rspec-support (3.8.0)
70
+ rubocop (0.61.1)
71
+ jaro_winkler (~> 1.5.1)
72
+ parallel (~> 1.10)
73
+ parser (>= 2.5, != 2.5.1.1)
74
+ powerpack (~> 0.1)
75
+ rainbow (>= 2.2.2, < 4.0)
76
+ ruby-progressbar (~> 1.7)
77
+ unicode-display_width (~> 1.4.0)
78
+ ruby-progressbar (1.10.0)
54
79
  statscloud.io-ruby-socket.io-client-simple (1.2.1.pre.3)
55
80
  event_emitter
56
81
  httparty
57
82
  json
58
83
  websocket-client-simple (~> 0.3.0)
84
+ thread_safe (0.3.6)
85
+ tzinfo (1.2.5)
86
+ thread_safe (~> 0.1)
59
87
  unf (0.1.4)
60
88
  unf_ext
61
89
  unf_ext (0.0.7.5)
90
+ unicode-display_width (1.4.0)
62
91
  websocket (1.2.8)
63
92
  websocket-client-simple (0.3.0)
64
93
  event_emitter
@@ -68,9 +97,11 @@ PLATFORMS
68
97
  ruby
69
98
 
70
99
  DEPENDENCIES
100
+ activesupport (~> 5.2.2)
71
101
  bundler (~> 1.16)
72
102
  rake (~> 10.0)
73
103
  rspec (~> 3.0)
104
+ rubocop (~> 0.61.1)
74
105
  statscloud!
75
106
 
76
107
  BUNDLED WITH
data/README.md CHANGED
@@ -28,32 +28,38 @@ Or install it yourself as:
28
28
 
29
29
  StatsCloud auth token is a required parameter for getting access to StatsCloud service. The best way to configure this auth token in your application is using environment variable `STATSCLOUD_AUTH_TOKEN`. StatsCloud add-on at Heroku configures this environment variable automatically during adding this add-on to your application. If you want to use statscloud service in your development process (or somewhere else) you can [copy Heroku config vars to your local .env file](https://devcenter.heroku.com/articles/heroku-local).
30
30
 
31
- 3. You can change `statscloud.rb` intializer file if you want to start StatsCloud service with a different environment or add some tags to your StatsmeterClient.
31
+ 3. You can change `statscloud.rb` intializer file if you want to start StatsCloud service with a different environment, create several instances of StatsCloud::Client with different configurations or add some tags to your StatsmeterClient.
32
32
 
33
33
  #### `with_environment('env')`
34
34
  This method allows you to record metrics from different environments (like `test`, `development`, `production`) separately, so
35
35
  ```ruby
36
- StatsCloud.with_environment('production').start
36
+ StatsCloud::Client.new.with_environment('production')
37
37
  ```
38
38
  would start StatsCloud service at `production` environment. Without this method, the environment would be chosen from the configuration file. If you don't configure it service would try to find env in the RAILS_ENV and if it is also missing it would set the value as the `default`.
39
39
 
40
40
  #### `with_tags(array[string])`
41
- Also, you can start service with sending some tags to the cluster. Tags are used to group metrics. By default, the host name is used as a tag, which allows you to track metrics for both the application as a whole and for each host individually. In the cloud, an array of tags `['region', 'server_name']` can be used, which allows you to track metrics for the application as a whole, separate regions or separate servers.
41
+ You can start service with sending some tags to the cluster. Tags are used to group metrics. By default, the host name is used as a tag, which allows you to track metrics for both the application as a whole and for each host individually. In the cloud, an array of tags `['region', 'server_name']` can be used, which allows you to track metrics for the application as a whole, separate regions or separate servers.
42
42
  ```ruby
43
- StatsCloud.with_tags(['region', 'server_name']).start
43
+ StatsCloud::Client.new.with_tags(['region', 'server_name'])
44
44
  ```
45
+ #### `with_config_file(file)`
46
+ Also, it's available to set up several StatsCloud clients with different configuration files via `with_config_file` method.
47
+ ```ruby
48
+ StatsCloud::Client.new.with_config_file('.production_statscloud.yml')
49
+ ```
50
+
45
51
  You can combine these methods calling them as a chain.
46
52
 
47
- 4. Use StatsCLoud.meter with `record_event` method for recording one event or `record_events` to send multiple events to cluster from any place of your application.
53
+ 4. StatsCloud initializer defines `StatsCloudClient` constant which you can use to record metrics with `record_event` method for recording one event or `record_events` to send multiple events to cluster from any place of your application:
48
54
 
49
55
  ```ruby
50
- StatsCloud.meter.record_event('event', 1)
51
- StatsCloud.meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
56
+ StatsCloudClient.meter.record_event('event', 1)
57
+ StatsCloudClient.meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
52
58
  ```
53
- Also, you can use these methods directly from StatsCloud module, starting with 1.0.6 version of the client.
59
+ Also, you can use these methods directly from StatsCloudClient:
54
60
  ```ruby
55
- StatsCloud.record_event('event', 1)
56
- StatsCloud.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
61
+ StatsCloudClient.record_event('event', 1)
62
+ StatsCloudClient.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
57
63
  ```
58
64
  ### Ruby
59
65
  1. Install `statscloud` gem:
@@ -64,28 +70,29 @@ Or install it yourself as:
64
70
 
65
71
  2. Require `statscloud` in the project and set up your application structure, with `.statscloud.yml` configuration file (at the root of the project) and metrics configs (read more about StatsCloud configuration [here](https://medium.com/@roman.kisilenko/software-application-monitoring-how-to-or-how-not-to-let-your-production-fail-9481dd0ef6de)).
66
72
 
67
- 3. Customize statscloud run with `with_environment` and `with_tags` methods and start it where you need it.
73
+ 3. Customize statscloud run with `with_environment`, `with_tags` and `with_config_file` methods and start it where you need it.
68
74
 
69
75
  ```ruby
70
- StatsCloud.with_environment('test').with_tags(['usa', 'server_1']).start
76
+ statscloud_client = StatsCloud::Client.new
77
+ statscloud_client.with_environment('test').with_tags(['usa', 'server_1']).start
71
78
  ```
72
- 4. Send metrics via StatsCloud.meter or directly via StatsCloud module.
79
+ 4. Send metrics via statscloud_client.meter or directly via StatsCloud::Client instance:
73
80
 
74
81
  ```ruby
75
- meter = StatsCloud.meter
82
+ meter = statscloud_client.meter
76
83
  meter.record_event('event', 1)
77
84
  meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
78
85
 
79
- # also available
86
+ # also available and preferable
80
87
 
81
- StatsCloud.record_event('some_event', 4)
82
- StatsCloud.record_events({name: 'some_gauge', measurement: 321})
88
+ statscloud_client.record_event('some_event', 4)
89
+ statscloud_client.record_events({name: 'some_gauge', measurement: 321})
83
90
 
84
91
  ```
85
92
 
86
- 5. Stop work of StatsCloud service when 'time is over'.
93
+ 5. Stop work of StatsCloud service when 'time is over':
87
94
  ```ruby
88
- StatsCloud.stop
95
+ statscloud_client.stop
89
96
  ```
90
97
 
91
98
  The full version of pure ruby example takes the form:
@@ -93,14 +100,18 @@ The full version of pure ruby example takes the form:
93
100
  require 'statscloud' # import gem
94
101
 
95
102
  # start the module, returns a thread
96
- StatsCloud.start
103
+ statscloud_client = StatsCloud::Client.new
104
+ statscloud_client.start
97
105
 
106
+ meter = statscloud_client.meter
98
107
  # track events (remember that you have to wait for the socket connection to the cluster before record any metrics)
99
- StatsCloud.meter.record_event('numeric', 123)
100
- StatsCloud.meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
108
+ meter.record_event('numeric', 123)
109
+ meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
110
+
111
+ statscloud_client.record_event('some_event', 123)
101
112
 
102
- # stop the module
103
- StatsCloud.stop
113
+ # stop cluster work
114
+ statscloud_client.stop
104
115
 
105
116
  ```
106
117
 
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "./helpers/parsed_response_helper"
4
- require_relative "./helpers/logger_helper"
5
3
  require "rest-client"
6
4
 
5
+ require_relative "./helpers/logger_helper"
6
+ require_relative "./helpers/parsed_response_helper"
7
+
7
8
  module StatsCloud
8
9
  # statscloud.io cluster-api service client.
9
10
  class ClusterClient
@@ -18,9 +19,9 @@ module StatsCloud
18
19
  # cluster api host.
19
20
  #
20
21
  # @api public
21
- def initialize(env, host = "https://cluster-api-v1.statscloud.statscloud.io")
22
+ def initialize(env, host)
22
23
  @environment = env
23
- @host = host
24
+ @host = host || "https://cluster-api-v1.statscloud.statscloud.io"
24
25
  end
25
26
 
26
27
  # Schedules cluster instance to be retrieved.
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "yaml"
4
+
3
5
  module StatsCloud
4
6
  # This helper works to gather and merge the configuration from files.
5
7
  module AssetsHelper
6
8
  private
7
9
 
8
10
  def asset_selector_matches(filename, asset_selector)
9
- includes = asset_selector[:include]
10
- excludes = asset_selector[:exclude]
11
+ includes = asset_selector["include"]
12
+ excludes = asset_selector["exclude"]
11
13
  process_match(includes, filename) && !process_match(excludes, filename)
12
14
  end
13
15
 
@@ -23,11 +25,11 @@ module StatsCloud
23
25
  end
24
26
 
25
27
  def get_checked_asset_selector(config)
26
- asset_selector = config[:assetSelector] || {
27
- include: ['.*\.statscloud.yml'], exclude: []
28
+ asset_selector = config["assetSelector"] || {
29
+ "include" => ['.*\.statscloud.yml'], "exclude" => []
28
30
  }
29
- asset_selector[:include] = put_to_array_if_needed(asset_selector[:include])
30
- asset_selector[:exclude] = put_to_array_if_needed(asset_selector[:exclude])
31
+ asset_selector["include"] = put_to_array_if_needed(asset_selector["include"])
32
+ asset_selector["exclude"] = put_to_array_if_needed(asset_selector["exclude"])
31
33
  asset_selector
32
34
  end
33
35
 
@@ -51,7 +53,7 @@ module StatsCloud
51
53
  end
52
54
 
53
55
  def process_match(list, filename)
54
- list.reduce(false) do |match, pattern|
56
+ list&.reduce(false) do |match, pattern|
55
57
  match || Regexp.new(pattern).match(filename)
56
58
  end
57
59
  end
@@ -85,7 +87,7 @@ module StatsCloud
85
87
  end
86
88
 
87
89
  def put_to_array_if_needed(object)
88
- object.is_a?(Array) ? object : [object]
90
+ object.is_a?(Array) ? object : [object].compact
89
91
  end
90
92
 
91
93
  def it_directory?(file_name, entry)
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../errors/client_error"
4
3
  require_relative "logger_helper"
4
+ require_relative "../errors/client_error"
5
5
 
6
6
  module StatsCloud
7
7
  # This helper configures StatsCloud plugins.
8
8
  module PluginsHelper
9
+ private
10
+
9
11
  def build_plugins(plugins)
10
12
  plugins&.each do |plugin|
11
13
  plugin = plugin.is_a?(Hash) ? plugin : simple_plugin_config(plugin)
@@ -19,8 +21,6 @@ module StatsCloud
19
21
  end
20
22
  end
21
23
 
22
- private
23
-
24
24
  def simple_plugin_config(plugin)
25
25
  {
26
26
  "name": plugin
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "statscloud.io-ruby-socket.io-client-simple"
4
+
3
5
  require_relative "logger_helper"
4
6
 
5
7
  module StatsCloud
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "leon"
3
+ require "socket"
4
4
  require_relative "../errors/client_error"
5
5
 
6
6
  module StatsCloud
@@ -8,6 +8,26 @@ module StatsCloud
8
8
  module StatsCloudHelper
9
9
  private
10
10
 
11
+ def initialize_values
12
+ @config = {}
13
+ @source_mappings = { metrics: [], admins: [], dashboards: [], alerts: [] }
14
+ update_config_file(".statscloud.yml")
15
+ end
16
+
17
+ def generate_configuration(base_config)
18
+ if base_config
19
+ join_configs(@config, base_config, @source_mappings, "")
20
+ else
21
+ base_config = get_config_from_file(@config_file)
22
+ join_configs(@config, base_config, @source_mappings, @config_file)
23
+ end
24
+ end
25
+
26
+ def clear_data
27
+ @config.delete("propagateErrors")
28
+ @config.delete("token")
29
+ end
30
+
11
31
  def config_values
12
32
  @config["sourceMaps"] = source_mappings
13
33
  config_token
@@ -27,6 +47,10 @@ module StatsCloud
27
47
  @tags = tags
28
48
  end
29
49
 
50
+ def update_config_file(file)
51
+ @config_file = file
52
+ end
53
+
30
54
  def config_token
31
55
  @token = @config["token"] || ENV["STATSCLOUD_AUTH_TOKEN"]
32
56
  end
@@ -49,7 +73,7 @@ module StatsCloud
49
73
  def cluster_error_message
50
74
  return "Unable to get information about cluster." unless @cluster
51
75
 
52
- @cluster[:status][:error]
76
+ @cluster["status"]["error"]
53
77
  end
54
78
 
55
79
  def app_name_missing_error_message
@@ -61,7 +85,7 @@ module StatsCloud
61
85
  end
62
86
 
63
87
  def successful_message
64
- "statscloud.io support configured, dashboard URLs are \n #{(@cluster['grafanaDashboardsUrls'] || []).join("\n")}"
88
+ "statscloud.io support configured, dashboard URLs are \n#{(@cluster['grafanaDashboardsUrls'] || []).join("\n")}"
65
89
  end
66
90
 
67
91
  def statscloud_error(message)
@@ -34,6 +34,20 @@ module StatsCloud
34
34
  @event_name_size_in_bytes = 0
35
35
  end
36
36
 
37
+ def process_single_event(event)
38
+ name = get_event_name(event)
39
+ measurement = get_event_measurement(event)&.to_f
40
+
41
+ return unless @names_map && @names_map[name]
42
+
43
+ binary_length = get_binary_length(@event_name_size_in_bytes)
44
+ plain_length = get_plain_length(name, measurement)
45
+
46
+ flush_events if flush_condition(binary_length, plain_length)
47
+ record_binary_event(name, measurement, binary_length)
48
+ record_plain_event(name, measurement, plain_length)
49
+ end
50
+
37
51
  def record_binary_event(name, measurement, binary_length)
38
52
  @pending_binary_events.writeInt32LE(@names_map[name], @pending_binary_offset)
39
53
  @pending_binary_events = @pending_binary_events.slice(0, @pending_binary_offset + @event_name_size_in_bytes)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "eventmachine"
4
+
4
5
  require_relative "../helpers/logger_helper"
5
6
 
6
7
  module StatsCloud
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "yaml"
4
- require "json"
5
- require "eventmachine"
6
- require "singleton"
7
- require "socket"
8
3
  require_relative "version"
9
4
  require_relative "statsmeter_client"
10
5
  require_relative "cluster_client"
@@ -16,12 +11,20 @@ require_relative "jobs/register_connection_job"
16
11
  module StatsCloud
17
12
  # Module which implements StatsCloud framework support.
18
13
  class Client
19
- include Singleton
20
14
  include StatsCloud::AssetsHelper
21
15
  include StatsCloud::LoggerHelper
22
16
  include StatsCloud::PluginsHelper
23
17
  include StatsCloud::StatsCloudHelper
24
18
 
19
+ # Creates new StatsCloud::Client instance with empty configuration.
20
+ #
21
+ # @return instance of StatsCloud::Client
22
+ #
23
+ # @api public
24
+ def initialize
25
+ initialize_values
26
+ end
27
+
25
28
  # Configures statsmeter.io support for application and initializes a statscloud.io client.
26
29
  #
27
30
  # @param [+String] env
@@ -34,19 +37,22 @@ module StatsCloud
34
37
  #
35
38
  # @api public
36
39
  def start(base_config = nil)
37
- initialize_values
38
- generate_configuration(base_config)
39
- collect_statscloud_assets(@config, @source_mappings)
40
- config_values
41
- clear_data
40
+ process_configuration(base_config)
42
41
  configure_statscloud_plugins
43
42
  configure_cluster
44
43
  connect_to_cluster
44
+ rescue StandardError => error
45
+ log_error error
45
46
  end
46
47
 
47
48
  # Configures Statscloud environment.
48
49
  #
49
- # @return StatsCloud::Client
50
+ # @param [+String+] env
51
+ # Environment
52
+ #
53
+ # @return instance of StatsCloud::Client
54
+ #
55
+ # @api public
50
56
  def with_environment(env)
51
57
  config_environment(env)
52
58
  self
@@ -54,15 +60,33 @@ module StatsCloud
54
60
 
55
61
  # Configures Statscloud tags.
56
62
  #
57
- # @return StatsCloud::Client
63
+ # @param [+Array+] tags
64
+ # List of tags
65
+ #
66
+ # @return instance of StatsCloud::Client
67
+ #
68
+ # @api public
58
69
  def with_tags(tags)
59
70
  config_tags(tags)
60
71
  self
61
72
  end
62
73
 
74
+ # Allows to start several instances of StatsCloud::Client with different confifurations.
75
+ #
76
+ # @param [+String+] file
77
+ # Main configuration file for StatsCloud::Client
78
+ #
79
+ # @return instance of StatsCloud::Client
80
+ #
81
+ # @api public
82
+ def with_config_file(file)
83
+ update_config_file(file)
84
+ self
85
+ end
86
+
63
87
  # Returns statscloud.io client aka Statsmeter client
64
88
  #
65
- # @return [StatsCloud::StatsmeterClient]
89
+ # @return instance of StatsCloud::StatsmeterClient
66
90
  #
67
91
  # @api public
68
92
  def meter
@@ -77,6 +101,8 @@ module StatsCloud
77
101
  # optional measurement, depending on metrics type.
78
102
  #
79
103
  # Calls statsmeter client record_event method.
104
+ #
105
+ # @api public
80
106
  def record_event(name, measurement = 0)
81
107
  @statsmeter_client&.record_event(name, measurement)
82
108
  end
@@ -87,6 +113,8 @@ module StatsCloud
87
113
  # events to send (each should have name and optional measurement fields).
88
114
  #
89
115
  # Calls statsmeter record_events method.
116
+ #
117
+ # @api public
90
118
  def record_events(*events)
91
119
  @statsmeter_client&.record_events(*events)
92
120
  end
@@ -97,6 +125,8 @@ module StatsCloud
97
125
  # array of events to send (each shoud have name and optional measurement fields).
98
126
  #
99
127
  # Calls statsmeter client record_events_array method.
128
+ #
129
+ # @api public
100
130
  def record_events_array(events)
101
131
  @statsmeter_client&.record_events_array(events)
102
132
  end
@@ -111,12 +141,14 @@ module StatsCloud
111
141
 
112
142
  cluster = @cluster_client.get_cluster(@token, @app)&.body
113
143
  check_cluster_status(cluster)
114
- cluster[:status][:status] if cluster
144
+ cluster["status"]["status"] if cluster
145
+ rescue StandardError => error
146
+ log_error error
115
147
  end
116
148
 
117
149
  # Stops statscloud.io service.
118
150
  #
119
- # @return [NilClass]
151
+ # @return nil
120
152
  #
121
153
  # @api public
122
154
  def stop
@@ -124,30 +156,19 @@ module StatsCloud
124
156
  @statsmeter_client = nil
125
157
  @cluster_client&.undeploy_cluster(@token, @app)
126
158
  @cluster_client = nil
159
+ rescue StandardError => error
160
+ log_error error
127
161
  end
128
162
 
129
163
  private
130
164
 
131
165
  attr_reader :config, :source_mappings, :statsmeter_client, :cluster, :token, :app, :graphite_url
132
166
 
133
- def initialize_values
134
- stop
135
- @config = {}
136
- @source_mappings = { metrics: [], admins: [], dashboards: [], alerts: [] }
137
- end
138
-
139
- def generate_configuration(base_config)
140
- if base_config
141
- join_configs(@config, base_config, @source_mappings, "")
142
- else
143
- base_config = get_config_from_file(File.join(__dir__, ".statscloud.yml"))
144
- join_configs(@config, base_config, @source_mappings, ".statscloud.yml")
145
- end
146
- end
147
-
148
- def clear_data
149
- @config.delete("propagateErrors")
150
- @config.delete("token")
167
+ def process_configuration(base_config)
168
+ generate_configuration(base_config)
169
+ collect_statscloud_assets(@config, @source_mappings)
170
+ config_values
171
+ clear_data
151
172
  end
152
173
 
153
174
  def configure_statscloud_plugins
@@ -1,14 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "crc32"
4
+ require "eventmachine"
5
+
3
6
  require_relative "helpers/socketio_helper"
4
7
  require_relative "helpers/statsmeter_helper"
5
8
  require_relative "helpers/logger_helper"
6
9
  require_relative "helpers/event_helper"
7
10
  require_relative "helpers/plugins_helper"
8
- require "leon"
9
- require "eventmachine"
10
- require "statscloud.io-ruby-socket.io-client-simple"
11
- require "crc32"
12
11
 
13
12
  module StatsCloud
14
13
  # Client for Statsmeter.
@@ -104,18 +103,10 @@ module StatsCloud
104
103
  # Save events in pending binary and plain arrays. Check flush condition.
105
104
  def record_events_array(events)
106
105
  events.each do |event|
107
- name = get_event_name(event)
108
- measurement = get_event_measurement(event)&.to_f
109
-
110
- next unless @names_map && @names_map[name]
111
-
112
- binary_length = get_binary_length(@event_name_size_in_bytes)
113
- plain_length = get_plain_length(name, measurement)
114
-
115
- flush_events if flush_condition(binary_length, plain_length)
116
- record_binary_event(name, measurement, binary_length)
117
- record_plain_event(name, measurement, plain_length)
106
+ process_single_event(event)
118
107
  end
108
+ rescue StandardError => error
109
+ log_error error
119
110
  end
120
111
 
121
112
  # Sends all pending events to statscloud.
@@ -140,8 +131,8 @@ module StatsCloud
140
131
  # Stops socket.io connection.
141
132
  def close
142
133
  stop_eventmachine
143
- client.auto_reconnection = false
144
- client.disconnect
134
+ client&.auto_reconnection = false
135
+ client&.disconnect
145
136
  set_client_to_nil
146
137
  end
147
138
 
@@ -176,7 +167,7 @@ module StatsCloud
176
167
  end
177
168
 
178
169
  def stop_eventmachine
179
- eventmachine.stop_event_loop
170
+ eventmachine.stop_event_loop if eventmachine.reactor_running?
180
171
  end
181
172
 
182
173
  def crc32
@@ -4,5 +4,5 @@ module StatsCloud
4
4
  # version of statscloud-ruby-client.
5
5
  #
6
6
  # Type: *String*
7
- VERSION = "1.0.9"
7
+ VERSION = "1.1.0"
8
8
  end
data/lib/statscloud.rb CHANGED
@@ -1,111 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "statscloud/statscloud_client"
4
- require "statscloud/helpers/logger_helper"
5
-
6
- # StatsCloud.io ruby client
7
- module StatsCloud
8
- class << self
9
- include LoggerHelper
10
-
11
- # Configures statsmeter.io support for application and initializes a statscloud.io client.
12
- #
13
- # @param [+String+] env
14
- # statsmeter.io cluster environment
15
- #
16
- # @param [+Hash+] base_config
17
- # statsmeter.io configuration
18
- #
19
- # @return [Thread]
20
- #
21
- # @api public
22
- def start(base_config = nil)
23
- stats_cloud_instance.start(base_config)
24
- rescue StandardError => error
25
- log_error error
26
- end
27
-
28
- # Configures Statscloud environment.
29
- #
30
- # @return StatsCloud::Client
31
- def with_environment(env)
32
- stats_cloud_instance.with_environment(env)
33
- rescue StandardError => error
34
- log_error error
35
- end
36
-
37
- # Configures Statscloud tags.
38
- #
39
- # @return StatsCloud::Client
40
- def with_tags(tags)
41
- stats_cloud_instance.with_tags(tags)
42
- rescue StandardError => error
43
- log_error error
44
- end
45
-
46
- # Returns statscloud.io client aka Statsmeter client
47
- #
48
- # @return [StatsCloud::StatsmeterClient]
49
- #
50
- # @api public
51
- def meter
52
- stats_cloud_instance.meter
53
- rescue StandardError => error
54
- log_error error
55
- end
56
-
57
- # Records a single event.
58
- #
59
- # @param [+String+] name
60
- # name of the event to record.
61
- # @param [+Integer+] measurement
62
- # optional measurement, depending on metrics type.
63
- def record_event(name, measurement = 0)
64
- stats_cloud_instance.record_event(name, measurement)
65
- end
66
-
67
- # Records several events at once.
68
- #
69
- # @param [+Array+] events
70
- # events to send (each should have name and optional measurement fields).
71
- def record_events(*events)
72
- stats_cloud_instance.record_events(*events)
73
- end
74
-
75
- # Records an array of events at once.
76
- #
77
- # @param [+Array+] events
78
- # array of events to send (each shoud have name and optional measurement fields).
79
- def record_events_array(events)
80
- stats_cloud_instance.record_events_array(events)
81
- end
82
-
83
- # Returns cluster status.
84
- #
85
- # @return [Hash]
86
- #
87
- # @api public
88
- def cluster_status
89
- stats_cloud_instance.cluster_status
90
- rescue StandardError => error
91
- log_error error
92
- end
93
-
94
- # Stops statscloud.io service.
95
- #
96
- # @return [NilClass]
97
- #
98
- # @api public
99
- def stop
100
- stats_cloud_instance.stop
101
- rescue StandardError => error
102
- log_error error
103
- end
104
-
105
- private
106
-
107
- def stats_cloud_instance
108
- StatsCloud::Client.instance
109
- end
110
- end
111
- end
4
+ require "statscloud/version"
@@ -1,4 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- StatsCloud.start
4
- at_exit { StatsCloud.stop }
3
+ unless defined?(::Rails::Console)
4
+ ::StatsCloudClient = StatsCloud::Client.new
5
+ StatsCloudClient.start
6
+ at_exit { StatsCloudClient.stop }
7
+ end
data/statscloud.gemspec CHANGED
@@ -41,7 +41,9 @@ Gem::Specification.new do |spec|
41
41
  spec.add_dependency "rest-client", "~> 2.0.2"
42
42
  spec.add_dependency "statscloud.io-ruby-socket.io-client-simple", "~> 1.2.1.pre.3"
43
43
 
44
+ spec.add_development_dependency "activesupport", "~> 5.2.2"
44
45
  spec.add_development_dependency "bundler", "~> 1.16"
45
46
  spec.add_development_dependency "rake", "~> 10.0"
46
47
  spec.add_development_dependency "rspec", "~> 3.0"
48
+ spec.add_development_dependency "rubocop", "~> 0.61.1"
47
49
  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.9
4
+ version: 1.1.0
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-12-12 00:00:00.000000000 Z
11
+ date: 2018-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: crc32
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: 1.2.1.pre.3
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 5.2.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 5.2.2
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: bundler
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,20 @@ dependencies:
150
164
  - - "~>"
151
165
  - !ruby/object:Gem::Version
152
166
  version: '3.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.61.1
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.61.1
153
181
  description: PaaS application monitoring system.
154
182
  email:
155
183
  - roman.o.as@agiliumlabs.com
@@ -158,6 +186,7 @@ extensions: []
158
186
  extra_rdoc_files: []
159
187
  files:
160
188
  - ".gitignore"
189
+ - ".gitlab-ci.yml"
161
190
  - ".rspec"
162
191
  - ".rubocop.yml"
163
192
  - Gemfile