upfluence-utils 0.14.1 → 0.15.0

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.
@@ -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.1'.freeze
3
+ VERSION = '0.15.0'.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'
@@ -18,18 +18,20 @@ Gem::Specification.new do |spec|
18
18
  spec.required_ruby_version = '>= 2.7.0'
19
19
 
20
20
  spec.add_development_dependency "bundler", ">= 2.2"
21
+ spec.add_development_dependency "appraisal", "~> 2.5"
21
22
  spec.add_development_dependency "rake", ">= 13.0.0"
22
23
  spec.add_development_dependency "rspec", ">= 3.10"
23
24
  spec.add_runtime_dependency 'upfluence-thrift'
24
- spec.add_runtime_dependency 'sinatra'
25
+ spec.add_runtime_dependency 'sinatra', '>= 2.1', '< 5'
25
26
  spec.add_runtime_dependency 'redis'
26
27
  spec.add_runtime_dependency 'sentry-ruby', '< 6.0'
27
- spec.add_runtime_dependency 'sinatra-contrib'
28
+ spec.add_runtime_dependency 'sinatra-contrib', '>= 2.1', '< 5'
28
29
  spec.add_runtime_dependency 'activesupport'
29
30
  spec.add_runtime_dependency 'puma', '>= 7.0'
30
- spec.add_runtime_dependency 'rack', '< 3.0'
31
+ spec.add_runtime_dependency 'rack', '>= 2.2', '< 4'
32
+ spec.add_runtime_dependency 'rackup', '>= 1.0', '< 3'
31
33
  spec.add_runtime_dependency 'stackprof'
32
- spec.add_runtime_dependency 'prometheus-client', '~> 2.1'
34
+ spec.add_runtime_dependency 'prometheus-client', '>= 4.1', '< 6'
33
35
  spec.add_runtime_dependency 'userializer'
34
36
  spec.add_runtime_dependency 'activerecord'
35
37
  spec.add_runtime_dependency 'loofah'