event_tracer 0.0.1 → 0.2.1

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: 14d9cc0ad49951e17c576a3fc4c1fbc9ffba1823
4
- data.tar.gz: 3bc38170384e3d53f614222fc6e26dfc434b1ca1
2
+ SHA256:
3
+ metadata.gz: 77058f3fa969027f3d51399425d8885c60037ae6250db1cfa686fbafb8524467
4
+ data.tar.gz: 40b98f105722939c88bb364a04bfbd708c9fcfc2c76815d41d5b2318994aa9bb
5
5
  SHA512:
6
- metadata.gz: 92ad282b66078714af4e6345d3ffdfc3c4c347dabeae22dcf6d957072dcb4c899257297ed0c33502dd1a04a099fe973306e639dacc40094fec9bab9f490612a8
7
- data.tar.gz: 7e520b9b01770affe29bd639aa3bc7828ead5a5ac9de73d12515029fe55ed7cba27ead64695d1795b4cab482bed9b2e13787e3453414cfac9ba65d992c142da2
6
+ metadata.gz: 31fb42ad32f68d2efd4b78dbfb9d55f16563bc897eaa4c4602c5855eb092bf0f03c4308c22a91ed011663c095548029155a7a795de638405588bb162a93d723b
7
+ data.tar.gz: 58c9ec65b708b9b44fb141c9da778fbc6d56fea371a8edc9e0aaf9ef4d4a2b89855e0a88afbafd0a61312c2b8023bd7c13325bc0f7c86be13ee7d87e534dc6ad
@@ -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
@@ -0,0 +1,28 @@
1
+ require_relative '../event_tracer'
2
+ require_relative './basic_decorator'
3
+ require 'json'
4
+
5
+ # Usage: EventTracer.register :base, EventTracer::BaseLogger.new(Logger.new(STDOUT))
6
+ module EventTracer
7
+ class BaseLogger < BasicDecorator
8
+
9
+ LOG_TYPES.each do |log_type|
10
+ define_method log_type do |*args|
11
+ send_message(log_type, *args)
12
+ LogResult.new(true)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :logger, :decoratee
19
+ alias_method :logger, :decoratee
20
+
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
+ end
26
+
27
+ end
28
+ 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.0.1"
2
+ VERSION = '0.2.1'.freeze
3
3
  end
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.0.1
4
+ version: 0.2.1
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-06 00:00:00.000000000 Z
11
+ date: 2020-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
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
@@ -62,14 +62,16 @@ extra_rdoc_files: []
62
62
  files:
63
63
  - lib/event_tracer.rb
64
64
  - lib/event_tracer/appsignal_logger.rb
65
+ - lib/event_tracer/base_logger.rb
65
66
  - lib/event_tracer/basic_decorator.rb
66
- - lib/event_tracer/logger.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.0.8
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
@@ -1,28 +0,0 @@
1
- require_relative '../event_tracer'
2
- require_relative './basic_decorator'
3
- require 'json'
4
-
5
- # Usage: EventTracer.register :base, EventTracer::Logger.new(Logger.new(STDOUT))
6
- module EventTracer
7
- class Logger < BasicDecorator
8
-
9
- LOG_TYPES.each do |log_type|
10
- define_method log_type do |*args|
11
- send_message(log_type, *args)
12
- end
13
- end
14
-
15
- private
16
-
17
- attr_reader :logger, :decoratee
18
- alias_method :logger, :decoratee
19
-
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}")
25
- end
26
-
27
- end
28
- end