chili_logger 0.0.6 → 0.0.10

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 +1024 -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.6.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 +5 -10
  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 -53
  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,23 +64,18 @@ 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
 
80
75
  def validate_config(config)
81
76
  raise(ChiliLogger::ConfigError, invalid_config_format_error) unless config.is_a?(Hash)
82
77
 
83
- main_config_keys = %i[user password ip port exchange_name routing_key]
78
+ main_config_keys = %i[user password ip port exchange_name]
84
79
  main_config_keys.each { |key| raise(ChiliLogger::ConfigError, config_attr_error(key)) unless config[key] }
85
80
  end
86
81
  end
@@ -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.6"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -1,4 +1,8 @@
1
1
  require 'message_writer/message_writer'
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'
2
6
  require 'byebug'
3
7
 
4
8
  # class for centralizing Creative's logging logic
@@ -8,14 +12,16 @@ class ChiliLogger
8
12
  def initialize(msg_broker, msg_writer)
9
13
  @msg_broker = msg_broker
10
14
  @msg_writer = msg_writer
15
+ @default = ChiliLogger::Values::Default.new
16
+ @user_uniformizer = ChiliLogger::Values::TypeUniformizer::User.new
17
+ @desc_uniformizer = ChiliLogger::Values::TypeUniformizer::Desc.new
18
+ @main_content_uniformizer = ChiliLogger::Values::TypeUniformizer::MainContent.new
11
19
  end
12
20
 
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
21
+ def publish(**options)
22
+ update_current_log(options)
17
23
 
18
- message = @msg_writer.write(desc, agent, main_content)
24
+ message = @msg_writer.write(desc: desc, user: user, main_content: main_content)
19
25
  routing_key = @msg_writer.write_description_tag(desc)
20
26
 
21
27
  @msg_broker.publish(message, routing_key)
@@ -23,98 +29,116 @@ class ChiliLogger
23
29
  end
24
30
 
25
31
  # 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'
32
+ # current log infos are stored in thread variables, so they can be accessed anywhere(controllers, models, tasks...)
33
+ def user
34
+ Thread.current[:current_log_user] ||= @default.user
30
35
  end
31
36
 
32
- def overwrite_agent(agent)
33
- Thread.current[:current_log_agent] = agent
34
- agent
37
+ def update_user(new_user)
38
+ return Thread.current[:current_log_user] = nil if new_user.nil?
39
+
40
+ uniformized_user = @user_uniformizer.user(new_user)
41
+ Thread.current[:current_log_user] = user.merge(uniformized_user)
35
42
  end
36
43
 
37
44
  def desc
38
- Thread.current[:current_log_desc] ||= { type: 'not_specified', service: 'not_specified', action: 'not_specified' }
45
+ Thread.current[:current_log_desc] ||= @default.desc
39
46
  end
40
47
 
41
- def overwrite_type(new_type_name)
42
- desc[:type] = new_type_name || 'not_specified'
48
+ def update_type(new_type_name)
49
+ desc[:type] = @desc_uniformizer.type(new_type_name)
43
50
  end
44
51
 
45
- def overwrite_service(new_service_name)
46
- desc[:service] = new_service_name || 'not_specified'
52
+ def update_service(new_service_name)
53
+ desc[:service] = @desc_uniformizer.service(new_service_name)
47
54
  end
48
55
 
49
- def overwrite_action(new_action_name)
50
- desc[:action] = new_action_name || 'not_specified'
56
+ def update_action(new_action_name)
57
+ desc[:action] = @desc_uniformizer.action(new_action_name)
51
58
  end
52
59
 
53
- def overwrite_desc(new_desc)
60
+ def update_desc(new_desc)
54
61
  return Thread.current[:current_log_desc] = nil if new_desc.nil?
55
- return unless new_desc.is_a?(Hash)
56
62
 
57
- overwrite_desc_items(new_desc, valid_desc_items)
58
- desc
63
+ uniformized_desc = @desc_uniformizer.desc(new_desc)
64
+ Thread.current[:current_log_desc] = desc.merge(uniformized_desc)
59
65
  end
60
66
 
61
67
  def main_content
62
- Thread.current[:current_log_main_content] ||= {}
68
+ Thread.current[:current_log_main_content] ||= @default.main_content
63
69
  end
64
70
 
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)
71
+ def update_main_content(new_content)
72
+ return Thread.current[:current_log_main_content] = nil if new_content.nil?
73
+
74
+ uniform_content = @main_content_uniformizer.main_content(new_content)
75
+ uniform_recs = @main_content_uniformizer.modified_records(uniform_content[:modified_records])
76
+ uniform_errors = @main_content_uniformizer.errors(uniform_content[:errors])
68
77
 
69
- Thread.current[:current_log_main_content] = new_main_content
78
+ # only overwrite if key was explicitly set
79
+ main_content[:modified_records] = uniform_recs if uniform_content.key?(:modified_records)
80
+ main_content[:errors] = uniform_errors if uniform_content.key?(:errors)
70
81
  end
71
82
 
72
83
  def add_to_main_content(new_content)
73
- return unless new_content.is_a?(Hash)
84
+ uniform_content = @main_content_uniformizer.main_content(new_content)
85
+ uniform_recs = uniform_content[:modified_records]
86
+ uniform_errs = uniform_content[:errors]
74
87
 
75
- Thread.current[:current_log_main_content] = main_content.merge(new_content)
88
+ # only overwrite if key was explicitly set
89
+ main_content[:modified_records] = modified_records.merge(uniform_recs) if uniform_recs
90
+ main_content[:errors] = errors + uniform_errs if uniform_errs
76
91
  end
77
92
 
78
93
  def modified_records
79
- main_content[:modified_records] ||= {}
94
+ main_content[:modified_records] ||= @default.modified_records
80
95
  end
81
96
 
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)
97
+ def overwrite_modified_records(new_mod_recs)
98
+ return main_content[:modified_records] = nil if new_mod_recs.nil?
85
99
 
86
- main_content[:modified_records] = modified_records
100
+ uniformized_recs = @main_content_uniformizer.modified_records(new_mod_recs)
101
+ main_content[:modified_records] = uniformized_recs
87
102
  end
88
103
 
89
- def add_modified_record(table_name, modified_record_hash)
90
- return unless table_name
104
+ def add_modified_record(table_name, new_mod_rec)
91
105
  return unless table_name.is_a?(String)
92
- return unless modified_record_hash
93
- return unless modified_record_hash.is_a?(Hash)
94
106
 
107
+ uniformized_rec = @main_content_uniformizer.modified_record(new_mod_rec)
95
108
  modified_records[table_name] ||= []
96
- modified_records[table_name] = modified_records[table_name].push(modified_record_hash)
109
+ modified_records[table_name] = modified_records[table_name].push(uniformized_rec)
97
110
  end
98
111
 
99
- def clear_log_info
100
- overwrite_desc(nil)
101
- overwrite_agent(nil)
102
- overwrite_main_content(nil)
112
+ def errors
113
+ main_content[:errors] ||= @default.log_errors
103
114
  end
104
115
 
105
- private
116
+ def overwrite_errors(new_errors)
117
+ return main_content[:errors] = nil if new_errors.nil?
106
118
 
107
- def valid_desc_items
108
- %i[type service action]
119
+ uniformized_errors = @main_content_uniformizer.errors(new_errors)
120
+ main_content[:errors] = uniformized_errors
109
121
  end
110
122
 
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)
123
+ def add_error(error)
124
+ uniformized_error = @main_content_uniformizer.error(error)
125
+
126
+ main_content[:errors] = errors.push(uniformized_error)
127
+ errors
128
+ end
129
+
130
+ def clear_log_info
131
+ update_desc(nil)
132
+ update_user(nil)
133
+ update_main_content(nil)
134
+ end
135
+
136
+ private
114
137
 
115
- item = new_desc[item_name]
116
- desc[item_name] = item || 'not_specified'
117
- end
138
+ def update_current_log(options)
139
+ update_desc(options[:desc]) if options[:desc]
140
+ update_user(options[:user]) if options[:user]
141
+ update_main_content(options[:main_content]) if options[:main_content]
118
142
  end
119
143
  end
120
144
  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