polyseerio 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 8358926a5d67bc28c9bc79a638d5095d8c69c33c
4
- data.tar.gz: d208509a37081b80f9fdd863b87d086f45ee6f16
3
+ metadata.gz: 16f2ef0fd6b6622958565152391927ab6ae358e7
4
+ data.tar.gz: 9166ea14ae11b4c31a068501e9f10aac305ba309
5
5
  SHA512:
6
- metadata.gz: 34422281004974f2fa7197f75523651cc32483cc042b724ec6d72fdb81c180c8923a8747c27686374b9238b0ede340917bc5df44b708ec1e6d564a27a94176fe
7
- data.tar.gz: 4980c3839bd68d77c8267ef361e4962c8a1b29938ab385f261fbe18b44d27624ef3869162d50e91d43c5737b15f36017879f9a76d1f26d3474e5304251d2debb
6
+ metadata.gz: bb3b8b002effa1260fba13e39b656a63328fb539e6b33b6ce80b7745b23c671993221fab0b02588ff43ce5a606eecd825bb0a3345ba2bf5acea87d0b099cd6b2
7
+ data.tar.gz: 88edf82571ff4fcba7583d57a501774d2fb61abd17d91a29302c5d0b9f533eab8df881263bf61ed00dc9b5c11f9ec320ca3ffaa30e779b24ada9bf75663cd941
data/README.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  The official Polyseer.io SDK for Ruby. Detailed API information can be found at (https://polyseer.io/documentation).
6
6
 
7
+ ## About
8
+
9
+ Polyseer.io is an Integrated Development Platform that instantly
10
+ provides teams with the resources needed to build, support, and maintain world
11
+ class software products.
12
+
7
13
  ## Requirements
8
14
  - Ruby
9
15
  - gem
@@ -20,14 +26,15 @@ To add to your project Gemfile, insert:
20
26
 
21
27
  ## Example
22
28
 
23
- Examples are available in /example
29
+ Be sure to check out the examples in /example.
24
30
 
25
31
  ## Environment Variables
26
32
 
27
33
  Certain values can be set in environment variables:
28
34
 
29
- * POLYSEERIO_TOKEN access-token used for API calls
30
- * RAILS_ENV the current environment
35
+ * POLYSEERIO_TOKEN access-token used for API calls
36
+ * RAILS_ENV the current environment
37
+ * POLYSEERIO_LOG_LEVEL SDK logging level
31
38
 
32
39
  ## Usage
33
40
 
@@ -63,7 +70,7 @@ delaying and chaining async work.
63
70
 
64
71
  ## Example
65
72
 
66
- Examples are available in /example
73
+ Be sure to check out the examples in /example.
67
74
 
68
75
  ## SDK Resources
69
76
 
@@ -74,20 +81,20 @@ client has been created you may use any of its resources.
74
81
  ### Polyseerio
75
82
 
76
83
  * Polyseerio
77
- * .`start(options = {}) ⇒ Polyseerio::Client`
78
- * Create a client and start an Agent.
79
- * `options (Hash)`
80
- - `:env` environment variable that holds the current environment
81
- - `:token` an api token
82
- - `:token_env` if no token is provided, this environment variable will be checked
83
- - `:agent` a hash that will be used when starting this client's agent
84
- - `:upsert_env` if an environment cannot be found it will be created
85
- - `:version` api version to use
86
- - `:timeout` integer containing number of ms to wait for server responses
87
- - `:deduce` if the environment should be deduced when not supplied
88
84
  * .`make(options = {}) ⇒ Polyseerio::Client`
89
85
  * Create a client.
90
86
  * `options (Hash)` see Polyseerio.start options.
87
+ - `:deduce (Boolean)` if the environment should be deduced when not supplied
88
+ - `:env (String)` environment variable that holds the current environment
89
+ - `:timeout (Integer)` integer containing number of ms to wait for server responses
90
+ - `:token (String)` an api token
91
+ - `:token_env (String)` if no token is provided, this environment variable will be checked
92
+ - `:upsert_env (Boolean)` if an environment cannot be found it will be created
93
+ - `:version (String)` api version to use
94
+ * .`start(options = {}) ⇒ Polyseerio::Client`
95
+ * Create a client and start an Agent.
96
+ * `options (Hash)`
97
+ - `:agent (Hash)` agent options (see client.start_agent options)
91
98
  * `::Enum`
92
99
  * `::Color (Hash)` platform color values
93
100
  * `::Determiner (Hash)` expectation deteriner types
@@ -104,17 +111,34 @@ client has been created you may use any of its resources.
104
111
  * `.current_environment ⇒ client.Environment`
105
112
  * Resolves the current environment **IF** it has been deduced.
106
113
  * `.start_agent(options = {}) ⇒ Polyseerio::Client`
107
- * Starts the Polyseer.io agent. Will use passed options or config.agent from client construction.
108
- * `.Alert`
109
- * `.Channel`
110
- * `.Environment`
111
- * `.Event`
112
- * `.Expectation`
113
- * `.Instance`
114
- * `.LogicBlock`
115
- * `.Member`
116
- * `.Settings`
117
- * `.Task`
114
+ * Starts the Polyseer.io agent for this client.
115
+ * `options`
116
+ - `.attach (Boolean)`
117
+ - `.attach_strategy (Symbol)`
118
+ - `.name (String)` instance name (will be used as a unique id)
119
+ - `.description (String)` a description of this instance
120
+ - `.group (String)` what group this instance belongs to
121
+ - `.direction (String)` the monitoring direction (inbound) // force this
122
+ - `.subtype (Polyseerio::Enum::Subtype)` the instance subtype: periodic or long_running.
123
+ - `.expectation` will be upserted for this instance
124
+ - `.is_alive (Boolean)` create an expectation that this process is alive
125
+ - `.fact`
126
+ - `.engine (Boolean)` the current ruby engine
127
+ - `.gid (Boolean)` the group if othe process is running under
128
+ - `.launch_arguments (Boolean)` command used to launch the instance
129
+ - `.pid (Boolean)` the id of the process
130
+ - `.platform (Boolean)` the operating platform of
131
+ - `.ruby_version (Boolean)` the version of ruby being used
132
+ - `.uid (Boolean)` user id the process is running as
133
+ - `.metric`
134
+ - `.cpu (Boolean)` track user and system cpu usage
135
+ - `.memory (Boolean)` track memory usage
136
+ - `.uptime (Boolean)` track process uptime
137
+ - `.event`
138
+ - `.start (Boolean)` event notice when agent starts
139
+ - `.stop (Boolean)` event notice when agent stops
140
+ - `.process_event`
141
+ - `.exit (Boolean)` event notice on process exit
118
142
 
119
143
  ### Alert
120
144
 
@@ -305,3 +329,8 @@ Requires the environment to have a root level access-token defined as:
305
329
  ### All
306
330
 
307
331
  make test
332
+
333
+ ## Debugging
334
+
335
+ Set the environment variable POLYSEERIO_LOG_LEVEL to debug in order to get
336
+ detailed logging information.
@@ -12,6 +12,8 @@ module Polyseerio
12
12
 
13
13
  # Starts the agent.
14
14
  def start(*args)
15
+ Polyseerio.log 'debug', 'Starting agent.'
16
+
15
17
  Executor.setup(client, *args)
16
18
  .then do |instance|
17
19
  @instance = instance
@@ -24,8 +24,13 @@ module Polyseerio
24
24
  is_alive: true
25
25
  },
26
26
  fact: {
27
- pid: true,
28
- gid: true
27
+ Enum::Fact::ENGINE => true,
28
+ Enum::Fact::GID => true,
29
+ Enum::Fact::LAUNCH_ARGUMENTS => true,
30
+ Enum::Fact::PID => true,
31
+ Enum::Fact::PLATFORM => true,
32
+ Enum::Fact::RUBY_VERSION => true,
33
+ Enum::Fact::UID => true
29
34
  },
30
35
  metric: {
31
36
  memory: true,
@@ -11,8 +11,13 @@ module Polyseerio
11
11
  end
12
12
 
13
13
  module Fact
14
- PID = :pid
15
- GID = :gid
14
+ ENGINE = :engine
15
+ GID = :gid
16
+ LAUNCH_ARGUMENTS = :launch_arguments
17
+ PID = :pid
18
+ PLATFORM = :platform
19
+ RUBY_VERSION = :ruby_version
20
+ UID = :uid
16
21
  end
17
22
 
18
23
  module Signal
@@ -11,6 +11,8 @@ module Polyseerio
11
11
  module Executor
12
12
  # Setsup a client's agent.
13
13
  def self.setup(client, options = {})
14
+ Polyseerio.log 'debug', 'Setting up client agent.'
15
+
14
16
  options = Polyseerio::Helper.defaults(options, DEFAULT_CONFIG)
15
17
 
16
18
  Concurrent::Promise.new do
@@ -42,6 +44,7 @@ module Polyseerio
42
44
  Concurrent::Promise.zip(*setups).execute.value
43
45
 
44
46
  # Start monitoring.
47
+ Polyseerio.log 'debug', 'Attaching instance to Polyseer.io'
45
48
  instance.attach.execute.value
46
49
 
47
50
  instance
@@ -4,12 +4,34 @@ module Polyseerio
4
4
  module Handler
5
5
  def self.fact
6
6
  {
7
- Fact::PID => proc do |_config, client|
8
- client.instance.set_fact('pid', ::Process.pid)
7
+ Fact::ENGINE => proc do |_config, client|
8
+ client.instance.add_fact('engine', RUBY_ENGINE)
9
9
  end,
10
10
 
11
11
  Fact::GID => proc do |_config, client|
12
- client.instance.set_fact('gid', ::Process.gid)
12
+ client.instance.add_fact('gid', ::Process.gid)
13
+ end,
14
+
15
+ Fact::LAUNCH_ARGUMENTS => proc do |_config, client|
16
+ value = ARGV.join('')
17
+
18
+ client.instance.add_fact('launch_arguments', value)
19
+ end,
20
+
21
+ Fact::PID => proc do |_config, client|
22
+ client.instance.add_fact('pid', ::Process.pid)
23
+ end,
24
+
25
+ Fact::PLATFORM => proc do |_config, client|
26
+ client.instance.add_fact('platform', RUBY_PLATFORM)
27
+ end,
28
+
29
+ Fact::RUBY_VERSION => proc do |_config, client|
30
+ client.instance.add_fact('ruby_version', RUBY_VERSION)
31
+ end,
32
+
33
+ Fact::UID => proc do |_config, client|
34
+ client.instance.add_fact('uid', ::Process.uid)
13
35
  end
14
36
  }
15
37
  end
@@ -23,7 +23,9 @@ module Polyseerio
23
23
  def self.create_handler(iteratee)
24
24
  proc do |map, client, config, type, *args|
25
25
  if map.key? type.to_sym
26
- if config.key? type
26
+ if config.key? type # to_sym?
27
+ Polyseerio.log 'debug', "Performing handler work for: #{type}."
28
+
27
29
  # TODO: unit-test passed args
28
30
  work = config.fetch(type)
29
31
  .map(&iteratee.call(map.fetch(type), client, *args))
@@ -42,6 +44,8 @@ module Polyseerio
42
44
  # Returns a setup handler function.
43
45
  @setup = proc do |handlers_outer, *args_outer|
44
46
  setup_iterator = proc do |handlers, key, value, *args|
47
+ Polyseerio.log 'debug', "Setting up handler for: #{key}."
48
+
45
49
  handler = handlers.fetch(key)
46
50
 
47
51
  if handler.respond_to? :call
@@ -59,6 +63,8 @@ module Polyseerio
59
63
 
60
64
  @teardown = proc do |handlers_outer, *args_outer|
61
65
  teardown_iterator = proc do |handlers, key, _value, *args|
66
+ Polyseerio.log 'debug', "Tearing down handler for: #{key}."
67
+
62
68
  if handlers.fetch(key).key? Handler::Interface::TEARDOWN
63
69
  handlers.fetch(key).fetch(Handler::Interface::TEARDOWN).call(*args)
64
70
  else
@@ -1,12 +1,13 @@
1
1
  module Polyseerio
2
2
  module Constant
3
- ACCESS_TOKEN_HEADER = 'X-AUTH-HEADER'.freeze
4
- DEFAULT_API_BASE_URL = 'api.polyseer.io/polyseer'.freeze
5
- DEFAULT_API_PROTOCOL = 'https://'.freeze
6
- DEFAULT_API_VERSION = 'v1'.freeze
7
- DEFAULT_ENV = 'RAILS_ENV'.freeze
8
- DEFAULT_ENVIRONMENT = 'development'.freeze
9
- DEFAULT_TIMEOUT = 10_000
10
- DEFAULT_TOKEN_ENV = 'POLYSEERIO_TOKEN'.freeze
3
+ ACCESS_TOKEN_HEADER = 'X-AUTH-HEADER'.freeze
4
+ DEFAULT_API_BASE_URL = 'api.polyseer.io/polyseer'.freeze
5
+ DEFAULT_API_PROTOCOL = 'https://'.freeze
6
+ DEFAULT_API_VERSION = 'v1'.freeze
7
+ DEFAULT_ENV = 'RAILS_ENV'.freeze
8
+ DEFAULT_ENVIRONMENT = 'development'.freeze
9
+ DEFAULT_HEARTBEAT_DELAY = 30_000
10
+ DEFAULT_TIMEOUT = 10_000
11
+ DEFAULT_TOKEN_ENV = 'POLYSEERIO_TOKEN'.freeze
11
12
  end
12
13
  end
@@ -0,0 +1,52 @@
1
+ require 'logger'
2
+
3
+ # Introduces a logger into the main module.
4
+ module Polyseerio
5
+ class << self
6
+ attr_writer :logger
7
+
8
+ # Convert a string log level to a severity.
9
+ def level_to_severity(level) # rubocop:disable all
10
+ case level
11
+ when 'debug'
12
+ Logger::DEBUG
13
+ when 'error'
14
+ Logger::ERROR
15
+ when 'fatal'
16
+ Logger::FATAL
17
+ when 'info'
18
+ Logger::INFO
19
+ when 'unknown'
20
+ Logger::UNKNOWN
21
+ when 'warn'
22
+ Logger::WARN
23
+ else
24
+ raise ArgumentError, "Unsupported log level: #{level}."
25
+ end
26
+ end
27
+
28
+ # Gem logger.
29
+ def logger
30
+ @logger ||= Logger.new($stdout).tap do |log|
31
+ log.level = if ENV.key? 'POLYSEERIO_LOG_LEVEL'
32
+ level_to_severity ENV['POLYSEERIO_LOG_LEVEL']
33
+ else
34
+ Logger::ERROR
35
+ end
36
+
37
+ log.progname = name
38
+ end
39
+ end
40
+
41
+ # Convenience logging method.
42
+ def log(severity, *args)
43
+ severity = if severity.is_a? String
44
+ level_to_severity severity
45
+ else
46
+ severity
47
+ end
48
+
49
+ logger.log(severity, *args)
50
+ end
51
+ end
52
+ end
@@ -2,6 +2,7 @@ require 'resource/factory'
2
2
  require 'request'
3
3
  require 'middleware'
4
4
  require 'constant'
5
+ require 'log'
5
6
  require 'enum'
6
7
  require 'client'
7
8
  require 'url_builder'
@@ -52,10 +53,12 @@ module Polyseerio
52
53
  }.freeze
53
54
 
54
55
  # Create a client and start it's agent.
55
- def self.start(*args)
56
- client = make(*args)
56
+ def self.start(options = {})
57
+ options = Helper.defaults(options, DEFAULT_CLIENT_OPTIONS)
58
+
59
+ client = make(options)
57
60
 
58
- client.start_agent
61
+ client.start_agent(options[:agent])
59
62
  end
60
63
 
61
64
  @make_call_count = 0
@@ -48,6 +48,16 @@ module Polyseerio
48
48
  id.nil?
49
49
  end
50
50
 
51
+ # TODO: move towards this
52
+ def get(key, default = nil)
53
+ @attributes.fetch(key, default)
54
+ end
55
+
56
+ # TODO: move towards this
57
+ def set(key, value)
58
+ @attributes[key] = value
59
+ end
60
+
51
61
  def method_missing(name, *args) # rubocop:disable all
52
62
  # Setter.
53
63
  if name =~ /^(\w+)=$/
@@ -60,8 +70,8 @@ module Polyseerio
60
70
  @attributes.fetch(name, nil)
61
71
  end
62
72
 
63
- def respond_to_missing?(_method_name)
64
- true
73
+ def respond_to_missing?(method_name)
74
+ !methods.include(method_name)
65
75
  end
66
76
 
67
77
  private
@@ -13,6 +13,11 @@ module Polyseerio
13
13
 
14
14
  # Given a request path an eid is returned or nil
15
15
  def self.get_eid_from_resource_path(path)
16
+ # TODO: consolidate with regex?
17
+ if path.include? '/environments/name/'
18
+ return path.split('/environments/name/').last
19
+ end
20
+
16
21
  result = EID_REGEX.match path
17
22
 
18
23
  return result if result.nil?
@@ -12,7 +12,7 @@ module Polyseerio
12
12
 
13
13
  statics = statics.each_with_object({}, &accumulator)
14
14
 
15
- # curry request, resource, and options
15
+ # curry request, resource, and options
16
16
  statics.each_with_object(statics) do |(name, static), hash|
17
17
  if static.arity > 3
18
18
  hash[name] = static.curry.call(request, resource, options)
@@ -27,7 +27,11 @@ module Polyseerio
27
27
  module Method
28
28
  # Generates method (instance) functions.
29
29
  def self.factory(_request, _resource, methods = [], _options = {})
30
- methods.each_with_object({}, &Helper.accumulate_procs('method', MAP))
30
+ accumulator = Helper.accumulate_procs('method', MAP)
31
+
32
+ methods = methods.each_with_object({}, &accumulator)
33
+
34
+ methods
31
35
  end
32
36
  end
33
37
  end
@@ -6,6 +6,16 @@ module Polyseerio
6
6
  module SDK
7
7
  # SDK helpers.
8
8
  module Helper
9
+ # Takes a hash and returns a new one that only contains values
10
+ # that are blocks / procs / anything callable aka resolvable.
11
+ def self.remove_non_resolving_values(hash)
12
+ hash.each_with_object({}) do |(key, value), acc|
13
+ acc[key] = value if value.respond_to? :call
14
+
15
+ acc
16
+ end
17
+ end
18
+
9
19
  # Takes an instance and returns a uri.
10
20
  def self.instance_to_uri(instance)
11
21
  URL.get_resource_path(
@@ -4,9 +4,9 @@ module Polyseerio
4
4
  module Method
5
5
  def self.add_fact
6
6
  proc do |instance, key, value|
7
- instance.facts ||= {}
7
+ instance._facts ||= {}
8
8
 
9
- instance.facts[key] = value
9
+ instance._facts[key] = value
10
10
  end
11
11
  end
12
12
  end
@@ -4,9 +4,9 @@ module Polyseerio
4
4
  module Method
5
5
  def self.add_gauge
6
6
  proc do |instance, key, value|
7
- instance.gauges ||= {}
7
+ instance._gauges ||= {}
8
8
 
9
- instance.gauges[key] = value
9
+ instance._gauges[key] = value
10
10
  end
11
11
  end
12
12
  end
@@ -1,5 +1,6 @@
1
1
  require 'sdk/helper'
2
2
  require 'concurrent'
3
+ require 'constant'
3
4
 
4
5
  module Polyseerio
5
6
  module SDK
@@ -7,13 +8,29 @@ module Polyseerio
7
8
  module Method
8
9
  def self.attach
9
10
  proc do |instance|
11
+ options = Polyseerio::Helper.defaults(
12
+ {},
13
+ delay: Constant::DEFAULT_HEARTBEAT_DELAY
14
+ )
15
+
10
16
  uri = Helper.instance_to_uri instance
11
17
 
18
+ options[:delay] = Polyseerio::Helper.ms_to_seconds options[:delay]
19
+
12
20
  Concurrent::Promise.new do
13
- heartbeat_thread = Thread.new(instance.request) do |req|
14
- loop do
15
- req.post("#{uri}/heartbeat", {}).execute.value
16
- sleep(5)
21
+ heartbeat_thread = Thread.new(instance, options) do |inst, opts|
22
+ begin
23
+ loop do
24
+ payload = {}
25
+
26
+ Polyseerio.log 'debug', 'Sending heartbeat.'
27
+ inst.request.post("#{uri}/heartbeat", payload).execute.value
28
+
29
+ sleep(opts[:delay])
30
+ end
31
+ rescue => err
32
+ Polyseerio.log 'error', 'Heartbeat failed.'
33
+ Polyseerio.log 'error', err.message
17
34
  end
18
35
  end
19
36
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'polyseerio'
3
- gem.version = '0.0.1'
3
+ gem.version = '0.0.2'
4
4
  gem.authors = ['Nicolas Feigenbaum']
5
5
  gem.email = ['support@kognizant.com']
6
6
  gem.description = 'The official Polyseer.io SDK for Ruby.'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyseerio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Feigenbaum
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-23 00:00:00.000000000 Z
11
+ date: 2017-01-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -138,6 +138,7 @@ files:
138
138
  - lib/constant.rb
139
139
  - lib/enum.rb
140
140
  - lib/helper.rb
141
+ - lib/log.rb
141
142
  - lib/middleware.rb
142
143
  - lib/polyseerio.rb
143
144
  - lib/request.rb
@@ -198,7 +199,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
199
  version: '0'
199
200
  requirements: []
200
201
  rubyforge_project:
201
- rubygems_version: 2.4.5.1
202
+ rubygems_version: 2.5.1
202
203
  signing_key:
203
204
  specification_version: 4
204
205
  summary: Polyseer.io SDK for Ruby.