chili_logger 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 517d555f3acba9a85b81507c6f321a89c17c1d20fb80eaa6b8ab01290c55fc83
4
+ data.tar.gz: 68a9d265d5ea5df6b7869b912a091d7271e5fe75b8a1990f31cd1b05dcffd385
5
+ SHA512:
6
+ metadata.gz: 58c4352a1f71b36ed91b07220391dcdbec09610a4645ed8202bf84fa288e78d976d56917dc999b4f7c548579bd82fe90b822cf04f391e04d0df4183b710fda89
7
+ data.tar.gz: 4bb3cbbc0f2dbac7e7ce8d26842e30ff282ccd2b35b908b01c1048d3b02fe2f5e7d0f34d7f49afe6a83634cb342a5335438224b65c3b84c054cf394ccacb5359
data/.byebug_history ADDED
@@ -0,0 +1,55 @@
1
+ continue
2
+ msg[type.to_sym]
3
+ msg[type]
4
+ pp msg
5
+ p msg
6
+ pp msg
7
+ msg
8
+ type
9
+ continue
10
+ mock_current_log.clear_log_info
11
+ mock_current_log
12
+ continue
13
+ n
14
+ item_name
15
+ item_names
16
+ new_desc
17
+ continue
18
+ desc_to_overwrite.keys.include?(:type)
19
+ type
20
+ desc_to_overwrite
21
+ continue
22
+ type
23
+ desc_to_overwrite
24
+ continue
25
+ desc_to_overwrite
26
+ continue
27
+ desc_to_overwrite
28
+ continue
29
+ desc_to_overwrite
30
+ continue
31
+ desc_to_overwrite
32
+ continue
33
+ invalid_desc.merge(valid_desc_elements)
34
+ new_desc
35
+ continue
36
+ desc_to_overwrite
37
+ new_desc
38
+ new-desc
39
+ continue
40
+ e
41
+ e.class_variables
42
+ e.variables
43
+ e.instance_variables
44
+ e.body
45
+ e.inspect
46
+ e
47
+ continue
48
+ config
49
+ continue
50
+ config[main_config_keys.first]
51
+ config
52
+ main_config_keys
53
+ continue
54
+ rabbit_publisher
55
+ rabbit_publisher.instance_variable_get('@user')
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3.3
3
+ Metrics/LineLength:
4
+ Max: 120
5
+ Metrics/BlockLength:
6
+ ExcludedMethods: ['describe', 'context', 'namespace']
7
+ Metrics/ModuleLength:
8
+ Max: 180
9
+ Style/FrozenStringLiteralComment:
10
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.3.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chili_logger.gemspec
4
+ gemspec
5
+
6
+ gem "activerecord", "~> 5.2"
7
+ gem 'bunny'
8
+ gem 'byebug'
9
+ gem 'rake', '~> 12.0'
10
+ gem 'rspec', '~> 3.0'
11
+ gem 'rubocop', '~> 0.60.0', require: false
12
+
13
+ gem "httparty", "~> 0.18.1"
data/Gemfile.lock ADDED
@@ -0,0 +1,88 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chili_logger (0.1.0)
5
+ bunny
6
+ httparty
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.2.4.3)
12
+ activesupport (= 5.2.4.3)
13
+ activerecord (5.2.4.3)
14
+ activemodel (= 5.2.4.3)
15
+ activesupport (= 5.2.4.3)
16
+ arel (>= 9.0)
17
+ activesupport (5.2.4.3)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 0.7, < 2)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ amq-protocol (2.3.1)
23
+ arel (9.0.0)
24
+ ast (2.4.0)
25
+ bunny (2.15.0)
26
+ amq-protocol (~> 2.3, >= 2.3.1)
27
+ byebug (11.0.1)
28
+ concurrent-ruby (1.1.6)
29
+ diff-lcs (1.3)
30
+ httparty (0.18.1)
31
+ mime-types (~> 3.0)
32
+ multi_xml (>= 0.5.2)
33
+ i18n (1.8.3)
34
+ concurrent-ruby (~> 1.0)
35
+ jaro_winkler (1.5.4)
36
+ mime-types (3.3.1)
37
+ mime-types-data (~> 3.2015)
38
+ mime-types-data (3.2020.0512)
39
+ minitest (5.14.1)
40
+ multi_xml (0.6.0)
41
+ parallel (1.19.1)
42
+ parser (2.7.1.3)
43
+ ast (~> 2.4.0)
44
+ powerpack (0.1.2)
45
+ rainbow (3.0.0)
46
+ rake (12.3.1)
47
+ rspec (3.9.0)
48
+ rspec-core (~> 3.9.0)
49
+ rspec-expectations (~> 3.9.0)
50
+ rspec-mocks (~> 3.9.0)
51
+ rspec-core (3.9.2)
52
+ rspec-support (~> 3.9.3)
53
+ rspec-expectations (3.9.2)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.9.0)
56
+ rspec-mocks (3.9.1)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.9.0)
59
+ rspec-support (3.9.3)
60
+ rubocop (0.60.0)
61
+ jaro_winkler (~> 1.5.1)
62
+ parallel (~> 1.10)
63
+ parser (>= 2.5, != 2.5.1.1)
64
+ powerpack (~> 0.1)
65
+ rainbow (>= 2.2.2, < 4.0)
66
+ ruby-progressbar (~> 1.7)
67
+ unicode-display_width (~> 1.4.0)
68
+ ruby-progressbar (1.10.1)
69
+ thread_safe (0.3.6)
70
+ tzinfo (1.2.7)
71
+ thread_safe (~> 0.1)
72
+ unicode-display_width (1.4.1)
73
+
74
+ PLATFORMS
75
+ ruby
76
+
77
+ DEPENDENCIES
78
+ activerecord (~> 5.2)
79
+ bunny
80
+ byebug
81
+ chili_logger!
82
+ httparty (~> 0.18.1)
83
+ rake (~> 12.0)
84
+ rspec (~> 3.0)
85
+ rubocop (~> 0.60.0)
86
+
87
+ BUNDLED WITH
88
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 lucas sandeville
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # ChiliLogger
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'chili_logger'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install chili_logger
22
+
23
+
24
+
25
+ ```ruby
26
+ require 'chili_logger'
27
+
28
+ is_deactivated = Rails.env.test?
29
+
30
+ ChiliLogger.instance.config({
31
+ deactivated: is_deactivated,
32
+ log_env: 'development',
33
+ log_layer: 'creatives',
34
+ server_url: ENV['server_url'],
35
+ cloud_provider: :aws,
36
+ msg_broker_name: :rabbitmq,
37
+ 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'
44
+ }
45
+ })
46
+ ```
47
+
48
+
49
+
50
+
51
+
52
+
53
+ ## Usage
54
+
55
+ TODO: Write usage instructions here
56
+
57
+ ## Development
58
+
59
+ 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
+
61
+ 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
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chili_logger.
66
+
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chili_logger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ require_relative 'lib/chili_logger/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'chili_logger'
5
+ spec.version = ChiliLogger::VERSION
6
+ spec.authors = ['lucas sandeville']
7
+ spec.email = ['lucas.sandeville@gmail.com']
8
+
9
+ spec.summary = 'placeholder'
10
+ spec.description = 'placeholder'
11
+ spec.homepage = 'https://gitlab.com/chiligumdev/chili_logger'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+ spec.add_dependency 'bunny'
15
+ spec.add_dependency 'httparty'
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+ end
@@ -0,0 +1,65 @@
1
+ require 'bunny'
2
+ require 'errors/rabbitmq_error'
3
+
4
+ # class for centralizing Creative's logging logic
5
+ class ChiliLogger
6
+ # class that configures and manages the RabbitMQ client
7
+ class RabbitBroker
8
+ def config(config)
9
+ validate_config(config)
10
+
11
+ @user = config[:user]
12
+ @password = config[:password]
13
+ @ip = config[:ip]
14
+ @port = config[:port]
15
+ @exchange_name = config[:exchange_name]
16
+ @routing_key = config[:routing_key]
17
+
18
+ connect
19
+ end
20
+
21
+ def connect
22
+ return if ChiliLogger.instance.deactivated
23
+
24
+ @connection = Bunny.new("amqp://#{@user}:#{@password}@#{@ip}:#{@port}")
25
+ @connection.start
26
+
27
+ @channel = @connection.create_channel
28
+ @exchange = @channel.topic(@exchange_name, durable: true)
29
+
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)
33
+ end
34
+
35
+ def disconnect
36
+ @connection.close
37
+ end
38
+
39
+ def publish(message, routing_key)
40
+ return if ChiliLogger.instance.deactivated
41
+
42
+ # 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
45
+ @exchange.publish(message.to_json, routing_key: key)
46
+ end
47
+
48
+ private
49
+
50
+ def invalid_config_format_error
51
+ 'The configuration object must include a Hash'
52
+ end
53
+
54
+ def config_attr_error(attr)
55
+ "The configuration object must include a ':#{attr}' attribute"
56
+ end
57
+
58
+ def validate_config(config)
59
+ raise(ChiliLogger::ConfigError, invalid_config_format_error) unless config.is_a?(Hash)
60
+
61
+ main_config_keys = %i[user password ip port exchange_name routing_key]
62
+ main_config_keys.each { |key| raise(ChiliLogger::ConfigError, config_attr_error(key)) unless config[key] }
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ class ChiliLogger
2
+ VERSION = '0.0.0'
3
+ end
@@ -0,0 +1,76 @@
1
+ require 'chili_logger/version'
2
+ require 'errors/config_error'
3
+ require 'brokers/rabbit_broker'
4
+ require 'message_writer/message_writer'
5
+ require 'singleton'
6
+
7
+ # class for centralizing Creative's logging logic
8
+ class ChiliLogger
9
+ attr_reader :msg_broker
10
+ attr_accessor :deactivated
11
+ # this class uses the singleton module, so instead of initializing it with ChiliLogger.new,
12
+ # initialize it with ChiliLogger.instance
13
+ include Singleton
14
+
15
+ def config(config)
16
+ @deactivated = config[:deactivated] || false # ChilLogger can be deactivated for test environents
17
+
18
+ config_msg_writer(config)
19
+ config_msg_broker(config)
20
+
21
+ @current_log_accessor = CurrentLogAccessor.new(@msg_broker, @msg_writer)
22
+ end
23
+
24
+ def current_log
25
+ @current_log_accessor
26
+ end
27
+
28
+ def start_new_log(new_desc = nil, new_agent = nil, new_main_content = nil)
29
+ current_log.clear_log_info
30
+
31
+ current_log.overwrite_desc(new_desc)
32
+ current_log.overwrite_agent(new_agent)
33
+ current_log.overwrite_main_content(new_main_content)
34
+
35
+ current_log
36
+ end
37
+
38
+ def publish_instant_log(desc, agent, main_content)
39
+ message = @msg_writer.write(desc, agent, main_content)
40
+ routing_key = @msg_writer.write_description_tag(desc)
41
+
42
+ @msg_broker.publish(message, routing_key)
43
+ end
44
+
45
+ private
46
+
47
+ def config_msg_writer(general_config)
48
+ msg_writer_config = {
49
+ env: general_config[:log_env],
50
+ layer: general_config[:log_layer],
51
+ server_url: general_config[:server_url],
52
+ cloud_provider: general_config[:cloud_provider]
53
+ }
54
+
55
+ @msg_writer = MessageWriter.new(msg_writer_config)
56
+ end
57
+
58
+ def config_msg_broker(config)
59
+ msg_broker_name = config[:msg_broker_name] || :rabbitmq
60
+ msg_broker_config = config[:msg_broker_config] || {}
61
+ @msg_broker = supported_msg_brokers[msg_broker_name.to_sym]
62
+ raise(ConfigError, broker_name_error(msg_broker_name)) unless @msg_broker
63
+
64
+ @msg_broker.config(msg_broker_config)
65
+ end
66
+
67
+ def broker_name_error(msg_broker_name)
68
+ "#{msg_broker_name} is not supported. Supported msg_broker_names: #{supported_msg_brokers}"
69
+ end
70
+
71
+ def supported_msg_brokers
72
+ {
73
+ rabbitmq: RabbitBroker.new
74
+ }
75
+ end
76
+ end
@@ -0,0 +1,120 @@
1
+ require 'message_writer/message_writer'
2
+ require 'byebug'
3
+
4
+ # class for centralizing Creative's logging logic
5
+ class ChiliLogger
6
+ # class for manipulating current thread variables
7
+ class CurrentLogAccessor
8
+ def initialize(msg_broker, msg_writer)
9
+ @msg_broker = msg_broker
10
+ @msg_writer = msg_writer
11
+ end
12
+
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
17
+
18
+ message = @msg_writer.write(desc, agent, main_content)
19
+ routing_key = @msg_writer.write_description_tag(desc)
20
+
21
+ @msg_broker.publish(message, routing_key)
22
+ clear_log_info
23
+ end
24
+
25
+ # 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'
30
+ end
31
+
32
+ def overwrite_agent(agent)
33
+ Thread.current[:current_log_agent] = agent
34
+ agent
35
+ end
36
+
37
+ def desc
38
+ Thread.current[:current_log_desc] ||= { type: 'not_specified', service: 'not_specified', action: 'not_specified' }
39
+ end
40
+
41
+ def overwrite_type(new_type_name)
42
+ desc[:type] = new_type_name || 'not_specified'
43
+ end
44
+
45
+ def overwrite_service(new_service_name)
46
+ desc[:service] = new_service_name || 'not_specified'
47
+ end
48
+
49
+ def overwrite_action(new_action_name)
50
+ desc[:action] = new_action_name || 'not_specified'
51
+ end
52
+
53
+ def overwrite_desc(new_desc)
54
+ return Thread.current[:current_log_desc] = nil if new_desc.nil?
55
+ return unless new_desc.is_a?(Hash)
56
+
57
+ overwrite_desc_items(new_desc, valid_desc_items)
58
+ desc
59
+ end
60
+
61
+ def main_content
62
+ Thread.current[:current_log_main_content] ||= {}
63
+ end
64
+
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)
68
+
69
+ Thread.current[:current_log_main_content] = new_main_content
70
+ end
71
+
72
+ def add_to_main_content(new_content)
73
+ return unless new_content.is_a?(Hash)
74
+
75
+ Thread.current[:current_log_main_content] = main_content.merge(new_content)
76
+ end
77
+
78
+ def modified_records
79
+ main_content[:modified_records] ||= {}
80
+ end
81
+
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)
85
+
86
+ main_content[:modified_records] = modified_records
87
+ end
88
+
89
+ def add_modified_record(table_name, modified_record_hash)
90
+ return unless table_name
91
+ return unless table_name.is_a?(String)
92
+ return unless modified_record_hash
93
+ return unless modified_record_hash.is_a?(Hash)
94
+
95
+ modified_records[table_name] ||= []
96
+ modified_records[table_name] = modified_records[table_name].push(modified_record_hash)
97
+ end
98
+
99
+ def clear_log_info
100
+ overwrite_desc(nil)
101
+ overwrite_agent(nil)
102
+ overwrite_main_content(nil)
103
+ end
104
+
105
+ private
106
+
107
+ def valid_desc_items
108
+ %i[type service action]
109
+ end
110
+
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)
114
+
115
+ item = new_desc[item_name]
116
+ desc[item_name] = item || 'not_specified'
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,9 @@
1
+ # class for wrapping all other gem's classes
2
+ class ChiliLogger
3
+ # class for wrapping configuration errors
4
+ class ConfigError < StandardError
5
+ def initialize(msg = 'Found an error when configuring ChiliLogger')
6
+ super(msg)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # class for wrapping all other gem's classes
2
+ class ChiliLogger
3
+ # class for wrapping RabbitMQ/Bunny errors
4
+ class RabbitMQError < StandardError
5
+ def initialize(msg = "The provided data couldn't be converted")
6
+ super(msg)
7
+ end
8
+ end
9
+ end