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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +24 -1
- data/.gitignore +1 -0
- data/Appraisals +13 -0
- data/README.md +27 -0
- data/gemfiles/rack_2.gemfile +10 -0
- data/gemfiles/rack_3.gemfile +10 -0
- data/lib/upfluence/http/builder.rb +12 -0
- data/lib/upfluence/http/endpoint/api_endpoint.rb +5 -4
- data/lib/upfluence/http/middleware/application_headers.rb +2 -2
- data/lib/upfluence/http/middleware/cors.rb +3 -3
- data/lib/upfluence/http/middleware/logger.rb +0 -1
- data/lib/upfluence/http/middleware/mount_point.rb +26 -0
- data/lib/upfluence/http/middleware/prometheus.rb +17 -67
- data/lib/upfluence/http/route_resolver.rb +79 -0
- data/lib/upfluence/http/server.rb +11 -6
- data/lib/upfluence/instrumentation/periodic_instrumenter.rb +19 -16
- data/lib/upfluence/pool.rb +35 -33
- data/lib/upfluence/stats/collector/prometheus.rb +104 -0
- data/lib/upfluence/stats/collector/static.rb +152 -0
- data/lib/upfluence/stats/instrument.rb +59 -0
- data/lib/upfluence/stats/noop_scope.rb +68 -0
- data/lib/upfluence/stats/root_scope.rb +52 -0
- data/lib/upfluence/stats/scope.rb +69 -0
- data/lib/upfluence/stats/timer.rb +47 -0
- data/lib/upfluence/stats/vector.rb +45 -0
- data/lib/upfluence/stats.rb +19 -0
- data/lib/upfluence/utils/version.rb +1 -1
- data/lib/upfluence.rb +1 -0
- data/upfluence_utils.gemspec +6 -4
- metadata +84 -12
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 480e2b671e179d0684542d11603fcf6cd4bb354415ba5f850a0b56b6fd67bfb5
|
|
4
|
+
data.tar.gz: 05abf4b8f6098cad04ee38c8c08ad74987787955cb2f74363f41e6dfb7ede58c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f84a5fcbe908b81312e884a33a95ac3a81235f852e1211a3023624e5d769dbb1ffa1e481ee652c7bf367a81e59e58540af7489e3744a8777ecdfe4f65859343d
|
|
7
|
+
data.tar.gz: a74c01034c3eab0687c34da00f9a0dc1b80b7ef4aaf9286789b910c69b4e726eeb479ef889884e14a721d6da29411756df273c74e49bcd25a3c84f8b7c19d64e
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -11,8 +11,31 @@ jobs:
|
|
|
11
11
|
uses: upfluence/actions/.github/workflows/lib-ruby-test.yml@master
|
|
12
12
|
secrets: inherit
|
|
13
13
|
|
|
14
|
+
rack-compatibility:
|
|
15
|
+
runs-on: ubuntu-24.04
|
|
16
|
+
strategy:
|
|
17
|
+
fail-fast: false
|
|
18
|
+
matrix:
|
|
19
|
+
appraisal:
|
|
20
|
+
- rack-2
|
|
21
|
+
- rack-3
|
|
22
|
+
steps:
|
|
23
|
+
- name: Checkout
|
|
24
|
+
uses: actions/checkout@v6
|
|
25
|
+
- name: Install Ruby
|
|
26
|
+
uses: ruby/setup-ruby@v1
|
|
27
|
+
with:
|
|
28
|
+
ruby-version: '3.4.1'
|
|
29
|
+
bundler-cache: true
|
|
30
|
+
- name: Install appraisal dependencies
|
|
31
|
+
run: bundle exec appraisal ${{ matrix.appraisal }} bundle install
|
|
32
|
+
- name: Run the test suite
|
|
33
|
+
run: bundle exec appraisal ${{ matrix.appraisal }} rspec
|
|
34
|
+
|
|
14
35
|
release:
|
|
15
|
-
needs:
|
|
36
|
+
needs:
|
|
37
|
+
- test
|
|
38
|
+
- rack-compatibility
|
|
16
39
|
if: github.ref == 'refs/heads/master'
|
|
17
40
|
runs-on: ubuntu-24.04
|
|
18
41
|
steps:
|
data/.gitignore
CHANGED
data/Appraisals
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
appraise 'rack-2' do
|
|
2
|
+
gem 'rack', '~> 2.2'
|
|
3
|
+
gem 'rackup', '~> 1.0'
|
|
4
|
+
gem 'sinatra', '~> 2.1.0'
|
|
5
|
+
gem 'sinatra-contrib', '~> 2.1.0'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
appraise 'rack-3' do
|
|
9
|
+
gem 'rack', '~> 3.2'
|
|
10
|
+
gem 'rackup', '~> 2.0'
|
|
11
|
+
gem 'sinatra', '~> 4.2'
|
|
12
|
+
gem 'sinatra-contrib', '~> 4.2'
|
|
13
|
+
end
|
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:
|
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
require 'rack/builder'
|
|
2
2
|
require 'thrift/server/rack_application'
|
|
3
|
+
require 'upfluence/http/middleware/mount_point'
|
|
3
4
|
require 'upfluence/utils'
|
|
4
5
|
|
|
5
6
|
module Upfluence
|
|
6
7
|
module HTTP
|
|
7
8
|
class Builder < Rack::Builder
|
|
9
|
+
def map(path, &block)
|
|
10
|
+
super(path) do
|
|
11
|
+
use Middleware::MountPoint, self
|
|
12
|
+
instance_eval(&block)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def endpoint
|
|
17
|
+
@run
|
|
18
|
+
end
|
|
19
|
+
|
|
8
20
|
def run_thrift(processor, handler, timeout = 30)
|
|
9
21
|
run Thrift::RackApplication.for(
|
|
10
22
|
'/',
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require 'sinatra/base'
|
|
2
2
|
require 'active_record'
|
|
3
3
|
require 'active_support/hash_with_indifferent_access'
|
|
4
|
+
require 'rack/rewindable_input'
|
|
4
5
|
require 'upfluence/http/endpoint/validation_error'
|
|
5
6
|
require 'upfluence/mixin/strong_parameters'
|
|
6
7
|
|
|
@@ -17,6 +18,8 @@ module Upfluence
|
|
|
17
18
|
|
|
18
19
|
enable :raise_errors
|
|
19
20
|
|
|
21
|
+
use Rack::RewindableInput::Middleware if defined?(Rack::RewindableInput::Middleware)
|
|
22
|
+
|
|
20
23
|
before { content_type :json }
|
|
21
24
|
|
|
22
25
|
configure :development do
|
|
@@ -81,11 +84,9 @@ module Upfluence
|
|
|
81
84
|
end
|
|
82
85
|
|
|
83
86
|
def request_body
|
|
84
|
-
|
|
85
|
-
data = request.body.read
|
|
87
|
+
request.env['upfluence.request_body'] ||= begin
|
|
86
88
|
request.body.rewind
|
|
87
|
-
|
|
88
|
-
data
|
|
89
|
+
request.body.read
|
|
89
90
|
end
|
|
90
91
|
end
|
|
91
92
|
|
|
@@ -16,8 +16,8 @@ module Upfluence
|
|
|
16
16
|
|
|
17
17
|
def build_headers(handler)
|
|
18
18
|
{
|
|
19
|
-
'
|
|
20
|
-
'
|
|
19
|
+
'x-upfluence-unit-name' => handler.getName,
|
|
20
|
+
'x-upfluence-version' => build_version(handler.getVersion)
|
|
21
21
|
}
|
|
22
22
|
end
|
|
23
23
|
|
|
@@ -3,9 +3,9 @@ module Upfluence
|
|
|
3
3
|
module Middleware
|
|
4
4
|
class CORS
|
|
5
5
|
HEADERS = {
|
|
6
|
-
'
|
|
7
|
-
'
|
|
8
|
-
'
|
|
6
|
+
'access-control-allow-origin' => '*',
|
|
7
|
+
'access-control-allow-headers' => 'Authorization, Content-Type, Scope, X-Upfluence-Plugin-Version',
|
|
8
|
+
'access-control-allow-methods' => 'GET, POST, PUT, OPTIONS, DELETE'
|
|
9
9
|
}.freeze
|
|
10
10
|
|
|
11
11
|
def initialize(app, headers = nil)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Upfluence
|
|
2
|
+
module HTTP
|
|
3
|
+
module Middleware
|
|
4
|
+
class MountPoint
|
|
5
|
+
ENV_KEY = 'upfluence.mount'.freeze
|
|
6
|
+
|
|
7
|
+
Mount = Struct.new(:script_name, :path_info, :endpoint)
|
|
8
|
+
|
|
9
|
+
def initialize(app, builder)
|
|
10
|
+
@app = app
|
|
11
|
+
@endpoint = builder.endpoint
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def call(env)
|
|
15
|
+
env[ENV_KEY] = Mount.new(
|
|
16
|
+
env['SCRIPT_NAME'].to_s,
|
|
17
|
+
env['PATH_INFO'].to_s,
|
|
18
|
+
@endpoint
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
@app.call(env)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -1,29 +1,26 @@
|
|
|
1
|
-
require 'prometheus/client'
|
|
2
1
|
require 'upfluence/environment'
|
|
2
|
+
require 'upfluence/http/route_resolver'
|
|
3
|
+
require 'upfluence/stats'
|
|
3
4
|
|
|
4
5
|
module Upfluence
|
|
5
6
|
module HTTP
|
|
6
7
|
module Middleware
|
|
7
8
|
class Prometheus
|
|
8
|
-
LABELS = %i[path method
|
|
9
|
+
LABELS = %i[path method].freeze
|
|
9
10
|
|
|
10
|
-
def initialize(app,
|
|
11
|
-
|
|
11
|
+
def initialize(app, scope = Upfluence::Stats.root_scope)
|
|
12
|
+
scope = scope.scope('uhttp_handler', env: Upfluence.env.to_s)
|
|
12
13
|
|
|
13
|
-
@request_total_count =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
:
|
|
17
|
-
docstring: 'Histogram of processed items',
|
|
18
|
-
labels: LABELS + %i[status]
|
|
14
|
+
@request_total_count = scope.counter_vector(
|
|
15
|
+
'requests_total',
|
|
16
|
+
LABELS + %i[status],
|
|
17
|
+
docstring: 'Histogram of processed items'
|
|
19
18
|
)
|
|
20
19
|
|
|
21
|
-
@request_histogram =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
:
|
|
25
|
-
docstring: 'Histogram of processing time',
|
|
26
|
-
labels: LABELS
|
|
20
|
+
@request_histogram = scope.histogram_vector(
|
|
21
|
+
'requests_duration_second',
|
|
22
|
+
LABELS,
|
|
23
|
+
docstring: 'Histogram of processing time'
|
|
27
24
|
)
|
|
28
25
|
|
|
29
26
|
@app = app
|
|
@@ -44,58 +41,11 @@ module Upfluence
|
|
|
44
41
|
end
|
|
45
42
|
|
|
46
43
|
def record(env, code, duration)
|
|
47
|
-
path =
|
|
44
|
+
path = RouteResolver.resolve(env, code)
|
|
45
|
+
method = env['REQUEST_METHOD'].downcase
|
|
48
46
|
|
|
49
|
-
@request_total_count.
|
|
50
|
-
|
|
51
|
-
path: path,
|
|
52
|
-
method: env['REQUEST_METHOD'].downcase,
|
|
53
|
-
status: code,
|
|
54
|
-
env: Upfluence.env.to_s
|
|
55
|
-
}
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
@request_histogram.observe(
|
|
59
|
-
duration,
|
|
60
|
-
labels: {
|
|
61
|
-
path: path,
|
|
62
|
-
method: env['REQUEST_METHOD'].downcase,
|
|
63
|
-
env: Upfluence.env.to_s
|
|
64
|
-
}
|
|
65
|
-
)
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def parse_route(env, code)
|
|
69
|
-
parse_route_sinatra(env) || parse_route_rails(env) ||
|
|
70
|
-
parse_route_default(env, code)
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
def parse_route_rails(env)
|
|
74
|
-
params = env['action_dispatch.request.parameters']
|
|
75
|
-
return nil if params.nil?
|
|
76
|
-
|
|
77
|
-
"#{params['controller']}##{params['action']}"
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
def parse_route_sinatra(env)
|
|
81
|
-
route = env['sinatra.route']
|
|
82
|
-
return nil if route.nil? || route.strip == ''
|
|
83
|
-
|
|
84
|
-
path = Rack::Request.new(env).path
|
|
85
|
-
|
|
86
|
-
splitted_template = route.split.last.split('/').reject do |v|
|
|
87
|
-
v.eql?('')
|
|
88
|
-
end.reverse
|
|
89
|
-
|
|
90
|
-
path.split('/').reverse.map.with_index do |part, i|
|
|
91
|
-
splitted_template[i] || part
|
|
92
|
-
end.reverse.join('/')
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def parse_route_default(env, code)
|
|
96
|
-
return 'unexpected-route' if code.to_s.eql? '404'
|
|
97
|
-
|
|
98
|
-
Rack::Request.new(env).path.gsub(%r{/\d+(/|$)}, '/:id\\1')
|
|
47
|
+
@request_total_count.with_labels(path: path, method: method, status: code).inc
|
|
48
|
+
@request_histogram.with_labels(path: path, method: method).record(duration)
|
|
99
49
|
end
|
|
100
50
|
end
|
|
101
51
|
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require 'rack/request'
|
|
2
|
+
require 'upfluence/http/middleware/mount_point'
|
|
3
|
+
|
|
4
|
+
module Upfluence
|
|
5
|
+
module HTTP
|
|
6
|
+
module RouteResolver
|
|
7
|
+
UNEXPECTED_ROUTE = 'unexpected-route'.freeze
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
def resolve(env, code = nil)
|
|
11
|
+
mount = env[Middleware::MountPoint::ENV_KEY]
|
|
12
|
+
not_found = code.to_s.eql?('404')
|
|
13
|
+
|
|
14
|
+
resolve_sinatra(env, mount) || resolve_rails(env) ||
|
|
15
|
+
resolve_mount(env, mount, not_found) ||
|
|
16
|
+
resolve_default(env, not_found)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def resolve_rails(env)
|
|
22
|
+
params = env['action_dispatch.request.parameters']
|
|
23
|
+
return nil if params.nil?
|
|
24
|
+
|
|
25
|
+
"#{params['controller']}##{params['action']}"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def resolve_sinatra(env, mount)
|
|
29
|
+
route = env['sinatra.route']
|
|
30
|
+
return nil if route.nil? || route.strip == ''
|
|
31
|
+
|
|
32
|
+
"#{mount&.script_name}#{route.split(' ', 2).last}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Used when the request never reached the router of the mounted app
|
|
36
|
+
# (e.g. throttled or failed in a middleware), so the route has to be
|
|
37
|
+
# resolved statically from the mount point.
|
|
38
|
+
def resolve_mount(env, mount, not_found)
|
|
39
|
+
return nil if mount.nil? || not_found
|
|
40
|
+
|
|
41
|
+
route = resolve_sinatra_route(
|
|
42
|
+
mount.endpoint,
|
|
43
|
+
env['REQUEST_METHOD'],
|
|
44
|
+
mount.path_info
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
route ||= '/*' unless ['', '/'].include?(mount.path_info)
|
|
48
|
+
|
|
49
|
+
"#{mount.script_name}#{route}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def resolve_sinatra_route(endpoint, verb, path)
|
|
53
|
+
return nil unless defined?(::Sinatra::Base)
|
|
54
|
+
|
|
55
|
+
klass = endpoint.respond_to?(:settings) ? endpoint.settings : endpoint
|
|
56
|
+
path = '/' if path.empty?
|
|
57
|
+
|
|
58
|
+
while klass.is_a?(Class) && klass <= ::Sinatra::Base
|
|
59
|
+
(klass.routes[verb] || []).each do |pattern, _conditions, _block|
|
|
60
|
+
return pattern.to_s if pattern.params(path)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
klass = klass.superclass
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
nil
|
|
67
|
+
rescue StandardError
|
|
68
|
+
nil
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def resolve_default(env, not_found)
|
|
72
|
+
return UNEXPECTED_ROUTE if not_found
|
|
73
|
+
|
|
74
|
+
Rack::Request.new(env).path.gsub(%r{/\d+(/|$)}, '/:id\\1')
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
require 'rack/handler'
|
|
2
1
|
require 'rack/etag'
|
|
3
2
|
require 'rack/timeout/base'
|
|
4
3
|
require 'prometheus/client'
|
|
@@ -20,6 +19,12 @@ require 'upfluence/http/middleware/cors'
|
|
|
20
19
|
require 'upfluence/http/middleware/request_stapler'
|
|
21
20
|
require 'upfluence/instrumentation'
|
|
22
21
|
|
|
22
|
+
begin
|
|
23
|
+
require 'rackup/handler'
|
|
24
|
+
rescue LoadError
|
|
25
|
+
require 'rack/handler'
|
|
26
|
+
end
|
|
27
|
+
|
|
23
28
|
Rack::Timeout::Logger.disable
|
|
24
29
|
|
|
25
30
|
module Upfluence
|
|
@@ -62,7 +67,8 @@ module Upfluence
|
|
|
62
67
|
|
|
63
68
|
@admin_builder = admin_builder(opts) if opts[:admin_port]
|
|
64
69
|
@production_builder = production_builder(opts, &block)
|
|
65
|
-
|
|
70
|
+
handler = defined?(Rackup::Handler) ? Rackup::Handler : Rack::Handler
|
|
71
|
+
@handler = handler.get(opts[:server])
|
|
66
72
|
@stoppables = []
|
|
67
73
|
end
|
|
68
74
|
|
|
@@ -241,7 +247,6 @@ module Upfluence
|
|
|
241
247
|
use Upfluence.error_logger.middleware
|
|
242
248
|
|
|
243
249
|
use Rack::ContentLength
|
|
244
|
-
use Rack::Chunked
|
|
245
250
|
use Rack::Lint if Upfluence.env.development?
|
|
246
251
|
use Rack::TempfileReaper
|
|
247
252
|
use Rack::ETag
|
|
@@ -257,9 +262,9 @@ module Upfluence
|
|
|
257
262
|
|
|
258
263
|
def run_prometheus_exporter
|
|
259
264
|
push = Prometheus::Client::Push.new(
|
|
260
|
-
@options[:app_name],
|
|
261
|
-
@options[:unit_name],
|
|
262
|
-
@options[:push_gateway_url]
|
|
265
|
+
job: @options[:app_name],
|
|
266
|
+
grouping_key: { instance: @options[:unit_name] },
|
|
267
|
+
gateway: @options[:push_gateway_url]
|
|
263
268
|
)
|
|
264
269
|
|
|
265
270
|
loop do
|
|
@@ -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,
|
|
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.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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
|
|
data/lib/upfluence/pool.rb
CHANGED
|
@@ -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
|
|
9
|
-
def self.standardized_metrics(n)
|
|
10
|
-
|
|
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(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@
|
|
18
|
-
@
|
|
19
|
-
@
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
@
|
|
23
|
-
|
|
24
|
-
@
|
|
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.
|
|
28
|
+
@get.inc
|
|
34
29
|
end
|
|
35
30
|
|
|
36
31
|
def observe_put
|
|
37
|
-
@put.
|
|
32
|
+
@put.inc
|
|
38
33
|
end
|
|
39
34
|
|
|
40
35
|
def observe_discard
|
|
41
|
-
@discard.
|
|
36
|
+
@discard.inc
|
|
42
37
|
end
|
|
43
38
|
|
|
44
39
|
def observe_wait(seconds)
|
|
45
|
-
@get_wait.
|
|
46
|
-
@get_wait_duration.
|
|
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.
|
|
45
|
+
@idle.update(count)
|
|
51
46
|
end
|
|
52
47
|
|
|
53
48
|
def update_checkout(count)
|
|
54
|
-
@checkout.
|
|
49
|
+
@checkout.update(count)
|
|
55
50
|
end
|
|
56
51
|
|
|
57
52
|
def set_size(count)
|
|
58
|
-
@size.
|
|
53
|
+
@size.update(count)
|
|
59
54
|
end
|
|
55
|
+
end
|
|
60
56
|
|
|
61
|
-
|
|
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
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
|