event_tracer 0.1.0 → 0.2.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
- SHA1:
3
- metadata.gz: 0c3405136a8481cab1e0af88ad64f86362caa41a
4
- data.tar.gz: 2ea5ddc4c86f075b15f13a568c0ceebd13fd3be5
2
+ SHA256:
3
+ metadata.gz: 72b9ead92b130709f1d0a48bac3508af836e65467b8815aa733c4f7101308472
4
+ data.tar.gz: 47dce77a79c44aa0c012f06a79f3ec3180777f107658c95d1b88d623f33efeba
5
5
  SHA512:
6
- metadata.gz: d1dc0e74037f877d946130b76a9ac6c336f1174601fb6263494341c26298547ca39e6aa0ca48109a107d88412cec8e4cf46408de2f4d7c1dd8cd400e0533b7b5
7
- data.tar.gz: 96341db4a9e6967a573d9aa4a0147d55357f5db20630785aff5de44491d129136a157f8ece4939b24e10dd0ce992696e489cdb93e364c391587f4b54251e23e9
6
+ metadata.gz: 2c421202ce3c265caa5db1c61d8138252b3bf4b8944de665569f3794b26ea1753e64023fda526577af4b36bb82e4c6724f89d5f823b9e2750a004830b206bafc
7
+ data.tar.gz: 005d7c0b9b652e5020bce39afa4619c2774db3a4101d0981dcf48ee0d2dc9568c053b66268d403ef6ab599f7d16a1c9d4053adea3f2e618767e356344e252cf2
data/lib/event_tracer.rb CHANGED
@@ -1,4 +1,5 @@
1
- require "event_tracer/version"
1
+ require 'event_tracer/version'
2
+ require 'event_tracer/log_result'
2
3
 
3
4
  module EventTracer
4
5
 
@@ -16,30 +17,68 @@ module EventTracer
16
17
 
17
18
  LOG_TYPES.each do |log_type|
18
19
  define_singleton_method log_type do |loggers: nil, **args|
19
- begin
20
- (selected_loggers(loggers) || @loggers).each do |_code, logger|
21
- logger.send(log_type, **args)
22
- end
23
- true
24
- rescue Exception => e
25
- p e.message
26
- false
27
- end
20
+ send_log_messages(
21
+ log_type,
22
+ (selected_loggers(loggers) || @loggers),
23
+ args
24
+ )
28
25
  end
29
26
  end
30
27
 
31
28
  private
32
29
 
30
+ def self.send_log_messages(log_type, loggers, args)
31
+ result = Result.new
32
+
33
+ loggers.each do |code, logger|
34
+ begin
35
+ if args[:action] && args[:message]
36
+ result.record code, logger.send(log_type, **filtered_log_arguments(code, args))
37
+ else
38
+ result.record code, LogResult.new(false, 'Fields action & message need to be populated')
39
+ end
40
+ rescue Exception => e
41
+ result.record code, LogResult.new(false, e.message)
42
+ end
43
+ end
44
+
45
+ result
46
+ end
47
+
33
48
  def self.selected_loggers(logger_codes)
34
49
  return unless logger_codes.is_a?(Array)
35
50
  return if logger_codes.detect { |code| !code.is_a?(Symbol) }
36
51
 
37
- selected_codes = logger_codes.uniq & @loggers.keys
52
+ selected_codes = logger_codes.uniq & registered_logger_codes
38
53
 
39
54
  return if selected_codes.empty?
40
55
  @loggers.select { |code, _logger| selected_codes.include?(code) }
41
56
  end
42
57
 
58
+ def self.filtered_log_arguments(logger_code, args)
59
+ blacklisted_logger_keys = registered_logger_codes - [logger_code]
60
+
61
+ args.reject do |key, _value|
62
+ blacklisted_logger_keys.include?(key)
63
+ end
64
+ end
65
+
66
+ def self.registered_logger_codes
67
+ @loggers.keys
68
+ end
69
+
70
+ class Result
71
+ attr_reader :records
72
+
73
+ def initialize
74
+ @records = {}
75
+ end
76
+
77
+ def record(logger_code, outcome)
78
+ records[logger_code] = outcome
79
+ end
80
+ end
81
+
43
82
  end
44
83
 
45
84
  project_root = File.dirname(File.absolute_path(__FILE__))
@@ -7,6 +7,7 @@ require_relative './basic_decorator'
7
7
  #
8
8
  # Usage: EventTracer.register :appsignal, EventTracer::AppsignalLogger.new(Appsignal)
9
9
  # appsignal_logger.info appsignal: { increment_counter: { counter_1: 1, counter_2: 2 }, set_gauge: { gauge_1: 1 } }
10
+ # appsignal_logger.info appsignal: { set_gauge: { gauge_1: { value: 1, tags: { region: 'eu' } } } }
10
11
  module EventTracer
11
12
  class AppsignalLogger < BasicDecorator
12
13
 
@@ -14,11 +15,18 @@ module EventTracer
14
15
 
15
16
  LOG_TYPES.each do |log_type|
16
17
  define_method log_type do |**args|
17
- SUPPORTED_METRICS.each do |metric|
18
- next unless args[:appsignal] && args[:appsignal][metric].is_a?(Hash)
19
- send_metric(metric, args[:appsignal][metric]) unless args[:appsignal][metric].empty?
18
+ return LogResult.new(false, "Invalid appsignal config") unless args[:appsignal] && args[:appsignal].is_a?(Hash)
19
+
20
+ applied_metrics(args[:appsignal]).each do |metric|
21
+ metric_args = args[:appsignal][metric]
22
+ return LogResult.new(false, "Appsignal metric #{metric} invalid") unless metric_args && metric_args.is_a?(Hash)
23
+
24
+ send_metric metric, metric_args
25
+ rescue InvalidTagError => e
26
+ return LogResult.new(false, e.message)
20
27
  end
21
- true
28
+
29
+ LogResult.new(true)
22
30
  end
23
31
  end
24
32
 
@@ -27,11 +35,29 @@ module EventTracer
27
35
  attr_reader :appsignal, :decoratee
28
36
  alias_method :appsignal, :decoratee
29
37
 
38
+ def applied_metrics(appsignal_args)
39
+ appsignal_args.keys.select { |metric| SUPPORTED_METRICS.include?(metric) }
40
+ end
41
+
30
42
  def send_metric(metric, payload)
31
- payload.each do |increment, value|
32
- appsignal.send(metric, increment, value)
43
+ payload.each do |increment, attribute|
44
+ if attribute.is_a?(Hash)
45
+ begin
46
+ appsignal.send(
47
+ metric,
48
+ increment,
49
+ attribute.fetch(:value),
50
+ attribute.fetch(:tags)
51
+ )
52
+ rescue KeyError
53
+ raise InvalidTagError, "Appsignal payload { #{increment}: #{attribute} } invalid"
54
+ end
55
+ else
56
+ appsignal.send(metric, increment, attribute)
57
+ end
33
58
  end
34
59
  end
35
-
36
60
  end
61
+
62
+ class InvalidTagError < StandardError; end
37
63
  end
@@ -7,8 +7,9 @@ module EventTracer
7
7
  class BaseLogger < BasicDecorator
8
8
 
9
9
  LOG_TYPES.each do |log_type|
10
- define_method log_type do |*args|
11
- send_message(log_type, *args)
10
+ define_method log_type do |**args|
11
+ send_message(log_type, **args)
12
+ LogResult.new(true)
12
13
  end
13
14
  end
14
15
 
@@ -17,11 +18,10 @@ module EventTracer
17
18
  attr_reader :logger, :decoratee
18
19
  alias_method :logger, :decoratee
19
20
 
20
- # execute only if there's a message to be logged
21
- def send_message(log_method, action: nil, simple_message: nil, **args)
22
- return false unless simple_message || args[:message]
23
- message_to_send = simple_message ? simple_message : args.to_json
24
- logger.send(log_method, "[#{action}] #{message_to_send}")
21
+ # EventTracer ensures action & message is always populated
22
+ def send_message(log_method, action:, message:, **args)
23
+ data = args.merge(action: action, message: message)
24
+ logger.public_send(log_method, data)
25
25
  end
26
26
 
27
27
  end
@@ -1,3 +1,5 @@
1
+ require 'delegate'
2
+
1
3
  module EventTracer
2
4
  class BasicDecorator < Delegator
3
5
 
@@ -15,4 +17,4 @@ module EventTracer
15
17
  end
16
18
 
17
19
  end
18
- end
20
+ end
@@ -0,0 +1,75 @@
1
+ require_relative '../event_tracer'
2
+ require_relative './basic_decorator'
3
+ # NOTES
4
+ # Datadog interface to send our usual actions
5
+ # BasicDecorator adds a transparent interface on top of the datadog interface
6
+ #
7
+ # Usage: EventTracer.register :datadog, EventTracer::DataDogLogger.new(DataDog)
8
+ # data_dog_logger.info datadog: { count: { counter_1: 1, counter_2: 2 }, set: { gauge_1: 1 } }
9
+ # data_dog_logger.info datadog: { count: { counter_1: { value: 1, tags: ['tag1, tag2']} } }
10
+
11
+ module EventTracer
12
+ class DatadogLogger < BasicDecorator
13
+
14
+ class InvalidTagError < StandardError; end
15
+
16
+ SUPPORTED_METRICS ||= %i[count set distribution gauge histogram].freeze
17
+
18
+ LOG_TYPES.each do |log_type|
19
+ define_method log_type do |**args|
20
+ return LogResult.new(false, 'Invalid datadog config') unless args[:datadog]&.is_a?(Hash)
21
+
22
+ applied_metrics(args[:datadog]).each do |metric|
23
+ metric_args = args[:datadog][metric]
24
+ return LogResult.new(false, "Datadog metric #{metric} invalid") unless metric_args.is_a?(Hash)
25
+
26
+ send_metric metric, metric_args
27
+ end
28
+
29
+ LogResult.new(true)
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :datadog, :decoratee
36
+ alias_method :datadog, :decoratee
37
+
38
+ def applied_metrics(datadog_args)
39
+ datadog_args.keys.select { |metric| SUPPORTED_METRICS.include?(metric) }
40
+ end
41
+
42
+ def send_metric(metric, payload)
43
+ payload.each do |increment, attribute|
44
+ if attribute.is_a?(Hash)
45
+ begin
46
+ datadog.public_send(
47
+ metric,
48
+ increment,
49
+ attribute.fetch(:value),
50
+ build_options(attribute[:tags])
51
+ )
52
+ rescue KeyError
53
+ raise InvalidTagError, "Datadog payload { #{increment}: #{attribute} } invalid"
54
+ end
55
+ else
56
+ datadog.public_send(metric, increment, attribute)
57
+ end
58
+ end
59
+ end
60
+
61
+ def build_options(tags)
62
+ return {} unless tags
63
+
64
+ formattted_tags =
65
+ if tags.is_a?(Array)
66
+ tags
67
+ else
68
+ tags.inject([]) do |acc, (tag, value)|
69
+ acc << "#{tag}:#{value}"
70
+ end
71
+ end
72
+ { tags: formattted_tags }
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+ module EventTracer
2
+ class LogResult < Struct.new(:success?, :error)
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module EventTracer
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - melvrickgoh
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-07 00:00:00.000000000 Z
11
+ date: 2021-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
19
+ version: 2.1.4
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.17'
26
+ version: 2.1.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -64,12 +64,14 @@ files:
64
64
  - lib/event_tracer/appsignal_logger.rb
65
65
  - lib/event_tracer/base_logger.rb
66
66
  - lib/event_tracer/basic_decorator.rb
67
+ - lib/event_tracer/datadog_logger.rb
68
+ - lib/event_tracer/log_result.rb
67
69
  - lib/event_tracer/version.rb
68
70
  homepage: https://github.com/melvrickgoh/event_tracer
69
71
  licenses:
70
72
  - MIT
71
73
  metadata: {}
72
- post_install_message:
74
+ post_install_message:
73
75
  rdoc_options: []
74
76
  require_paths:
75
77
  - lib/event_tracer
@@ -85,9 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
87
  - !ruby/object:Gem::Version
86
88
  version: '0'
87
89
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.5.1
90
- signing_key:
90
+ rubygems_version: 3.2.3
91
+ signing_key:
91
92
  specification_version: 4
92
93
  summary: Thin wrapper for formatted logging/ metric services to be used as a single
93
94
  service