event_tracer 0.4.1 → 0.4.5

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: 335d4c810b2a3e378d826518f8e8c6c2a8a1310f029401381ac2f407437554ca
4
- data.tar.gz: 95971a338ad15cb6f5f74bb78f10f59dfba0918c7578a3e1057f8d11e5dc7bbb
3
+ metadata.gz: db21e7db07d318ce6c78448f6e7ce6ae8819c89456708c9eac9b44dddde547b1
4
+ data.tar.gz: dcc79806b5e10f6f34ebd19c8a77bdfcae27bb3c7b064811196b9617c4d3c0b8
5
5
  SHA512:
6
- metadata.gz: 473b42b4728e7233bdddaa4d85c681b33b41c71c26eb78ee9578c332f878e78234e6007b37c6c120e0adf13eaa8dd87e18f09d2069cfe7ca578e2ef8f1fc6079
7
- data.tar.gz: 5a58acc598909419d8164a4cfb46c2c9695b12faee98bdf2ae937648dc964c4c70e04c78e8a2395ed133a245ca86d4ac7d471972a7e72510267d2bcb754b1e56
6
+ metadata.gz: 6ab07f8de34b3c499a4b3b16c3d1e3895b4776dcca97e34a701d68bbdb1654312f7803d78e2d2c9e69a2a8173b8087e613749231de6eed1762eb24f6c6029220
7
+ data.tar.gz: 37ef3ac7ded60e1c4d1ebe75e00e3a76b9ae2230f7eeba4b8961be96054bc5e54b2cc4b6a0e7224e30e232b7e27f80e3bcc88d308c0c0f1dafc244fb4082f073
@@ -1,4 +1,3 @@
1
- require_relative '../event_tracer'
2
1
  require_relative './basic_decorator'
3
2
 
4
3
  # NOTES
@@ -21,9 +20,11 @@ module EventTracer
21
20
  DEFAULT_METRIC_TYPE = :increment_counter
22
21
  DEFAULT_COUNTER = 1
23
22
 
23
+ attr_reader :allowed_tags
24
+
24
25
  def initialize(decoratee, allowed_tags: [])
25
26
  super(decoratee)
26
- @allowed_tags = allowed_tags
27
+ @allowed_tags = allowed_tags.freeze
27
28
  end
28
29
 
29
30
  LOG_TYPES.each do |log_type|
@@ -53,7 +54,6 @@ module EventTracer
53
54
 
54
55
  private
55
56
 
56
- attr_reader :decoratee, :allowed_tags
57
57
  alias_method :appsignal, :decoratee
58
58
 
59
59
  def valid_args?(metrics)
@@ -1,4 +1,3 @@
1
- require_relative '../event_tracer'
2
1
  require_relative './basic_decorator'
3
2
  require 'json'
4
3
 
@@ -15,7 +14,6 @@ module EventTracer
15
14
 
16
15
  private
17
16
 
18
- attr_reader :logger, :decoratee
19
17
  alias_method :logger, :decoratee
20
18
 
21
19
  # EventTracer ensures action & message is always populated
@@ -24,5 +24,8 @@ module EventTracer
24
24
  LogResult.new(false, message)
25
25
  end
26
26
 
27
+ private
28
+
29
+ attr_reader :decoratee
27
30
  end
28
31
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+
5
+ module EventTracer
6
+ class BufferedLogger
7
+ def initialize(log_processor:, worker:, buffer: Buffer.new(buffer_size: 0))
8
+ @buffer = buffer
9
+ @worker = worker
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, :worker
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
+ execute_payload(all_payloads)
29
+ end
30
+
31
+ LogResult.new(true)
32
+ end
33
+
34
+ def execute_payload(payloads)
35
+ worker.perform_async(payloads)
36
+ rescue JSON::GeneratorError => e
37
+ filtered_payloads = filter_invalid_data(payloads)
38
+
39
+ EventTracer.warn(
40
+ loggers: %i(base),
41
+ action: self.class.name,
42
+ app: EventTracer::Config.config.app_name,
43
+ error: e.class.name,
44
+ message: e.message,
45
+ payload: payloads - filtered_payloads
46
+ )
47
+
48
+ worker.perform_async(filtered_payloads) if filtered_payloads.any?
49
+ end
50
+
51
+ def filter_invalid_data(payloads)
52
+ payloads.select { |payload| payload.to_json rescue false }
53
+ end
54
+ end
55
+ end
@@ -1,13 +1,24 @@
1
1
  require 'dry-configurable'
2
+ require 'dry/configurable/version'
2
3
 
3
4
  module EventTracer
4
5
  class Config
5
6
  extend Dry::Configurable
6
7
 
7
- setting :app_name, default: 'app_name'
8
+ if Dry::Configurable::VERSION >= "0.13"
9
+ setting :app_name, default: 'app_name'
8
10
 
9
- # TODO: switch to namespace in v1.0
10
- setting :dynamo_db_table_name, default: 'logs'
11
- setting :dynamo_db_client
11
+ # TODO: switch to namespace in v1.0
12
+ setting :dynamo_db_table_name, default: 'logs'
13
+ setting :dynamo_db_client
14
+ setting :dynamo_db_queue_name, default: 'low'
15
+ else
16
+ setting :app_name, 'app_name'
17
+
18
+ # TODO: switch to namespace in v1.0
19
+ setting :dynamo_db_table_name, 'logs'
20
+ setting :dynamo_db_client
21
+ setting :dynamo_db_queue_name, 'low'
22
+ end
12
23
  end
13
24
  end
@@ -1,4 +1,3 @@
1
- require_relative '../event_tracer'
2
1
  require_relative './basic_decorator'
3
2
  # NOTES
4
3
  # Datadog interface to send our usual actions
@@ -23,9 +22,11 @@ module EventTracer
23
22
  DEFAULT_METRIC_TYPE = :count
24
23
  DEFAULT_COUNTER = 1
25
24
 
25
+ attr_reader :allowed_tags
26
+
26
27
  def initialize(decoratee, allowed_tags: [])
27
28
  super(decoratee)
28
- @allowed_tags = allowed_tags
29
+ @allowed_tags = allowed_tags.freeze
29
30
  end
30
31
 
31
32
  LOG_TYPES.each do |log_type|
@@ -55,7 +56,6 @@ module EventTracer
55
56
 
56
57
  private
57
58
 
58
- attr_reader :decoratee, :allowed_tags
59
59
  alias_method :datadog, :decoratee
60
60
 
61
61
  def valid_args?(metrics)
@@ -7,33 +7,10 @@ require_relative 'default_processor'
7
7
 
8
8
  module EventTracer
9
9
  module DynamoDB
10
- class Logger
10
+ class Logger < BufferedLogger
11
11
  def initialize(buffer: Buffer.new(buffer_size: 0), log_processor: DefaultProcessor.new)
12
- @buffer = buffer
13
- @log_processor = log_processor
12
+ super(buffer: buffer, log_processor: log_processor, worker: Worker)
14
13
  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
14
  end
38
15
  end
39
16
  end
@@ -15,7 +15,7 @@ module EventTracer
15
15
  class Worker
16
16
  include ::Sidekiq::Worker
17
17
 
18
- sidekiq_options retry: 1, queue: 'low'
18
+ sidekiq_options retry: 1, queue: EventTracer::Config.config.dynamo_db_queue_name
19
19
 
20
20
  # See https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/DynamoDB/Client.html#batch_write_item-instance_method
21
21
  MAX_DYNAMO_DB_ITEM_PER_REQUEST = 25
@@ -1,3 +1,3 @@
1
1
  module EventTracer
2
- VERSION = '0.4.1'.freeze
2
+ VERSION = '0.4.5'.freeze
3
3
  end
data/lib/event_tracer.rb CHANGED
@@ -3,7 +3,7 @@ require 'event_tracer/log_result'
3
3
 
4
4
  module EventTracer
5
5
 
6
- LOG_TYPES = %i(info warn error)
6
+ LOG_TYPES = %i(debug info warn error)
7
7
 
8
8
  @loggers = {}
9
9
 
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.4.1
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - melvrickgoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-01 00:00:00.000000000 Z
11
+ date: 2022-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -93,6 +93,7 @@ files:
93
93
  - lib/event_tracer/base_logger.rb
94
94
  - lib/event_tracer/basic_decorator.rb
95
95
  - lib/event_tracer/buffer.rb
96
+ - lib/event_tracer/buffered_logger.rb
96
97
  - lib/event_tracer/config.rb
97
98
  - lib/event_tracer/datadog_logger.rb
98
99
  - lib/event_tracer/dynamo_db/client.rb