event_tracer 0.5.1 → 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: 3c2e5bbf4d4546e3ece6589508e00fc01eb30ca41b572ff2afbb4185b73c93d5
4
- data.tar.gz: e3fe3b13cf8fc1e43732a75386502618427cab7a449fe95b8d2c69789383947b
3
+ metadata.gz: e27db9c289f5e94f2f3370fccb3c219222ccb538c21ffc022d67d83a3a42b1c6
4
+ data.tar.gz: efae6fe651ef5833b44a8ec420d72df76f2ca234e639348b3d756c92343640b5
5
5
  SHA512:
6
- metadata.gz: 17cd02fe83f7725b8e2a9d495979f5b183e2a63fc071b1b58795adfb910ed0c9048f1692b608d78d0348377b4c9216c1601481234ff60791206b2c1797e7d51f
7
- data.tar.gz: ba23fdd140689fa467671a1f28024f38ea6b5c9e1ddd911c4980cbb91d57faf7a5ad2ce028aaffcead4f75d06ac4d5e30b0728525a3dac074ac366b8318f2797
6
+ metadata.gz: 1071f39a1ee582dad14d580bf3086330d83fefaf3942d48abb8d01402b13888a9f83867cb0795c6ed57a150c793d0acf76c0245cfef7cdfda2e330e7afb8674e
7
+ data.tar.gz: 6abbecd3c7e32610c66b336f1d2e304376a42e73789538f4ea93b18bad96f67f6b4949363fb0831d19ad6bfa6dfa69de2269fca6ce27cfe7652808792b6fcef3
@@ -67,7 +67,7 @@ module EventTracer
67
67
  end
68
68
 
69
69
  def build_tags(args)
70
- @default_tags.merge(args.slice(*allowed_tags)).map do |tag, value|
70
+ default_tags.merge(args.slice(*allowed_tags)).map do |tag, value|
71
71
  "#{tag}:#{value}"
72
72
  end
73
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.1'.freeze
2
+ VERSION = '0.6.0'.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.1
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-05-18 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
@@ -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: