event_tracer 0.4.6 → 0.6.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: 64dde68b086b57c4b676d62613a5a36be4fa5c754ccb4acd167aa5e69b4247d8
4
- data.tar.gz: '083bca3d0a990b4d0ac49a20935c6d160b4880604fdeb48bb050720f1713820f'
3
+ metadata.gz: e27db9c289f5e94f2f3370fccb3c219222ccb538c21ffc022d67d83a3a42b1c6
4
+ data.tar.gz: efae6fe651ef5833b44a8ec420d72df76f2ca234e639348b3d756c92343640b5
5
5
  SHA512:
6
- metadata.gz: 68f56c5bab8b095372ee8dbbe993eb08d9c1692acf7774e3c003abcebb8ac07093eddabb79012c9a6abbb42662da626d4b69ad11221616a6dcebd012c56fb868
7
- data.tar.gz: ec619f4d64f70a81371063a0ac3ec2fa6666db0030b3af45b364b01de6664757c9824ac010179eeb3af147eb9b2fbd48acc53a3094926891199613ba31d2826a
6
+ metadata.gz: 1071f39a1ee582dad14d580bf3086330d83fefaf3942d48abb8d01402b13888a9f83867cb0795c6ed57a150c793d0acf76c0245cfef7cdfda2e330e7afb8674e
7
+ data.tar.gz: 6abbecd3c7e32610c66b336f1d2e304376a42e73789538f4ea93b18bad96f67f6b4949363fb0831d19ad6bfa6dfa69de2269fca6ce27cfe7652808792b6fcef3
@@ -46,6 +46,8 @@ module EventTracer
46
46
  )
47
47
 
48
48
  worker.perform_async(filtered_payloads) if filtered_payloads.any?
49
+ rescue StandardError => error
50
+ raise EventTracer::ErrorWithPayload.new(error, payloads)
49
51
  end
50
52
 
51
53
  def filter_invalid_data(payloads)
@@ -12,6 +12,7 @@ module EventTracer
12
12
  setting :dynamo_db_table_name, default: 'logs'
13
13
  setting :dynamo_db_client
14
14
  setting :dynamo_db_queue_name, default: 'low'
15
+ setting :error_handler, default: ->(error, _payload) { raise error }
15
16
  else
16
17
  setting :app_name, 'app_name'
17
18
 
@@ -19,6 +20,7 @@ module EventTracer
19
20
  setting :dynamo_db_table_name, 'logs'
20
21
  setting :dynamo_db_client
21
22
  setting :dynamo_db_queue_name, 'low'
23
+ setting :error_handler, ->(error, _payload) { raise error }
22
24
  end
23
25
  end
24
26
  end
@@ -18,15 +18,16 @@ module EventTracer
18
18
  gauge: :gauge,
19
19
  set: :set,
20
20
  histogram: :histogram
21
- }
21
+ }.freeze
22
22
  DEFAULT_METRIC_TYPE = :count
23
23
  DEFAULT_COUNTER = 1
24
24
 
25
25
  attr_reader :allowed_tags
26
26
 
27
- def initialize(decoratee, allowed_tags: [])
27
+ def initialize(decoratee, allowed_tags: [], default_tags: {})
28
28
  super(decoratee)
29
29
  @allowed_tags = allowed_tags.freeze
30
+ @default_tags = default_tags.freeze
30
31
  end
31
32
 
32
33
  LOG_TYPES.each do |log_type|
@@ -59,12 +60,14 @@ module EventTracer
59
60
 
60
61
  alias_method :datadog, :decoratee
61
62
 
63
+ attr_reader :default_tags
64
+
62
65
  def valid_args?(metrics)
63
66
  metrics && (metrics.is_a?(Hash) || metrics.is_a?(Array))
64
67
  end
65
68
 
66
69
  def build_tags(args)
67
- args.slice(*allowed_tags).map do |tag, value|
70
+ default_tags.merge(args.slice(*allowed_tags)).map do |tag, value|
68
71
  "#{tag}:#{value}"
69
72
  end
70
73
  end
@@ -0,0 +1,10 @@
1
+ module EventTracer
2
+ class ErrorWithPayload < StandardError
3
+ attr_reader :payload
4
+
5
+ def initialize(error, payload)
6
+ super(error)
7
+ @payload = payload
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,97 @@
1
+ module EventTracer
2
+ class PrometheusLogger < BasicDecorator
3
+
4
+ SUPPORTED_METRIC_TYPES = {
5
+ counter: :increment_count,
6
+ gauge: :set_gauge
7
+ }.freeze
8
+ DEFAULT_INCREMENT = 1
9
+
10
+ attr_reader :allowed_tags
11
+
12
+ def initialize(prometheus, allowed_tags: [], default_tags: {}, raise_if_missing: true)
13
+ super(prometheus)
14
+ @allowed_tags = allowed_tags.freeze
15
+ @default_tags = default_tags.freeze
16
+ @raise_if_missing = raise_if_missing
17
+ end
18
+
19
+ LOG_TYPES.each do |log_type|
20
+ define_method log_type do |**args|
21
+ metrics = args[:metrics]
22
+
23
+ return fail_result('Invalid metrics for Prometheus') unless valid_args?(metrics)
24
+ return success_result if metrics.empty?
25
+
26
+ labels = build_metric_labels(args)
27
+
28
+ case metrics
29
+ when Array
30
+ metrics.each do |metric_name|
31
+ increment_count(metric_name, DEFAULT_INCREMENT, labels: labels)
32
+ end
33
+ when Hash
34
+ metrics.each do |metric_name, metric_payload|
35
+ payload = metric_payload.transform_keys(&:to_sym)
36
+ metric_type = SUPPORTED_METRIC_TYPES[payload.fetch(:type).to_sym]
37
+
38
+ if metric_type
39
+ send(
40
+ metric_type,
41
+ metric_name,
42
+ payload.fetch(:value),
43
+ labels: labels
44
+ )
45
+ end
46
+ end
47
+ end
48
+
49
+ success_result
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ alias_method :prometheus, :decoratee
56
+
57
+ attr_reader :default_tags, :raise_if_missing
58
+
59
+ def valid_args?(metrics)
60
+ metrics && (metrics.is_a?(Hash) || metrics.is_a?(Array))
61
+ end
62
+
63
+ def increment_count(metric_name, value, labels:)
64
+ metric = get_metric(metric_name.to_sym, :counter)
65
+ metric.increment(by: value, labels: labels)
66
+ end
67
+
68
+ def set_gauge(metric_name, value, labels:)
69
+ metric = get_metric(metric_name.to_sym, :gauge)
70
+ metric.set(value, labels: labels)
71
+ end
72
+
73
+ def get_metric(metric_name, metric_type)
74
+ metric = prometheus.get(metric_name)
75
+
76
+ return metric if metric
77
+ raise "Metric #{metric_name} not registered" if raise_if_missing
78
+
79
+ prometheus.public_send(
80
+ metric_type,
81
+ metric_name,
82
+ docstring: "A #{metric_type} for #{metric_name}",
83
+ labels: labels_for_registration
84
+ )
85
+ end
86
+
87
+ def build_metric_labels(args)
88
+ allowed_tags.inject(default_tags) do |metric_labels, tag|
89
+ metric_labels.merge(tag => args[tag])
90
+ end
91
+ end
92
+
93
+ def labels_for_registration
94
+ (allowed_tags + default_tags.keys).uniq
95
+ end
96
+ end
97
+ end
@@ -1,3 +1,3 @@
1
1
  module EventTracer
2
- VERSION = '0.4.6'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
data/lib/event_tracer.rb CHANGED
@@ -32,7 +32,15 @@ module EventTracer
32
32
 
33
33
  loggers.each do |code, logger|
34
34
  if args[:action] && args[:message]
35
- result.record code, logger.send(log_type, **args)
35
+ begin
36
+ result.record code, logger.send(log_type, **args)
37
+ rescue EventTracer::ErrorWithPayload => error
38
+ EventTracer::Config.config.error_handler.call(error.cause || error, error.payload)
39
+ result.record code, LogResult.new(false, error.message)
40
+ rescue StandardError => error
41
+ EventTracer::Config.config.error_handler.call(error, args)
42
+ result.record code, LogResult.new(false, error.message)
43
+ end
36
44
  else
37
45
  result.record code, LogResult.new(false, 'Fields action & message need to be populated')
38
46
  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.4.6
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - melvrickgoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-23 00:00:00.000000000 Z
11
+ date: 2022-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -100,7 +100,9 @@ files:
100
100
  - lib/event_tracer/dynamo_db/default_processor.rb
101
101
  - lib/event_tracer/dynamo_db/logger.rb
102
102
  - lib/event_tracer/dynamo_db/worker.rb
103
+ - lib/event_tracer/error_with_payload.rb
103
104
  - lib/event_tracer/log_result.rb
105
+ - lib/event_tracer/prometheus_logger.rb
104
106
  - lib/event_tracer/version.rb
105
107
  homepage: https://github.com/melvrickgoh/event_tracer
106
108
  licenses: