chili_logger 0.0.0 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 517d555f3acba9a85b81507c6f321a89c17c1d20fb80eaa6b8ab01290c55fc83
4
- data.tar.gz: 68a9d265d5ea5df6b7869b912a091d7271e5fe75b8a1990f31cd1b05dcffd385
3
+ metadata.gz: e49414c8428f038f87301e96eace682892b68a6962c0e3f2081beaf60ebd9569
4
+ data.tar.gz: 2fa17c2d0599ba5beba8f483e9514b6ef25d9ed093b71a802adc9d8e27b529e6
5
5
  SHA512:
6
- metadata.gz: 58c4352a1f71b36ed91b07220391dcdbec09610a4645ed8202bf84fa288e78d976d56917dc999b4f7c548579bd82fe90b822cf04f391e04d0df4183b710fda89
7
- data.tar.gz: 4bb3cbbc0f2dbac7e7ce8d26842e30ff282ccd2b35b908b01c1048d3b02fe2f5e7d0f34d7f49afe6a83634cb342a5335438224b65c3b84c054cf394ccacb5359
6
+ metadata.gz: d1d36617a2c983490b0edc92eca2c9b8f4f96e701bdbb2aabaf0b9abf676b2b34d75e5dc8b712b2b1573ffd5a73a5438a5719d3e88656fbd5228d56ae34b7dc2
7
+ data.tar.gz: ebd5d8ef5bfebc12bab504975e17c19850664701f811765aa06a30a9ce9f40c2cec05f26e50f0f8d993938d30fc62d9af67fc36f47b010a16b567a9e58b058e9
@@ -1,4 +1,38 @@
1
1
  continue
2
+ e.class
3
+ e.is_a?(Bunny:TCPConnectionFailed)
4
+ e
5
+ @sqs.send_message('queue_message')
6
+ @sqs.get_queue_url(queue_name: 'mockeesd').queue_url
7
+ @sqs.get_queue_url(queue_name: 'mock').queue_url
8
+ @sqs.get_queue_url(queue_name: queue_name).queue_url
9
+ @sqs
10
+ response
11
+ continue
12
+ cloud_metadata[@cloud_provider]
13
+ @cloud_provider
14
+ continue
15
+ @cloud_provider
16
+ continue
17
+ @cloud_provider
18
+ continue
19
+ @cloud_provider
20
+ continue
21
+ @cloud_provider
22
+ continue
23
+ @cloud_provider
24
+ continue
25
+ @cloud_provider
26
+ continue
27
+ @cloud_provider
28
+ continue
29
+ cloud_metadata[nil]
30
+ @cloud_provider
31
+ continue
32
+ @ip
33
+ @user
34
+ @port
35
+ continue
2
36
  msg[type.to_sym]
3
37
  msg[type]
4
38
  pp msg
data/.gitignore CHANGED
@@ -7,5 +7,7 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ local_tests_cheatsheet.rb
11
+
10
12
  # rspec failure tracking
11
13
  .rspec_status
data/Gemfile CHANGED
@@ -3,11 +3,12 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in chili_logger.gemspec
4
4
  gemspec
5
5
 
6
- gem "activerecord", "~> 5.2"
6
+ gem 'activerecord', '~> 5.2'
7
+ gem 'aws-sdk', '2.9.2'
7
8
  gem 'bunny'
8
9
  gem 'byebug'
9
10
  gem 'rake', '~> 12.0'
10
11
  gem 'rspec', '~> 3.0'
11
12
  gem 'rubocop', '~> 0.60.0', require: false
12
13
 
13
- gem "httparty", "~> 0.18.1"
14
+ gem 'httparty', '~> 0.18.1'
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chili_logger (0.1.0)
4
+ chili_logger (0.0.7)
5
+ aws-sdk (~> 2.9, >= 2.9.0)
5
6
  bunny
6
7
  httparty
7
8
 
@@ -21,29 +22,40 @@ GEM
21
22
  tzinfo (~> 1.1)
22
23
  amq-protocol (2.3.1)
23
24
  arel (9.0.0)
24
- ast (2.4.0)
25
+ ast (2.4.1)
26
+ aws-eventstream (1.1.0)
27
+ aws-sdk (2.9.2)
28
+ aws-sdk-resources (= 2.9.2)
29
+ aws-sdk-core (2.9.2)
30
+ aws-sigv4 (~> 1.0)
31
+ jmespath (~> 1.0)
32
+ aws-sdk-resources (2.9.2)
33
+ aws-sdk-core (= 2.9.2)
34
+ aws-sigv4 (1.1.4)
35
+ aws-eventstream (~> 1.0, >= 1.0.2)
25
36
  bunny (2.15.0)
26
37
  amq-protocol (~> 2.3, >= 2.3.1)
27
38
  byebug (11.0.1)
28
39
  concurrent-ruby (1.1.6)
29
- diff-lcs (1.3)
40
+ diff-lcs (1.4.2)
30
41
  httparty (0.18.1)
31
42
  mime-types (~> 3.0)
32
43
  multi_xml (>= 0.5.2)
33
44
  i18n (1.8.3)
34
45
  concurrent-ruby (~> 1.0)
35
46
  jaro_winkler (1.5.4)
47
+ jmespath (1.4.0)
36
48
  mime-types (3.3.1)
37
49
  mime-types-data (~> 3.2015)
38
50
  mime-types-data (3.2020.0512)
39
51
  minitest (5.14.1)
40
52
  multi_xml (0.6.0)
41
- parallel (1.19.1)
42
- parser (2.7.1.3)
43
- ast (~> 2.4.0)
53
+ parallel (1.19.2)
54
+ parser (2.7.1.4)
55
+ ast (~> 2.4.1)
44
56
  powerpack (0.1.2)
45
57
  rainbow (3.0.0)
46
- rake (12.3.1)
58
+ rake (12.3.3)
47
59
  rspec (3.9.0)
48
60
  rspec-core (~> 3.9.0)
49
61
  rspec-expectations (~> 3.9.0)
@@ -76,6 +88,7 @@ PLATFORMS
76
88
 
77
89
  DEPENDENCIES
78
90
  activerecord (~> 5.2)
91
+ aws-sdk (= 2.9.2)
79
92
  bunny
80
93
  byebug
81
94
  chili_logger!
@@ -85,4 +98,4 @@ DEPENDENCIES
85
98
  rubocop (~> 0.60.0)
86
99
 
87
100
  BUNDLED WITH
88
- 2.1.4
101
+ 1.17.3
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # ChiliLogger
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/chili_logger`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ ChiliLogger is gem developed for internal use in Chiligum Creatives. It is used to monitor our applications and generate logs with some uniformization.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## How it works
6
+
7
+ ---
6
8
 
7
9
  ## Installation
8
10
 
@@ -21,8 +23,9 @@ Or install it yourself as:
21
23
  $ gem install chili_logger
22
24
 
23
25
 
24
-
26
+ Create a initializer file and configure ChiliLogger passing all the relevant informations:
25
27
  ```ruby
28
+ # config/initializers/chili_logger.rb
26
29
  require 'chili_logger'
27
30
 
28
31
  is_deactivated = Rails.env.test?
@@ -31,35 +34,70 @@ ChiliLogger.instance.config({
31
34
  deactivated: is_deactivated,
32
35
  log_env: 'development',
33
36
  log_layer: 'creatives',
34
- server_url: ENV['server_url'],
35
- cloud_provider: :aws,
37
+ server_url: ENV['SERVER_URL'],
38
+ cloud_provider: ENV['CLOUD_PROVIDER'],
36
39
  msg_broker_name: :rabbitmq,
37
40
  msg_broker_config: {
38
- user: 'agent',
39
- password: 'Ag2Rbt$29211620',
40
- ip: '54.164.142.164',
41
- port: '5672',
42
- exchange_name: 'platform-topic',
43
- routing_key: 'logstash'
41
+ user: ENV['RABBIT_USER'],
42
+ password: ENV['RABBIT_PASSWORD'],
43
+ ip: ENV['RABBIT_IP'],
44
+ port: ENV['RABBIT_PORT'],
45
+ exchange_name: ENV['RABBIT_EXCHANGE'],
46
+ routing_key: ENV['RABBIT_ROUTING_KEY']
47
+ },
48
+ error_handler: :aws_sqs,
49
+ error_handler_config: {
50
+ region: ENV['CHILI_LOGGER_SQS_HANDLER_REGION'],
51
+ access_key_id: ENV['CHILI_LOGGER_SQS_HANDLER_KEY_ID'],
52
+ secret_access_key: ENV['CHILI_LOGGER_SQS_HANDLER_SECRET_KEY'],
53
+ queue_name: ENV['CHILI_LOGGER_SQS_HANDLER_QUEUE_NAME']
44
54
  }
45
55
  })
46
56
  ```
47
57
 
58
+ When configuring ChiliLogger, you MUST set msg_broker_name and msg_broker_config. All logs are published to this message broker and, without it, ChiliLogger can't work. For the time being, only :rabbitmq is supported.
59
+
60
+ You ALSO MUST set error_handler and error_handler_config. The error handler is the fallback option if the message broker is not available and, without it, ChiliLogger will break the application every time the message broker is down. For the time being, only :aws_sqs is supported.
61
+
62
+ Please note that if ChiliLogger tries to publish a log and both the Message Broker and the Error Handler are simultaneously down, the log will be discarded. This behavior makes sure logging problems never cause the app to break.
63
+
64
+
65
+ ---
66
+
67
+ ## Basic Usage
68
+ The easiest way to use ChiliLogger is with the `publish_instant_log` method. It requires three arguments: `desc`, `agent` and `main_content`, which are used to create the log. All of them are optional arguments and have default values:
69
+ ```ruby
70
+ desc = { type: }
71
+ ChiliLogger.instance.publish_instant_log(desc, agent, log)
48
72
 
49
73
 
74
+ ChiliLogger.instance.publish_instant_log
75
+ ```
50
76
 
51
77
 
52
78
 
53
- ## Usage
79
+ Notice that ChiliLogger uses the Singleton pattern. So, to call its methods, you must first access the instance method. Calling ChiliLogger.new will return an error:
80
+ ```ruby
81
+ ChiliLogger.publish_instant_log(desc, agent, log)
82
+ # NoMethodError: private method `new' called for ChiliLogger:Class
54
83
 
55
- TODO: Write usage instructions here
84
+ ChiliLogger.instance.publish_instant_log(desc, agent, log)
85
+ # works fine
86
+ ```
56
87
 
88
+ ---
89
+
90
+ ## Advanced Usage
91
+
92
+ ---
57
93
  ## Development
58
94
 
59
95
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
96
 
61
97
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
98
 
99
+ ---
100
+
63
101
  ## Contributing
64
102
 
65
103
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chili_logger.
Binary file
@@ -11,6 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.homepage = 'https://gitlab.com/chiligumdev/chili_logger'
12
12
  spec.license = 'MIT'
13
13
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+ spec.add_dependency 'aws-sdk', '~> 2.9', '>= 2.9.0'
14
15
  spec.add_dependency 'bunny'
15
16
  spec.add_dependency 'httparty'
16
17
 
@@ -1,10 +1,15 @@
1
1
  require 'bunny'
2
- require 'errors/rabbitmq_error'
2
+ require 'errors/error_messages/rabbitmq_error'
3
+ require 'errors/logging_error_handler/logging_error_handler'
3
4
 
4
5
  # class for centralizing Creative's logging logic
5
6
  class ChiliLogger
6
7
  # class that configures and manages the RabbitMQ client
7
8
  class RabbitBroker
9
+ def initialize(logging_error_handler)
10
+ @logging_error_handler = logging_error_handler
11
+ end
12
+
8
13
  def config(config)
9
14
  validate_config(config)
10
15
 
@@ -13,7 +18,7 @@ class ChiliLogger
13
18
  @ip = config[:ip]
14
19
  @port = config[:port]
15
20
  @exchange_name = config[:exchange_name]
16
- @routing_key = config[:routing_key]
21
+ @routing_key_overwriter = config[:routing_key]
17
22
 
18
23
  connect
19
24
  end
@@ -21,15 +26,15 @@ class ChiliLogger
21
26
  def connect
22
27
  return if ChiliLogger.instance.deactivated
23
28
 
24
- @connection = Bunny.new("amqp://#{@user}:#{@password}@#{@ip}:#{@port}")
29
+ @connection = Bunny.new(connection_config)
25
30
  @connection.start
26
31
 
27
32
  @channel = @connection.create_channel
28
33
  @exchange = @channel.topic(@exchange_name, durable: true)
29
34
 
30
- # wraps errors in a ChiliLogger class, so it's easier for user to see that the problem happened in chili_logger gem
31
- rescue Bunny::Exception => e
32
- raise(ChiliLogger::RabbitMQError, e.message)
35
+ rescue StandardError => e
36
+ puts 'Could not connect to RabbitMQ due to the following error:'
37
+ puts e
33
38
  end
34
39
 
35
40
  def disconnect
@@ -40,25 +45,42 @@ class ChiliLogger
40
45
  return if ChiliLogger.instance.deactivated
41
46
 
42
47
  # if no routing_key was provided when configuring RabbitBroker, than use the one received by the method here
43
- # temporary solution so we can test, while we don't implement correct routing_keys in logstash/elasticsearch
44
- key = @routing_key || routing_key
48
+ # temporary solution so we can force configure ChiliLogger to use a specific routing_key
49
+ key = @routing_key_overwriter || routing_key
45
50
  @exchange.publish(message.to_json, routing_key: key)
51
+
52
+ rescue StandardError => e
53
+ @logging_error_handler.handle_error(e, message)
46
54
  end
47
55
 
48
56
  private
49
57
 
50
58
  def invalid_config_format_error
51
- 'The configuration object must include a Hash'
59
+ 'The configuration object must be a Hash'
52
60
  end
53
61
 
54
62
  def config_attr_error(attr)
55
63
  "The configuration object must include a ':#{attr}' attribute"
56
64
  end
57
65
 
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
+ {
72
+ user: @user,
73
+ password: @password,
74
+ host: @ip,
75
+ port: @port,
76
+ recovery_attempts: recovery_attempts
77
+ }
78
+ end
79
+
58
80
  def validate_config(config)
59
81
  raise(ChiliLogger::ConfigError, invalid_config_format_error) unless config.is_a?(Hash)
60
82
 
61
- main_config_keys = %i[user password ip port exchange_name routing_key]
83
+ main_config_keys = %i[user password ip port exchange_name]
62
84
  main_config_keys.each { |key| raise(ChiliLogger::ConfigError, config_attr_error(key)) unless config[key] }
63
85
  end
64
86
  end
@@ -1,7 +1,9 @@
1
1
  require 'chili_logger/version'
2
- require 'errors/config_error'
2
+ require 'current_log_accessor'
3
+ require 'errors/error_messages/config_error'
3
4
  require 'brokers/rabbit_broker'
4
5
  require 'message_writer/message_writer'
6
+ require 'errors/logging_error_handler/logging_error_handler'
5
7
  require 'singleton'
6
8
 
7
9
  # class for centralizing Creative's logging logic
@@ -15,6 +17,7 @@ class ChiliLogger
15
17
  def config(config)
16
18
  @deactivated = config[:deactivated] || false # ChilLogger can be deactivated for test environents
17
19
 
20
+ config_logging_error_handler(config)
18
21
  config_msg_writer(config)
19
22
  config_msg_broker(config)
20
23
 
@@ -44,6 +47,14 @@ class ChiliLogger
44
47
 
45
48
  private
46
49
 
50
+ def config_logging_error_handler(general_config)
51
+ return @logging_error_handler = nil if @deactivated
52
+
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)
56
+ end
57
+
47
58
  def config_msg_writer(general_config)
48
59
  msg_writer_config = {
49
60
  env: general_config[:log_env],
@@ -55,12 +66,17 @@ class ChiliLogger
55
66
  @msg_writer = MessageWriter.new(msg_writer_config)
56
67
  end
57
68
 
58
- def config_msg_broker(config)
59
- msg_broker_name = config[:msg_broker_name] || :rabbitmq
60
- msg_broker_config = config[:msg_broker_config] || {}
69
+ 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] || {}
61
72
  @msg_broker = supported_msg_brokers[msg_broker_name.to_sym]
62
73
  raise(ConfigError, broker_name_error(msg_broker_name)) unless @msg_broker
63
74
 
75
+ # TODO: implement MessageBroker class, an interface between ChiliLogger and possible brokers.
76
+ # TODO: add "supported_msg_broker", "broker_name_error" and "return @msg_broker if @deactivated" to MessageBroker
77
+ # prevents broker from even starting connection if ChiliLogger is deactivated
78
+ return @msg_broker if @deactivated
79
+
64
80
  @msg_broker.config(msg_broker_config)
65
81
  end
66
82
 
@@ -70,7 +86,7 @@ class ChiliLogger
70
86
 
71
87
  def supported_msg_brokers
72
88
  {
73
- rabbitmq: RabbitBroker.new
89
+ rabbitmq: RabbitBroker.new(@logging_error_handler)
74
90
  }
75
91
  end
76
92
  end
@@ -1,3 +1,3 @@
1
1
  class ChiliLogger
2
- VERSION = '0.0.0'
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,45 @@
1
+ require 'errors/logging_error_handler/sqs_handler'
2
+ require 'errors/error_messages/config_error'
3
+
4
+ # class for centralizing Creative's logging logic
5
+ class ChiliLogger
6
+ # class that handles errors when message broker can't be reached, etc...
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)
10
+
11
+ name = name.to_sym if name
12
+ error_handler_class = supported_error_handlers[name]
13
+ unsupported_handler_error unless error_handler_class
14
+
15
+ @error_handler = error_handler_class.new(config)
16
+ end
17
+
18
+ def handle_error(error, log = nil)
19
+ @error_handler.handle_error(error, log)
20
+ rescue StandardError => e
21
+ puts '
22
+ There was a problem with both the Message Broker and the Logging Error Handler simultaneously.
23
+ To keep the application running and prevent downtime,
24
+ 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
+ '
27
+ puts e
28
+ end
29
+
30
+ private
31
+
32
+ def unsupported_handler_error
33
+ error_handlers = supported_error_handlers.keys.join(', ')
34
+ unsupported_err = ":error_handler must be present and have one of the following values: #{error_handlers}"
35
+
36
+ raise(ChiliLogger::ConfigError, unsupported_err)
37
+ end
38
+
39
+ def supported_error_handlers
40
+ {
41
+ aws_sqs: AWS::SqsHandler
42
+ }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,71 @@
1
+ require 'aws-sdk'
2
+ require 'errors/error_messages/rabbitmq_error'
3
+
4
+ # class for centralizing Creative's logging logic
5
+ class ChiliLogger
6
+ # module with classes using Amazon Web Services services
7
+ module AWS
8
+ # class that handles errors when message broker can't be reached, etc...
9
+ class SqsHandler
10
+ def initialize(config)
11
+ @sqs_config = validate_config(config)
12
+ @queue_name = config[:queue_name]
13
+
14
+ @sqs = Aws::SQS::Client.new(@sqs_config)
15
+ end
16
+
17
+ def handle_error(error = StandardError.new, log = 'not_specified')
18
+ message_body = message_body(error, log)
19
+ queue_message = queue_message(message_body)
20
+
21
+ @sqs.send_message(queue_message)
22
+ end
23
+
24
+ private
25
+
26
+ def queue_message(message_body)
27
+ {
28
+ queue_url: queue_url(@queue_name),
29
+ message_body: message_body.to_json,
30
+ message_group_id: SecureRandom.hex(8).to_s,
31
+ message_deduplication_id: SecureRandom.hex(8).to_s
32
+ }
33
+ end
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
+ def queue_url(queue_name)
46
+ @sqs.get_queue_url(queue_name: queue_name).queue_url
47
+ end
48
+
49
+ def invalid_config_format_error
50
+ 'The :error_hanldler_config object must be a Hash'
51
+ end
52
+
53
+ def config_attr_error(attr)
54
+ "The configuration object must include a ':#{attr}' attribute"
55
+ end
56
+
57
+ def validate_config(config)
58
+ raise(ChiliLogger::ConfigError, invalid_config_format_error) unless config.is_a?(Hash)
59
+
60
+ main_config_keys = %i[region access_key_id secret_access_key queue_name]
61
+ main_config_keys.each { |key| raise(ChiliLogger::ConfigError, config_attr_error(key)) unless config[key] }
62
+
63
+ {
64
+ region: config[:region],
65
+ access_key_id: config[:access_key_id],
66
+ secret_access_key: config[:secret_access_key]
67
+ }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,21 +1,21 @@
1
1
  require 'yaml'
2
+ require 'fileutils'
2
3
 
3
4
  # class for centralizing Creative's logging logic
4
5
  class ChiliLogger
5
6
  # class for keeping logs coverage up to date
6
7
  class CoverageWriter
7
8
  def self.write(desc, backtrace)
8
- coverage_path = './log/chili_logger_coverage.yml'
9
- coverage = YAML.load_file(coverage_path) || {}
9
+ coverage = read_file
10
10
 
11
11
  type = desc[:type]
12
12
  service = desc[:service]
13
13
  action = desc[:action]
14
-
15
14
  keys = [type, service, action]
15
+
16
16
  coverage = add_nested_value(coverage, keys, backtrace)
17
17
 
18
- File.open(coverage_path, 'w') { |f| YAML.dump(coverage, f) }
18
+ write_file(coverage)
19
19
  end
20
20
 
21
21
  def self.add_nested_value(object, keys, final_value)
@@ -28,5 +28,29 @@ class ChiliLogger
28
28
 
29
29
  object
30
30
  end
31
+
32
+ def self.read_file
33
+ file_exists = File.file?(file_path)
34
+
35
+ file_exists ? YAML.load_file(file_path) : {}
36
+ end
37
+
38
+ def self.write_file(file)
39
+ FileUtils.mkdir(dir_name) unless File.directory?(dir_name)
40
+ File.open(file_path, 'w') { |f| YAML.dump(file, f) }
41
+ true
42
+ end
43
+
44
+ def self.dir_name
45
+ 'log'
46
+ end
47
+
48
+ def self.file_name
49
+ 'chili-logger-coverage.yml'
50
+ end
51
+
52
+ def self.file_path
53
+ File.join('.', dir_name, file_name)
54
+ end
31
55
  end
32
56
  end
@@ -1,6 +1,5 @@
1
1
  require 'httparty'
2
2
  require 'logs_coverage/coverage_writer'
3
- require 'helpers/rails_backtrace_cleaner'
4
3
  require 'message_writer/aws_ops_metadata'
5
4
 
6
5
  # class for centralizing Creative's logging logic
@@ -1,5 +1,4 @@
1
1
  require 'logs_coverage/coverage_writer'
2
- require 'helpers/rails_backtrace_cleaner'
3
2
  require 'message_writer/aws_ops_metadata'
4
3
 
5
4
  # class for centralizing Creative's logging logic
@@ -11,7 +10,6 @@ class ChiliLogger
11
10
  @layer = config[:layer] || 'not_specified'
12
11
  @server_url = config[:server_url] || 'not_specified'
13
12
  @cloud_provider = config[:cloud_provider]
14
- @backtrace_cleaner = RailsBacktraceCleaner.new
15
13
  end
16
14
 
17
15
  def write(desc = {}, agent = 'not_specified', main_content = {})
@@ -20,11 +18,16 @@ class ChiliLogger
20
18
  desc ||= {}
21
19
  main_content ||= {}
22
20
 
23
- main_content.merge!({ backtrace: @backtrace_cleaner.clean(caller) })
21
+ main_content.merge!( backtrace: clean_backtrace(caller) )
24
22
  update_logs_coverage(desc, main_content)
25
23
  message_hash(desc, agent, main_content)
26
24
  end
27
25
 
26
+ def clean_backtrace(backtrace)
27
+ backtrace.reject! { |line| line =~ %r{bundle\/gems} }
28
+ backtrace
29
+ end
30
+
28
31
  def write_description_tag(desc)
29
32
  env = desc[:env] || @env
30
33
  layer = desc[:layer] || @layer
@@ -39,7 +42,7 @@ class ChiliLogger
39
42
 
40
43
  # uses message infos to keep logs_coverage up to date with what logs are being generated in the application
41
44
  def update_logs_coverage(desc, main_content)
42
- backtrace = main_content[:backtrace] || @backtrace_cleaner.clean(caller)
45
+ backtrace = main_content[:backtrace] || clean_backtrace(caller)
43
46
  CoverageWriter.write(desc, backtrace)
44
47
  end
45
48
 
@@ -61,16 +64,21 @@ class ChiliLogger
61
64
  # rubocop:enable Metrics/MethodLength
62
65
 
63
66
  def write_ops_metadata
67
+ return unless @cloud_provider
68
+
64
69
  provider = @cloud_provider.is_a?(String) ? @cloud_provider.to_sym : @cloud_provider
70
+ cloud_ops_metadata = cloud_metadata[provider]
71
+ return unless cloud_ops_metadata
72
+
65
73
  {
66
74
  server_url: @server_url,
67
- cloud: cloud_metadata[provider]
75
+ cloud: cloud_ops_metadata.write
68
76
  }
69
77
  end
70
78
 
71
79
  def cloud_metadata
72
80
  {
73
- aws: AwsOpsMetadata.write
81
+ aws: AwsOpsMetadata
74
82
  }
75
83
  end
76
84
  end
@@ -0,0 +1,50 @@
1
+ ---
2
+ test:
3
+ console:
4
+ publish_test:
5
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/chili_logger-0.0.6/lib/chili_logger.rb:42:in
6
+ `publish_instant_log'"
7
+ - "(pry):28:in `__pry__'"
8
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in
9
+ `eval'"
10
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in
11
+ `evaluate_ruby'"
12
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:659:in
13
+ `handle_line'"
14
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:261:in
15
+ `block (2 levels) in eval'"
16
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in
17
+ `catch'"
18
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in
19
+ `block in eval'"
20
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in
21
+ `catch'"
22
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in
23
+ `eval'"
24
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:77:in `block
25
+ in repl'"
26
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:67:in `loop'"
27
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:67:in `repl'"
28
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:38:in `block
29
+ in start'"
30
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/input_lock.rb:61:in
31
+ `__with_ownership'"
32
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/input_lock.rb:78:in
33
+ `with_ownership'"
34
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:38:in `start'"
35
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/repl.rb:15:in `start'"
36
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_class.rb:191:in
37
+ `start'"
38
+ - "/home/lucas/.irbrc:8:in `<top (required)>'"
39
+ - "/usr/share/rvm/scripts/irbrc.rb:40:in `load'"
40
+ - "/usr/share/rvm/scripts/irbrc.rb:40:in `<top (required)>'"
41
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in
42
+ `require'"
43
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in
44
+ `require'"
45
+ - "/usr/share/rvm/rubies/ruby-2.3.3/.irbrc:11:in `<top (required)>'"
46
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/init.rb:231:in `load'"
47
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/init.rb:231:in `run_config'"
48
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb/init.rb:20:in `setup'"
49
+ - "/usr/share/rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/irb.rb:378:in `start'"
50
+ - "/usr/share/rvm/rubies/ruby-2.3.3/bin/irb:11:in `<main>'"
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chili_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - lucas sandeville
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-27 00:00:00.000000000 Z
11
+ date: 2020-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.9.0
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.9'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.9.0
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.9'
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: bunny
15
35
  requirement: !ruby/object:Gem::Requirement
@@ -57,19 +77,22 @@ files:
57
77
  - Rakefile
58
78
  - bin/console
59
79
  - bin/setup
80
+ - chili_logger-0.0.6.gem
60
81
  - chili_logger.gemspec
61
82
  - lib/brokers/rabbit_broker.rb
62
83
  - lib/chili_logger.rb
63
84
  - lib/chili_logger/version.rb
64
85
  - lib/current_log_accessor.rb
65
- - lib/errors/config_error.rb
66
- - lib/errors/rabbitmq_error.rb
67
- - lib/helpers/rails_backtrace_cleaner.rb
86
+ - lib/errors/error_messages/config_error.rb
87
+ - lib/errors/error_messages/rabbitmq_error.rb
88
+ - lib/errors/logging_error_handler/logging_error_handler.rb
89
+ - lib/errors/logging_error_handler/sqs_handler.rb
68
90
  - lib/logs_coverage/chili_logger_coverage.yml
69
91
  - lib/logs_coverage/coverage_writer.rb
70
92
  - lib/logs_coverage/levantamento_provisório.yaml
71
93
  - lib/message_writer/aws_ops_metadata.rb
72
94
  - lib/message_writer/message_writer.rb
95
+ - log/chili-logger-coverage.yml
73
96
  homepage: https://gitlab.com/chiligumdev/chili_logger
74
97
  licenses:
75
98
  - MIT
@@ -1,130 +0,0 @@
1
- # Backtraces often include many lines that are not relevant for the context
2
- # under review. This makes it hard to find the signal amongst the backtrace
3
- # noise, and adds debugging time. With a BacktraceCleaner, filters and
4
- # silencers are used to remove the noisy lines, so that only the most relevant
5
- # lines remain.
6
- #
7
- # Filters are used to modify lines of data, while silencers are used to remove
8
- # lines entirely. The typical filter use case is to remove lengthy path
9
- # information from the start of each line, and view file paths relevant to the
10
- # app directory instead of the file system root. The typical silencer use case
11
- # is to exclude the output of a noisy library from the backtrace, so that you
12
- # can focus on the rest.
13
- #
14
- # bc = ActiveSupport::BacktraceCleaner.new
15
- # bc.add_filter { |line| line.gsub(Rails.root.to_s, '') } # strip the Rails.root prefix
16
- # bc.add_silencer { |line| line =~ /puma|rubygems/ } # skip any lines from puma or rubygems
17
- # bc.clean(exception.backtrace) # perform the cleanup
18
- #
19
- # To reconfigure an existing BacktraceCleaner (like the default one in Rails)
20
- # and show as much data as possible, you can always call
21
- # <tt>BacktraceCleaner#remove_silencers!</tt>, which will restore the
22
- # backtrace to a pristine state. If you need to reconfigure an existing
23
- # BacktraceCleaner so that it does not filter or modify the paths of any lines
24
- # of the backtrace, you can call <tt>BacktraceCleaner#remove_filters!</tt>
25
- # These two methods will give you a completely untouched backtrace.
26
-
27
- # BacktraceCleaner from Rails, copied ipsis literis
28
- # github: https://github.com/rails/rails/blob/fbe2433be6e052a1acac63c7faf287c52ed3c5ba/activesupport/lib/active_support/backtrace_cleaner.rb#L41
29
- class RailsBacktraceCleaner
30
- def initialize
31
- @filters = []
32
- @silencers = []
33
- add_gem_filter
34
- add_gem_silencer
35
- add_stdlib_silencer
36
- end
37
-
38
- # Returns the backtrace after all filters and silencers have been run
39
- # against it. Filters run first, then silencers.
40
- def clean(backtrace, kind = :silent)
41
- filtered = filter_backtrace(backtrace)
42
-
43
- case kind
44
- when :silent
45
- silence(filtered)
46
- when :noise
47
- noise(filtered)
48
- else
49
- filtered
50
- end
51
- end
52
- alias filter clean
53
-
54
- # Adds a filter from the block provided. Each line in the backtrace will be
55
- # mapped against this filter.
56
- #
57
- # # Will turn "/my/rails/root/app/models/person.rb" into "/app/models/person.rb"
58
- # backtrace_cleaner.add_filter { |line| line.gsub(Rails.root, '') }
59
- def add_filter(&block)
60
- @filters << block
61
- end
62
-
63
- # Adds a silencer from the block provided. If the silencer returns +true+
64
- # for a given line, it will be excluded from the clean backtrace.
65
- #
66
- # # Will reject all lines that include the word "puma", like "/gems/puma/server.rb" or "/app/my_puma_server/rb"
67
- # backtrace_cleaner.add_silencer { |line| line =~ /puma/ }
68
- def add_silencer(&block)
69
- @silencers << block
70
- end
71
-
72
- # Removes all silencers, but leaves in the filters. Useful if your
73
- # context of debugging suddenly expands as you suspect a bug in one of
74
- # the libraries you use.
75
- def remove_silencers!
76
- @silencers = []
77
- end
78
-
79
- # Removes all filters, but leaves in the silencers. Useful if you suddenly
80
- # need to see entire filepaths in the backtrace that you had already
81
- # filtered out.
82
- def remove_filters!
83
- @filters = []
84
- end
85
-
86
- private
87
-
88
- FORMATTED_GEMS_PATTERN = %r{/\A[^\/]+ \([\w.]+\) /}.freeze
89
-
90
- def add_gem_filter
91
- gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) }
92
- return if gems_paths.empty?
93
-
94
- gems_regexp = %r{(#{gems_paths.join('|')})/(bundler/)?gems/([^/]+)-([\w.]+)/(.*)}
95
- gems_result = '\3 (\4) \5'
96
- add_filter { |line| line.sub(gems_regexp, gems_result) }
97
- end
98
-
99
- def add_gem_silencer
100
- add_silencer { |line| FORMATTED_GEMS_PATTERN.match?(line) }
101
- end
102
-
103
- def add_stdlib_silencer
104
- add_silencer { |line| line.start_with?(RbConfig::CONFIG['rubylibdir']) }
105
- end
106
-
107
- def filter_backtrace(backtrace)
108
- @filters.each do |f|
109
- backtrace = backtrace.map { |line| f.call(line) }
110
- end
111
-
112
- backtrace
113
- end
114
-
115
- def silence(backtrace)
116
- @silencers.each do |s|
117
- backtrace = backtrace.reject { |line| s.call(line) }
118
- end
119
-
120
- backtrace
121
- end
122
-
123
- def noise(backtrace)
124
- backtrace.select do |line|
125
- @silencers.any? do |s|
126
- s.call(line)
127
- end
128
- end
129
- end
130
- end