event_tracer 0.3.2 → 0.4.0

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: '002520584662d6b31ce2f05310a70c4fb8af37c3c865f6bdf2b8ebc44bf16654'
4
- data.tar.gz: daa3fc169b3daa2771adb7d832872c055b723dbe25d28fff4ba24c618f2b54a2
3
+ metadata.gz: 5f5987bb0a399ec5960ef763bfcd0dbfa5ccd6057706deb1a1916daa126208be
4
+ data.tar.gz: 4ccd9d11711d43ff6bb9facc9bed872f2023d09f1c1669469eb1583bce46783b
5
5
  SHA512:
6
- metadata.gz: '038e91c1d03b17070b97c3c170d73992d4050b9cee39f3529fc712a2cfda324d8afdfb10013f6a6838176966f94d6dd17b5f4851a62af6bdc0e6c8ea2b2e95b1'
7
- data.tar.gz: fdeb28420163369840b82b0a56e6bd83a638fe4c5e7069f280c6807d8f9ad00b9044c642263fc89391628c110af8e527596c2e86c2b759a50691c3d13a7390c5
6
+ metadata.gz: 0d741c12baacd00c430f8d312d364161ba5959ccbcbfc79093ca520908b1d0c1537d9c50d918b3cb1df3610e5384f2fe36416342b9154fcec7e0d3d07b4d5d9f
7
+ data.tar.gz: 5bfd26725b5048a49a94746015127385cf03ada9fa9cede00d8a329780d3524442d40e4157a75c66c820e4076baa1d531381e7fd92374dcf98d0880c419be935
@@ -0,0 +1,9 @@
1
+ module EventTracer
2
+ module DynamoDB
3
+ class Client
4
+ def self.call
5
+ Aws::DynamoDB::Client.new
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module EventTracer
2
+ module DynamoDB
3
+ class DefaultProcessor
4
+ def call(log_type, action:, message:, args:)
5
+ args.merge(
6
+ timestamp: Time.now.utc.iso8601(6),
7
+ action: action,
8
+ message: message,
9
+ log_type: log_type,
10
+ app: EventTracer::Config.config.app_name
11
+ )
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+ require_relative 'client'
5
+ require_relative 'worker'
6
+ require_relative 'default_processor'
7
+
8
+ module EventTracer
9
+ module DynamoDB
10
+ class Logger
11
+ def initialize(buffer: Buffer.new(buffer_size: 0), log_processor: DefaultProcessor.new)
12
+ @buffer = buffer
13
+ @log_processor = log_processor
14
+ end
15
+
16
+ EventTracer::LOG_TYPES.each do |log_type|
17
+ define_method log_type do |**args|
18
+ save_message log_type, **args
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :buffer, :log_processor
25
+
26
+ def save_message(log_type, action:, message:, **args)
27
+ payload = log_processor.call(log_type, action: action, message: message, args: args)
28
+
29
+ unless buffer.add(payload)
30
+ all_payloads = buffer.flush + [payload]
31
+ Worker.perform_async(all_payloads)
32
+ end
33
+
34
+ LogResult.new(true)
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'client'
4
+
5
+ begin
6
+ require 'sidekiq'
7
+ require 'aws-sdk-dynamodb'
8
+ rescue LoadError => e
9
+ puts "Please add the missing gem into your app Gemfile: #{e.message}"
10
+ raise
11
+ end
12
+
13
+ module EventTracer
14
+ module DynamoDB
15
+ class Worker
16
+ include ::Sidekiq::Worker
17
+
18
+ sidekiq_options retry: 1, queue: 'low'
19
+
20
+ # See https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/DynamoDB/Client.html#batch_write_item-instance_method
21
+ MAX_DYNAMO_DB_ITEM_PER_REQUEST = 25
22
+
23
+ def perform(items)
24
+ wrap(items).each_slice(MAX_DYNAMO_DB_ITEM_PER_REQUEST) do |batch|
25
+ data = batch.map do |item|
26
+ { put_request: { item: clean_empty_values(item) } }
27
+ end
28
+
29
+ Client.call.batch_write_item(
30
+ request_items: { EventTracer::Config.config.dynamo_db_table_name => data }
31
+ )
32
+
33
+ rescue Aws::DynamoDB::Errors::ServiceError => e
34
+ EventTracer.error(
35
+ loggers: %i(base),
36
+ action: 'DynamoDBWorker',
37
+ app: EventTracer::Config.config.app_name,
38
+ error: e.class.name,
39
+ message: e.message
40
+ )
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def wrap(items)
47
+ # NOTE: This allows us to handle either buffered or unbuffered payloads
48
+ if items.is_a?(Hash)
49
+ [items]
50
+ else
51
+ Array(items)
52
+ end
53
+ end
54
+
55
+ # dynamo can't serialise empty strings/ non-zero numerics
56
+ def clean_empty_values(data)
57
+ data.delete_if do |_key, value|
58
+ case value
59
+ when Hash
60
+ clean_empty_values(value)
61
+ false
62
+ when String then value.empty?
63
+ else false
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module EventTracer
2
- VERSION = '0.3.2'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
data/lib/event_tracer.rb CHANGED
@@ -70,4 +70,4 @@ module EventTracer
70
70
  end
71
71
 
72
72
  project_root = File.dirname(File.absolute_path(__FILE__))
73
- Dir.glob("#{project_root}/event_tracer/*") {|file| require file}
73
+ Dir.glob("#{project_root}/event_tracer/*.rb") {|file| require file}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - melvrickgoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-11 00:00:00.000000000 Z
11
+ date: 2021-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -95,10 +95,10 @@ files:
95
95
  - lib/event_tracer/buffer.rb
96
96
  - lib/event_tracer/config.rb
97
97
  - lib/event_tracer/datadog_logger.rb
98
- - lib/event_tracer/dynamo_db_client.rb
99
- - lib/event_tracer/dynamo_db_default_processor.rb
100
- - lib/event_tracer/dynamo_db_log_worker.rb
101
- - lib/event_tracer/dynamo_db_logger.rb
98
+ - lib/event_tracer/dynamo_db/client.rb
99
+ - lib/event_tracer/dynamo_db/default_processor.rb
100
+ - lib/event_tracer/dynamo_db/logger.rb
101
+ - lib/event_tracer/dynamo_db/worker.rb
102
102
  - lib/event_tracer/log_result.rb
103
103
  - lib/event_tracer/version.rb
104
104
  homepage: https://github.com/melvrickgoh/event_tracer
@@ -121,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0'
123
123
  requirements: []
124
- rubygems_version: 3.2.28
124
+ rubygems_version: 3.2.3
125
125
  signing_key:
126
126
  specification_version: 4
127
127
  summary: Thin wrapper for formatted logging/ metric services to be used as a single
@@ -1,7 +0,0 @@
1
- module EventTracer
2
- class DynamoDBClient
3
- def self.call
4
- Aws::DynamoDB::Client.new
5
- end
6
- end
7
- end
@@ -1,13 +0,0 @@
1
- module EventTracer
2
- class DynamoDBDefaultProcessor
3
- def call(log_type, action:, message:, args:)
4
- args.merge(
5
- timestamp: Time.now.utc.iso8601(6),
6
- action: action,
7
- message: message,
8
- log_type: log_type,
9
- app: EventTracer::Config.config.app_name
10
- )
11
- end
12
- end
13
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'dynamo_db_client'
4
-
5
- begin
6
- require 'sidekiq'
7
- require 'aws-sdk-dynamodb'
8
- rescue LoadError => e
9
- puts "Please add the missing gem into your app Gemfile: #{e.message}"
10
- raise
11
- end
12
-
13
- module EventTracer
14
- class DynamoDBLogWorker
15
- include ::Sidekiq::Worker
16
-
17
- sidekiq_options retry: 1, queue: 'low'
18
-
19
- # See https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/DynamoDB/Client.html#batch_write_item-instance_method
20
- MAX_DYNAMO_DB_ITEM_PER_REQUEST = 25
21
-
22
- def perform(items)
23
- wrap(items).each_slice(MAX_DYNAMO_DB_ITEM_PER_REQUEST) do |batch|
24
- data = batch.map do |item|
25
- { put_request: { item: clean_empty_values(item) } }
26
- end
27
-
28
- EventTracer::DynamoDBClient.call.batch_write_item(
29
- request_items: { EventTracer::Config.config.dynamo_db_table_name => data }
30
- )
31
-
32
- rescue Aws::DynamoDB::Errors::ServiceError => e
33
- EventTracer.error(
34
- loggers: %i(base),
35
- action: 'DynamoDBLogWorker',
36
- app: EventTracer::Config.config.app_name,
37
- error: e.class.name,
38
- message: e.message
39
- )
40
- end
41
- end
42
-
43
- private
44
-
45
- def wrap(items)
46
- # NOTE: This allows us to handle either buffered or unbuffered payloads
47
- if items.is_a?(Hash)
48
- [items]
49
- else
50
- Array(items)
51
- end
52
- end
53
-
54
- # dynamo can't serialise empty strings/ non-zero numerics
55
- def clean_empty_values(data)
56
- data.delete_if do |_key, value|
57
- case value
58
- when Hash
59
- clean_empty_values(value)
60
- false
61
- when String then value.empty?
62
- else false
63
- end
64
- end
65
- end
66
-
67
- end
68
- end
@@ -1,35 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'time'
4
- require_relative 'dynamo_db_client'
5
-
6
- module EventTracer
7
- class DynamoDBLogger
8
- def initialize(buffer: Buffer.new(buffer_size: 0), log_processor: EventTracer::DynamoDBDefaultProcessor.new)
9
- @buffer = buffer
10
- @log_processor = log_processor
11
- end
12
-
13
- EventTracer::LOG_TYPES.each do |log_type|
14
- define_method log_type do |**args|
15
- save_message log_type, **args
16
- end
17
- end
18
-
19
- private
20
-
21
- attr_reader :buffer, :log_processor
22
-
23
- def save_message(log_type, action:, message:, **args)
24
- payload = log_processor.call(log_type, action: action, message: message, args: args)
25
-
26
- unless buffer.add(payload)
27
- all_payloads = buffer.flush + [payload]
28
- DynamoDBLogWorker.perform_async(all_payloads)
29
- end
30
-
31
- LogResult.new(true)
32
- end
33
-
34
- end
35
- end