pgq_prometheus 0.2.3 → 0.2.5

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: 111af82c3aa1cbd83e1f176802001cb2c7b79daaa3ae7f327b72b96f362d6de2
4
- data.tar.gz: ec0fc1d7f8f653ec2bedd062bb293807182af62b915162130dfe81724d551357
3
+ metadata.gz: 2ee74de1c1b67ad26f4b845c6f0b9a1d0404d848b7e20e29d34397ed3498bd57
4
+ data.tar.gz: d3c354ed2728b3bdaacfa56bbbeace43ad25e1c1a24d9bec1a9c7aea941aafd5
5
5
  SHA512:
6
- metadata.gz: 737f11851a5781a25f42f3b702afcd27a88e2d5bbec7d8d389124995b99f99e82e8b0aafbb39e28bff155b12ce12565e02140e354b537e61eb97689573404373
7
- data.tar.gz: 437da382c3917b97f065332c52c7d12169f2ea05832fb45e818558ac224e89ebca18095a6b250e584b3aed5fc3f54f6c16985c952281f704265ce02094e59122
6
+ metadata.gz: 07a8614b15adaa4878a33c6bd6542a6ce570c37a456655648a61300d7e7b579613af9516fb96072e4c51a56490ea6fd16ff15b9a4421e6bca3b1f004f8cdf814
7
+ data.tar.gz: a2d1edc34a27fb5156c7d2d0fd70ca30a24714bbbe39d5ed424efcd7b12cfaf18cfec946644bc6da63b7e17d0a51dde84e598835726781104409ecebd7f53e6c
data/Gemfile CHANGED
@@ -6,4 +6,4 @@ source 'https://rubygems.org'
6
6
  gemspec
7
7
 
8
8
  gem 'minitest', '~> 5.0'
9
- gem 'rake', '~> 12.0'
9
+ gem 'rake', '~> 13.0'
@@ -8,6 +8,7 @@ module PgqPrometheus
8
8
  MAX_METRIC_AGE = 30
9
9
 
10
10
  def initialize
11
+ super
11
12
  @data = []
12
13
  @observers = {}
13
14
 
@@ -30,11 +31,7 @@ module PgqPrometheus
30
31
  metrics = {}
31
32
 
32
33
  @data.map do |obj|
33
- labels = {}
34
- # labels are passed by PgqPrometheus::Processor
35
- labels.merge!(obj['metric_labels']) if obj['metric_labels']
36
- # custom_labels are passed by PrometheusExporter::Client
37
- labels.merge!(obj['custom_labels']) if obj['custom_labels']
34
+ labels = gather_labels(obj)
38
35
 
39
36
  @observers.each do |name, observer|
40
37
  name = name.to_s
@@ -56,5 +53,16 @@ module PgqPrometheus
56
53
  @data.delete_if { |m| m['created_at'] + MAX_METRIC_AGE < now }
57
54
  @data << obj
58
55
  end
56
+
57
+ private
58
+
59
+ def gather_labels(obj)
60
+ labels = {}
61
+ # labels are passed by PgqPrometheus::Processor
62
+ labels.merge!(obj['metric_labels']) if obj['metric_labels']
63
+ # custom_labels are passed by PrometheusExporter::Client
64
+ labels.merge!(obj['custom_labels']) if obj['custom_labels']
65
+ labels.to_h { |key, value| [key.to_s, value.to_s] }
66
+ end
59
67
  end
60
68
  end
@@ -4,7 +4,7 @@ require 'prometheus_exporter/metric'
4
4
 
5
5
  module PgqPrometheus
6
6
  module Config
7
- ALLOWED_FROM = [:queue, :consumer, nil]
7
+ ALLOWED_FROM = [:queue, :consumer, nil].freeze
8
8
 
9
9
  class << self
10
10
  attr_accessor :type, :_metrics
@@ -41,12 +41,12 @@ module PgqPrometheus
41
41
  end
42
42
 
43
43
  _metrics[name] = {
44
- metric_class: metric_class,
45
- help: help,
46
- metric_args: options[:metric_args] || [],
47
- labels: options[:labels] || {},
48
- from: from,
49
- apply: apply
44
+ metric_class: metric_class,
45
+ help: help,
46
+ metric_args: options[:metric_args] || [],
47
+ labels: options[:labels] || {},
48
+ from: from,
49
+ apply: apply
50
50
  }
51
51
  end
52
52
 
@@ -25,18 +25,7 @@ module PgqPrometheus
25
25
  sql_caller.release_connection
26
26
  logger&.info { "Start #{name}" }
27
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
28
+ run_once(process_collector:, client:)
40
29
  sleep frequency
41
30
  end
42
31
  end
@@ -45,6 +34,21 @@ module PgqPrometheus
45
34
  true
46
35
  end
47
36
 
37
+ def run_once(process_collector:, client:)
38
+ wrap_execution do
39
+ before_collect&.call
40
+ metrics = process_collector.collect
41
+ metrics.each do |metric|
42
+ client.send_json metric
43
+ end
44
+ after_collect&.call
45
+ rescue StandardError => e
46
+ warn "#{self.class} Failed To Collect Stats #{e.class} #{e.message}"
47
+ logger&.error { "#{e.class} #{e.message} #{e.backtrace.join("\n")}" }
48
+ on_error&.call(e)
49
+ end
50
+ end
51
+
48
52
  def stop
49
53
  @thread&.kill
50
54
  @thread = nil
@@ -54,10 +58,20 @@ module PgqPrometheus
54
58
  defined?(@thread) && @thread
55
59
  end
56
60
 
57
- def wrap_thread_loop(*tags)
61
+ def wrap_thread_loop(*tags, &block)
58
62
  return yield if logger.nil? || !logger.respond_to?(:tagged)
59
63
 
60
- logger.tagged(*tags) { yield }
64
+ logger.tagged(*tags, &block)
65
+ end
66
+
67
+ def wrap_execution(&)
68
+ if defined?(Rails) && Rails.application
69
+ # When run inside Rails, this is a correct way to wrap app code
70
+ Rails.application.reloader.wrap(&)
71
+ else
72
+ # When using just ActiveRecord, with_connection will be enough
73
+ sql_caller.with_connection(&)
74
+ end
61
75
  end
62
76
  end
63
77
 
@@ -67,33 +81,30 @@ module PgqPrometheus
67
81
 
68
82
  def collect
69
83
  metrics = []
70
- sql_caller.with_connection do
84
+ sql_caller.queue_info.each do |queue_info|
85
+ queue = queue_info[:queue_name]
71
86
 
72
- sql_caller.queue_info.each do |queue_info|
73
- queue = queue_info[:queue_name]
74
-
75
- queue_metric_opts.each do |name, opts|
76
- value = opts[:apply].call(queue_info)
77
- labels = opts[:labels].merge(queue: queue)
78
- metrics << format_metric(name, value, labels)
79
- end
87
+ queue_metric_opts.each do |name, opts|
88
+ value = opts[:apply].call(queue_info)
89
+ labels = opts[:labels].merge(queue: queue)
90
+ metrics << format_metric(name, value, labels)
91
+ end
80
92
 
81
- sql_caller.consumer_info(queue).each do |consumer_info|
82
- consumer = consumer_info[:consumer_name]
93
+ sql_caller.consumer_info(queue).each do |consumer_info|
94
+ consumer = consumer_info[:consumer_name]
83
95
 
84
- consumer_metric_opts.each do |name, opts|
85
- value = opts[:apply].call(consumer_info, queue_info)
86
- labels = opts[:labels].merge(queue: queue, consumer: consumer)
87
- metrics << format_metric(name, value, labels)
88
- end
96
+ consumer_metric_opts.each do |name, opts|
97
+ value = opts[:apply].call(consumer_info, queue_info)
98
+ labels = opts[:labels].merge(queue: queue, consumer: consumer)
99
+ metrics << format_metric(name, value, labels)
89
100
  end
90
101
  end
102
+ end
91
103
 
92
- custom_metric_opts.each do |name, opts|
93
- value, labels = opts[:apply].call
94
- labels = (labels || {}).merge(opts[:labels])
95
- metrics << format_metric(name, value, labels)
96
- end
104
+ custom_metric_opts.each do |name, opts|
105
+ value, labels = opts[:apply].call
106
+ labels = (labels || {}).merge(opts[:labels])
107
+ metrics << format_metric(name, value, labels)
97
108
  end
98
109
 
99
110
  metrics
@@ -119,9 +130,9 @@ module PgqPrometheus
119
130
 
120
131
  def format_metric(name, value, labels)
121
132
  {
122
- type: Config.type,
123
- name => value,
124
- metric_labels: labels.merge(@metric_labels)
133
+ type: Config.type,
134
+ name => value,
135
+ metric_labels: labels.merge(@metric_labels)
125
136
  }
126
137
  end
127
138
  end
@@ -49,8 +49,8 @@ module PgqPrometheus
49
49
  # Acquires pg connection during block execution.
50
50
  # Release it after block executed.
51
51
  # @yield
52
- def with_connection
53
- model_class.connection_pool.with_connection { yield }
52
+ def with_connection(&)
53
+ model_class.connection_pool.with_connection(&)
54
54
  end
55
55
 
56
56
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgqPrometheus
4
- VERSION = '0.2.3'
4
+ VERSION = '0.2.5'
5
5
  end
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.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-07 00:00:00.000000000 Z
11
+ date: 2026-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prometheus_exporter
@@ -69,7 +69,7 @@ metadata:
69
69
  homepage_uri: https://github.com/didww/pgq_prometheus
70
70
  source_code_uri: https://github.com/didww/pgq_prometheus
71
71
  changelog_uri: https://github.com/didww/pgq_prometheus
72
- post_install_message:
72
+ post_install_message:
73
73
  rdoc_options: []
74
74
  require_paths:
75
75
  - lib
@@ -84,8 +84,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  requirements: []
87
- rubygems_version: 3.0.9
88
- signing_key:
87
+ rubygems_version: 3.5.6
88
+ signing_key:
89
89
  specification_version: 4
90
90
  summary: Prometheus metrics for PGQ postgres extension
91
91
  test_files: []