Skip to content

Commit 5566a53

Browse files
slashd0tdarinhoward
authored andcommitted
RT 3680 Add socket transport to Ruby Logging API (#26)
RT-3680 - adding in socket transport feature
1 parent 1e065cb commit 5566a53

13 files changed

+606
-82
lines changed

lib/proto/stackify-agent.rb

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/stackify-api-ruby.rb

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
require 'stackify/utils/methods'
33
require 'core_ext/core_ext' unless defined? Rails
44

5+
require 'google/protobuf'
6+
require 'proto/stackify-agent.rb'
7+
58
module Stackify
69

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

1115
autoload :Backtrace, 'stackify/utils/backtrace'
1216
autoload :MsgObject, 'stackify/utils/msg_object'
17+
autoload :ProtobufLogObject, 'stackify/utils/protobuf_log_object'
1318
autoload :Configuration, 'stackify/utils/configuration'
1419
autoload :HttpClient, 'stackify/http_client'
1520
autoload :Authorizable, 'stackify/authorization/authorizable'
@@ -24,7 +29,10 @@ module Stackify
2429
autoload :AddMsgWorker, 'stackify/workers/add_msg_worker'
2530
autoload :MsgsQueue, 'stackify/msgs_queue'
2631
autoload :LoggerClient, 'stackify/logger_client'
32+
autoload :UnixSocketClient, 'stackify/unix_socket_client'
33+
autoload :TransportSelector, 'stackify/transport_selector'
2734
autoload :LogsSender, 'stackify/logs_sender'
35+
autoload :UnixSocketSender, 'stackify/unix_socket_sender'
2836
autoload :LoggerProxy, 'stackify/logger_proxy'
2937
autoload :StackifiedError, 'stackify/error'
3038
autoload :StringException, 'stackify/error'
@@ -44,6 +52,7 @@ def configuration
4452
def setup
4553
@workers = []
4654
yield(configuration) if block_given?
55+
configuration.validate_transport_type
4756
if configuration.is_valid?
4857
@status = STATUSES[:working]
4958
else
@@ -53,7 +62,6 @@ def setup
5362
end
5463
raise msg
5564
end
56-
5765
end
5866

5967
def msgs_queue
@@ -64,8 +72,16 @@ def logger_client
6472
@logger_client ||= Stackify::LoggerClient.new
6573
end
6674

67-
def logs_sender
68-
@logs_sender ||= Stackify::LogsSender.new
75+
def unix_socket_client
76+
@unix_socket_client ||= Stackify::UnixSocketClient.new
77+
end
78+
79+
def get_transport
80+
@logger_client.get_transport
81+
end
82+
83+
def send_unix_socket
84+
@unix_socket ||= Stackify::UnixSocketSender.new
6985
end
7086

7187
def logger
@@ -105,24 +121,43 @@ def internal_log level, msg
105121

106122
def run
107123
Stackify::Utils.is_api_enabled
124+
Stackify.internal_log :debug, "Stackify.run = #{Stackify.configuration.transport}"
108125
if Stackify.configuration.api_enabled
109126
if Stackify.is_valid?
110-
at_exit { make_remained_job }
111-
t1 = Thread.new { Stackify.authorize }
112-
case Stackify.configuration.mode
113-
when MODES[:both]
114-
t2 = start_logging
115-
t3 = start_metrics
116-
when MODES[:logging]
117-
t2 = start_logging
118-
when MODES[:metrics]
119-
t3 = start_metrics
127+
# check transport types
128+
case Stackify.configuration.transport
129+
when Stackify::DEFAULT
130+
if Stackify.is_valid?
131+
at_exit { make_remained_job }
132+
t1 = Thread.new { Stackify.authorize }
133+
case Stackify.configuration.mode
134+
when MODES[:both]
135+
t2 = start_logging
136+
t3 = start_metrics
137+
when MODES[:logging]
138+
t2 = start_logging
139+
when MODES[:metrics]
140+
t3 = start_metrics
141+
end
142+
143+
t1.join
144+
t3.join if t3
145+
else
146+
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
147+
end
148+
when Stackify::UNIX_SOCKET
149+
case Stackify.configuration.mode
150+
when MODES[:logging]
151+
start_logging
152+
when MODES[:both]
153+
start_logging
154+
start_metrics
155+
when MODES[:metrics]
156+
start_metrics
157+
end
158+
else
159+
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
120160
end
121-
122-
t1.join
123-
t3.join if t3
124-
else
125-
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
126161
end
127162
end
128163
end

lib/stackify/logger_client.rb

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,21 @@ class LoggerClient
33

44
def initialize
55
@@errors_governor = Stackify::ErrorsGovernor.new
6+
@@transport = Stackify::TransportSelector.new(Stackify.configuration.transport).transport
67
end
78

89
def log level, msg, call_trace
9-
Stackify::Utils.do_only_if_authorized_and_mode_is_on Stackify::MODES[:logging] do
10-
if acceptable?(level, msg) && Stackify.working?
11-
worker = Stackify::AddMsgWorker.new
12-
task = log_message_task level, msg, call_trace
13-
worker.async_perform ScheduleDelay.new, task
14-
end
15-
end
10+
task = log_message_task level, msg, call_trace
11+
@@transport.log level, msg, call_trace, task
1612
end
1713

1814
def log_exception level= :error, ex
19-
if ex.is_a?(Stackify::StackifiedError)
20-
Stackify::Utils.do_only_if_authorized_and_mode_is_on Stackify::MODES[:logging] do
21-
if acceptable?(level, ex.message) && Stackify.working?
22-
if @@errors_governor.can_send? ex
23-
worker = Stackify::AddMsgWorker.new
24-
task = log_exception_task level, ex
25-
worker.async_perform ScheduleDelay.new, task
26-
else
27-
Stackify.internal_log :warn,
28-
"LoggerClient: logging of exception with message \"#{ex.message}\" is skipped - flood_limit is exceeded"
29-
end
30-
end
31-
end
32-
else
33-
Stackify.log_internal_error 'LoggerClient: log_exception should get StackifiedError object'
34-
end
15+
task = log_exception_task level, ex
16+
@@transport.log_exception level, ex, task
17+
end
18+
19+
def get_transport
20+
@@transport
3521
end
3622

3723
private
@@ -52,39 +38,11 @@ def is_correct_log_level? level
5238
end
5339

5440
def log_message_task level, msg, call_trace, trans_id=nil, log_uuid=nil
55-
Stackify::ScheduleTask.new ({limit: 1}) do
56-
if %w(error fatal).include?(level)
57-
ex = if ruby_exception?(msg) && msg.class != Class
58-
msg.set_backtrace(call_trace)
59-
msg
60-
else
61-
e = StringException.new(msg)
62-
e.set_backtrace(call_trace)
63-
e
64-
end
65-
ex = StackifiedError.new(ex, binding())
66-
Stackify.msgs_queue << Stackify::MsgObject.new(level, ex.message, caller[0], trans_id, log_uuid, ex).to_h
67-
else
68-
Stackify.msgs_queue << Stackify::MsgObject.new(level, msg, caller[0], trans_id, log_uuid).to_h
69-
end
70-
end
41+
@@transport.log_message_task level, msg, call_trace, trans_id, log_uuid
7142
end
7243

7344
def log_exception_task level, ex, trans_id=nil, log_uuid=nil
74-
Stackify::ScheduleTask.new ({limit: 1}) do
75-
Stackify.msgs_queue << Stackify::MsgObject.new(level, ex.message, caller[0], trans_id, log_uuid, ex).to_h
76-
end
77-
end
78-
79-
def ruby_exception? klass
80-
klass = klass.class == Class ? klass : klass.class
81-
klasses = [klass]
82-
while klass != Object do
83-
klasses << klass.superclass
84-
klass = klass.superclass
85-
end
86-
klasses.include?(Exception)
45+
@@transport.log_exception_task level, ex, trans_id, log_uuid
8746
end
8847
end
89-
9048
end

0 commit comments

Comments
 (0)