Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions lib/proto/stackify-agent.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 53 additions & 18 deletions lib/stackify-api-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
require 'stackify/utils/methods'
require 'core_ext/core_ext' unless defined? Rails

require 'google/protobuf'
require 'proto/stackify-agent.rb'

module Stackify

INTERNAL_LOG_PREFIX = '[Stackify]'.freeze
STATUSES = { working: 'working', terminating: 'terminating', terminated: 'terminated'}
MODES = { logging: :logging, metrics: :metrics, both: :both }
TRANSPORT = [DEFAULT = 'default', UNIX_SOCKET = 'agent_socket']

autoload :Backtrace, 'stackify/utils/backtrace'
autoload :MsgObject, 'stackify/utils/msg_object'
autoload :ProtobufLogObject, 'stackify/utils/protobuf_log_object'
autoload :Configuration, 'stackify/utils/configuration'
autoload :HttpClient, 'stackify/http_client'
autoload :Authorizable, 'stackify/authorization/authorizable'
Expand All @@ -24,7 +29,10 @@ module Stackify
autoload :AddMsgWorker, 'stackify/workers/add_msg_worker'
autoload :MsgsQueue, 'stackify/msgs_queue'
autoload :LoggerClient, 'stackify/logger_client'
autoload :UnixSocketClient, 'stackify/unix_socket_client'
autoload :TransportSelector, 'stackify/transport_selector'
autoload :LogsSender, 'stackify/logs_sender'
autoload :UnixSocketSender, 'stackify/unix_socket_sender'
autoload :LoggerProxy, 'stackify/logger_proxy'
autoload :StackifiedError, 'stackify/error'
autoload :StringException, 'stackify/error'
Expand All @@ -44,6 +52,7 @@ def configuration
def setup
@workers = []
yield(configuration) if block_given?
configuration.validate_transport_type
if configuration.is_valid?
@status = STATUSES[:working]
else
Expand All @@ -53,7 +62,6 @@ def setup
end
raise msg
end

end

def msgs_queue
Expand All @@ -64,8 +72,16 @@ def logger_client
@logger_client ||= Stackify::LoggerClient.new
end

def logs_sender
@logs_sender ||= Stackify::LogsSender.new
def unix_socket_client
@unix_socket_client ||= Stackify::UnixSocketClient.new
end

def get_transport
@logger_client.get_transport
end

def send_unix_socket
@unix_socket ||= Stackify::UnixSocketSender.new
end

def logger
Expand Down Expand Up @@ -105,24 +121,43 @@ def internal_log level, msg

def run
Stackify::Utils.is_api_enabled
Stackify.internal_log :debug, "Stackify.run = #{Stackify.configuration.transport}"
if Stackify.configuration.api_enabled
if Stackify.is_valid?
at_exit { make_remained_job }
t1 = Thread.new { Stackify.authorize }
case Stackify.configuration.mode
when MODES[:both]
t2 = start_logging
t3 = start_metrics
when MODES[:logging]
t2 = start_logging
when MODES[:metrics]
t3 = start_metrics
# check transport types
case Stackify.configuration.transport
when Stackify::DEFAULT
if Stackify.is_valid?
at_exit { make_remained_job }
t1 = Thread.new { Stackify.authorize }
case Stackify.configuration.mode
when MODES[:both]
t2 = start_logging
t3 = start_metrics
when MODES[:logging]
t2 = start_logging
when MODES[:metrics]
t3 = start_metrics
end

t1.join
t3.join if t3
else
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
end
when Stackify::UNIX_SOCKET
case Stackify.configuration.mode
when MODES[:logging]
start_logging
when MODES[:both]
start_logging
start_metrics
when MODES[:metrics]
start_metrics
end
else
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
end

t1.join
t3.join if t3
else
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
end
end
end
Expand Down
64 changes: 11 additions & 53 deletions lib/stackify/logger_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,21 @@ class LoggerClient

def initialize
@@errors_governor = Stackify::ErrorsGovernor.new
@@transport = Stackify::TransportSelector.new(Stackify.configuration.transport).transport
end

def log level, msg, call_trace
Stackify::Utils.do_only_if_authorized_and_mode_is_on Stackify::MODES[:logging] do
if acceptable?(level, msg) && Stackify.working?
worker = Stackify::AddMsgWorker.new
task = log_message_task level, msg, call_trace
worker.async_perform ScheduleDelay.new, task
end
end
task = log_message_task level, msg, call_trace
@@transport.log level, msg, call_trace, task
end

def log_exception level= :error, ex
if ex.is_a?(Stackify::StackifiedError)
Stackify::Utils.do_only_if_authorized_and_mode_is_on Stackify::MODES[:logging] do
if acceptable?(level, ex.message) && Stackify.working?
if @@errors_governor.can_send? ex
worker = Stackify::AddMsgWorker.new
task = log_exception_task level, ex
worker.async_perform ScheduleDelay.new, task
else
Stackify.internal_log :warn,
"LoggerClient: logging of exception with message \"#{ex.message}\" is skipped - flood_limit is exceeded"
end
end
end
else
Stackify.log_internal_error 'LoggerClient: log_exception should get StackifiedError object'
end
task = log_exception_task level, ex
@@transport.log_exception level, ex, task
end

def get_transport
@@transport
end

private
Expand All @@ -52,39 +38,11 @@ def is_correct_log_level? level
end

def log_message_task level, msg, call_trace, trans_id=nil, log_uuid=nil
Stackify::ScheduleTask.new ({limit: 1}) do
if %w(error fatal).include?(level)
ex = if ruby_exception?(msg) && msg.class != Class
msg.set_backtrace(call_trace)
msg
else
e = StringException.new(msg)
e.set_backtrace(call_trace)
e
end
ex = StackifiedError.new(ex, binding())
Stackify.msgs_queue << Stackify::MsgObject.new(level, ex.message, caller[0], trans_id, log_uuid, ex).to_h
else
Stackify.msgs_queue << Stackify::MsgObject.new(level, msg, caller[0], trans_id, log_uuid).to_h
end
end
@@transport.log_message_task level, msg, call_trace, trans_id, log_uuid
end

def log_exception_task level, ex, trans_id=nil, log_uuid=nil
Stackify::ScheduleTask.new ({limit: 1}) do
Stackify.msgs_queue << Stackify::MsgObject.new(level, ex.message, caller[0], trans_id, log_uuid, ex).to_h
end
end

def ruby_exception? klass
klass = klass.class == Class ? klass : klass.class
klasses = [klass]
while klass != Object do
klasses << klass.superclass
klass = klass.superclass
end
klasses.include?(Exception)
@@transport.log_exception_task level, ex, trans_id, log_uuid
end
end

end
Loading