event_tracer 0.5.0 → 0.6.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
2
  SHA256:
3
- metadata.gz: f84ff22349c762a429e58f5913884a3eff066c983881ed16d9f846785c27184f
4
- data.tar.gz: ccb8508e0eba3e08f2312248618d2fff80ff5bab70c08c5c9754b04ee000088e
3
+ metadata.gz: bc787e6f2e531d8525792eec4d55f3576f7b6543e356319fce7c065c436385d6
4
+ data.tar.gz: 22172cf85886ee0b668820d55bd13346997cd648b542337cc982c91510f01f50
5
5
  SHA512:
6
- metadata.gz: 588eb7e39ca26221be2c15a986d5862a6f102713a8be329e245ae086c0503daa0c2e1d54d5996a008c1a6a93855f5f16b33eea551902438926fb1e29dcd29720
7
- data.tar.gz: 43d3d079f656213b87f8666bca064dd83910282a4f7ffe3bb9dc1106f25ceea8c6b2e6dafbb8181c20c1377e515a6a37311cb495d4b9cab7c48016f2a79124ea
6
+ metadata.gz: d2afa747344e9e07aa85fa1d22a6612fca83df74e2bcfeb384b4f91e742e2d4a545c1070ceb9006592dff7bc97fefefe40ed4f326002f7f76119be24e9e26287
7
+ data.tar.gz: c176243abadde0675d267138476b2dbe9cbe20f78a3d49a479c96e9a086abb83ff4df859da65029aadb115d7d42a83fddf630de32ed797e7f7c8d930dec05fcf
@@ -58,8 +58,15 @@ module EventTracer
58
58
  attr_reader :buffer_size, :flush_interval, :buffer
59
59
 
60
60
  def add_item?
61
- buffer.size < buffer_size &&
62
- (buffer.empty? || buffer.first[:created_at] > Time.now - flush_interval)
61
+ return false if buffer.size >= buffer_size
62
+
63
+ # NOTE: we cannot use buffer.empty? then buffer.first here
64
+ # due to race-condition when another thread flushes the buffer
65
+ # right after buffer.empty? but before buffer.first is called.
66
+ # If we have more complicated use case, we may need to start introducing
67
+ # mutext.
68
+ first_item = buffer.first
69
+ first_item.nil? || first_item[:created_at] > Time.now - flush_interval
63
70
  end
64
71
  end
65
72
  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,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.5.0'.freeze
2
+ VERSION = '0.6.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.5.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - melvrickgoh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-26 00:00:00.000000000 Z
11
+ date: 2022-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -102,6 +102,7 @@ files:
102
102
  - lib/event_tracer/dynamo_db/worker.rb
103
103
  - lib/event_tracer/error_with_payload.rb
104
104
  - lib/event_tracer/log_result.rb
105
+ - lib/event_tracer/prometheus_logger.rb
105
106
  - lib/event_tracer/version.rb
106
107
  homepage: https://github.com/melvrickgoh/event_tracer
107
108
  licenses: