chili_logger 0.0.12 → 0.1.2

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: c91c57ffcdb641479b3174ea6ad523c976d6668f4fe049025d51bcd0574e716a
4
- data.tar.gz: d57923238e3c0bba29cc00b209ee30f3b8eaa4e026553d3cae6a1b4586072fef
3
+ metadata.gz: 61d8df3241131a360ca82cbc122ccde86989a46b8b67227e9f686136b792115b
4
+ data.tar.gz: 5250e4ba9b5f27b8ed56ef0d6bb619b306783c390c17d27ee34dada2e29a862b
5
5
  SHA512:
6
- metadata.gz: 94ef9e11202aff8cc8fa0039eaeb3a758de300cf1913ce20b3536b1187ed931a3bc00f05ff6571e51d3d6d22eeb6d40496b0454f5709f8b8228acfe6d0eeee6d
7
- data.tar.gz: 7091ca4a4be36329d7a7e426cc4a1fbb8b0bd9ac67938677fd6fd5fe71295bf8d7f695666987c7755fdee2b254abd62b4607979eb1fbdcf603a45be79c85c7ab
6
+ metadata.gz: 00fbfa5e42ac2ec4190665535d7e30d6faf47528a0be7924714a15a6dda60dcf65d9c629ddec9b1201d69164201df4cde86fd07a3191b4fabad189ba65046ebe
7
+ data.tar.gz: e1a02abebf9d9f437d707647d94c26bd03f22853029f3053c94cf05ec7ebaa1920482bf0cef6eb6499cdae9f4301dfe17c361a38387e3e7c447de47d64fbf3a9
@@ -1,126 +1,42 @@
1
1
  continue
2
- options
3
- continue
4
- options
5
- continue
6
- @desc_uniformizer.desc([])
7
- @desc_uniformizer.desc(options[:desc])
8
- @desc_uniformizer.desc
9
- @desc_uniformizer.desc(options[:desc])
10
- options
11
- continue
12
- options
13
- continue
14
- main_content.merge(uniformized_content)
15
- main_content
16
- continue
17
- main_content
18
- uniformized_content
19
- continue
20
- desc
21
- continue
22
- uniformized_content
23
- continue
24
- uniformized_content
2
+ @sqs_config[:region]
3
+ @sqs_config['region']
4
+ @sqs_config
25
5
  quit
26
- val.first.is_a?(Hash)
27
- val.first.is_a?(Hash) ? {} : record
28
- modified_record(val.first)
29
- val
30
- uniformized_val
6
+ queue_url(@queue_name)
31
7
  quit
32
- continue
33
- error.inspect.is_a?(String)
34
- error.inspect
35
- error.class.ancestors.include?(Exception)
36
- error
37
- new_error
38
- errors
39
- all_errors
40
- continue
41
- new_error.is_a?(String)
42
- continue
43
- new_error.is_a?(String)
44
- new_error.class
45
- new_error
46
- continue
47
- new_error
48
- continue
49
- new_error
50
- continue
51
- error
52
- new_error
8
+ queue_url(@queue_name)
53
9
  quit
54
- main_content[:errors] ||= @default.log_errors
55
- errors
56
- continue
57
- user[:cognito_id]
58
- new_user[:cognito_id]
59
- continuenew_user[:cognito_id]
60
10
  continue
61
- new_user
11
+ queue_url(@queue_name)
62
12
  continue
63
- user
64
- Thread.current[:current_log_user]
65
- Thread.current[:current_log_user] ||= default_user
13
+ mock_broker.class.ancestors
14
+ mock_broker.ancestors
66
15
  continue
67
- Thread.current[:current_log_user] ||= default_user
16
+ stubbed_sqs_poller.poll
17
+ stubbed_sqs_poller.poll { |msg| }
18
+ stubbed_sqs_poller.poll {}
19
+ stubbed_sqs_poller.poll
20
+ stubbed_sqs_poller
21
+ Aws::SQS::QueuePoller.new(stub_responses: true).poll
22
+ ENV['AWS_REGION'] = 'us-east-1'
23
+ stubbed_sqs_poller.poll
68
24
  continue
69
- Thread.current[:current_log_user] ||= default_user
25
+ ChiliLogger.instance.instance_variable_get('@unpublished_logs_manager')
26
+ mock_unpub_logs_mngr == ChiliLogger.instance.instance_variable_get('@unpublished_logs_manager')
27
+ mock_unpub_logs_mngr
70
28
  continue
71
- Thread.current[:current_log_user] ||= default_user
72
- user
29
+ file
30
+ throw :stop_polling
73
31
  continue
74
- log_user
75
- continue
76
- Thread.current[:current_log_user] ||= default_user
77
- Thread.current[:current_log_user]
78
- user
79
- continue
80
- e.class
81
- e.is_a?(Bunny:TCPConnectionFailed)
82
- e
83
- @sqs.send_message('queue_message')
84
- @sqs.get_queue_url(queue_name: 'mockeesd').queue_url
85
- @sqs.get_queue_url(queue_name: 'mock').queue_url
86
- @sqs.get_queue_url(queue_name: queue_name).queue_url
87
- @sqs
88
- response
89
- continue
90
- cloud_metadata[@cloud_provider]
91
- @cloud_provider
92
- continue
93
- @cloud_provider
94
- continue
95
- @cloud_provider
96
- continue
97
- @cloud_provider
98
- continue
99
- @cloud_provider
100
- continue
101
- @cloud_provider
102
- continue
103
- @cloud_provider
104
- continue
105
- @cloud_provider
106
- continue
107
- cloud_metadata[nil]
108
- @cloud_provider
109
- continue
110
- @ip
111
- @user
112
- @port
113
- continue
114
- msg[type.to_sym]
115
- msg[type]
116
- pp msg
117
- p msg
118
- pp msg
119
- msg
120
- type
121
- continue
122
- mock_current_log.clear_log_info
123
- mock_current_log
32
+ error
33
+ @rabbit_exchange.publish(log.to_json, routing_key: routing_key)
34
+ routing_key = log['desc']
35
+ log = JSON.parse(sqs_msg.body)['body']['log']
36
+ @rabbit_exchange.publish(log.to_json, routing_key: routing_key)
37
+ routing_key = log['desc']
38
+ log = JSON.parse(sqs_msg.body)['body']['log']
39
+ @rabbit_exchange
124
40
  continue
125
41
  n
126
42
  item_name
data/Gemfile CHANGED
@@ -4,7 +4,8 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'activerecord', '~> 5.2'
7
- gem 'aws-sdk', '2.9.2'
7
+ gem 'aws-sdk-secretsmanager', '~> 1.36.0'
8
+ gem 'aws-sdk-sqs', '~> 1.30.0'
8
9
  gem 'bunny'
9
10
  gem 'byebug'
10
11
  gem 'rake', '~> 12.0'
@@ -13,4 +14,4 @@ gem 'rubocop', '~> 0.60.0', require: false
13
14
 
14
15
  gem 'httparty', '~> 0.18.1'
15
16
 
16
- gem "simplecov", "~> 0.17.1"
17
+ gem 'simplecov', '~> 0.17.1'
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chili_logger (0.0.12)
5
- aws-sdk (~> 2.9, >= 2.9.0)
4
+ chili_logger (0.1.2)
5
+ aws-sdk-secretsmanager (~> 1.36.0)
6
+ aws-sdk-sqs (~> 1.30.0)
6
7
  bunny
7
8
  httparty
8
9
 
@@ -20,24 +21,29 @@ GEM
20
21
  i18n (>= 0.7, < 2)
21
22
  minitest (~> 5.1)
22
23
  tzinfo (~> 1.1)
23
- amq-protocol (2.3.1)
24
+ amq-protocol (2.3.2)
24
25
  arel (9.0.0)
25
26
  ast (2.4.1)
26
27
  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)
28
+ aws-partitions (1.341.0)
29
+ aws-sdk-core (3.103.0)
30
+ aws-eventstream (~> 1, >= 1.0.2)
31
+ aws-partitions (~> 1, >= 1.239.0)
32
+ aws-sigv4 (~> 1.1)
31
33
  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)
34
+ aws-sdk-secretsmanager (1.36.0)
35
+ aws-sdk-core (~> 3, >= 3.71.0)
36
+ aws-sigv4 (~> 1.1)
37
+ aws-sdk-sqs (1.30.0)
38
+ aws-sdk-core (~> 3, >= 3.99.0)
39
+ aws-sigv4 (~> 1.1)
40
+ aws-sigv4 (1.2.1)
41
+ aws-eventstream (~> 1, >= 1.0.2)
36
42
  bunny (2.15.0)
37
43
  amq-protocol (~> 2.3, >= 2.3.1)
38
44
  byebug (11.0.1)
39
45
  concurrent-ruby (1.1.6)
40
- diff-lcs (1.4.2)
46
+ diff-lcs (1.4.4)
41
47
  docile (1.3.2)
42
48
  httparty (0.18.1)
43
49
  mime-types (~> 3.0)
@@ -95,7 +101,8 @@ PLATFORMS
95
101
 
96
102
  DEPENDENCIES
97
103
  activerecord (~> 5.2)
98
- aws-sdk (= 2.9.2)
104
+ aws-sdk-secretsmanager (~> 1.36.0)
105
+ aws-sdk-sqs (~> 1.30.0)
99
106
  bunny
100
107
  byebug
101
108
  chili_logger!
data/README.md CHANGED
@@ -3,60 +3,62 @@
3
3
  ChiliLogger is a gem developed by Chiligum Creatives for internal use. It is used to monitor our ruby applications and generate logs with some uniformization.
4
4
 
5
5
  ## Table of Contents
6
- * [READ BEFORE CONTRIBUTING](##read-before-contributing)
7
- * [How it Works](##how-it-works)
8
- * [Overview](###overview)
9
- * [Anatomy of a Chiligum Log](###anatomy-of-a-chiligum-log)
10
- * [Example Log](####example-log)
11
- * [Environment](####environment)
12
- * [Layer](####layer)
13
- * [Type](####type)
14
- * [Service](####service)
15
- * [Action](####action)
16
- * [Description](####description)
17
- * [User](####user)
18
- * [Main Content](####main-content)
19
- * [Ops Metadata](####ops-metadata)
20
- * [Timestamp](####timestamp)
21
- * [A Primer on RabbitMQ and ElasticSearch](###a-primer-on-rabbitmq-and-elasticsearch)
22
- * [RabbitMQ](####rabbitmq)
23
- * [ElasticSearch](####elasticsearch)
24
- * [Putting It All Together](####putting-it-all-together)
25
- * [Installation](##installation)
26
- * [Basic Usage](##basic-usage)
27
- * [publish_instant_log params](####publish_instant_log-params)
28
- * [publish_instant_log usage example](####publish_instant_log-usage-example)
29
- * [publish_instant_log default argument values](####publish_instant_log-default-argument-values)
30
- * [ChiliLogger's Singleton pattern](####ChiliLoggers-singleton-pattern)
31
- * [Advanced Usage](##advanced-usage)
32
- * [Code Example](####advanced-code-example)
33
- * [Error Logging](####error-logging)
34
- * [Customizing a Started Log](####customizing-a-started-log)
35
- * [Accessing the current_log](####accessing-the-current_log)
36
- * [user](####user)
37
- * [update_user](####update_user)
38
- * [desc](####desc)
39
- * [update_type](####update_type)
40
- * [update_service](####update_service)
41
- * [update_action](####update_action)
42
- * [update_desc](####update_desc)
43
- * [main_content](####main_content)
44
- * [update_main_content](####update_main_content)
45
- * [add_to_main_content](####add_to_main_content)
46
- * [modified_records](####modified_records)
47
- * [update_modified_records](####update_modified_records)
48
- * [add_modified_record](####add_modified_record)
49
- * [clear_log_info](####clear_log_info)
50
- * [Snippets For Quicker Logging](##snippets-for-quicker-logging)
51
- * [Papertrail Optional Use](###papertrail-optional-use)
52
- * [Logging Transactions in HTP Requests](###logging-transactions-in-http-requests)
53
- * [Controllers](####controllers-logging-logic)
54
- * [Models](####models-logging-logic)
55
- * [Logging Rake Tasks](###logging-rake-tasks)
56
- * [Logging Uncaught task Errors](####logging-uncaught-task-errors)
57
- * [Logging Transactions in Tasks](####logging-transactions-in-tasks)
58
- * [Logging Heartbeats in Tasks](####logging-heartbeats-in-tasks)
59
- * [Coverage](##Coverage)
6
+ * [READ BEFORE CONTRIBUTING](#read-before-contributing)
7
+ * [How it Works](#how-it-works)
8
+ * [Overview](#overview)
9
+ * [Anatomy of a Chiligum Log](#anatomy-of-a-chiligum-log)
10
+ * [Example Log](#example-log)
11
+ * [Environment](#environment)
12
+ * [Layer](#layer)
13
+ * [Type](#type)
14
+ * [Service](#service)
15
+ * [Action](#action)
16
+ * [Description](#description)
17
+ * [User](#user)
18
+ * [Main Content](#main-content)
19
+ * [Ops Metadata](#ops-metadata)
20
+ * [Timestamp](#timestamp)
21
+ * [A Primer on RabbitMQ and ElasticSearch](#a-primer-on-rabbitmq-and-elasticsearch)
22
+ * [RabbitMQ](#rabbitmq)
23
+ * [ElasticSearch](#elasticsearch)
24
+ * [Putting It All Together](#putting-it-all-together)
25
+ * [Installation](#installation)
26
+ * [Basic Initialization](#basic-initialization)
27
+ * [Customizing the Message Brokers](#customizing-the-message-brokers)
28
+ * [Basic Usage](#basic-usage)
29
+ * [publish_instant_log params](#publish_instant_log-params)
30
+ * [publish_instant_log usage example](#publish_instant_log-usage-example)
31
+ * [publish_instant_log default argument values](#publish_instant_log-default-argument-values)
32
+ * [ChiliLogger's Singleton pattern](#ChiliLoggers-singleton-pattern)
33
+ * [Advanced Usage](#advanced-usage)
34
+ * [Code Example](#advanced-code-example)
35
+ * [Error Logging](#error-logging)
36
+ * [Customizing a Started Log](#customizing-a-started-log)
37
+ * [Accessing the current_log](#accessing-the-current_log)
38
+ * [user](#user)
39
+ * [update_user](#update_user)
40
+ * [desc](#desc)
41
+ * [update_type](#update_type)
42
+ * [update_service](#update_service)
43
+ * [update_action](#update_action)
44
+ * [update_desc](#update_desc)
45
+ * [main_content](#main_content)
46
+ * [update_main_content](#update_main_content)
47
+ * [add_to_main_content](#add_to_main_content)
48
+ * [modified_records](#modified_records)
49
+ * [update_modified_records](#update_modified_records)
50
+ * [add_modified_record](#add_modified_record)
51
+ * [clear_log_info](#clear_log_info)
52
+ * [Snippets For Quicker Logging](#snippets-for-quicker-logging)
53
+ * [Papertrail Optional Use](#papertrail-optional-use)
54
+ * [Logging Transactions in HTP Requests](#logging-transactions-in-http-requests)
55
+ * [Controllers](#controllers-logging-logic)
56
+ * [Models](#models-logging-logic)
57
+ * [Logging Rake Tasks](#logging-rake-tasks)
58
+ * [Logging Uncaught task Errors](#logging-uncaught-task-errors)
59
+ * [Logging Transactions in Tasks](#logging-transactions-in-tasks)
60
+ * [Logging Heartbeats in Tasks](#logging-heartbeats-in-tasks)
61
+ * [Coverage](#Coverage)
60
62
 
61
63
  ## READ BEFORE CONTRIBUTING
62
64
  The main thing you need to know before changing Chililogger's source code is this: we send our logs in JSON format and [Logstash parses and converts them to Elasticsearch objects](####elasticsearch), requiring all fields to have consistent primitive types. So suppose you send these logs:
@@ -264,7 +266,28 @@ gem 'chili_logger'
264
266
 
265
267
  #### Basic Initialization
266
268
 
267
- Create a initializer file and configure ChiliLogger passing all the relevant information. It is recommended to deactivate ChiliLogger when running tests:
269
+ ChiliLogger currently relies on aws-sdk and, for that reason, requires you to intall and [configure it in your machine](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html).
270
+
271
+ To configure ChiliLogger, create a initializer file and configure it by passing all the relevant data. It is recommended to deactivate ChiliLogger when running tests:
272
+
273
+ ```ruby
274
+ # config/initializers/chili_logger.rb
275
+ require 'chili_logger'
276
+
277
+ is_deactivated = Rails.env.test?
278
+
279
+ ChiliLogger.instance.config({
280
+ deactivated: is_deactivated,
281
+ log_env: Rails.env, # sets the log's env attribute
282
+ log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
283
+ server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
284
+ cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute, usually it is set to 'aws' or nil
285
+ })
286
+ ```
287
+ The attributes `log_env` and `log_layer` set the fields ['env'](###environment) and ['layer'](###layer) in all logs that will be published by ChiliLogger. `server_url` and `cloud_provided` are not mandatory, but they provide additional ops data and are recommended when running in staging or production environments.
288
+
289
+ #### Customizing the Message Brokers
290
+ Usually, we want to allow the gem to use the default configuration for the [Main Message Broker and the Fallback Message Broker](#overview). However, if you need to customize it for testing or special cases, you can do it by passing the appropriate data to ChiliLogger when initializing it:
268
291
 
269
292
  ```ruby
270
293
  # config/initializers/chili_logger.rb
@@ -278,7 +301,7 @@ ChiliLogger.instance.config({
278
301
  log_layer: ENV['APPLICATION_NAME'], # sets the log's layer attribute
279
302
  server_url: ENV['SERVER_URL'], # used for log's ops_metadata attribute
280
303
  cloud_provider: ENV['CLOUD_PROVIDER'], # used for log's ops_metadata attribute
281
- msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker
304
+ msg_broker_name: :rabbitmq, # ChiliLogger's Message Broker; optional, defaults to :rabbitmq
282
305
  msg_broker_config: {
283
306
  user: ENV['RABBIT_USER'],
284
307
  password: ENV['RABBIT_PASSWORD'],
@@ -286,7 +309,7 @@ ChiliLogger.instance.config({
286
309
  port: ENV['RABBIT_PORT'],
287
310
  exchange_name: ENV['RABBIT_EXCHANGE'],
288
311
  },
289
- fallback_broker: :aws_sqs, # Chiligum's fallback option in case of Message Broker being down
312
+ fallback_broker: :aws_sqs, # ChiliLogger's fallback option in case of Message Broker being down; optional, defaults to :aws_sqs
290
313
  fallback_broker_config: {
291
314
  region: ENV['CHILI_LOGGER_SQS_HANDLER_REGION'],
292
315
  access_key_id: ENV['CHILI_LOGGER_SQS_HANDLER_KEY_ID'],
@@ -295,11 +318,7 @@ ChiliLogger.instance.config({
295
318
  }
296
319
  })
297
320
  ```
298
- The attributes `log_env` and `log_layer` set the fields ['env'](###environment) and ['layer'](###layer) in all logs that will be published by ChiliLogger.
299
-
300
- When configuring ChiliLogger, you MUST set `msg_broker_name` and `msg_broker_config`. All logs are published to the configured message broker and, without it, ChiliLogger can't work. For the time being, only `:rabbitmq` is supported.
301
-
302
- You ALSO MUST set `fallback_broker` and `fallback_broker_config`. This is the fallback option if the Message Broker is not available and, without it, ChiliLogger would break the application every time the Message Broker was down. For the time being, only `:aws_sqs` is supported.
321
+ For the time being, only `:rabbitmq` is supported as the `msg_broker_name` and only `:aws_sqs` is supported as the `fallback_broker`.
303
322
 
304
323
  Please note that if ChiliLogger tries to publish a log and both the configured 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.
305
324
 
@@ -415,7 +434,7 @@ ChiliLogger.instance.publish_instant_log(desc: desc, user: user, main_content: m
415
434
  ```
416
435
 
417
436
  #### publish_instant_log default argument values
418
- Passing `desc`, `agent`, and `main_content` is optional, since all of them have default values. This is so ChiliLogger is resiliant and doesn't break if any of these arguments is forgotten. But notice that their default values are not very descriptive and it results in low-quality, almost useless logs:
437
+ Passing `desc`, `user`, and `main_content` is optional, since all of them have default values. This is so ChiliLogger is resiliant and doesn't break if any of these arguments is forgotten. But notice that their default values are not very descriptive and it results in low-quality, almost useless logs:
419
438
 
420
439
  ```ruby
421
440
  ChiliLogger.instance.publish_instant_log
@@ -448,19 +467,19 @@ ChiliLogger.instance.publish_instant_log
448
467
  #### ChiliLogger's Singleton pattern
449
468
  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:
450
469
  ```ruby
451
- ChiliLogger.publish_instant_log(desc, agent, log)
470
+ ChiliLogger.publish_instant_log(deasc: desc, user: user, main_content: main_content)
452
471
  # undefined method `publish_instant_log' for ChiliLogger:Class
453
472
 
454
- ChiliLogger.new.publish_instant_log(desc, agent, log)
473
+ ChiliLogger.new.publish_instant_log(deasc: desc, user: user, main_content: main_content)
455
474
  # NoMethodError: private method `new' called for ChiliLogger:Class
456
475
 
457
- ChiliLogger.instance.publish_instant_log(desc, agent, log)
476
+ ChiliLogger.instance.publish_instant_log(deasc: desc, user: user, main_content: main_content)
458
477
  # works fine
459
478
  ```
460
479
 
461
480
 
462
481
  ## Advanced Usage
463
- It will often happen that the information needed for a log is distributed in many parts of the application and can't be accessed in a single moment nor in a single place of the code base. For instance, you may want to set the log's agent with information about the current_user that's only available in the Controllers Layer. And you might want to add data to the log every time a DB record is modified, which is easier to do in the Models Layer. For these use cases, ChiliLogger's `publish_instant_log` will not be suitable and you should use more advanced features.
482
+ It will often happen that the information needed for a log is distributed in many parts of the application and can't be accessed in a single moment nor in a single place of the code base. For instance, you may want to set the log's user with information about the current_user that's only available in the Controllers Layer. And you might want to add data to the log every time a DB record is modified, which is easier to do in the Models Layer. For these use cases, ChiliLogger's `publish_instant_log` will not be suitable and you should use more advanced features.
464
483
 
465
484
  ChiliLogger takes advantage of Ruby's Thread class and allows you to start a log and keep adding information to it during the current thread's lifespan. This is particularly useful when logging requests to a controller, since these requests will run from beginning to end in a single thread.
466
485
 
@@ -472,8 +491,9 @@ Once a log is started, it can be accessed and modified by using ChiliLogger's `s
472
491
  class VideosController
473
492
  def create
474
493
  desc = { type: 'transaction', service: 'videos', action: 'create' }
475
- agent = { user: current_user.as_json, company: current_user.as_json }
476
- current_log = ChiliLogger.instance.start_new_log(desc, agent) # Step One
494
+ user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
495
+ company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
496
+ current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user) # Step One
477
497
 
478
498
  Video.create(title: 'Lateralus') # Step Two happens in the Video model
479
499
 
@@ -501,15 +521,12 @@ end
501
521
  "service": "videos",
502
522
  "action": "create",
503
523
  "desc": "development.creatives.transaction.videos.create",
504
- "agent": {
505
- "user": {
506
- "id": 88,
507
- "email": 'exemple@test.com'
508
- },
509
- "company": {
510
- "id": 75,
511
- "name": 'Chiligum'
512
- }
524
+ "user": {
525
+ "cognito_id": 'not_specified',
526
+ "email": 'not_specified',
527
+ "company_cognito_id": 'not_specified',
528
+ "company_name": 'not_specified',
529
+ "ghost_user_cognito_id": 'not_specified'
513
530
  },
514
531
  "transaction": {
515
532
  "modified_records": {
@@ -526,7 +543,7 @@ end
526
543
  }
527
544
  ```
528
545
 
529
- Notice that the log is started in VideosController#create with some initial info about the agent user and the log description; following, the same log has its main_content customized in Video.add_modified_record_to_log, by accessing ChiliLogger's `current_log` and calling its `add_modified_record` method. And, afterwards, the same log is finished and published with `current_log.publish`, again in VideosController#create.
546
+ Notice that the log is started in VideosController#create with some initial info about the user and the log description; following, the same log has its main_content customized in Video.add_modified_record_to_log, by accessing ChiliLogger's `current_log` and calling its `add_modified_record` method. And, afterwards, the same log is finished and published with `current_log.publish`, again in VideosController#create.
530
547
 
531
548
  Check the [Snippets For Quick Logging](##snippets-for-quick-logging) section to see how this functionality can be used to quickly set an all-embracing logging system for your application.
532
549
 
@@ -537,8 +554,9 @@ The controller code above could be improved by adding some logic to log unexpect
537
554
  class VideosController
538
555
  def create
539
556
  desc = { type: 'transaction', service: 'videos', action: 'create' }
540
- agent = { user: current_user.as_json, company: current_user.as_json } # .as_json converts ActiveReacords into Hash
541
- current_log = ChiliLogger.instance.start_new_log(desc, agent)
557
+ user = { cognito_id: 88, email: 'exemple@test.com', company_cognito_id: 75,
558
+ company_name: "Chiligum Creatives", ghost_user_cognito_id: 55 },
559
+ current_log = ChiliLogger.instance.start_new_log(desc: desc, user: user)
542
560
 
543
561
  Video.create(title: 'Lateralus')
544
562
 
@@ -581,7 +599,7 @@ current_log.user
581
599
  # { cognito_id: 'not_specified', email: 'not_specified', company_cognito_id: 'not_specified',
582
600
  # company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
583
601
 
584
- current_log.update_user({ email: 'new_email' } })
602
+ current_log.update_user(email: 'new_email')
585
603
  current_log.user
586
604
  # { cognito_id: 'not_specified', email: 'new_email', company_cognito_id: 'not_specified',
587
605
  # company_name: 'not_specified', ghost_user_cognito_id: 'not_specified' }
@@ -788,7 +806,7 @@ current_log.errors
788
806
  ```
789
807
 
790
808
  #### clear_log_info
791
- sets agent, desc, and main_content to their default values.
809
+ sets user, desc, and main_content to their default values.
792
810
 
793
811
 
794
812
  ## Snippets for Quicker Logging
@@ -865,7 +883,6 @@ class ApplicationController < ActionController::Base
865
883
 
866
884
  # if unexpected errors happen, will change log type and publish it for debugging/audit
867
885
  def publish_error_log(error)
868
- error = [error.inspect, Rails.backtrace_cleaner.clean(error.backtrace)]
869
886
  current_log = ChiliLogger.instance.current_log
870
887
 
871
888
  current_log.add_error(error)
@@ -889,7 +906,7 @@ class ApplicationController < ActionController::Base
889
906
  multi_user_record = MultiUser::User.where(external_id: current_user&.cognito_id).first
890
907
  log_user = {
891
908
  email: current_user&.email,
892
- cognito_id: multi_user_record&.id,
909
+ cognito_id: multi_user_record&.external_id,
893
910
  company_name: current_user&.company&.name,
894
911
  company_cognito_id: multi_user_record&.organization&.id
895
912
  }
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.license = 'MIT'
13
13
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
14
  spec.add_dependency 'aws-sdk-sqs', '~> 1.30.0'
15
+ spec.add_dependency 'aws-sdk-secretsmanager', '~> 1.36.0'
15
16
  spec.add_dependency 'bunny'
16
17
  spec.add_dependency 'httparty'
17
18
 
@@ -0,0 +1,47 @@
1
+ require 'errors/error_messages/config_error'
2
+ require 'brokers/rabbit_broker'
3
+
4
+ # class for centralizing Creative's logging logic
5
+ class ChiliLogger
6
+ # class that configures and manages all supported brokers
7
+ class MainBroker
8
+ def initialize(broker_name, broker_config, logging_error_handler)
9
+ @default = ChiliLogger::Values::Default.new
10
+ @logging_error_handler = logging_error_handler
11
+ broker_config ||= {}
12
+ validate_config(broker_config)
13
+
14
+ broker_name ||= @default.msg_broker
15
+ broker_class = supported_brokers[broker_name.to_sym]
16
+ unsupported_broker_error(broker_name) unless broker_class
17
+
18
+ @broker = broker_class.new(broker_config)
19
+ end
20
+
21
+ def publish(message)
22
+ return if ChiliLogger.instance.deactivated
23
+
24
+ @broker.publish(message)
25
+ rescue StandardError => e
26
+ @logging_error_handler.handle_error(e, message)
27
+ end
28
+
29
+ private
30
+
31
+ def validate_config(config)
32
+ invalid_config_err = ':msg_broker_config must be a hash'
33
+ raise(ChiliLogger::ConfigError, invalid_config_err) unless [Hash, NilClass].include?(config.class)
34
+ end
35
+
36
+ def unsupported_broker_error(broker_name)
37
+ err_msg = "'#{broker_name}' is not a supported broker_name. Supported broker_names: #{supported_brokers}"
38
+ raise(ConfigError, err_msg)
39
+ end
40
+
41
+ def supported_brokers
42
+ {
43
+ rabbitmq: RabbitBroker
44
+ }
45
+ end
46
+ end
47
+ end
@@ -1,23 +1,21 @@
1
1
  require 'bunny'
2
2
  require 'brokers/sqs_broker'
3
+ require 'helpers/values/secrets/secrets'
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
8
- def initialize(logging_error_handler)
9
- @logging_error_handler = logging_error_handler
10
- end
11
-
12
- def config(config)
13
- validate_config(config)
9
+ def initialize(custom_config = {})
10
+ custom_config ||= {}
11
+ config = rabbit_config(custom_config)
14
12
 
15
13
  @user = config[:user]
16
14
  @password = config[:password]
17
15
  @ip = config[:ip]
18
16
  @port = config[:port]
19
17
  @exchange_name = config[:exchange_name]
20
- @routing_key_overwriter = config[:routing_key_overwriter]
18
+ @routing_key_overwriter = custom_config[:routing_key_overwriter]
21
19
 
22
20
  connect
23
21
  end
@@ -30,39 +28,22 @@ class ChiliLogger
30
28
 
31
29
  @channel = @connection.create_channel
32
30
  @exchange = @channel.topic(@exchange_name, durable: true)
33
-
34
31
  rescue StandardError => e
35
32
  puts 'Could not connect to RabbitMQ due to the following error:'
36
33
  puts e
37
34
  end
38
35
 
39
- def disconnect
40
- @connection.close
41
- end
42
-
43
- def publish(message, routing_key)
36
+ def publish(message)
44
37
  return if ChiliLogger.instance.deactivated
45
38
 
46
- # if no routing_key was provided when configuring RabbitBroker, than use the one received by the method here
47
- # temporary solution so we can force configure ChiliLogger to use a specific routing_key
48
- key = @routing_key_overwriter || routing_key
39
+ # if no routing_key was provided when configuring RabbitBroker, than use the message's description tag
40
+ key = @routing_key_overwriter || message[:desc] || message['desc']
49
41
  @exchange.publish(message.to_json, routing_key: key)
50
42
  puts "sent message to #{@exchange_name}, with routing_key = '#{key}'"
51
-
52
- rescue StandardError => e
53
- @logging_error_handler.handle_error(e, message)
54
43
  end
55
44
 
56
45
  private
57
46
 
58
- def invalid_config_format_error
59
- 'The configuration object must be a Hash'
60
- end
61
-
62
- def config_attr_error(attr)
63
- "The configuration object must include a ':#{attr}' attribute"
64
- end
65
-
66
47
  def connection_config
67
48
  {
68
49
  user: @user,
@@ -72,11 +53,16 @@ class ChiliLogger
72
53
  }
73
54
  end
74
55
 
75
- def validate_config(config)
76
- raise(ChiliLogger::ConfigError, invalid_config_format_error) unless config.is_a?(Hash)
56
+ def rabbit_config(custom_config)
57
+ default_secrets = Values::Secrets.new.get_secrets_collection('ChiliLoggerRabbitCredentials')
58
+ default_keys = %i[user password ip port exchange_name]
59
+ config = {}
60
+
61
+ default_keys.each do |key|
62
+ config[key] = custom_config[key] || default_secrets[key.to_s]
63
+ end
77
64
 
78
- main_config_keys = %i[user password ip port exchange_name]
79
- main_config_keys.each { |key| raise(ChiliLogger::ConfigError, config_attr_error(key)) unless config[key] }
65
+ config
80
66
  end
81
67
  end
82
68
  end
@@ -6,21 +6,50 @@ class ChiliLogger
6
6
  module AWS
7
7
  # class that handles errors when message broker can't be reached, etc...
8
8
  class SqsBroker
9
- def initialize(config)
10
- @sqs_config = validate_config(config)
11
- @queue_name = config[:queue_name]
9
+ def initialize(custom_config = {})
10
+ custom_config ||= {}
11
+
12
+ @default_secrets = Values::Secrets.new.get_secrets_collection('ChiliLoggerSQSCredentials')
12
13
  @default = ChiliLogger::Values::Default.new
14
+ @queue_name = custom_config[:queue_name] || @default_secrets['queue_name']
13
15
 
16
+ @sqs_config = sqs_config(custom_config)
14
17
  @sqs = Aws::SQS::Client.new(@sqs_config)
18
+
19
+ ENV['AWS_REGION'] = @sqs_config[:region] # Aws::SQS::QueuePoller requires region as an env variable
20
+ @poller = Aws::SQS::QueuePoller.new(queue_url(@queue_name), skip_delete: true)
15
21
  end
16
22
 
17
23
  def publish(message = @default.sqs_message)
24
+ return if ChiliLogger.instance.deactivated
25
+
18
26
  message_body = { body: message }
19
27
  queue_message = queue_message(message_body)
20
28
 
21
29
  @sqs.send_message(queue_message)
22
30
  end
23
31
 
32
+ def fetch_messages
33
+ return if ChiliLogger.instance.deactivated
34
+
35
+ remaining_msgs = num_of_msgs_in_queue
36
+ messages = []
37
+
38
+ @poller.poll do |sqs_msg|
39
+ log = JSON.parse(sqs_msg.body)['body']['log']
40
+ messages << { log: log, fallback_broker_msg: sqs_msg }
41
+
42
+ remaining_msgs -= 1
43
+ throw :stop_polling if remaining_msgs <= 0 # prevents infinite loop
44
+ end
45
+
46
+ messages
47
+ end
48
+
49
+ def delete_message(msg)
50
+ @poller.delete_messages([msg])
51
+ end
52
+
24
53
  private
25
54
 
26
55
  def queue_message(message_body)
@@ -36,25 +65,20 @@ class ChiliLogger
36
65
  @sqs.get_queue_url(queue_name: queue_name).queue_url
37
66
  end
38
67
 
39
- def invalid_config_format_error
40
- 'The :error_hanldler_config object must be a Hash'
68
+ def num_of_msgs_in_queue
69
+ sqs_queue_attrs = @sqs.get_queue_attributes(queue_url: queue_url(@queue_name), attribute_names: ['All']).attributes
70
+ sqs_queue_attrs['ApproximateNumberOfMessages'].to_i
41
71
  end
42
72
 
43
- def config_attr_error(attr)
44
- "The configuration object must include a ':#{attr}' attribute"
45
- end
73
+ def sqs_config(custom_config)
74
+ default_keys = %i[region access_key_id secret_access_key]
75
+ config = {}
46
76
 
47
- def validate_config(config)
48
- raise(ChiliLogger::ConfigError, invalid_config_format_error) unless config.is_a?(Hash)
77
+ default_keys.each do |key|
78
+ config[key] = custom_config[key] || @default_secrets[key.to_s]
79
+ end
49
80
 
50
- main_config_keys = %i[region access_key_id secret_access_key queue_name]
51
- main_config_keys.each { |key| raise(ChiliLogger::ConfigError, config_attr_error(key)) unless config[key] }
52
-
53
- {
54
- region: config[:region],
55
- access_key_id: config[:access_key_id],
56
- secret_access_key: config[:secret_access_key]
57
- }
81
+ config
58
82
  end
59
83
  end
60
84
  end
@@ -1,15 +1,14 @@
1
+ require 'singleton'
2
+ require 'brokers/main_broker'
1
3
  require 'chili_logger/version'
2
4
  require 'current_log_accessor'
3
- require 'errors/error_messages/config_error'
4
5
  require 'errors/logging_error_handler/logging_error_handler'
5
- require 'brokers/rabbit_broker'
6
- require 'message_writer/message_writer'
7
- require 'brokers/sqs_broker'
8
6
  require 'helpers/values/default'
9
7
  require 'helpers/values/type_uniformizer/desc'
10
8
  require 'helpers/values/type_uniformizer/user'
11
9
  require 'helpers/values/type_uniformizer/main_content'
12
- require 'singleton'
10
+ require 'message_writer/message_writer'
11
+ require 'unpublished_logs_manager/unpublished_logs_manager'
13
12
 
14
13
  # class for centralizing Creative's logging logic
15
14
  class ChiliLogger
@@ -32,6 +31,7 @@ class ChiliLogger
32
31
  config_logging_error_handler(config)
33
32
  config_msg_writer(config)
34
33
  config_msg_broker(config)
34
+ config_unpublished_logs_manager
35
35
 
36
36
  @current_log_accessor = CurrentLogAccessor.new(@msg_broker, @msg_writer)
37
37
  end
@@ -59,16 +59,16 @@ class ChiliLogger
59
59
  end
60
60
 
61
61
  message = @msg_writer.write(options)
62
- routing_key = @msg_writer.write_description_tag(options[:desc])
62
+ @msg_broker.publish(message)
63
+ end
63
64
 
64
- @msg_broker.publish(message, routing_key)
65
+ def republish_unpublished_logs
66
+ @unpublished_logs_manager.republish
65
67
  end
66
68
 
67
69
  private
68
70
 
69
71
  def config_logging_error_handler(general_config)
70
- return @logging_error_handler = nil if @deactivated
71
-
72
72
  fallback_name = general_config[:fallback_broker]
73
73
  fallback_config = general_config[:fallback_broker_config]
74
74
  @logging_error_handler = LoggingErrorHandler.new(fallback_name, fallback_config)
@@ -76,8 +76,8 @@ class ChiliLogger
76
76
 
77
77
  def config_msg_writer(general_config)
78
78
  msg_writer_config = {
79
- env: general_config[:log_env],
80
- layer: general_config[:log_layer],
79
+ env: general_config[:log_env]&.downcase,
80
+ layer: general_config[:log_layer]&.downcase,
81
81
  server_url: general_config[:server_url],
82
82
  cloud_provider: general_config[:cloud_provider]
83
83
  }
@@ -86,26 +86,13 @@ class ChiliLogger
86
86
  end
87
87
 
88
88
  def config_msg_broker(general_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
91
- @msg_broker = supported_msg_brokers[msg_broker_name.to_sym]
92
- raise(ConfigError, broker_name_error(msg_broker_name)) unless @msg_broker
93
-
94
- # TODO: implement MessageBroker class, an interface between ChiliLogger and possible brokers.
95
- # TODO: add "supported_msg_broker", "broker_name_error" and "return @msg_broker if @deactivated" to MessageBroker
96
- # prevents broker from even starting connection if ChiliLogger is deactivated
97
- return @msg_broker if @deactivated
89
+ msg_broker_name = general_config[:msg_broker_name]
90
+ msg_broker_config = general_config[:msg_broker_config]
98
91
 
99
- @msg_broker.config(msg_broker_config)
92
+ @msg_broker = MainBroker.new(msg_broker_name, msg_broker_config, @logging_error_handler)
100
93
  end
101
94
 
102
- def broker_name_error(msg_broker_name)
103
- "#{msg_broker_name} is not supported. Supported msg_broker_names: #{supported_msg_brokers}"
104
- end
105
-
106
- def supported_msg_brokers
107
- {
108
- rabbitmq: RabbitBroker.new(@logging_error_handler)
109
- }
95
+ def config_unpublished_logs_manager
96
+ @unpublished_logs_manager = UnpublishedLogsManager.new(@msg_broker, @logging_error_handler)
110
97
  end
111
98
  end
@@ -1,3 +1,3 @@
1
1
  class ChiliLogger
2
- VERSION = '0.0.12'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'message_writer/message_writer'
2
- require 'helpers/values/default'
3
2
  require 'helpers/values/type_uniformizer/desc'
4
3
  require 'helpers/values/type_uniformizer/main_content'
5
4
  require 'helpers/values/type_uniformizer/user'
@@ -19,11 +18,9 @@ class ChiliLogger
19
18
 
20
19
  def publish(**options)
21
20
  update_current_log(options)
22
-
23
21
  message = @msg_writer.write(desc: desc, user: user, main_content: main_content)
24
- routing_key = @msg_writer.write_description_tag(desc)
22
+ @msg_broker.publish(message)
25
23
 
26
- @msg_broker.publish(message, routing_key)
27
24
  clear_log_info
28
25
  end
29
26
 
@@ -5,31 +5,46 @@ require 'errors/error_messages/config_error'
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(fallback_name, config)
9
- raise(ChiliLogger::ConfigError, ':fallback_broker_config must be present and be a hash') unless config.is_a?(Hash)
8
+ def initialize(fallback_name = nil, config = {})
9
+ @default = ChiliLogger::Values::Default.new
10
+ config ||= {}
11
+ validate_config(config)
10
12
 
11
- fallback_name = fallback_name.to_sym if fallback_name
12
- fallback_broker_class = supported_fallback_brokers[fallback_name]
13
+ fallback_name ||= @default.fallback_broker
14
+ fallback_broker_class = supported_fallback_brokers[fallback_name.to_sym]
13
15
  unsupported_fallback_broker_error unless fallback_broker_class
14
16
 
15
17
  @fallback_broker = fallback_broker_class.new(config)
16
18
  end
17
19
 
18
20
  def handle_error(error, log = nil)
21
+ return if ChiliLogger.instance.deactivated
22
+
19
23
  message = message(error, log)
20
24
  @fallback_broker.publish(message)
21
25
  rescue StandardError => e
22
- puts '
23
- There was a problem with both the Message Broker and the Fallback Broker simultaneously.
26
+ puts 'There was a problem with both the Message Broker and the Fallback Broker simultaneously.
24
27
  To keep the application running and prevent downtime,
25
28
  ChiliLogger will ignore this errors and discard the log it was currently trying to publish.
26
- Please note that logs are being permanently lost.
27
- '
29
+ Please note that logs are being permanently lost.'
28
30
  puts e
29
31
  end
30
32
 
33
+ def fetch_unpublished_logs
34
+ @fallback_broker.fetch_messages
35
+ end
36
+
37
+ def delete_unpublished_log(msg)
38
+ @fallback_broker.delete_message(msg)
39
+ end
40
+
31
41
  private
32
42
 
43
+ def validate_config(config)
44
+ invalid_config_err = ':fallback_broker_config must be a hash'
45
+ raise(ChiliLogger::ConfigError, invalid_config_err) unless [Hash, NilClass].include?(config.class)
46
+ end
47
+
33
48
  def message(error, log)
34
49
  {
35
50
  error_type: error.class.name,
@@ -1,6 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'fileutils'
3
- require 'helpers/values/default'
3
+ require 'byebug'
4
4
 
5
5
  # class for centralizing Creative's logging logic
6
6
  class ChiliLogger
@@ -22,8 +22,10 @@ class ChiliLogger
22
22
 
23
23
  def self.read_file
24
24
  file_exists = File.file?(file_path)
25
+ file = file_exists ? YAML.load_file(file_path) : {}
25
26
 
26
- file_exists ? YAML.load_file(file_path) : {}
27
+ # workaround for a bug in YAML lib, in which #load_file returns nil if file is empty, instead of an empty hash
28
+ file.nil? ? {} : file
27
29
  end
28
30
 
29
31
  def self.write_file(file)
@@ -8,8 +8,8 @@ class ChiliLogger
8
8
  :rabbitmq
9
9
  end
10
10
 
11
- def msg_broker_config
12
- {}
11
+ def fallback_broker
12
+ :aws_sqs
13
13
  end
14
14
 
15
15
  def user
@@ -0,0 +1,63 @@
1
+ require 'aws-sdk-secretsmanager'
2
+ require 'base64'
3
+
4
+ # class for centralizing Creative's logging logic
5
+ class ChiliLogger
6
+ # module responsible for uniformization of values in ChiliLogger
7
+ module Values
8
+ # class to centralize access to all secrets stored in 3rd-party managers
9
+ class Secrets
10
+ # Module to load secrets environment variables
11
+ class AwsSecretsManager
12
+ def initialize(region_name = 'us-east-1')
13
+ @client = Aws::SecretsManager::Client.new(region: region_name)
14
+ end
15
+
16
+ def get_secrets_collection(collection_name)
17
+ JSON.parse(get_secret(collection_name))
18
+ end
19
+
20
+ private
21
+
22
+ # rubocop:disable Metrics/MethodLength
23
+ # Use this code snippet in your app.
24
+ # If you need more information about configurations or implementing the sample code, visit the AWS docs:
25
+ # https://aws.amazon.com/developers/getting-started/ruby/
26
+ # method from AWS example, ipsis literis
27
+ def get_secret(secret_name)
28
+ # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
29
+ # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
30
+ # We rethrow the exception by default.
31
+ get_secret_value_response = @client.get_secret_value(secret_id: secret_name)
32
+ rescue Aws::SecretsManager::Errors::DecryptionFailure => _e
33
+ # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
34
+ # Deal with the exception here, and/or rethrow at your discretion.
35
+ raise
36
+ rescue Aws::SecretsManager::Errors::InternalServiceError => _e
37
+ # An error occurred on the server side.
38
+ # Deal with the exception here, and/or rethrow at your discretion.
39
+ raise
40
+ rescue Aws::SecretsManager::Errors::InvalidParameterException => _e
41
+ # You provided an invalid value for a parameter.
42
+ # Deal with the exception here, and/or rethrow at your discretion.
43
+ raise
44
+ rescue Aws::SecretsManager::Errors::InvalidRequestException => _e
45
+ # You provided a parameter value that is not valid for the current state of the resource.
46
+ # Deal with the exception here, and/or rethrow at your discretion.
47
+ raise
48
+ rescue Aws::SecretsManager::Errors::ResourceNotFoundException => _e
49
+ # We can't find the resource that you asked for.
50
+ # Deal with the exception here, and/or rethrow at your discretion.
51
+ raise
52
+ else
53
+ # This block is ran if there were no exceptions.
54
+
55
+ # Decrypts secret using the associated KMS CMK.
56
+ # Depending on whether the secret is a string or binary, one of these fields will be populated.
57
+ get_secret_value_response.secret_string || Base64.decode64(get_secret_value_response.secret_binary)
58
+ end
59
+ # rubocop:enable Metrics/MethodLength
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,29 @@
1
+ require 'helpers/values/secrets/aws_secrets_manager'
2
+
3
+ # class for centralizing Creative's logging logic
4
+ class ChiliLogger
5
+ # module responsible for uniformization of values in ChiliLogger
6
+ module Values
7
+ # class to centralize access to all secrets stored in 3rd-party managers
8
+ class Secrets
9
+ def initialize(manager_name = 'aws')
10
+ manager_class = supported_managers[manager_name.to_s]
11
+ manager_class ||= supported_managers['aws']
12
+
13
+ @secrets_manager = manager_class.new
14
+ end
15
+
16
+ def get_secrets_collection(collection_name)
17
+ @secrets_manager.get_secrets_collection(collection_name)
18
+ end
19
+
20
+ private
21
+
22
+ def supported_managers
23
+ {
24
+ 'aws' => AwsSecretsManager
25
+ }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,46 @@
1
+ require 'errors/error_messages/config_error'
2
+ require 'brokers/main_broker'
3
+ require 'errors/logging_error_handler/logging_error_handler'
4
+
5
+ # class for centralizing Creative's logging logic
6
+ class ChiliLogger
7
+ # class for manipulating unpublished logs that were sent to the fallback_broker
8
+ class UnpublishedLogsManager
9
+ def initialize(msg_broker, logging_error_handler)
10
+ validate_msg_broker_type(msg_broker)
11
+ validate_logging_error_handler_type(logging_error_handler)
12
+
13
+ @msg_broker = msg_broker
14
+ @logging_error_handler = logging_error_handler
15
+ end
16
+
17
+ def republish
18
+ unpub_logs = @logging_error_handler.fetch_unpublished_logs
19
+ unpub_logs.each do |unpub_log|
20
+ fallback_broker_msg = unpub_log[:fallback_broker_msg]
21
+ log = unpub_log[:log]
22
+
23
+ # if a message can't be published by MainBroker, LoggingErrorHandler sends it to the fallback_broker
24
+ # to avoid duplicates in this scenario, we delete the message before even trying to publish it
25
+ @logging_error_handler.delete_unpublished_log(fallback_broker_msg)
26
+ @msg_broker.publish(log)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def validate_msg_broker_type(msg_broker)
33
+ err_type = ChiliLogger::ConfigError
34
+ err_msg = "#{msg_broker.inspect} should be an instance of ChiliLogger::MainBroker"
35
+
36
+ raise(err_type, err_msg) unless msg_broker.class == ChiliLogger::MainBroker
37
+ end
38
+
39
+ def validate_logging_error_handler_type(error_handler)
40
+ err_type = ChiliLogger::ConfigError
41
+ err_msg = "#{error_handler} should be an instance of ChiliLogger::LoggingError::Handler"
42
+
43
+ raise(err_type, err_msg) unless error_handler.class == ChiliLogger::LoggingErrorHandler
44
+ end
45
+ end
46
+ end
@@ -51,9 +51,15 @@ test:
51
51
  transaction:
52
52
  console:
53
53
  publish_test:
54
- - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/chili_logger-0.0.12/lib/chili_logger.rb:61:in
54
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/chili_logger-0.1.0/lib/chili_logger.rb:61:in
55
55
  `publish_instant_log'"
56
- - "(pry):50:in `__pry__'"
56
+ - "(pry):189:in `block (2 levels) in __pry__'"
57
+ uncaught_error:
58
+ console:
59
+ publish_test:
60
+ - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/chili_logger-0.1.0/lib/chili_logger.rb:61:in
61
+ `publish_instant_log'"
62
+ - "(pry):10:in `__pry__'"
57
63
  - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in
58
64
  `eval'"
59
65
  - "/home/lucas/.rvm/gems/ruby-2.3.3/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chili_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.1.2
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-07-15 00:00:00.000000000 Z
11
+ date: 2020-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-sqs
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.30.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-secretsmanager
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.36.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.36.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bunny
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -76,6 +90,7 @@ files:
76
90
  - bin/console
77
91
  - bin/setup
78
92
  - chili_logger.gemspec
93
+ - lib/brokers/main_broker.rb
79
94
  - lib/brokers/rabbit_broker.rb
80
95
  - lib/brokers/sqs_broker.rb
81
96
  - lib/chili_logger.rb
@@ -87,11 +102,14 @@ files:
87
102
  - lib/helpers/logs_coverage/coverage_writer.rb
88
103
  - lib/helpers/logs_coverage/levantamento_provisório.yaml
89
104
  - lib/helpers/values/default.rb
105
+ - lib/helpers/values/secrets/aws_secrets_manager.rb
106
+ - lib/helpers/values/secrets/secrets.rb
90
107
  - lib/helpers/values/type_uniformizer/desc.rb
91
108
  - lib/helpers/values/type_uniformizer/main_content.rb
92
109
  - lib/helpers/values/type_uniformizer/user.rb
93
110
  - lib/message_writer/aws_ops_metadata.rb
94
111
  - lib/message_writer/message_writer.rb
112
+ - lib/unpublished_logs_manager/unpublished_logs_manager.rb
95
113
  - log/chili-logger-coverage.yml
96
114
  homepage: https://gitlab.com/chiligumdev/chili_logger
97
115
  licenses: