pgq_prometheus 0.1.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 +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +2 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/pgq_prometheus.rb +11 -0
- data/lib/pgq_prometheus/collector.rb +52 -0
- data/lib/pgq_prometheus/config.rb +106 -0
- data/lib/pgq_prometheus/processor.rb +121 -0
- data/lib/pgq_prometheus/sql_caller/active_record.rb +56 -0
- data/lib/pgq_prometheus/version.rb +5 -0
- data/pgq_prometheus_metrics.gemspec +33 -0
- metadata +91 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7bfd83bd5e5c4ff547cc53f7424922ba0a25c957ede3a2b5564a91ecefb015f3
|
4
|
+
data.tar.gz: 88a8700c82abf2513a91a4c389831401e76f7f51c35f61b4c292c3dce816006a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fcd9e6f57018fd3e8451c35799e9f2d5c0d39871f177e24de8ebe65e7c7abc4f6d8329832033f0fb7ce442bde1342268c24f9a43448825216943e0f312db2f2b
|
7
|
+
data.tar.gz: 92174e946b347278960575e6bfb83a141e71b3df55f65b688866b1093e2994437c6b5a2b6ae4b4386c7d2b7a02f97eb33a537831f36c7dcd4fc7317a41be888a
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at senid231@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Denis Talakevich
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# PgqPrometheus
|
2
|
+
|
3
|
+
[](https://travis-ci.com/senid231/pgq_prometheus)
|
4
|
+
|
5
|
+
Highly configurable Prometheus metrics for PGQ postgres extension
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'pgq_prometheus'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install pgq_prometheus
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Basic usage with rails
|
26
|
+
|
27
|
+
### config/initializers/pgq_prometheus.rb
|
28
|
+
```ruby
|
29
|
+
require 'pgq_prometheus'
|
30
|
+
require 'pgq_prometheus/processor'
|
31
|
+
|
32
|
+
# If you use postgresql library/orm other then active_record
|
33
|
+
# look at PgqPrometheus::SqlCaller::ActiveRecord for example of what sql caller should return.
|
34
|
+
require 'pgq_prometheus/sql_caller/active_record'
|
35
|
+
|
36
|
+
# We will configure processor here
|
37
|
+
PgqPrometheus::Processor.tap do |processor|
|
38
|
+
# you must set sql_caller to retrieve data from postgres
|
39
|
+
processor.sql_caller = PgqPrometheus::SqlCaller::ActiveRecord.new('ApplicationRecord')
|
40
|
+
# you can setup processor logger
|
41
|
+
processor.logger = Rails.logger
|
42
|
+
# you can do something custom when processor rescues and exception
|
43
|
+
processor.on_error = proc { |e| ErrorMailer.notify(e).deliver_later }
|
44
|
+
end
|
45
|
+
|
46
|
+
# we keep configuration is separate file because both processor and collector should require it.
|
47
|
+
require 'pgq_prometheus_config' # @see lib/pgq_prometheus_configure.rb
|
48
|
+
|
49
|
+
# Will start thread which will collect pgq metrics every 30 seconds.
|
50
|
+
PgqPrometheus::Processor.start(frequency: 30)
|
51
|
+
```
|
52
|
+
|
53
|
+
### lib/pgq_prometheus_config.rb
|
54
|
+
```ruby
|
55
|
+
# We will keep metrics configuration here
|
56
|
+
|
57
|
+
PgqPrometheus.configure do |config|
|
58
|
+
# these are default metrics - no need to define them manually
|
59
|
+
config.register_gauge :new_events, 'new events qty for queue',
|
60
|
+
from: :queue, column: :ev_new
|
61
|
+
|
62
|
+
config.register_gauge :events_per_second, 'new events qty for queue',
|
63
|
+
from: :queue, column: :ev_per_sec
|
64
|
+
|
65
|
+
config.register_gauge :pending_events, 'pending events qty for queue and consumer',
|
66
|
+
from: :consumer
|
67
|
+
|
68
|
+
# you can define custom metrics
|
69
|
+
config.register_histogram :new_events_hist, 'PGQ new events histogram',
|
70
|
+
from: :queue,
|
71
|
+
labels: { custom_label_name: 'qwe' },
|
72
|
+
buckets: [1_000, 100, 50, 10, 1, 0]
|
73
|
+
config.register_counter :custom_queue_metric, 'something custom for queue',
|
74
|
+
from: :queue,
|
75
|
+
apply: proc { |queue_info| CustomQueueMetric.call queue_info[:queue_name] }
|
76
|
+
config.register_counter :custom_consumer_metric, 'something custom',
|
77
|
+
from: :queue,
|
78
|
+
apply: proc { |consumer_info, queue_info| CustomConsumerMetric.call(queue_info, consumer_info) }
|
79
|
+
|
80
|
+
# and remove metrics (event default ones)
|
81
|
+
config.unregister_metric :new_events_hist
|
82
|
+
|
83
|
+
# and override type
|
84
|
+
config.type = 'postresql_queue'
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
### lib/prometheus_collectors.rb
|
89
|
+
```ruby
|
90
|
+
# Require all custom prometheus collectors here
|
91
|
+
|
92
|
+
require 'pgq_prometheus'
|
93
|
+
require 'pgq_prometheus/collector'
|
94
|
+
require_relative 'pgq_prometheus_config'
|
95
|
+
```
|
96
|
+
|
97
|
+
$ bundle exec prometheus_exporter -a /path/to/mypoject/lib/prometheus_collectors.rb
|
98
|
+
$ rails s
|
99
|
+
|
100
|
+
## Development
|
101
|
+
|
102
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
103
|
+
|
104
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/senid231/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/senid231/pgq_prometheus/blob/master/CODE_OF_CONDUCT.md).
|
109
|
+
|
110
|
+
|
111
|
+
## License
|
112
|
+
|
113
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
114
|
+
|
115
|
+
## Code of Conduct
|
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/senid231/pgq_prometheus/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'pgq_prometheus'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative 'config'
|
2
|
+
require 'prometheus_exporter/server'
|
3
|
+
|
4
|
+
module PgqPrometheus
|
5
|
+
class Collector < PrometheusExporter::Server::TypeCollector
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@data = []
|
9
|
+
@observers = {}
|
10
|
+
|
11
|
+
Config._metrics.each do |name, opts|
|
12
|
+
metric_class = Kernel.const_get opts[:metric_class].to_s
|
13
|
+
help = opts[:help]
|
14
|
+
metric_args = opts[:metric_args]
|
15
|
+
@observers[name] = metric_class.new("#{type}_#{name}", help, *metric_args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def type
|
20
|
+
Config.type
|
21
|
+
end
|
22
|
+
|
23
|
+
def metrics
|
24
|
+
return [] if @data.length == 0
|
25
|
+
|
26
|
+
metrics = {}
|
27
|
+
|
28
|
+
@data.map do |obj|
|
29
|
+
labels = {}
|
30
|
+
# labels are passed by PgqPrometheus::Processor
|
31
|
+
labels.merge!(obj['metric_labels']) if obj['metric_labels']
|
32
|
+
# custom_labels are passed by PrometheusExporter::Client
|
33
|
+
labels.merge!(obj['custom_labels']) if obj['custom_labels']
|
34
|
+
|
35
|
+
@observers.each do |name, observer|
|
36
|
+
name = name.to_s
|
37
|
+
value = obj[name]
|
38
|
+
if value
|
39
|
+
observer.observe(value, labels)
|
40
|
+
metrics[name] = observer
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
metrics.values
|
46
|
+
end
|
47
|
+
|
48
|
+
def collect(obj)
|
49
|
+
@data << obj
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'prometheus_exporter/metric'
|
2
|
+
|
3
|
+
module PgqPrometheus
|
4
|
+
module Config
|
5
|
+
ALLOWED_FROM = [:queue, :consumer, nil]
|
6
|
+
|
7
|
+
class << self
|
8
|
+
attr_accessor :type, :_metrics
|
9
|
+
end
|
10
|
+
|
11
|
+
# @param metric_class [Class<Object>, String]
|
12
|
+
# @param name [Symbol, String]
|
13
|
+
# @param help [String]
|
14
|
+
# @param options [String] keys :from, :column, :apply
|
15
|
+
# @raise ArgumentError
|
16
|
+
def self.register_metric(metric_class, name, help, options = {})
|
17
|
+
raise ArgumentError, 'metric_class must be present' if metric_class.nil?
|
18
|
+
|
19
|
+
name = name.to_sym
|
20
|
+
raise ArgumentError, "metric #{name} already defined - unregister it" if _metrics.key?(name)
|
21
|
+
|
22
|
+
from = options[:from]
|
23
|
+
column = options[:column] || name
|
24
|
+
apply = options[:apply]
|
25
|
+
|
26
|
+
unless ALLOWED_FROM.include?(from)
|
27
|
+
raise ArgumentError, "invalid :from, allowed: #{ALLOWED_FROM.map(&:inspect).join(', ')}"
|
28
|
+
end
|
29
|
+
|
30
|
+
if apply.nil?
|
31
|
+
case from
|
32
|
+
when :queue
|
33
|
+
apply = proc { |queue_info| queue_info[column.to_sym] }
|
34
|
+
when :consumer
|
35
|
+
apply = proc { |consumer_info, _queue_info| consumer_info[column.to_sym] }
|
36
|
+
else
|
37
|
+
raise ArgumentError, 'require :apply block for metric without :from'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
_metrics[name] = {
|
42
|
+
metric_class: metric_class,
|
43
|
+
help: help,
|
44
|
+
metric_args: options[:metric_args] || [],
|
45
|
+
labels: options[:labels] || {},
|
46
|
+
from: from,
|
47
|
+
apply: apply
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
# @param name [Symbol, String]
|
52
|
+
def self.unregister_metric(name)
|
53
|
+
_metrics.delete(name.to_sym)
|
54
|
+
end
|
55
|
+
|
56
|
+
# @param name [Symbol, String]
|
57
|
+
# @param help [String]
|
58
|
+
# @param options [String] keys :from, :column, :apply
|
59
|
+
# @raise ArgumentError
|
60
|
+
def self.register_counter(name, help, options = {})
|
61
|
+
register_metric 'PrometheusExporter::Metric::Counter', name, help, options
|
62
|
+
end
|
63
|
+
|
64
|
+
# @param name [Symbol, String]
|
65
|
+
# @param help [String]
|
66
|
+
# @param options [String] keys :from, :column, :apply
|
67
|
+
# @raise ArgumentError
|
68
|
+
def self.register_gauge(name, help, options = {})
|
69
|
+
register_metric 'PrometheusExporter::Metric::Gauge', name, help, options
|
70
|
+
end
|
71
|
+
|
72
|
+
# @param name [Symbol, String]
|
73
|
+
# @param help [String]
|
74
|
+
# @param options [String] keys :from, :column, :apply
|
75
|
+
# @raise ArgumentError
|
76
|
+
def self.register_histogram(name, help, options = {})
|
77
|
+
buckets = options.delete(:buckets)
|
78
|
+
options[:metric_args] ||= [buckets: buckets]
|
79
|
+
register_metric 'PrometheusExporter::Metric::Histogram', name, help, options
|
80
|
+
end
|
81
|
+
|
82
|
+
# @param name [Symbol, String]
|
83
|
+
# @param help [String]
|
84
|
+
# @param options [String] keys :from, :column, :apply
|
85
|
+
# @raise ArgumentError
|
86
|
+
def self.register_summary(name, help, options = {})
|
87
|
+
buckets = options.delete(:quantiles)
|
88
|
+
options[:metric_args] ||= [quantiles: buckets]
|
89
|
+
register_metric 'PrometheusExporter::Metric::Summary', name, help, options
|
90
|
+
end
|
91
|
+
|
92
|
+
self.type = 'pgq'
|
93
|
+
self._metrics = {}
|
94
|
+
|
95
|
+
register_gauge :new_events, 'new events qty for queue',
|
96
|
+
from: :queue, column: :ev_new
|
97
|
+
|
98
|
+
register_gauge :events_per_second, 'new events qty for queue',
|
99
|
+
from: :queue,
|
100
|
+
apply: proc { |queue_info| queue_info[:ev_per_sec] || 0.0 }
|
101
|
+
|
102
|
+
register_gauge :pending_events, 'pending events qty for queue and consumer',
|
103
|
+
from: :consumer
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'prometheus_exporter/client'
|
2
|
+
|
3
|
+
module PgqPrometheus
|
4
|
+
class Processor
|
5
|
+
class << self
|
6
|
+
attr_accessor :sql_caller, :logger, :on_error
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.start(client: nil, frequency: 30, labels: nil)
|
10
|
+
raise ArgumentError, "#{name}.sql_caller must be defined" if sql_caller.nil?
|
11
|
+
|
12
|
+
client ||= PrometheusExporter::Client.default
|
13
|
+
metric_labels = labels&.dup || {}
|
14
|
+
process_collector = new(metric_labels)
|
15
|
+
|
16
|
+
stop if running?
|
17
|
+
|
18
|
+
@thread = Thread.new do
|
19
|
+
while true
|
20
|
+
begin
|
21
|
+
metrics = process_collector.collect
|
22
|
+
metrics.each do |metric|
|
23
|
+
client.send_json metric
|
24
|
+
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
|
+
end
|
30
|
+
sleep frequency
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.stop
|
38
|
+
return unless running?
|
39
|
+
|
40
|
+
@thread.kill
|
41
|
+
@thread = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.running?
|
45
|
+
defined?(@thread) && @thread
|
46
|
+
end
|
47
|
+
|
48
|
+
def initialize(labels = {})
|
49
|
+
@metric_labels = labels || {}
|
50
|
+
end
|
51
|
+
|
52
|
+
def collect
|
53
|
+
metrics = []
|
54
|
+
|
55
|
+
within_logger_tags(self.class.name) do
|
56
|
+
|
57
|
+
self.class.sql_caller.queue_info.each do |queue_info|
|
58
|
+
queue = queue_info[:queue_name]
|
59
|
+
|
60
|
+
queue_metric_opts.each do |name, opts|
|
61
|
+
value = opts[:apply].call(queue_info)
|
62
|
+
labels = opts[:labels].merge(queue: queue)
|
63
|
+
metrics << format_metric(name, value, labels)
|
64
|
+
end
|
65
|
+
|
66
|
+
self.class.sql_caller.consumer_info(queue).each do |consumer_info|
|
67
|
+
consumer = consumer_info[:consumer_name]
|
68
|
+
|
69
|
+
consumer_metric_opts.each do |name, opts|
|
70
|
+
value = opts[:apply].call(consumer_info, queue_info)
|
71
|
+
labels = opts[:labels].merge(queue: queue, consumer: consumer)
|
72
|
+
metrics << format_metric(name, value, labels)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
custom_metric_opts.each do |name, opts|
|
78
|
+
value, labels = opts[:apply].call
|
79
|
+
labels = (labels || {}).merge(opts[:labels])
|
80
|
+
metrics << format_metric(name, value, labels)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
metrics
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def logger
|
90
|
+
self.class.logger
|
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
|
99
|
+
end
|
100
|
+
|
101
|
+
def queue_metric_opts
|
102
|
+
Config._metrics.select { |_, opts| opts[:from] == :queue }
|
103
|
+
end
|
104
|
+
|
105
|
+
def consumer_metric_opts
|
106
|
+
Config._metrics.select { |_, opts| opts[:from] == :consumer }
|
107
|
+
end
|
108
|
+
|
109
|
+
def custom_metric_opts
|
110
|
+
Config._metrics.select { |_, opts| opts[:from].nil? }
|
111
|
+
end
|
112
|
+
|
113
|
+
def format_metric(name, value, labels)
|
114
|
+
{
|
115
|
+
type: Config.type,
|
116
|
+
name => value,
|
117
|
+
metric_labels: labels.merge(@metric_labels)
|
118
|
+
}
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module PgqPrometheus
|
2
|
+
module SqlCaller
|
3
|
+
class ActiveRecord
|
4
|
+
# Simple Sql caller for active record.
|
5
|
+
# Usage:
|
6
|
+
#
|
7
|
+
# require 'pgq_prometheus'
|
8
|
+
# require 'pgq_prometheus/sql_caller/active_record'
|
9
|
+
# PgqPrometheus::Processor.sql_caller = PgqPrometheus::SqlCaller::ActiveRecord.new('ApplicationRecord')
|
10
|
+
|
11
|
+
# @param model_class_name [Class<Object>,String] class or class name
|
12
|
+
def initialize(model_class_name)
|
13
|
+
@model_class_name = model_class_name.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns pgq.get_queue_info for one or all queues.
|
17
|
+
# @param queue [String, nil] queue name
|
18
|
+
# @return [Hash<Symbol>] when queue is present.
|
19
|
+
# @return [Array<Hash<Symbol>>] when queue is nil.
|
20
|
+
def queue_info(queue = nil)
|
21
|
+
if queue
|
22
|
+
select_hashes('SELECT * FROM pgq.get_queue_info(?)', queue.to_s).first
|
23
|
+
else
|
24
|
+
select_hashes('SELECT * FROM pgq.get_queue_info()')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns pgq.get_consumer_info for one or all consumers of queue.
|
29
|
+
# @param queue [String] queue name
|
30
|
+
# @param consumer [String, nil] consumer name
|
31
|
+
# @return [Hash<Symbol>] when queue is present.
|
32
|
+
# @return [Array<Hash<Symbol>>] when queue is nil.
|
33
|
+
def consumer_info(queue, consumer = nil)
|
34
|
+
if consumer
|
35
|
+
select_hashes('SELECT * FROM pgq.get_consumer_info(?, ?)', queue.to_s, consumer.to_s).first
|
36
|
+
else
|
37
|
+
select_hashes('SELECT * FROM pgq.get_consumer_info(?)', queue.to_s)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def model_class
|
44
|
+
@model_class ||= Kernel.const_get(@model_class_name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def select_hashes(sql, *bindings)
|
48
|
+
sql = model_class.send :sanitize_sql_array, bindings.unshift(sql) unless bindings.empty?
|
49
|
+
result = model_class.connection.select_all(sql)
|
50
|
+
result.map do |row|
|
51
|
+
row.map { |k, v| [k.to_sym, result.column_types[k].deserialize(v)] }.to_h
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/pgq_prometheus/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'pgq_prometheus'
|
7
|
+
spec.version = PgqPrometheus::VERSION
|
8
|
+
spec.authors = ['Denis Talakevich']
|
9
|
+
spec.email = ['senid231@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Prometheus metrics for PGQ postgres extension'
|
12
|
+
spec.description = 'Prometheus metrics for PGQ postgres extension'
|
13
|
+
spec.homepage = 'https://github.com/senid231/pgq_prometheus'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
19
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.add_dependency 'prometheus_exporter'
|
31
|
+
|
32
|
+
spec.add_development_dependency 'rubocop'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pgq_prometheus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denis Talakevich
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: prometheus_exporter
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Prometheus metrics for PGQ postgres extension
|
42
|
+
email:
|
43
|
+
- senid231@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- ".travis.yml"
|
51
|
+
- CODE_OF_CONDUCT.md
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/pgq_prometheus.rb
|
59
|
+
- lib/pgq_prometheus/collector.rb
|
60
|
+
- lib/pgq_prometheus/config.rb
|
61
|
+
- lib/pgq_prometheus/processor.rb
|
62
|
+
- lib/pgq_prometheus/sql_caller/active_record.rb
|
63
|
+
- lib/pgq_prometheus/version.rb
|
64
|
+
- pgq_prometheus_metrics.gemspec
|
65
|
+
homepage: https://github.com/senid231/pgq_prometheus
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata:
|
69
|
+
homepage_uri: https://github.com/senid231/pgq_prometheus
|
70
|
+
source_code_uri: https://github.com/senid231/pgq_prometheus
|
71
|
+
changelog_uri: https://github.com/senid231/pgq_prometheus
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 2.3.0
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubygems_version: 3.1.2
|
88
|
+
signing_key:
|
89
|
+
specification_version: 4
|
90
|
+
summary: Prometheus metrics for PGQ postgres extension
|
91
|
+
test_files: []
|