event_tracer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 14d9cc0ad49951e17c576a3fc4c1fbc9ffba1823
4
+ data.tar.gz: 3bc38170384e3d53f614222fc6e26dfc434b1ca1
5
+ SHA512:
6
+ metadata.gz: 92ad282b66078714af4e6345d3ffdfc3c4c347dabeae22dcf6d957072dcb4c899257297ed0c33502dd1a04a099fe973306e639dacc40094fec9bab9f490612a8
7
+ data.tar.gz: 7e520b9b01770affe29bd639aa3bc7828ead5a5ac9de73d12515029fe55ed7cba27ead64695d1795b4cab482bed9b2e13787e3453414cfac9ba65d992c142da2
@@ -0,0 +1,37 @@
1
+ require_relative '../event_tracer'
2
+ require_relative './basic_decorator'
3
+
4
+ # NOTES
5
+ # Appsignal interface to send our usual actions
6
+ # BasicDecorator adds a transparent interface on top of the appsignal interface
7
+ #
8
+ # Usage: EventTracer.register :appsignal, EventTracer::AppsignalLogger.new(Appsignal)
9
+ # appsignal_logger.info appsignal: { increment_counter: { counter_1: 1, counter_2: 2 }, set_gauge: { gauge_1: 1 } }
10
+ module EventTracer
11
+ class AppsignalLogger < BasicDecorator
12
+
13
+ SUPPORTED_METRICS ||= %i(increment_counter add_distribution_value set_gauge)
14
+
15
+ LOG_TYPES.each do |log_type|
16
+ 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?
20
+ end
21
+ true
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :appsignal, :decoratee
28
+ alias_method :appsignal, :decoratee
29
+
30
+ def send_metric(metric, payload)
31
+ payload.each do |increment, value|
32
+ appsignal.send(metric, increment, value)
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ module EventTracer
2
+ class BasicDecorator < Delegator
3
+
4
+ def initialize(decoratee)
5
+ super
6
+ @delegate_sd_obj = @decoratee = decoratee
7
+ end
8
+
9
+ def __getobj__
10
+ @delegate_sd_obj
11
+ end
12
+
13
+ def __setobj__(obj)
14
+ @delegate_sd_obj = obj
15
+ end
16
+
17
+ end
18
+ 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::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
@@ -0,0 +1,3 @@
1
+ module EventTracer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require "event_tracer/version"
2
+
3
+ module EventTracer
4
+
5
+ LOG_TYPES ||= %i(info warn error)
6
+
7
+ @loggers = {}
8
+
9
+ def self.register(code, logger)
10
+ @loggers[code] = logger
11
+ end
12
+
13
+ def self.find(code)
14
+ @loggers[code]
15
+ end
16
+
17
+ LOG_TYPES.each do |log_type|
18
+ 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
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def self.selected_loggers(logger_codes)
34
+ return unless logger_codes.is_a?(Array)
35
+ return if logger_codes.detect { |code| !code.is_a?(Symbol) }
36
+
37
+ selected_codes = logger_codes.uniq & @loggers.keys
38
+
39
+ return if selected_codes.empty?
40
+ @loggers.select { |code, _logger| selected_codes.include?(code) }
41
+ end
42
+
43
+ end
44
+
45
+ project_root = File.dirname(File.absolute_path(__FILE__))
46
+ Dir.glob("#{project_root}/event_tracer/*") {|file| require file}
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: event_tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - melvrickgoh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: 'Thin wrapper for formatted logging/ metric services to be used as a
56
+ single service. External service(s) supported: Appsignal'
57
+ email:
58
+ - melvrickgoh@hotmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/event_tracer.rb
64
+ - lib/event_tracer/appsignal_logger.rb
65
+ - lib/event_tracer/basic_decorator.rb
66
+ - lib/event_tracer/logger.rb
67
+ - lib/event_tracer/version.rb
68
+ homepage: https://github.com/melvrickgoh/event_tracer
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib/event_tracer
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.5.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Thin wrapper for formatted logging/ metric services to be used as a single
93
+ service
94
+ test_files: []