pgq_prometheus 0.1.0 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/pgq_prometheus/collector.rb +8 -0
- data/lib/pgq_prometheus/config.rb +2 -0
- data/lib/pgq_prometheus/processor.rb +53 -46
- data/lib/pgq_prometheus/sql_caller/active_record.rb +23 -1
- data/lib/pgq_prometheus/version.rb +1 -1
- data/pgq_prometheus_metrics.gemspec +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 111af82c3aa1cbd83e1f176802001cb2c7b79daaa3ae7f327b72b96f362d6de2
|
4
|
+
data.tar.gz: ec0fc1d7f8f653ec2bedd062bb293807182af62b915162130dfe81724d551357
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 737f11851a5781a25f42f3b702afcd27a88e2d5bbec7d8d389124995b99f99e82e8b0aafbb39e28bff155b12ce12565e02140e354b537e61eb97689573404373
|
7
|
+
data.tar.gz: 437da382c3917b97f065332c52c7d12169f2ea05832fb45e818558ac224e89ebca18095a6b250e584b3aed5fc3f54f6c16985c952281f704265ce02094e59122
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# PgqPrometheus
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.
|
3
|
+
[![Build Status](https://travis-ci.org/didww/pgq_prometheus.svg?branch=master)](https://travis-ci.org/didww/pgq_prometheus)
|
4
4
|
|
5
5
|
Highly configurable Prometheus metrics for PGQ postgres extension
|
6
6
|
|
@@ -105,7 +105,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
105
105
|
|
106
106
|
## Contributing
|
107
107
|
|
108
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/didww/pgq_prometheus. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/didww/pgq_prometheus/blob/master/CODE_OF_CONDUCT.md).
|
109
109
|
|
110
110
|
|
111
111
|
## License
|
@@ -114,4 +114,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
114
114
|
|
115
115
|
## Code of Conduct
|
116
116
|
|
117
|
-
Everyone interacting in the PgqPrometheus project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
117
|
+
Everyone interacting in the PgqPrometheus project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/didwwpgq_prometheus/blob/master/CODE_OF_CONDUCT.md).
|
@@ -1,8 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'config'
|
2
4
|
require 'prometheus_exporter/server'
|
3
5
|
|
4
6
|
module PgqPrometheus
|
5
7
|
class Collector < PrometheusExporter::Server::TypeCollector
|
8
|
+
MAX_METRIC_AGE = 30
|
6
9
|
|
7
10
|
def initialize
|
8
11
|
@data = []
|
@@ -23,6 +26,7 @@ module PgqPrometheus
|
|
23
26
|
def metrics
|
24
27
|
return [] if @data.length == 0
|
25
28
|
|
29
|
+
@observers.each_value(&:reset!)
|
26
30
|
metrics = {}
|
27
31
|
|
28
32
|
@data.map do |obj|
|
@@ -46,6 +50,10 @@ module PgqPrometheus
|
|
46
50
|
end
|
47
51
|
|
48
52
|
def collect(obj)
|
53
|
+
now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
54
|
+
|
55
|
+
obj['created_at'] = now
|
56
|
+
@data.delete_if { |m| m['created_at'] + MAX_METRIC_AGE < now }
|
49
57
|
@data << obj
|
50
58
|
end
|
51
59
|
end
|
@@ -1,48 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'prometheus_exporter/client'
|
2
4
|
|
3
5
|
module PgqPrometheus
|
4
6
|
class Processor
|
5
7
|
class << self
|
6
|
-
attr_accessor :sql_caller,
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
client
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
8
|
+
attr_accessor :sql_caller,
|
9
|
+
:logger,
|
10
|
+
:on_error,
|
11
|
+
:before_collect,
|
12
|
+
:after_collect
|
13
|
+
|
14
|
+
def start(client: nil, frequency: 30, labels: nil)
|
15
|
+
raise ArgumentError, "#{name}.sql_caller must be defined" if sql_caller.nil?
|
16
|
+
|
17
|
+
stop
|
18
|
+
|
19
|
+
client ||= PrometheusExporter::Client.default
|
20
|
+
metric_labels = labels&.dup || {}
|
21
|
+
process_collector = new(metric_labels)
|
22
|
+
|
23
|
+
@thread = Thread.new do
|
24
|
+
wrap_thread_loop(name) do
|
25
|
+
sql_caller.release_connection
|
26
|
+
logger&.info { "Start #{name}" }
|
27
|
+
while true
|
28
|
+
begin
|
29
|
+
before_collect&.call
|
30
|
+
metrics = process_collector.collect
|
31
|
+
metrics.each do |metric|
|
32
|
+
client.send_json metric
|
33
|
+
end
|
34
|
+
after_collect&.call
|
35
|
+
rescue => e
|
36
|
+
STDERR.puts "#{self.class} Failed To Collect Stats #{e.class} #{e.message}"
|
37
|
+
logger&.error { "#{e.class} #{e.message} #{e.backtrace.join("\n")}" }
|
38
|
+
on_error&.call(e)
|
39
|
+
end
|
40
|
+
sleep frequency
|
24
41
|
end
|
25
|
-
rescue => e
|
26
|
-
STDERR.puts "#{self.class} Failed To Collect Stats #{e.class} #{e.message}"
|
27
|
-
logger&.error { "#{e.class} #{e.message} #{e.backtrace.join("\n")}" }
|
28
|
-
on_error&.call(e)
|
29
42
|
end
|
30
|
-
sleep frequency
|
31
43
|
end
|
44
|
+
|
45
|
+
true
|
32
46
|
end
|
33
47
|
|
34
|
-
|
35
|
-
|
48
|
+
def stop
|
49
|
+
@thread&.kill
|
50
|
+
@thread = nil
|
51
|
+
end
|
36
52
|
|
37
|
-
|
38
|
-
|
53
|
+
def running?
|
54
|
+
defined?(@thread) && @thread
|
55
|
+
end
|
39
56
|
|
40
|
-
|
41
|
-
|
42
|
-
end
|
57
|
+
def wrap_thread_loop(*tags)
|
58
|
+
return yield if logger.nil? || !logger.respond_to?(:tagged)
|
43
59
|
|
44
|
-
|
45
|
-
|
60
|
+
logger.tagged(*tags) { yield }
|
61
|
+
end
|
46
62
|
end
|
47
63
|
|
48
64
|
def initialize(labels = {})
|
@@ -51,10 +67,9 @@ module PgqPrometheus
|
|
51
67
|
|
52
68
|
def collect
|
53
69
|
metrics = []
|
70
|
+
sql_caller.with_connection do
|
54
71
|
|
55
|
-
|
56
|
-
|
57
|
-
self.class.sql_caller.queue_info.each do |queue_info|
|
72
|
+
sql_caller.queue_info.each do |queue_info|
|
58
73
|
queue = queue_info[:queue_name]
|
59
74
|
|
60
75
|
queue_metric_opts.each do |name, opts|
|
@@ -63,7 +78,7 @@ module PgqPrometheus
|
|
63
78
|
metrics << format_metric(name, value, labels)
|
64
79
|
end
|
65
80
|
|
66
|
-
|
81
|
+
sql_caller.consumer_info(queue).each do |consumer_info|
|
67
82
|
consumer = consumer_info[:consumer_name]
|
68
83
|
|
69
84
|
consumer_metric_opts.each do |name, opts|
|
@@ -86,16 +101,8 @@ module PgqPrometheus
|
|
86
101
|
|
87
102
|
private
|
88
103
|
|
89
|
-
|
90
|
-
self.class.
|
91
|
-
end
|
92
|
-
|
93
|
-
def within_logger_tags(*tags)
|
94
|
-
if logger.nil? || !logger.respond_to?(:tagged)
|
95
|
-
yield
|
96
|
-
else
|
97
|
-
logger.tagged(*tags) { yield }
|
98
|
-
end
|
104
|
+
[:sql_caller, :logger].each do |meth|
|
105
|
+
define_method(meth) { |*args, &block| self.class.public_send(meth, *args, &block) }
|
99
106
|
end
|
100
107
|
|
101
108
|
def queue_metric_opts
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module PgqPrometheus
|
2
4
|
module SqlCaller
|
3
5
|
class ActiveRecord
|
@@ -38,6 +40,19 @@ module PgqPrometheus
|
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
43
|
+
# Releases active pg connection in thread.
|
44
|
+
# Do nothing if no connection captured.
|
45
|
+
def release_connection
|
46
|
+
model_class.connection_pool.release_connection
|
47
|
+
end
|
48
|
+
|
49
|
+
# Acquires pg connection during block execution.
|
50
|
+
# Release it after block executed.
|
51
|
+
# @yield
|
52
|
+
def with_connection
|
53
|
+
model_class.connection_pool.with_connection { yield }
|
54
|
+
end
|
55
|
+
|
41
56
|
private
|
42
57
|
|
43
58
|
def model_class
|
@@ -48,9 +63,16 @@ module PgqPrometheus
|
|
48
63
|
sql = model_class.send :sanitize_sql_array, bindings.unshift(sql) unless bindings.empty?
|
49
64
|
result = model_class.connection.select_all(sql)
|
50
65
|
result.map do |row|
|
51
|
-
row.map { |k, v| [k.to_sym, result
|
66
|
+
row.map { |k, v| [k.to_sym, deserialize_value(result, k, v)] }.to_h
|
52
67
|
end
|
53
68
|
end
|
69
|
+
|
70
|
+
def deserialize_value(result, key, value)
|
71
|
+
pg_type = result.column_types[key]
|
72
|
+
return value if pg_type.nil?
|
73
|
+
|
74
|
+
pg_type.deserialize(value)
|
75
|
+
end
|
54
76
|
end
|
55
77
|
end
|
56
78
|
end
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = 'Prometheus metrics for PGQ postgres extension'
|
12
12
|
spec.description = 'Prometheus metrics for PGQ postgres extension'
|
13
|
-
spec.homepage = 'https://github.com/
|
13
|
+
spec.homepage = 'https://github.com/didww/pgq_prometheus'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pgq_prometheus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Talakevich
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: prometheus_exporter
|
@@ -62,13 +62,13 @@ files:
|
|
62
62
|
- lib/pgq_prometheus/sql_caller/active_record.rb
|
63
63
|
- lib/pgq_prometheus/version.rb
|
64
64
|
- pgq_prometheus_metrics.gemspec
|
65
|
-
homepage: https://github.com/
|
65
|
+
homepage: https://github.com/didww/pgq_prometheus
|
66
66
|
licenses:
|
67
67
|
- MIT
|
68
68
|
metadata:
|
69
|
-
homepage_uri: https://github.com/
|
70
|
-
source_code_uri: https://github.com/
|
71
|
-
changelog_uri: https://github.com/
|
69
|
+
homepage_uri: https://github.com/didww/pgq_prometheus
|
70
|
+
source_code_uri: https://github.com/didww/pgq_prometheus
|
71
|
+
changelog_uri: https://github.com/didww/pgq_prometheus
|
72
72
|
post_install_message:
|
73
73
|
rdoc_options: []
|
74
74
|
require_paths:
|
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
- !ruby/object:Gem::Version
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
|
-
rubygems_version: 3.
|
87
|
+
rubygems_version: 3.0.9
|
88
88
|
signing_key:
|
89
89
|
specification_version: 4
|
90
90
|
summary: Prometheus metrics for PGQ postgres extension
|