chili_logger 0.0.7 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/.byebug_history +78 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +9 -1
  5. data/README.md +1029 -35
  6. data/assets/images/chili-logger-flow.png +0 -0
  7. data/assets/images/chiligum-logging-system.gif +0 -0
  8. data/assets/images/rabbit-basic-functioning.png +0 -0
  9. data/assets/images/rabbit-topic-explanation.webp +0 -0
  10. data/chili_logger-0.0.10.gem +0 -0
  11. data/chili_logger-0.0.7.gem +0 -0
  12. data/chili_logger-0.0.8.gem +0 -0
  13. data/chili_logger-0.0.9.1.gem +0 -0
  14. data/chili_logger-0.0.9.gem +0 -0
  15. data/lib/brokers/rabbit_broker.rb +4 -9
  16. data/lib/{errors/logging_error_handler/sqs_handler.rb → brokers/sqs_broker.rb} +4 -14
  17. data/lib/chili_logger.rb +33 -14
  18. data/lib/chili_logger/version.rb +1 -1
  19. data/lib/current_log_accessor.rb +77 -54
  20. data/lib/errors/logging_error_handler/logging_error_handler.rb +23 -14
  21. data/lib/{logs_coverage → helpers/logs_coverage}/chili_logger_coverage.yml +0 -0
  22. data/lib/{logs_coverage → helpers/logs_coverage}/coverage_writer.rb +20 -14
  23. data/lib/{logs_coverage → helpers/logs_coverage}/levantamento_provis/303/263rio.yaml +0 -0
  24. data/lib/helpers/values/default.rb +92 -0
  25. data/lib/helpers/values/type_uniformizer/desc.rb +64 -0
  26. data/lib/helpers/values/type_uniformizer/main_content.rb +62 -0
  27. data/lib/helpers/values/type_uniformizer/user.rb +43 -0
  28. data/lib/message_writer/aws_ops_metadata.rb +1 -1
  29. data/lib/message_writer/message_writer.rb +21 -18
  30. data/log/chili-logger-coverage.yml +50 -1
  31. metadata +19 -7
  32. data/lib/errors/error_messages/rabbitmq_error.rb +0 -9
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,6 +1,5 @@
1
1
  require 'bunny'
2
- require 'errors/error_messages/rabbitmq_error'
3
- require 'errors/logging_error_handler/logging_error_handler'
2
+ require 'brokers/sqs_broker'
4
3
 
5
4
  # class for centralizing Creative's logging logic
6
5
  class ChiliLogger
@@ -18,7 +17,7 @@ class ChiliLogger
18
17
  @ip = config[:ip]
19
18
  @port = config[:port]
20
19
  @exchange_name = config[:exchange_name]
21
- @routing_key_overwriter = config[:routing_key]
20
+ @routing_key_overwriter = config[:routing_key_overwriter]
22
21
 
23
22
  connect
24
23
  end
@@ -48,6 +47,7 @@ class ChiliLogger
48
47
  # temporary solution so we can force configure ChiliLogger to use a specific routing_key
49
48
  key = @routing_key_overwriter || routing_key
50
49
  @exchange.publish(message.to_json, routing_key: key)
50
+ puts "sent message to #{@exchange_name}, with routing_key = '#{key}'"
51
51
 
52
52
  rescue StandardError => e
53
53
  @logging_error_handler.handle_error(e, message)
@@ -64,16 +64,11 @@ class ChiliLogger
64
64
  end
65
65
 
66
66
  def connection_config
67
- # shouldn't try to recover connection,
68
- # it may cause app in production to collapse if reconnection doesn't work or takes too many attempts!!!
69
- recovery_attempts = 0
70
-
71
67
  {
72
68
  user: @user,
73
69
  password: @password,
74
70
  host: @ip,
75
- port: @port,
76
- recovery_attempts: recovery_attempts
71
+ port: @port
77
72
  }
78
73
  end
79
74
 
@@ -1,21 +1,21 @@
1
1
  require 'aws-sdk'
2
- require 'errors/error_messages/rabbitmq_error'
3
2
 
4
3
  # class for centralizing Creative's logging logic
5
4
  class ChiliLogger
6
5
  # module with classes using Amazon Web Services services
7
6
  module AWS
8
7
  # class that handles errors when message broker can't be reached, etc...
9
- class SqsHandler
8
+ class SqsBroker
10
9
  def initialize(config)
11
10
  @sqs_config = validate_config(config)
12
11
  @queue_name = config[:queue_name]
12
+ @default = ChiliLogger::Values::Default.new
13
13
 
14
14
  @sqs = Aws::SQS::Client.new(@sqs_config)
15
15
  end
16
16
 
17
- def handle_error(error = StandardError.new, log = 'not_specified')
18
- message_body = message_body(error, log)
17
+ def publish(message = @default.sqs_message)
18
+ message_body = { body: message }
19
19
  queue_message = queue_message(message_body)
20
20
 
21
21
  @sqs.send_message(queue_message)
@@ -32,16 +32,6 @@ class ChiliLogger
32
32
  }
33
33
  end
34
34
 
35
- def message_body(error, log)
36
- message_body = {
37
- error_type: error.class.name,
38
- error_message: error.message,
39
- log: log
40
- }
41
-
42
- { body: message_body }
43
- end
44
-
45
35
  def queue_url(queue_name)
46
36
  @sqs.get_queue_url(queue_name: queue_name).queue_url
47
37
  end
@@ -1,9 +1,14 @@
1
1
  require 'chili_logger/version'
2
2
  require 'current_log_accessor'
3
3
  require 'errors/error_messages/config_error'
4
+ require 'errors/logging_error_handler/logging_error_handler'
4
5
  require 'brokers/rabbit_broker'
5
6
  require 'message_writer/message_writer'
6
- require 'errors/logging_error_handler/logging_error_handler'
7
+ require 'brokers/sqs_broker'
8
+ require 'helpers/values/default'
9
+ require 'helpers/values/type_uniformizer/desc'
10
+ require 'helpers/values/type_uniformizer/user'
11
+ require 'helpers/values/type_uniformizer/main_content'
7
12
  require 'singleton'
8
13
 
9
14
  # class for centralizing Creative's logging logic
@@ -14,6 +19,13 @@ class ChiliLogger
14
19
  # initialize it with ChiliLogger.instance
15
20
  include Singleton
16
21
 
22
+ def initialize
23
+ @default = ChiliLogger::Values::Default.new
24
+ @user_uniformizer = ChiliLogger::Values::TypeUniformizer::User.new
25
+ @desc_uniformizer = ChiliLogger::Values::TypeUniformizer::Desc.new
26
+ @main_content_uniformizer = ChiliLogger::Values::TypeUniformizer::MainContent.new
27
+ end
28
+
17
29
  def config(config)
18
30
  @deactivated = config[:deactivated] || false # ChilLogger can be deactivated for test environents
19
31
 
@@ -28,19 +40,26 @@ class ChiliLogger
28
40
  @current_log_accessor
29
41
  end
30
42
 
31
- def start_new_log(new_desc = nil, new_agent = nil, new_main_content = nil)
43
+ def start_new_log(**options)
32
44
  current_log.clear_log_info
33
45
 
34
- current_log.overwrite_desc(new_desc)
35
- current_log.overwrite_agent(new_agent)
36
- current_log.overwrite_main_content(new_main_content)
46
+ current_log.update_desc(options[:desc])
47
+ current_log.update_user(options[:user])
48
+ current_log.update_main_content(options[:main_content])
37
49
 
38
50
  current_log
39
51
  end
40
52
 
41
- def publish_instant_log(desc, agent, main_content)
42
- message = @msg_writer.write(desc, agent, main_content)
43
- routing_key = @msg_writer.write_description_tag(desc)
53
+ def publish_instant_log(**options)
54
+ # enforces that all attributes have valid primitive types
55
+ if options
56
+ options[:desc] = @desc_uniformizer.desc(options[:desc])
57
+ options[:user] = @user_uniformizer.user(options[:user])
58
+ options[:main_content] = @main_content_uniformizer.main_content(options[:main_content])
59
+ end
60
+
61
+ message = @msg_writer.write(options)
62
+ routing_key = @msg_writer.write_description_tag(options[:desc])
44
63
 
45
64
  @msg_broker.publish(message, routing_key)
46
65
  end
@@ -48,11 +67,11 @@ class ChiliLogger
48
67
  private
49
68
 
50
69
  def config_logging_error_handler(general_config)
51
- return @logging_error_handler = nil if @deactivated
70
+ return @logging_error_handler = nil if @deactivated
52
71
 
53
- error_handler_name = general_config[:error_handler]
54
- handler_config = general_config[:error_handler_config]
55
- @logging_error_handler = LoggingErrorHandler.new(error_handler_name, handler_config)
72
+ fallback_name = general_config[:fallback_broker]
73
+ fallback_config = general_config[:fallback_broker_config]
74
+ @logging_error_handler = LoggingErrorHandler.new(fallback_name, fallback_config)
56
75
  end
57
76
 
58
77
  def config_msg_writer(general_config)
@@ -67,8 +86,8 @@ class ChiliLogger
67
86
  end
68
87
 
69
88
  def config_msg_broker(general_config)
70
- msg_broker_name = general_config[:msg_broker_name] || :rabbitmq
71
- msg_broker_config = general_config[:msg_broker_config] || {}
89
+ msg_broker_name = general_config[:msg_broker_name] || @default.msg_broker
90
+ msg_broker_config = general_config[:msg_broker_config] || @default.msg_broker_config
72
91
  @msg_broker = supported_msg_brokers[msg_broker_name.to_sym]
73
92
  raise(ConfigError, broker_name_error(msg_broker_name)) unless @msg_broker
74
93
 
@@ -1,3 +1,3 @@
1
1
  class ChiliLogger
2
- VERSION = "0.0.7"
2
+ VERSION = '0.0.11'
3
3
  end
@@ -1,5 +1,8 @@
1
1
  require 'message_writer/message_writer'
2
- require 'byebug'
2
+ require 'helpers/values/default'
3
+ require 'helpers/values/type_uniformizer/desc'
4
+ require 'helpers/values/type_uniformizer/main_content'
5
+ require 'helpers/values/type_uniformizer/user'
3
6
 
4
7
  # class for centralizing Creative's logging logic
5
8
  class ChiliLogger
@@ -8,14 +11,16 @@ class ChiliLogger
8
11
  def initialize(msg_broker, msg_writer)
9
12
  @msg_broker = msg_broker
10
13
  @msg_writer = msg_writer
14
+ @default = ChiliLogger::Values::Default.new
15
+ @user_uniformizer = ChiliLogger::Values::TypeUniformizer::User.new
16
+ @desc_uniformizer = ChiliLogger::Values::TypeUniformizer::Desc.new
17
+ @main_content_uniformizer = ChiliLogger::Values::TypeUniformizer::MainContent.new
11
18
  end
12
19
 
13
- def publish(new_desc = nil, new_agent = nil, new_main_content = nil)
14
- overwrite_desc(new_desc) if new_desc
15
- overwrite_agent(new_agent) if new_agent
16
- overwrite_main_content(new_main_content) if new_main_content
20
+ def publish(**options)
21
+ update_current_log(options)
17
22
 
18
- message = @msg_writer.write(desc, agent, main_content)
23
+ message = @msg_writer.write(desc: desc, user: user, main_content: main_content)
19
24
  routing_key = @msg_writer.write_description_tag(desc)
20
25
 
21
26
  @msg_broker.publish(message, routing_key)
@@ -23,98 +28,116 @@ class ChiliLogger
23
28
  end
24
29
 
25
30
  # ACCESSOR METHODS BELOW
26
- # all relevant infos for the current log are stored in thread variables,
27
- # so they can be accessed anywhere (controllers, models, tasks, etc...)
28
- def agent
29
- Thread.current[:current_log_agent] ||= 'not_specified'
31
+ # current log infos are stored in thread variables, so they can be accessed anywhere(controllers, models, tasks...)
32
+ def user
33
+ Thread.current[:current_log_user] ||= @default.user
30
34
  end
31
35
 
32
- def overwrite_agent(agent)
33
- Thread.current[:current_log_agent] = agent
34
- agent
36
+ def update_user(new_user)
37
+ return Thread.current[:current_log_user] = nil if new_user.nil?
38
+
39
+ uniformized_user = @user_uniformizer.user(new_user)
40
+ Thread.current[:current_log_user] = user.merge(uniformized_user)
35
41
  end
36
42
 
37
43
  def desc
38
- Thread.current[:current_log_desc] ||= { type: 'not_specified', service: 'not_specified', action: 'not_specified' }
44
+ Thread.current[:current_log_desc] ||= @default.desc
39
45
  end
40
46
 
41
- def overwrite_type(new_type_name)
42
- desc[:type] = new_type_name || 'not_specified'
47
+ def update_type(new_type_name)
48
+ desc[:type] = @desc_uniformizer.type(new_type_name)
43
49
  end
44
50
 
45
- def overwrite_service(new_service_name)
46
- desc[:service] = new_service_name || 'not_specified'
51
+ def update_service(new_service_name)
52
+ desc[:service] = @desc_uniformizer.service(new_service_name)
47
53
  end
48
54
 
49
- def overwrite_action(new_action_name)
50
- desc[:action] = new_action_name || 'not_specified'
55
+ def update_action(new_action_name)
56
+ desc[:action] = @desc_uniformizer.action(new_action_name)
51
57
  end
52
58
 
53
- def overwrite_desc(new_desc)
59
+ def update_desc(new_desc)
54
60
  return Thread.current[:current_log_desc] = nil if new_desc.nil?
55
- return unless new_desc.is_a?(Hash)
56
61
 
57
- overwrite_desc_items(new_desc, valid_desc_items)
58
- desc
62
+ uniformized_desc = @desc_uniformizer.desc(new_desc)
63
+ Thread.current[:current_log_desc] = desc.merge(uniformized_desc)
59
64
  end
60
65
 
61
66
  def main_content
62
- Thread.current[:current_log_main_content] ||= {}
67
+ Thread.current[:current_log_main_content] ||= @default.main_content
63
68
  end
64
69
 
65
- def overwrite_main_content(new_main_content)
66
- return Thread.current[:current_log_main_content] = nil if new_main_content.nil?
67
- return unless new_main_content.is_a?(Hash)
70
+ def update_main_content(new_content)
71
+ return Thread.current[:current_log_main_content] = nil if new_content.nil?
72
+
73
+ uniform_content = @main_content_uniformizer.main_content(new_content)
74
+ uniform_recs = @main_content_uniformizer.modified_records(uniform_content[:modified_records])
75
+ uniform_errors = @main_content_uniformizer.errors(uniform_content[:errors])
68
76
 
69
- Thread.current[:current_log_main_content] = new_main_content
77
+ # only overwrite if key was explicitly set
78
+ main_content[:modified_records] = uniform_recs if uniform_content.key?(:modified_records)
79
+ main_content[:errors] = uniform_errors if uniform_content.key?(:errors)
70
80
  end
71
81
 
72
82
  def add_to_main_content(new_content)
73
- return unless new_content.is_a?(Hash)
83
+ uniform_content = @main_content_uniformizer.main_content(new_content)
84
+ uniform_recs = uniform_content[:modified_records]
85
+ uniform_errs = uniform_content[:errors]
74
86
 
75
- Thread.current[:current_log_main_content] = main_content.merge(new_content)
87
+ # only overwrite if key was explicitly set
88
+ main_content[:modified_records] = modified_records.merge(uniform_recs) if uniform_recs
89
+ main_content[:errors] = errors + uniform_errs if uniform_errs
76
90
  end
77
91
 
78
92
  def modified_records
79
- main_content[:modified_records] ||= {}
93
+ main_content[:modified_records] ||= @default.modified_records
80
94
  end
81
95
 
82
- def overwrite_modified_records(modified_records)
83
- return Thread.current[:current_log_main_content] = nil if modified_records.nil?
84
- return unless modified_records.is_a?(Hash)
96
+ def overwrite_modified_records(new_mod_recs)
97
+ return main_content[:modified_records] = nil if new_mod_recs.nil?
85
98
 
86
- main_content[:modified_records] = modified_records
99
+ uniformized_recs = @main_content_uniformizer.modified_records(new_mod_recs)
100
+ main_content[:modified_records] = uniformized_recs
87
101
  end
88
102
 
89
- def add_modified_record(table_name, modified_record_hash)
90
- return unless table_name
103
+ def add_modified_record(table_name, new_mod_rec)
91
104
  return unless table_name.is_a?(String)
92
- return unless modified_record_hash
93
- return unless modified_record_hash.is_a?(Hash)
94
105
 
106
+ uniformized_rec = @main_content_uniformizer.modified_record(new_mod_rec)
95
107
  modified_records[table_name] ||= []
96
- modified_records[table_name] = modified_records[table_name].push(modified_record_hash)
108
+ modified_records[table_name] = modified_records[table_name].push(uniformized_rec)
97
109
  end
98
110
 
99
- def clear_log_info
100
- overwrite_desc(nil)
101
- overwrite_agent(nil)
102
- overwrite_main_content(nil)
111
+ def errors
112
+ main_content[:errors] ||= @default.log_errors
103
113
  end
104
114
 
105
- private
115
+ def overwrite_errors(new_errors)
116
+ return main_content[:errors] = nil if new_errors.nil?
106
117
 
107
- def valid_desc_items
108
- %i[type service action]
118
+ uniformized_errors = @main_content_uniformizer.errors(new_errors)
119
+ main_content[:errors] = uniformized_errors
109
120
  end
110
121
 
111
- def overwrite_desc_items(new_desc, item_names)
112
- item_names.each do |item_name|
113
- next unless new_desc.keys.include?(item_name)
122
+ def add_error(error)
123
+ uniformized_error = @main_content_uniformizer.error(error)
124
+
125
+ main_content[:errors] = errors.push(uniformized_error)
126
+ errors
127
+ end
128
+
129
+ def clear_log_info
130
+ update_desc(nil)
131
+ update_user(nil)
132
+ update_main_content(nil)
133
+ end
134
+
135
+ private
114
136
 
115
- item = new_desc[item_name]
116
- desc[item_name] = item || 'not_specified'
117
- end
137
+ def update_current_log(options)
138
+ update_desc(options[:desc]) if options[:desc]
139
+ update_user(options[:user]) if options[:user]
140
+ update_main_content(options[:main_content]) if options[:main_content]
118
141
  end
119
142
  end
120
143
  end
@@ -1,44 +1,53 @@
1
- require 'errors/logging_error_handler/sqs_handler'
1
+ require 'brokers/sqs_broker'
2
2
  require 'errors/error_messages/config_error'
3
3
 
4
4
  # class for centralizing Creative's logging logic
5
5
  class ChiliLogger
6
6
  # class that handles errors when message broker can't be reached, etc...
7
7
  class LoggingErrorHandler
8
- def initialize(name, config)
9
- raise(ChiliLogger::ConfigError, ':error_handler_config must be present and be a hash') unless config.is_a?(Hash)
8
+ def initialize(fallback_name, config)
9
+ raise(ChiliLogger::ConfigError, ':fallback_broker_config must be present and be a hash') unless config.is_a?(Hash)
10
10
 
11
- name = name.to_sym if name
12
- error_handler_class = supported_error_handlers[name]
13
- unsupported_handler_error unless error_handler_class
11
+ fallback_name = fallback_name.to_sym if fallback_name
12
+ fallback_broker_class = supported_fallback_brokers[fallback_name]
13
+ unsupported_fallback_broker_error unless fallback_broker_class
14
14
 
15
- @error_handler = error_handler_class.new(config)
15
+ @fallback_broker = fallback_broker_class.new(config)
16
16
  end
17
17
 
18
18
  def handle_error(error, log = nil)
19
- @error_handler.handle_error(error, log)
19
+ message = message(error, log)
20
+ @fallback_broker.publish(message)
20
21
  rescue StandardError => e
21
22
  puts '
22
- There was a problem with both the Message Broker and the Logging Error Handler simultaneously.
23
+ There was a problem with both the Message Broker and the Fallback Broker simultaneously.
23
24
  To keep the application running and prevent downtime,
24
25
  ChiliLogger will ignore this errors and discard the log it was currently trying to publish.
25
- Please note that logs are being permanatly lost.
26
+ Please note that logs are being permanently lost.
26
27
  '
27
28
  puts e
28
29
  end
29
30
 
30
31
  private
31
32
 
32
- def unsupported_handler_error
33
- error_handlers = supported_error_handlers.keys.join(', ')
33
+ def message(error, log)
34
+ {
35
+ error_type: error.class.name,
36
+ error_message: error.message,
37
+ log: log
38
+ }
39
+ end
40
+
41
+ def unsupported_fallback_broker_error
42
+ error_handlers = supported_fallback_brokers.keys.join(', ')
34
43
  unsupported_err = ":error_handler must be present and have one of the following values: #{error_handlers}"
35
44
 
36
45
  raise(ChiliLogger::ConfigError, unsupported_err)
37
46
  end
38
47
 
39
- def supported_error_handlers
48
+ def supported_fallback_brokers
40
49
  {
41
- aws_sqs: AWS::SqsHandler
50
+ aws_sqs: AWS::SqsBroker
42
51
  }
43
52
  end
44
53
  end