stackify-api-ruby 1.1.0 → 1.2.3

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: 000465ac09f10d108d71cf5b9441315f01aa42d81b6ff90ce31c053724688536
4
- data.tar.gz: 6b151709a6b4e297b3e1655b1de68297a43d65af1866abf034d64f16df04b438
3
+ metadata.gz: f1f1b93729cecc706d1b21204353788ccad7cce6a4214a62a2a1b846c7bcbd02
4
+ data.tar.gz: 8697a19561e8c73493dee6d8295b85959d10012a796dd4fcee00cdd9d35bf7be
5
5
  SHA512:
6
- metadata.gz: e42e021203b70b6d43535ea43163b42af23ba4528ef0e7a2a377223541ca3122d0773034e66ad0b6e58b77d9b43ea355456efbcad97b9d2432cf134ab3fcf6c3
7
- data.tar.gz: ff51578f76cea1ceed8d89e78570237e2ef0b67266a1f9f92c0f866edd727925ae0a5c0c98c3af662643980a062120e3baef8ffb3cad08cce3310cf91c9015d0
6
+ metadata.gz: e6e1329ed8486fe2e337411152bce6d1acade98c77a230100af90604d931606fe23d898a2684c1815a094e1735904917f2a053997dc0e0aa3e2d26a39d708140
7
+ data.tar.gz: 3e754806858b59d863277a411e99e0c78843a2f67f91622e694776941b7b274e986112e858fe294697f69d3bc4b4fa6fded0cd4570dfccb294909f145580c4db
@@ -10,7 +10,7 @@ module Stackify
10
10
  INTERNAL_LOG_PREFIX = '[Stackify]'.freeze
11
11
  STATUSES = { working: 'working', terminating: 'terminating', terminated: 'terminated'}
12
12
  MODES = { logging: :logging, metrics: :metrics, both: :both }
13
- TRANSPORT = [DEFAULT = 'default', UNIX_SOCKET = 'agent_socket']
13
+ TRANSPORT = [DEFAULT = 'default', UNIX_SOCKET = 'agent_socket', AGENT_HTTP = 'agent_http']
14
14
 
15
15
  autoload :Backtrace, 'stackify/utils/backtrace'
16
16
  autoload :MsgObject, 'stackify/utils/msg_object'
@@ -29,10 +29,12 @@ module Stackify
29
29
  autoload :AddMsgWorker, 'stackify/workers/add_msg_worker'
30
30
  autoload :MsgsQueue, 'stackify/msgs_queue'
31
31
  autoload :LoggerClient, 'stackify/logger_client'
32
- autoload :UnixSocketClient, 'stackify/unix_socket_client'
32
+ autoload :AgentClient, 'stackify/agent_client'
33
33
  autoload :TransportSelector, 'stackify/transport_selector'
34
34
  autoload :LogsSender, 'stackify/logs_sender'
35
+ autoload :AgentBaseSender, 'stackify/agent_base_sender'
35
36
  autoload :UnixSocketSender, 'stackify/unix_socket_sender'
37
+ autoload :AgentHTTPSender, 'stackify/agent_http_sender'
36
38
  autoload :LoggerProxy, 'stackify/logger_proxy'
37
39
  autoload :StackifiedError, 'stackify/error'
38
40
  autoload :StringException, 'stackify/error'
@@ -72,8 +74,8 @@ module Stackify
72
74
  @logger_client ||= Stackify::LoggerClient.new
73
75
  end
74
76
 
75
- def unix_socket_client
76
- @unix_socket_client ||= Stackify::UnixSocketClient.new
77
+ def agent_client
78
+ @agent_client ||= Stackify::AgentClient.new
77
79
  end
78
80
 
79
81
  def get_transport
@@ -145,7 +147,7 @@ module Stackify
145
147
  else
146
148
  Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
147
149
  end
148
- when Stackify::UNIX_SOCKET
150
+ when Stackify::UNIX_SOCKET, Stackify::AGENT_HTTP
149
151
  case Stackify.configuration.mode
150
152
  when MODES[:logging]
151
153
  start_logging
@@ -0,0 +1,54 @@
1
+ #
2
+ # This class will handle the sending of protobuf message to agent
3
+ #
4
+ module Stackify
5
+ class AgentBaseSender < Worker
6
+
7
+ # send_logs() Function to put the msg in the Worker
8
+ def send_logs msgs, attempts = 3
9
+ worker = Stackify::LogsSenderWorker.new('UnixSocketSender worker')
10
+ task = send_logs_task attempts, msgs
11
+ worker.async_perform ScheduleDelay.new, task
12
+ end
13
+
14
+ private
15
+
16
+ def properties
17
+ {
18
+ success_condition: lambda { |result| result.try(:status) == 200 },
19
+ limit: 1
20
+ }.dup
21
+ end
22
+
23
+ def send_logs_task attempts = nil, msgs
24
+ properties[:attempts] = attempts if attempts
25
+ Stackify::ScheduleTask.new properties do
26
+ data = create_log_group msgs
27
+ send_request data
28
+ end
29
+ end
30
+
31
+ # create_log_group() This function will create a log group protobuf object
32
+ # @msgs {Object} Protobuf message
33
+ # return {Object} Return an object
34
+ def create_log_group msgs
35
+ # @details {Object} it will return the properties based in Stackify.setup() configuration
36
+ details = Stackify::Utils.get_app_settings
37
+ log_group = Stackify::LogGroup.new
38
+ msgs.each do |msg|
39
+ log_group.logs << msg
40
+ end
41
+ log_group.environment = details['env'].to_s
42
+ log_group.server_name = details['server_name'].to_s
43
+ log_group.application_name = details['app_name'].to_s
44
+ log_group.application_location = details['app_location'].to_s
45
+ log_group.logger = 'Ruby logger'
46
+ log_group.platform = 'ruby'
47
+ log_group
48
+ end
49
+
50
+ def send_request log_group
51
+ raise NotImplementedError
52
+ end
53
+ end
54
+ end
@@ -1,10 +1,15 @@
1
1
  module Stackify
2
- class UnixSocketClient
2
+ class AgentClient
3
3
 
4
4
  def initialize
5
- Stackify.internal_log :info, '[UnixSocketClient]: initialize()'
5
+ Stackify.internal_log :info, '[AgentClient]: initialize()'
6
6
  @@errors_governor = Stackify::ErrorsGovernor.new
7
- @@sender = Stackify::UnixSocketSender.new
7
+ case Stackify.configuration.transport
8
+ when Stackify::UNIX_SOCKET
9
+ @@sender = Stackify::UnixSocketSender.new
10
+ when Stackify::AGENT_HTTP
11
+ @@sender = Stackify::AgentHTTPSender.new
12
+ end
8
13
  end
9
14
 
10
15
  def log level, msg, call_trace, task
@@ -22,11 +27,11 @@ module Stackify
22
27
  worker.async_perform ScheduleDelay.new, task
23
28
  else
24
29
  Stackify.internal_log :warn,
25
- "UnixSocketClient: logging of exception with message \"#{ex.message}\" is skipped - flood_limit is exceeded"
30
+ "AgentClient: logging of exception with message \"#{ex.message}\" is skipped - flood_limit is exceeded"
26
31
  end
27
32
  end
28
33
  else
29
- Stackify.log_internal_error 'UnixSocketClient: log_exception should get StackifiedError object'
34
+ Stackify.log_internal_error 'AgentClient: log_exception should get StackifiedError object'
30
35
  end
31
36
  end
32
37
 
@@ -0,0 +1,41 @@
1
+ require 'uri'
2
+ require 'faraday'
3
+ require 'ostruct'
4
+
5
+ #
6
+ # This class will handle the sending of protobuf message to agent using http request
7
+ #
8
+ module Stackify
9
+ class AgentHTTPSender < AgentBaseSender
10
+
11
+ HEADERS = {
12
+ 'Content-Type' => 'application/x-protobuf'
13
+ }
14
+
15
+ # send_request() This function will post an http request
16
+ # @msgs {Object} Protobuf message
17
+ # return {Object} Return an object {status, message}
18
+ def send_request log_group
19
+ begin
20
+ # Convert data into binary and send it to agent
21
+ message = Stackify::LogGroup.encode(log_group)
22
+ conn = Faraday.new(proxy: Stackify.configuration.proxy, ssl: { verify: false })
23
+ @response = conn.post do |req|
24
+ req.url URI(Stackify.configuration.http_endpoint + Stackify.configuration.agent_log_url)
25
+ req.headers = HEADERS
26
+ req.body = message
27
+ end
28
+ if @response.try(:status) == 200
29
+ Stackify.internal_log :debug, "[AgentHTTPSender]: Successfully send message via http request."
30
+ return OpenStruct.new({status: 200, msg: 'OK'})
31
+ else
32
+ Stackify.internal_log :debug, "[AgentHTTPSender] Sending failed."
33
+ return OpenStruct.new({status: 500, msg: 'Not OK'})
34
+ end
35
+ rescue => exception
36
+ Stackify.log_internal_error "[AgentHTTPSender] send_logs() Error: #{exception}"
37
+ return OpenStruct.new({status: 500, msg: exception})
38
+ end
39
+ end
40
+ end
41
+ end
@@ -31,7 +31,11 @@ module Stackify
31
31
  end
32
32
 
33
33
  def error_type
34
- @exception.class
34
+ if @exception.class.to_s == 'StringException'
35
+ @exception.message.split(" ")[0].to_s
36
+ else
37
+ @exception.class
38
+ end
35
39
  end
36
40
 
37
41
  def to_h
@@ -4,14 +4,17 @@ module Stackify
4
4
  def initialize
5
5
  @@errors_governor = Stackify::ErrorsGovernor.new
6
6
  @@transport = Stackify::TransportSelector.new(Stackify.configuration.transport).transport
7
+ return if @@transport.nil?
7
8
  end
8
9
 
9
10
  def log level, msg, call_trace
11
+ return if @@transport.nil?
10
12
  task = log_message_task level, msg, call_trace
11
13
  @@transport.log level, msg, call_trace, task
12
14
  end
13
15
 
14
16
  def log_exception level= :error, ex
17
+ return if @@transport.nil?
15
18
  task = log_exception_task level, ex
16
19
  @@transport.log_exception level, ex, task
17
20
  end
@@ -38,10 +41,12 @@ module Stackify
38
41
  end
39
42
 
40
43
  def log_message_task level, msg, call_trace, trans_id=nil, log_uuid=nil
44
+ return if @@transport.nil?
41
45
  @@transport.log_message_task level, msg, call_trace, trans_id, log_uuid
42
46
  end
43
47
 
44
48
  def log_exception_task level, ex, trans_id=nil, log_uuid=nil
49
+ return if @@transport.nil?
45
50
  @@transport.log_exception_task level, ex, trans_id, log_uuid
46
51
  end
47
52
  end
@@ -52,7 +52,7 @@ module Stackify
52
52
  Stackify::Utils.do_only_if_authorized_and_mode_is_on Stackify::MODES[:logging] do
53
53
  old_push(msg)
54
54
  end
55
- when Stackify::UNIX_SOCKET
55
+ when Stackify::UNIX_SOCKET, Stackify::AGENT_HTTP
56
56
  old_push(msg)
57
57
  end
58
58
  end
@@ -9,9 +9,9 @@ module Stackify
9
9
  Stackify::Utils.do_only_if_authorized_and_mode_is_on Stackify::MODES[:logging] do
10
10
  @transport = Stackify::LogsSender.new
11
11
  end
12
- when Stackify::UNIX_SOCKET
13
- @transport = Stackify::UnixSocketClient.new
12
+ when Stackify::UNIX_SOCKET, Stackify::AGENT_HTTP
13
+ @transport = Stackify::AgentClient.new
14
14
  end
15
15
  end
16
16
  end
17
- end
17
+ end
@@ -5,50 +5,7 @@ require 'ostruct'
5
5
  # This class will handle the sending of protobuf message to unix domain socket
6
6
  #
7
7
  module Stackify
8
- class UnixSocketSender < Worker
9
-
10
- # send_logs() Function to put the msg in the Worker
11
- def send_logs msgs, attempts = 3
12
- worker = Stackify::LogsSenderWorker.new('UnixSocketSender worker')
13
- task = send_logs_task attempts, msgs
14
- worker.async_perform ScheduleDelay.new, task
15
- end
16
-
17
- private
18
-
19
- def properties
20
- {
21
- success_condition: lambda { |result| result.try(:status) == 200 },
22
- limit: 1
23
- }.dup
24
- end
25
-
26
- def send_logs_task attempts = nil, msgs
27
- properties[:attempts] = attempts if attempts
28
- Stackify::ScheduleTask.new properties do
29
- data = create_log_group msgs
30
- send_request data
31
- end
32
- end
33
-
34
- # create_log_group() This function will create a log group protobuf object
35
- # @msgs {Object} Protobuf message
36
- # return {Object} Return an object
37
- def create_log_group msgs
38
- # @details {Object} it will return the properties based in Stackify.setup() configuration
39
- details = Stackify::Utils.get_app_settings
40
- log_group = Stackify::LogGroup.new
41
- msgs.each do |msg|
42
- log_group.logs << msg
43
- end
44
- log_group.environment = details['env']
45
- log_group.server_name = details['server_name']
46
- log_group.application_name = details['app_name']
47
- log_group.application_location = details['app_location']
48
- log_group.logger = 'Ruby logger'
49
- log_group.platform = 'ruby'
50
- log_group
51
- end
8
+ class UnixSocketSender < AgentBaseSender
52
9
 
53
10
  # send_request() This function will send http request via unix domain socket
54
11
  # @msgs {Object} Protobuf message
@@ -58,7 +15,7 @@ module Stackify
58
15
  # Convert data into binary and send it to unix domain socket
59
16
  message = Stackify::LogGroup.encode(log_group)
60
17
  client = NetX::HTTPUnix.new('unix://' + Stackify.configuration.unix_socket_path)
61
- req = Net::HTTP::Post.new(Stackify.configuration.unix_socket_url)
18
+ req = Net::HTTP::Post.new(Stackify.configuration.agent_log_url)
62
19
  req.set_content_type('application/x-protobuf')
63
20
  req.body = message
64
21
  response = client.request(req)
@@ -3,16 +3,16 @@ module Stackify
3
3
  class Configuration
4
4
 
5
5
  attr_accessor :api_key, :app_name, :app_location, :env, :log_level, :logger,
6
- :proxy, :mode, :base_api_url, :api_enabled, :transport, :errors
6
+ :proxy, :mode, :base_api_url, :api_enabled, :transport, :errors, :http_endpoint
7
7
 
8
- attr_reader :send_interval, :flood_limit, :queue_max_size, :unix_socket_path, :unix_socket_url
8
+ attr_reader :send_interval, :flood_limit, :queue_max_size, :agent_log_url, :unix_socket_path, :http_endpoint
9
9
 
10
10
  def initialize
11
11
  @base_api_url = 'https://api.stackify.com'
12
12
  @errors = []
13
13
  @app_name = ''
14
14
  @api_key = ''
15
- @transport = 'default'
15
+ @transport = get_env 'STACKIFY_TRANSPORT', 'default'
16
16
  @env = :production
17
17
  @flood_limit = 100
18
18
  @queue_max_size = 10000
@@ -22,23 +22,32 @@ module Stackify
22
22
  @mode = MODES[:both]
23
23
  @logger = Logger.new(STDOUT)
24
24
  @logger.level = Logger::UNKNOWN
25
+ @agent_log_url = '/log'
25
26
  @unix_socket_path = '/usr/local/stackify/stackify.sock'
26
- @unix_socket_url = '/log'
27
+ @http_endpoint = get_env 'STACKIFY_TRANSPORT_HTTP_ENDPOINT', 'https://localhost:10601'
28
+ end
29
+
30
+ def get_env env_key, default
31
+ value = default
32
+ if ENV.keys.include? env_key
33
+ value = ENV[env_key]
34
+ end
35
+ return value
27
36
  end
28
37
 
29
38
  def is_valid?
30
39
  case Stackify.configuration.transport
31
40
  when Stackify::DEFAULT
32
41
  validate_default_transport
33
- when Stackify::UNIX_SOCKET
34
- validate_unix_domain_socket_transport
42
+ when Stackify::UNIX_SOCKET, Stackify::AGENT_HTTP
43
+ validate_agent_transport
35
44
  end
36
45
  @errors.empty?
37
46
  end
38
47
 
39
48
  def validate_transport_type
40
- return true if ['agent_socket', 'default'].include? @transport
41
- @errors << 'Transport should be one of these values: [agent_socket, default]. Should be a String.'
49
+ return true if ['agent_socket', 'agent_http', 'default'].include? @transport
50
+ @errors << 'Transport should be one of these values: [agent_socket, agent_http, default]. Should be a String.'
42
51
  end
43
52
 
44
53
  private
@@ -60,9 +69,9 @@ module Stackify
60
69
  validate_mode_type
61
70
  end
62
71
 
63
- # Perform validation if transport type is agent_socket
72
+ # Perform validation if transport type is agent_socket or agent_http
64
73
  # Required parameters are: env, app_name, log_level
65
- def validate_unix_domain_socket_transport
74
+ def validate_agent_transport
66
75
  validate_env &&
67
76
  validate_transport_type &&
68
77
  validate_app_name &&
@@ -1,3 +1,3 @@
1
1
  module Stackify
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stackify-api-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stackify
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-14 00:00:00.000000000 Z
11
+ date: 2019-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,6 +101,9 @@ files:
101
101
  - lib/generators/stackify/templates/stackify.rb
102
102
  - lib/proto/stackify-agent.rb
103
103
  - lib/stackify-api-ruby.rb
104
+ - lib/stackify/agent_base_sender.rb
105
+ - lib/stackify/agent_client.rb
106
+ - lib/stackify/agent_http_sender.rb
104
107
  - lib/stackify/authorization/authorizable.rb
105
108
  - lib/stackify/authorization/authorization_client.rb
106
109
  - lib/stackify/engine.rb
@@ -124,7 +127,6 @@ files:
124
127
  - lib/stackify/schedule_task.rb
125
128
  - lib/stackify/scheduler.rb
126
129
  - lib/stackify/transport_selector.rb
127
- - lib/stackify/unix_socket_client.rb
128
130
  - lib/stackify/unix_socket_sender.rb
129
131
  - lib/stackify/utils/backtrace.rb
130
132
  - lib/stackify/utils/configuration.rb