upfluence-utils 0.14.3 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 920e21b518b65d1fbf2666cf2cb402080cdc680ad5f6d6a32fede37731b6a8b9
4
- data.tar.gz: 6b6be2530c8e316c33571f4f4d8c6ed4fd90766be61b213d88fbc474ff7bfe9f
3
+ metadata.gz: 2993f9e10ebafc6c2d8a150d2d17534daf9378b3439395bd41c1467b5d30f7e8
4
+ data.tar.gz: d350718d2b7c39f0bca846c1a57d220c9fddf390a880f4a63521c559ac6b876d
5
5
  SHA512:
6
- metadata.gz: b5fdd076d5ff2b44442da2b2a67a8b9f25fa7bb87ff4962f0345ff3b745115c8a9e2277c35ae5c457dfd52100aa74a6c5b54876b9c9682f486999acccdea5490
7
- data.tar.gz: 8056d4d48c051c0ecfe50e3f1bbd983a73669bbb755bbb216b4d9c7e0d36811c56b43bec169495ddfb8fcaa1bf9bc2671a97fded343ceb8f30b1111a0191e424
6
+ metadata.gz: a2417b561cc327beac3cfc12f223b25695c2592728348db4a0d80ca26f5b067e0e7fa80a1c43091b042ef37970d55e8415846ebbf506335a2348cf6c92cfbff9
7
+ data.tar.gz: 7ed82de23b4da72d9e929d7cd23a12ebc92e6341d8462434363fdc65c033c5b3719da6c84eef09b7a083e869d1989d6e1f7155320b035c905140796271cf7c71
data/README.md CHANGED
@@ -49,6 +49,33 @@ Provide some utils for http requests, such as:
49
49
  - Prometheus logger
50
50
  - Base Exceptions
51
51
 
52
+ ### Stats
53
+
54
+ A Ruby port of [upfluence/stats](https://github.com/upfluence/stats): metrics
55
+ are created from scopes, which carry a namespace and a set of tags inherited by
56
+ their children.
57
+
58
+ ```ruby
59
+ scope = Upfluence::Stats.root_scope # backed by Prometheus::Client.registry
60
+
61
+ db = scope.scope('database', service: 'user-api')
62
+ redis = db.scope('redis', cluster: 'cache-01')
63
+
64
+ redis.counter('queries_total').inc # database_redis_queries_total{service,cluster}
65
+ redis.counter_vector('errors_total', %w[kind]).with_labels(kind: 'timeout').add(2)
66
+ scope.gauge('active_connections').update(42)
67
+ scope.histogram('latency_seconds', buckets: [0.01, 0.1, 1]).record(0.12)
68
+
69
+ Upfluence::Stats::Timer.new(scope, 'op').time { work } # op_seconds
70
+ Upfluence::Stats::Instrument.new(scope, 'job').exec { work }
71
+ # job_started_total, job_total{status}, job_duration_seconds
72
+ ```
73
+
74
+ Use `Upfluence::Stats::RootScope.new(collector)` to build a root scope on a
75
+ custom collector (`Collector::Prometheus.new(registry)` or
76
+ `Collector::Static.new` for specs), and `Upfluence::Stats::NOOP_SCOPE` to
77
+ disable metrics.
78
+
52
79
  ### Mixins
53
80
 
54
81
  Based on the concept of [mixins](https://en.wikipedia.org/wiki/Mixin#:~:text=at%20the%20time.-,Definition,mixed%20in%20with%20other%20classes.), these classes provide some good tools, like:
@@ -2,6 +2,7 @@ require 'sinatra/base'
2
2
  require 'active_record'
3
3
  require 'active_support/hash_with_indifferent_access'
4
4
  require 'rack/rewindable_input'
5
+ require 'upfluence/environment'
5
6
  require 'upfluence/http/endpoint/validation_error'
6
7
  require 'upfluence/mixin/strong_parameters'
7
8
 
@@ -12,6 +13,8 @@ module Upfluence
12
13
  class APIEndpoint < Sinatra::Base
13
14
  VALIDATION_ERROR_KLASS = ValidationError
14
15
 
16
+ set :environment, Upfluence.env.to_sym
17
+
15
18
  disable :show_exceptions
16
19
  disable :logging
17
20
  disable :dump_errors
@@ -1,30 +1,26 @@
1
- require 'prometheus/client'
2
1
  require 'upfluence/environment'
3
2
  require 'upfluence/http/route_resolver'
3
+ require 'upfluence/stats'
4
4
 
5
5
  module Upfluence
6
6
  module HTTP
7
7
  module Middleware
8
8
  class Prometheus
9
- LABELS = %i[path method env].freeze
9
+ LABELS = %i[path method].freeze
10
10
 
11
- def initialize(app, registry = ::Prometheus::Client.registry)
12
- @registry = registry
11
+ def initialize(app, scope = Upfluence::Stats.root_scope)
12
+ scope = scope.scope('uhttp_handler', env: Upfluence.env.to_s)
13
13
 
14
- @request_total_count = @registry.get(
15
- :uhttp_handler_requests_total
16
- ) || @registry.counter(
17
- :uhttp_handler_requests_total,
18
- docstring: 'Histogram of processed items',
19
- labels: LABELS + %i[status]
14
+ @request_total_count = scope.counter_vector(
15
+ 'requests_total',
16
+ LABELS + %i[status],
17
+ docstring: 'Histogram of processed items'
20
18
  )
21
19
 
22
- @request_histogram = @registry.get(
23
- :uhttp_handler_requests_duration_second
24
- ) || @registry.histogram(
25
- :uhttp_handler_requests_duration_second,
26
- docstring: 'Histogram of processing time',
27
- labels: LABELS
20
+ @request_histogram = scope.histogram_vector(
21
+ 'requests_duration_second',
22
+ LABELS,
23
+ docstring: 'Histogram of processing time'
28
24
  )
29
25
 
30
26
  @app = app
@@ -46,24 +42,10 @@ module Upfluence
46
42
 
47
43
  def record(env, code, duration)
48
44
  path = RouteResolver.resolve(env, code)
45
+ method = env['REQUEST_METHOD'].downcase
49
46
 
50
- @request_total_count.increment(
51
- labels: {
52
- path: path,
53
- method: env['REQUEST_METHOD'].downcase,
54
- status: code,
55
- env: Upfluence.env.to_s
56
- }
57
- )
58
-
59
- @request_histogram.observe(
60
- duration,
61
- labels: {
62
- path: path,
63
- method: env['REQUEST_METHOD'].downcase,
64
- env: Upfluence.env.to_s
65
- }
66
- )
47
+ @request_total_count.with_labels(path: path, method: method, status: code).inc
48
+ @request_histogram.with_labels(path: path, method: method).record(duration)
67
49
  end
68
50
  end
69
51
  end
@@ -1,18 +1,17 @@
1
+ require 'upfluence/stats'
2
+
1
3
  module Upfluence
2
4
  module Instrumentation
3
5
  class PeriodicInstrumenter
4
- def initialize(interval: 30, registry: ::Prometheus::Client.registry)
6
+ def initialize(interval: 30, scope: Upfluence::Stats.root_scope)
7
+ scope = scope.scope(prefix.to_s)
8
+
5
9
  @interval = interval
6
- @gauges = metrics.reduce({}) do |acc, (metric, values)|
7
- metric_name = [prefix, metric].compact.join("_").to_sym
8
-
9
- acc.merge(
10
- metric => registry.get(metric_name) || registry.gauge(
11
- metric_name,
12
- docstring: values[:docstring] || '',
13
- labels: values[:labels] || []
14
- )
15
- )
10
+ @gauges = metrics.to_h do |metric, values|
11
+ [
12
+ metric,
13
+ scope.gauge_vector(metric, values[:labels] || [], docstring: values[:docstring])
14
+ ]
16
15
  end
17
16
 
18
17
  @stop_thread = false
@@ -22,11 +21,7 @@ module Upfluence
22
21
  @thread ||= Thread.new do
23
22
  until @stop_thread
24
23
  begin
25
- values.each do |(metric, vs)|
26
- vs.each do |v|
27
- @gauges[metric].set(v[:value], labels: v[:labels] || {})
28
- end
29
- end
24
+ collect
30
25
  rescue => e
31
26
  Upfluence.error_logger.notify(e)
32
27
  ensure
@@ -36,6 +31,14 @@ module Upfluence
36
31
  end
37
32
  end
38
33
 
34
+ def collect
35
+ values.each do |(metric, vs)|
36
+ vs.each do |v|
37
+ @gauges[metric].with_labels(**(v[:labels] || {})).update(v[:value])
38
+ end
39
+ end
40
+ end
41
+
39
42
  def stop
40
43
  return unless @thread&.alive?
41
44
 
@@ -1,3 +1,4 @@
1
+ require 'upfluence/stats'
1
2
  require 'upfluence/timeout'
2
3
 
3
4
  module Upfluence
@@ -5,65 +6,66 @@ module Upfluence
5
6
  class UnknownResource < RuntimeError; end
6
7
  class NoInstanciationBlock < RuntimeError; end
7
8
 
8
- class PrometheusMetrics
9
- def self.standardized_metrics(n)
10
- require 'prometheus/client'
11
- new(Prometheus::Client.registry, 'upfluence_iopool_pool', { pool: n })
9
+ class Metrics
10
+ def self.standardized_metrics(n, scope: Upfluence::Stats.root_scope)
11
+ new(scope.scope('upfluence_iopool_pool', pool: n))
12
12
  end
13
13
 
14
- def initialize(registry, prefix, labels)
15
- label_keys = labels.keys
16
-
17
- @labels = labels
18
- @get = register(registry, :counter, :"#{prefix}_get_total", 'Total pool get calls', label_keys)
19
- @put = register(registry, :counter, :"#{prefix}_put_total", 'Total pool put calls', label_keys)
20
- @discard = register(registry, :counter, :"#{prefix}_discard_total", 'Total pool discard calls',
21
- label_keys)
22
- @get_wait = register(registry, :counter, :"#{prefix}_get_wait_total", 'Pool gets that had to wait',
23
- label_keys)
24
- @get_wait_duration = register(registry, :counter, :"#{prefix}_get_wait_millisecond_total",
25
- 'Total milliseconds waiting for pool get', label_keys)
26
- @idle = register(registry, :gauge, :"#{prefix}_idle", 'Idle resources in pool', label_keys)
27
- @checkout = register(registry, :gauge, :"#{prefix}_checkout", 'Checked out resources in pool',
28
- label_keys)
29
- @size = register(registry, :gauge, :"#{prefix}_size", 'Total pool size', label_keys)
14
+ def initialize(scope)
15
+ @get = scope.counter('get_total', docstring: 'Total pool get calls')
16
+ @put = scope.counter('put_total', docstring: 'Total pool put calls')
17
+ @discard = scope.counter('discard_total', docstring: 'Total pool discard calls')
18
+ @get_wait = scope.counter('get_wait_total', docstring: 'Pool gets that had to wait')
19
+ @get_wait_duration = scope.counter(
20
+ 'get_wait_millisecond_total', docstring: 'Total milliseconds waiting for pool get'
21
+ )
22
+ @idle = scope.gauge('idle', docstring: 'Idle resources in pool')
23
+ @checkout = scope.gauge('checkout', docstring: 'Checked out resources in pool')
24
+ @size = scope.gauge('size', docstring: 'Total pool size')
30
25
  end
31
26
 
32
27
  def observe_get
33
- @get.increment(labels: @labels)
28
+ @get.inc
34
29
  end
35
30
 
36
31
  def observe_put
37
- @put.increment(labels: @labels)
32
+ @put.inc
38
33
  end
39
34
 
40
35
  def observe_discard
41
- @discard.increment(labels: @labels)
36
+ @discard.inc
42
37
  end
43
38
 
44
39
  def observe_wait(seconds)
45
- @get_wait.increment(labels: @labels)
46
- @get_wait_duration.increment(by: (seconds * 1000).to_i, labels: @labels)
40
+ @get_wait.inc
41
+ @get_wait_duration.add((seconds * 1000).to_i)
47
42
  end
48
43
 
49
44
  def update_idle(count)
50
- @idle.set(count, labels: @labels)
45
+ @idle.update(count)
51
46
  end
52
47
 
53
48
  def update_checkout(count)
54
- @checkout.set(count, labels: @labels)
49
+ @checkout.update(count)
55
50
  end
56
51
 
57
52
  def set_size(count)
58
- @size.set(count, labels: @labels)
53
+ @size.update(count)
59
54
  end
55
+ end
60
56
 
61
- private
57
+ # Deprecated: use Metrics with a Upfluence::Stats scope instead.
58
+ class PrometheusMetrics < Metrics
59
+ def self.standardized_metrics(n)
60
+ Metrics.standardized_metrics(n)
61
+ end
62
62
 
63
- def register(registry, type, name, docstring, label_keys)
64
- registry.public_send(type, name, docstring: docstring, labels: label_keys)
65
- rescue Prometheus::Client::Registry::AlreadyRegisteredError
66
- registry.get(name)
63
+ def initialize(registry, prefix, labels)
64
+ super(
65
+ Upfluence::Stats::RootScope.new(
66
+ Upfluence::Stats::Collector::Prometheus.new(registry)
67
+ ).scope(prefix, **labels)
68
+ )
67
69
  end
68
70
  end
69
71
 
@@ -0,0 +1,104 @@
1
+ require 'prometheus/client'
2
+
3
+ module Upfluence
4
+ module Stats
5
+ module Collector
6
+ class Prometheus
7
+ class Counter
8
+ def initialize(metric)
9
+ @metric = metric
10
+ end
11
+
12
+ def inc
13
+ add(1)
14
+ end
15
+
16
+ def add(value)
17
+ @metric.increment(by: value)
18
+ end
19
+
20
+ def get
21
+ @metric.get
22
+ end
23
+ end
24
+
25
+ class Gauge
26
+ def initialize(metric)
27
+ @metric = metric
28
+ end
29
+
30
+ def update(value)
31
+ @metric.set(value)
32
+ end
33
+
34
+ def get
35
+ @metric.get
36
+ end
37
+ end
38
+
39
+ class Histogram
40
+ def initialize(metric)
41
+ @metric = metric
42
+ end
43
+
44
+ def record(value)
45
+ @metric.observe(value)
46
+ end
47
+ end
48
+
49
+ class Vector
50
+ def initialize(metric, wrapper)
51
+ @metric = metric
52
+ @wrapper = wrapper
53
+ @mutex = Mutex.new
54
+ @metrics = {}
55
+ end
56
+
57
+ def with_labels(labels)
58
+ @mutex.synchronize do
59
+ @metrics[labels] ||= @wrapper.new(
60
+ labels.empty? ? @metric : @metric.with_labels(labels)
61
+ )
62
+ end
63
+ end
64
+ end
65
+
66
+ def initialize(registry = ::Prometheus::Client.registry)
67
+ @registry = registry
68
+ end
69
+
70
+ def register_counter(name, labels, docstring: nil)
71
+ Vector.new(fetch(:counter, name, labels, docstring: docstring), Counter)
72
+ end
73
+
74
+ def register_gauge(name, labels, docstring: nil)
75
+ Vector.new(fetch(:gauge, name, labels, docstring: docstring), Gauge)
76
+ end
77
+
78
+ def register_histogram(name, labels, buckets:, docstring: nil)
79
+ Vector.new(
80
+ fetch(:histogram, name, labels, docstring: docstring, buckets: buckets),
81
+ Histogram
82
+ )
83
+ end
84
+
85
+ private
86
+
87
+ def fetch(type, name, labels, docstring:, **opts)
88
+ name = name.to_sym
89
+ metric = @registry.get(name) || @registry.public_send(
90
+ type, name, docstring: docstring || name.to_s, labels: labels, **opts
91
+ )
92
+
93
+ if metric.type != type || metric.labels.sort != labels.sort
94
+ raise ArgumentError, "#{name} already registered as a #{metric.type} with labels #{metric.labels}"
95
+ end
96
+
97
+ metric
98
+ rescue ::Prometheus::Client::Registry::AlreadyRegisteredError
99
+ retry
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,152 @@
1
+ module Upfluence
2
+ module Stats
3
+ module Collector
4
+ class Static
5
+ Snapshot = Struct.new(:counters, :gauges, :histograms, keyword_init: true)
6
+ ValueSnapshot = Struct.new(:name, :labels, :value, keyword_init: true)
7
+ HistogramSnapshot = Struct.new(:name, :labels, :count, :sum, :buckets, keyword_init: true)
8
+ Bucket = Struct.new(:upper_bound, :count, keyword_init: true)
9
+
10
+ class Value
11
+ def initialize
12
+ @mutex = Mutex.new
13
+ @value = 0
14
+ end
15
+
16
+ def inc
17
+ add(1)
18
+ end
19
+
20
+ def add(value)
21
+ @mutex.synchronize { @value += value }
22
+ end
23
+
24
+ def update(value)
25
+ @mutex.synchronize { @value = value }
26
+ end
27
+
28
+ def get
29
+ @mutex.synchronize { @value }
30
+ end
31
+ end
32
+
33
+ class Histogram
34
+ def initialize(buckets)
35
+ @upper_bounds = buckets + [Float::INFINITY]
36
+ @mutex = Mutex.new
37
+ @counts = Array.new(@upper_bounds.length, 0)
38
+ @sum = 0
39
+ end
40
+
41
+ def record(value)
42
+ index = @upper_bounds.index { |upper_bound| value <= upper_bound }
43
+
44
+ @mutex.synchronize do
45
+ @counts[index] += 1
46
+ @sum += value
47
+ end
48
+ end
49
+
50
+ def count
51
+ @mutex.synchronize { @counts.sum }
52
+ end
53
+
54
+ def sum
55
+ @mutex.synchronize { @sum }
56
+ end
57
+
58
+ def buckets
59
+ @mutex.synchronize do
60
+ @upper_bounds.zip(@counts).map do |upper_bound, count|
61
+ Bucket.new(upper_bound: upper_bound, count: count)
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ class Vector
68
+ def initialize(&build)
69
+ @build = build
70
+ @mutex = Mutex.new
71
+ @metrics = {}
72
+ end
73
+
74
+ def with_labels(labels)
75
+ @mutex.synchronize { @metrics[labels] ||= @build.call }
76
+ end
77
+
78
+ def entries
79
+ @mutex.synchronize { @metrics.to_a }
80
+ end
81
+ end
82
+
83
+ def initialize
84
+ @mutex = Mutex.new
85
+ @vectors = { counter: {}, gauge: {}, histogram: {} }
86
+ end
87
+
88
+ def register_counter(name, _labels, docstring: nil)
89
+ register(:counter, name) { Value.new }
90
+ end
91
+
92
+ def register_gauge(name, _labels, docstring: nil)
93
+ register(:gauge, name) { Value.new }
94
+ end
95
+
96
+ def register_histogram(name, _labels, buckets:, docstring: nil)
97
+ register(:histogram, name) { Histogram.new(buckets) }
98
+ end
99
+
100
+ def snapshot
101
+ Snapshot.new(
102
+ counters: value_snapshots(:counter),
103
+ gauges: value_snapshots(:gauge),
104
+ histograms: histogram_snapshots
105
+ )
106
+ end
107
+
108
+ private
109
+
110
+ def register(type, name, &build)
111
+ @mutex.synchronize { @vectors[type][name] = Vector.new(&build) }
112
+ end
113
+
114
+ def entries(type)
115
+ vectors = @mutex.synchronize { @vectors[type].to_a }
116
+
117
+ vectors.flat_map do |name, vector|
118
+ vector.entries.map { |labels, metric| [name, labels, metric] }
119
+ end
120
+ end
121
+
122
+ def value_snapshots(type)
123
+ sort(
124
+ entries(type).map do |name, labels, metric|
125
+ ValueSnapshot.new(name: name, labels: labels, value: metric.get)
126
+ end
127
+ )
128
+ end
129
+
130
+ def histogram_snapshots
131
+ sort(
132
+ entries(:histogram).map do |name, labels, metric|
133
+ HistogramSnapshot.new(
134
+ name: name,
135
+ labels: labels,
136
+ count: metric.count,
137
+ sum: metric.sum,
138
+ buckets: metric.buckets
139
+ )
140
+ end
141
+ )
142
+ end
143
+
144
+ def sort(snapshots)
145
+ snapshots.sort_by do |snapshot|
146
+ [snapshot.name, snapshot.labels.map { |k, v| [k.to_s, v] }.sort]
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,59 @@
1
+ require 'upfluence/stats/noop_scope'
2
+ require 'upfluence/stats/timer'
3
+ require 'upfluence/stats/vector'
4
+
5
+ module Upfluence
6
+ module Stats
7
+ class Instrument
8
+ DEFAULT_FORMATTER = ->(error) { error.nil? ? 'success' : 'failed' }
9
+
10
+ def initialize(scope, name, track_started: true, track_duration: true,
11
+ counter_label: 'status', formatter: DEFAULT_FORMATTER,
12
+ timer_opts: {})
13
+ @counter_label = counter_label.to_sym
14
+ @formatter = formatter
15
+ @started = track_started ? scope.counter("#{name}_started_total") : NOOP_METRIC
16
+ @timer = Timer.new(scope, "#{name}_duration", **timer_opts) if track_duration
17
+ @finished = scope.counter_vector("#{name}_total", [@counter_label])
18
+ end
19
+
20
+ def start
21
+ @started.inc
22
+
23
+ Operation.new(@finished, @counter_label, @formatter, @timer&.start)
24
+ end
25
+
26
+ def exec
27
+ operation = start
28
+ error = nil
29
+
30
+ yield
31
+ rescue Exception => e
32
+ error = e
33
+ raise
34
+ ensure
35
+ operation&.finish(error)
36
+ end
37
+ end
38
+
39
+ class Operation
40
+ def initialize(finished, counter_label, formatter, stop_watch)
41
+ @finished = finished
42
+ @counter_label = counter_label
43
+ @formatter = formatter
44
+ @stop_watch = stop_watch
45
+ end
46
+
47
+ def finish(error = nil)
48
+ @stop_watch&.stop
49
+ @finished.with_labels(@counter_label => @formatter.call(error)).inc
50
+ end
51
+ end
52
+
53
+ class InstrumentVector < EntityVector
54
+ def initialize(scope, name, labels, **opts)
55
+ super(scope, labels) { |s| Instrument.new(s, name, **opts) }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,68 @@
1
+ module Upfluence
2
+ module Stats
3
+ class NoopMetric
4
+ def inc; end
5
+ def add(_value); end
6
+ def update(_value); end
7
+ def record(_value); end
8
+
9
+ def get
10
+ 0
11
+ end
12
+ end
13
+
14
+ NOOP_METRIC = NoopMetric.new
15
+
16
+ class NoopVector
17
+ def with_labels(**_values)
18
+ NOOP_METRIC
19
+ end
20
+ end
21
+
22
+ NOOP_VECTOR = NoopVector.new
23
+
24
+ class NoopScope
25
+ def namespace
26
+ ''
27
+ end
28
+
29
+ def tags
30
+ {}
31
+ end
32
+
33
+ def counter(_name, **_opts)
34
+ NOOP_METRIC
35
+ end
36
+
37
+ def counter_vector(_name, _labels, **_opts)
38
+ NOOP_VECTOR
39
+ end
40
+
41
+ def gauge(_name, **_opts)
42
+ NOOP_METRIC
43
+ end
44
+
45
+ def gauge_vector(_name, _labels, **_opts)
46
+ NOOP_VECTOR
47
+ end
48
+
49
+ def histogram(_name, **_opts)
50
+ NOOP_METRIC
51
+ end
52
+
53
+ def histogram_vector(_name, _labels, **_opts)
54
+ NOOP_VECTOR
55
+ end
56
+
57
+ def scope(_namespace = '', **_tags)
58
+ self
59
+ end
60
+
61
+ def root_scope
62
+ self
63
+ end
64
+ end
65
+
66
+ NOOP_SCOPE = NoopScope.new
67
+ end
68
+ end
@@ -0,0 +1,52 @@
1
+ require 'upfluence/stats/scope'
2
+
3
+ module Upfluence
4
+ module Stats
5
+ class RootScope < Scope
6
+ def initialize(collector)
7
+ super(Registry.new(collector), '', {})
8
+ end
9
+ end
10
+
11
+ class Registry
12
+ Entry = Struct.new(:type, :labels, :vector)
13
+
14
+ def initialize(collector)
15
+ @collector = collector
16
+ @mutex = Mutex.new
17
+ @entries = {}
18
+ end
19
+
20
+ def register(type, name, labels, **opts)
21
+ if labels.uniq.length != labels.length
22
+ raise ArgumentError, "duplicated labels for #{name}: #{labels}"
23
+ end
24
+
25
+ @mutex.synchronize do
26
+ entry = @entries[name]
27
+
28
+ return fetch(entry, type, name, labels) if entry
29
+
30
+ vector = @collector.public_send(:"register_#{type}", name, labels, **opts)
31
+ @entries[name] = Entry.new(type, labels.sort, vector)
32
+
33
+ vector
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def fetch(entry, type, name, labels)
40
+ if entry.type != type
41
+ raise ArgumentError, "#{entry.type} with the same name already registered: #{name}"
42
+ end
43
+
44
+ if entry.labels != labels.sort
45
+ raise ArgumentError, "#{type} #{name} already registered with different labels: #{entry.labels}"
46
+ end
47
+
48
+ entry.vector
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,69 @@
1
+ require 'upfluence/stats/vector'
2
+
3
+ module Upfluence
4
+ module Stats
5
+ DEFAULT_BUCKETS = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10].freeze
6
+
7
+ class Scope
8
+ attr_reader :namespace, :tags
9
+
10
+ def initialize(registry, namespace, tags)
11
+ @registry = registry
12
+ @namespace = namespace
13
+ @tags = tags
14
+ end
15
+
16
+ def counter(name, docstring: nil)
17
+ counter_vector(name, [], docstring: docstring).with_labels
18
+ end
19
+
20
+ def counter_vector(name, labels, docstring: nil)
21
+ register(:counter, name, labels, docstring: docstring)
22
+ end
23
+
24
+ def gauge(name, docstring: nil)
25
+ gauge_vector(name, [], docstring: docstring).with_labels
26
+ end
27
+
28
+ def gauge_vector(name, labels, docstring: nil)
29
+ register(:gauge, name, labels, docstring: docstring)
30
+ end
31
+
32
+ def histogram(name, buckets: DEFAULT_BUCKETS, docstring: nil)
33
+ histogram_vector(name, [], buckets: buckets, docstring: docstring).with_labels
34
+ end
35
+
36
+ def histogram_vector(name, labels, buckets: DEFAULT_BUCKETS, docstring: nil)
37
+ register(:histogram, name, labels, buckets: buckets, docstring: docstring)
38
+ end
39
+
40
+ def scope(namespace = '', **tags)
41
+ Scope.new(
42
+ @registry,
43
+ join_names(@namespace, namespace),
44
+ @tags.merge(tags.to_h { |k, v| [k.to_sym, v.to_s] })
45
+ )
46
+ end
47
+
48
+ def root_scope
49
+ Scope.new(@registry, '', {})
50
+ end
51
+
52
+ private
53
+
54
+ def register(type, name, labels, **opts)
55
+ labels = labels.map(&:to_sym)
56
+
57
+ Vector.new(
58
+ @registry.register(type, join_names(@namespace, name), @tags.keys + labels, **opts),
59
+ @tags,
60
+ labels
61
+ )
62
+ end
63
+
64
+ def join_names(*names)
65
+ names.map(&:to_s).reject(&:empty?).join('_')
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,47 @@
1
+ require 'upfluence/stats/vector'
2
+
3
+ module Upfluence
4
+ module Stats
5
+ class Timer
6
+ DEFAULT_SUFFIX = '_seconds'.freeze
7
+
8
+ def initialize(scope, name, suffix: DEFAULT_SUFFIX, **histogram_opts)
9
+ @histogram = scope.histogram("#{name}#{suffix}", **histogram_opts)
10
+ end
11
+
12
+ def start
13
+ StopWatch.new(@histogram)
14
+ end
15
+
16
+ def time
17
+ stop_watch = start
18
+ yield
19
+ ensure
20
+ stop_watch&.stop
21
+ end
22
+ end
23
+
24
+ class StopWatch
25
+ def initialize(histogram)
26
+ @histogram = histogram
27
+ @started_at = now
28
+ end
29
+
30
+ def stop
31
+ @histogram.record(now - @started_at)
32
+ end
33
+
34
+ private
35
+
36
+ def now
37
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
38
+ end
39
+ end
40
+
41
+ class TimerVector < EntityVector
42
+ def initialize(scope, name, labels, **opts)
43
+ super(scope, labels) { |s| Timer.new(s, name, **opts) }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ module Upfluence
2
+ module Stats
3
+ class Vector
4
+ attr_reader :labels
5
+
6
+ def initialize(vector, tags, labels)
7
+ @vector = vector
8
+ @tags = tags
9
+ @labels = labels
10
+ end
11
+
12
+ def with_labels(**values)
13
+ @vector.with_labels(@tags.merge(Vector.normalize_values(@labels, values)))
14
+ end
15
+
16
+ def self.normalize_values(labels, values)
17
+ values = values.to_h { |k, v| [k.to_sym, v.to_s] }
18
+
19
+ return values if values.length == labels.length && values.keys.sort == labels.sort
20
+
21
+ raise ArgumentError, "wrong label values: labels: #{labels}, values: #{values.keys}"
22
+ end
23
+ end
24
+
25
+ class EntityVector
26
+ attr_reader :labels
27
+
28
+ def initialize(scope, labels, &build)
29
+ @scope = scope
30
+ @labels = labels.map(&:to_sym)
31
+ @build = build
32
+ @mutex = Mutex.new
33
+ @entities = {}
34
+ end
35
+
36
+ def with_labels(**values)
37
+ values = Vector.normalize_values(@labels, values)
38
+
39
+ @mutex.synchronize do
40
+ @entities[values] ||= @build.call(@scope.scope('', **values))
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ require 'upfluence/stats/scope'
2
+ require 'upfluence/stats/root_scope'
3
+ require 'upfluence/stats/noop_scope'
4
+ require 'upfluence/stats/timer'
5
+ require 'upfluence/stats/instrument'
6
+ require 'upfluence/stats/collector/prometheus'
7
+ require 'upfluence/stats/collector/static'
8
+
9
+ module Upfluence
10
+ module Stats
11
+ class << self
12
+ attr_writer :root_scope
13
+
14
+ def root_scope
15
+ @root_scope ||= RootScope.new(Collector::Prometheus.new)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,5 @@
1
1
  module Upfluence
2
2
  module Utils
3
- VERSION = '0.14.3'.freeze
3
+ VERSION = '0.15.1'.freeze
4
4
  end
5
5
  end
data/lib/upfluence.rb CHANGED
@@ -4,3 +4,4 @@ require 'upfluence/endpoint/api_endpoint'
4
4
  require 'upfluence/mixin/strong_parameters'
5
5
  require 'upfluence/mixin/pagination'
6
6
  require 'upfluence/resources'
7
+ require 'upfluence/stats'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upfluence-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.3
4
+ version: 0.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Upfluence
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-11-17 00:00:00.000000000 Z
10
+ date: 2026-11-19 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bundler
@@ -403,6 +403,15 @@ files:
403
403
  - lib/upfluence/pool.rb
404
404
  - lib/upfluence/resources.rb
405
405
  - lib/upfluence/resources/countries.rb
406
+ - lib/upfluence/stats.rb
407
+ - lib/upfluence/stats/collector/prometheus.rb
408
+ - lib/upfluence/stats/collector/static.rb
409
+ - lib/upfluence/stats/instrument.rb
410
+ - lib/upfluence/stats/noop_scope.rb
411
+ - lib/upfluence/stats/root_scope.rb
412
+ - lib/upfluence/stats/scope.rb
413
+ - lib/upfluence/stats/timer.rb
414
+ - lib/upfluence/stats/vector.rb
406
415
  - lib/upfluence/thread.rb
407
416
  - lib/upfluence/timeout.rb
408
417
  - lib/upfluence/utils.rb