upfluence-utils 0.12.11 → 0.12.12

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: 422d6056047e387e14e46823773b9ba7f1d3463657b1ac5797a4c492ee772db8
4
- data.tar.gz: 113685a071e08a026bc2518567b9539698b4f9be99a1b24b344e41b73c81b346
3
+ metadata.gz: 5ddcbd6854f88cf5645e0295a48ac21f9aec1da81d71cf20d49214c61d57f8d9
4
+ data.tar.gz: 43907a8dc7d24c0609d914f46c8954dd67b6af664f474f26038ae5551eac6f65
5
5
  SHA512:
6
- metadata.gz: 0e243d46b108b49effee29ebc105ffeb498d69554158267308d9665d6f4be6c7d0dd6e581e1479186fdb0f9e87857aafeb8fbf4cad29d3a1a2d2c1daed54ed8a
7
- data.tar.gz: 68d33676ad949085e6ee8a8f456fb9d792b94f8438bd16b93f41e679a82bfe70b1291bdedf677e35f1ed1f727a57fc4ab84a9e77de1f83cd241acee66bbf5e10
6
+ metadata.gz: 4a54773f0b7123115b0139bb417972df0b47347e10b03bfa9299adfe6e9921166510328776863ea5850ad6f36a9102c84a58c55a3e23fe6ad758c169ce9066f8
7
+ data.tar.gz: 85b2ac6104fb0e8eeda611b6e7cc2027f24214e49e726e083b0ba111a237e8daade7ef18f40f256a03300cd21b80445989d674f5c8ca3ae9dfa5789b152b2447
@@ -18,6 +18,7 @@ require 'upfluence/http/middleware/handle_exception'
18
18
  require 'upfluence/http/middleware/prometheus'
19
19
  require 'upfluence/http/middleware/cors'
20
20
  require 'upfluence/http/middleware/request_stapler'
21
+ require 'upfluence/instrumentation'
21
22
 
22
23
  Rack::Timeout::Logger.disable
23
24
 
@@ -42,6 +43,11 @@ module Upfluence
42
43
  max_threads: ENV.fetch('HTTP_SERVER_MAX_THREADS', 5).to_i,
43
44
  request_timeout: ENV['HTTP_SERVER_REQUEST_TIMEOUT']&.to_i,
44
45
  middlewares: [],
46
+ instrumentations: [
47
+ Instrumentation::PumaInstrumenter.new,
48
+ Instrumentation::GCInstrumenter.new,
49
+ Instrumentation::ActiveRecordPoolInstrumenter.new
50
+ ],
45
51
  debug: ENV.fetch('DEBUG', nil)
46
52
  }
47
53
 
@@ -103,6 +109,8 @@ module Upfluence
103
109
 
104
110
  Thread.new { run_prometheus_exporter } if @options[:push_gateway_url]
105
111
 
112
+ @options[:instrumentations].each(&:start)
113
+
106
114
  @handler.run(@builder, **@options) do |server|
107
115
  server.threaded = @options[:threaded] if server.respond_to? :threaded=
108
116
 
@@ -0,0 +1,30 @@
1
+ require 'upfluence/instrumentation/periodic_instrumenter'
2
+ require 'active_record'
3
+
4
+ module Upfluence
5
+ module Instrumentation
6
+ class ActiveRecordPoolInstrumenter < PeriodicInstrumenter
7
+ KEYS = %i[
8
+ size connections busy dead idle waiting
9
+ ].freeze
10
+
11
+ def prefix
12
+ 'active_record_pool'
13
+ end
14
+
15
+ def metrics
16
+ KEYS.reduce({}) do |acc, k|
17
+ acc.merge(k => { docstring: "Gauge for #{k}" })
18
+ end
19
+ end
20
+
21
+ def values
22
+ return {} unless ActiveRecord::Base.connected?
23
+
24
+ ActiveRecord::Base.connection_pool.stat.slice(*KEYS).reduce({}) do |acc, (k, v)|
25
+ acc.merge(k => [{ value: v }])
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ require 'upfluence/instrumentation/periodic_instrumenter'
2
+
3
+ module Upfluence
4
+ module Instrumentation
5
+ class GCInstrumenter < PeriodicInstrumenter
6
+ KEYS = %i[
7
+ heap_live_slots heap_free_slots major_gc_count minor_gc_count
8
+ total_allocated_objects
9
+ ].freeze
10
+
11
+ def prefix
12
+ 'ruby_gc'
13
+ end
14
+
15
+ def metrics
16
+ KEYS.reduce({}) do |acc, k|
17
+ acc.merge(k => { docstring: "Gauge for #{k}" })
18
+ end
19
+ end
20
+
21
+ def values
22
+ GC.stat.slice(*KEYS).reduce({}) do |acc, (k, v)|
23
+ acc.merge(k => [{ value: v }])
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,61 @@
1
+ module Upfluence
2
+ module Instrumentation
3
+ class PeriodicInstrumenter
4
+ def initialize(interval: 30, registry: ::Prometheus::Client.registry)
5
+ @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
+ )
16
+ end
17
+
18
+ @stop_thread = false
19
+ end
20
+
21
+ def start
22
+ @thread ||= Thread.new do
23
+ until @stop_thread
24
+ 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
30
+ rescue => e
31
+ Upfluence.error_logger.notify(e)
32
+ ensure
33
+ sleep @interval
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def stop
40
+ return unless @thread&.alive?
41
+
42
+ @stop_thread = true
43
+ @thread.wakeup
44
+ @thread.join
45
+ @thread = nil
46
+ end
47
+
48
+ def prefix
49
+ nil
50
+ end
51
+
52
+ def values
53
+ raise "Please implement a subcalss"
54
+ end
55
+
56
+ def metrics
57
+ raise "Please implement a subcalss"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,33 @@
1
+ require 'upfluence/instrumentation/periodic_instrumenter'
2
+ require 'puma'
3
+
4
+ module Upfluence
5
+ module Instrumentation
6
+ class PumaInstrumenter < PeriodicInstrumenter
7
+ KEYS = %i[backlog_thread running_thread pool_capacity requests_count].freeze
8
+
9
+ def prefix
10
+ 'puma'
11
+ end
12
+
13
+ def metrics
14
+ KEYS.reduce({}) do |acc, k|
15
+ acc.merge(k => { docstring: "Gauge for #{k}" })
16
+ end
17
+ end
18
+
19
+ def values
20
+ stats = Puma.stats_hash
21
+
22
+ {
23
+ requests_count: [{ value: stats[:requests_count] }],
24
+ backlog_thread: [{ value: stats[:backlog] }],
25
+ running_thread: [{ value: stats[:running] }],
26
+ pool_capacity: [{ value: stats[:pool_capacity] }]
27
+ }
28
+ rescue NoMethodError
29
+ {}
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ require 'upfluence/instrumentation/puma_instrumenter'
2
+ require 'upfluence/instrumentation/gc_instrumenter'
3
+ require 'upfluence/instrumentation/active_record_pool_instrumenter'
@@ -1,5 +1,5 @@
1
1
  module Upfluence
2
2
  module Utils
3
- VERSION = '0.12.11'.freeze
3
+ VERSION = '0.12.12'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upfluence-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.11
4
+ version: 0.12.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Upfluence
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-12 00:00:00.000000000 Z
11
+ date: 2025-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -283,6 +283,11 @@ files:
283
283
  - lib/upfluence/http/middleware/prometheus.rb
284
284
  - lib/upfluence/http/middleware/request_stapler.rb
285
285
  - lib/upfluence/http/server.rb
286
+ - lib/upfluence/instrumentation.rb
287
+ - lib/upfluence/instrumentation/active_record_pool_instrumenter.rb
288
+ - lib/upfluence/instrumentation/gc_instrumenter.rb
289
+ - lib/upfluence/instrumentation/periodic_instrumenter.rb
290
+ - lib/upfluence/instrumentation/puma_instrumenter.rb
286
291
  - lib/upfluence/logger.rb
287
292
  - lib/upfluence/mixin/html_scrubbing.rb
288
293
  - lib/upfluence/mixin/pagination.rb