pgbus 0.9.5 → 0.9.7

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.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +56 -3
  3. data/app/controllers/pgbus/dead_letter_controller.rb +5 -1
  4. data/app/helpers/pgbus/application_helper.rb +8 -6
  5. data/app/views/pgbus/dashboard/_recent_failures.html.erb +1 -1
  6. data/app/views/pgbus/dashboard/_stats_cards.html.erb +1 -0
  7. data/app/views/pgbus/dead_letter/_messages_table.html.erb +33 -1
  8. data/app/views/pgbus/insights/show.html.erb +1 -1
  9. data/app/views/pgbus/jobs/_failed_table.html.erb +1 -1
  10. data/app/views/pgbus/jobs/show.html.erb +1 -1
  11. data/app/views/pgbus/queues/show.html.erb +3 -0
  12. data/config/locales/da.yml +7 -0
  13. data/config/locales/de.yml +7 -0
  14. data/config/locales/en.yml +7 -0
  15. data/config/locales/es.yml +7 -0
  16. data/config/locales/fi.yml +7 -0
  17. data/config/locales/fr.yml +7 -0
  18. data/config/locales/it.yml +7 -0
  19. data/config/locales/ja.yml +7 -0
  20. data/config/locales/nb.yml +7 -0
  21. data/config/locales/nl.yml +7 -0
  22. data/config/locales/pt.yml +7 -0
  23. data/config/locales/sv.yml +7 -0
  24. data/lib/pgbus/configuration.rb +5 -2
  25. data/lib/pgbus/integrations/appsignal/dashboard.json +353 -0
  26. data/lib/pgbus/integrations/appsignal/dashboards/pgbus_health.json +224 -85
  27. data/lib/pgbus/integrations/appsignal/dashboards/pgbus_streams.json +154 -63
  28. data/lib/pgbus/integrations/appsignal/dashboards/pgbus_throughput.json +218 -79
  29. data/lib/pgbus/integrations/appsignal/probe.rb +17 -2
  30. data/lib/pgbus/integrations/appsignal.rb +17 -0
  31. data/lib/pgbus/version.rb +1 -1
  32. data/lib/pgbus/web/data_source.rb +21 -0
  33. data/lib/pgbus/web/payload_filter.rb +68 -0
  34. metadata +3 -1
@@ -1,81 +1,220 @@
1
1
  {
2
- "title": "Pgbus — Throughput & Latency",
3
- "description": "Job and event throughput, perform-duration percentiles, and PGMQ send/read counts. Drives the most common 'is the worker keeping up?' question.",
4
- "graphs": [
5
- {
6
- "title": "Jobs processed per minute",
7
- "description": "Successful, failed, and dead-lettered jobs. A spike in failed without a spike in processed usually means a deploy regression.",
8
- "line_label": "%queue %job_class %status",
9
- "format": "number",
10
- "format_input": null,
11
- "kind": "timeseries",
12
- "draw_null_as_zero": true,
13
- "metrics": [
14
- { "name": "pgbus_queue_job_count", "fields": ["COUNTER"], "tags": [] }
15
- ]
16
- },
17
- {
18
- "title": "Job perform duration (ms)",
19
- "description": "Distribution of how long perform_now takes per job class. P95 and P99 are the lines to watch.",
20
- "line_label": "%job_class",
21
- "format": "duration",
22
- "format_input": "millisecond",
23
- "kind": "timeseries",
24
- "metrics": [
25
- { "name": "pgbus_job_duration_ms", "fields": ["MEAN", "P95", "P99"], "tags": [] }
26
- ]
27
- },
28
- {
29
- "title": "Events processed per minute",
30
- "description": "Event-bus handler invocations grouped by routing key.",
31
- "line_label": "%routing_key %handler %status",
32
- "format": "number",
33
- "kind": "timeseries",
34
- "draw_null_as_zero": true,
35
- "metrics": [
36
- { "name": "pgbus_event_count", "fields": ["COUNTER"], "tags": [] }
37
- ]
38
- },
39
- {
40
- "title": "Event handler duration (ms)",
41
- "line_label": "%handler",
42
- "format": "duration",
43
- "format_input": "millisecond",
44
- "kind": "timeseries",
45
- "metrics": [
46
- { "name": "pgbus_event_duration_ms", "fields": ["MEAN", "P95", "P99"], "tags": [] }
47
- ]
48
- },
49
- {
50
- "title": "PGMQ messages sent (per minute)",
51
- "line_label": "%queue",
52
- "format": "number",
53
- "kind": "timeseries",
54
- "draw_null_as_zero": true,
55
- "metrics": [
56
- { "name": "pgbus_messages_sent", "fields": ["COUNTER"], "tags": [] }
57
- ]
58
- },
59
- {
60
- "title": "Send duration (ms)",
61
- "line_label": "%queue",
62
- "format": "duration",
63
- "format_input": "millisecond",
64
- "kind": "timeseries",
65
- "metrics": [
66
- { "name": "pgbus_send_duration_ms", "fields": ["MEAN", "P95", "P99"], "tags": [] }
67
- ]
68
- },
69
- {
70
- "title": "PGMQ messages read (per minute)",
71
- "description": "Messages fetched from queues by workers. Compare against 'sent' to spot backlog growth.",
72
- "line_label": "%queue",
73
- "format": "number",
74
- "kind": "timeseries",
75
- "draw_null_as_zero": true,
76
- "metrics": [
77
- { "name": "pgbus_messages_read", "fields": ["COUNTER"], "tags": [] }
78
- ]
79
- }
80
- ]
2
+ "metric_keys": [
3
+ "pgbus_queue_job_count",
4
+ "pgbus_event_count",
5
+ "pgbus_messages_sent",
6
+ "pgbus_messages_read"
7
+ ],
8
+ "dashboard": {
9
+ "title": "Pgbus — Throughput & Latency",
10
+ "description": "Job and event throughput, perform-duration percentiles, and PGMQ send/read counts.\nDrives the most common 'is the worker keeping up?' question.\n",
11
+ "visuals": [
12
+ {
13
+ "type": "timeseries",
14
+ "display": "LINE",
15
+ "title": "Jobs processed per minute",
16
+ "description": "Successful, failed, and dead-lettered jobs. A spike in failed without a spike in processed usually means a deploy regression.",
17
+ "line_label": "%queue% - %job_class% - %status%",
18
+ "format": "number",
19
+ "draw_null_as_zero": true,
20
+ "metrics": [
21
+ {
22
+ "name": "pgbus_queue_job_count",
23
+ "fields": [
24
+ {
25
+ "field": "COUNTER"
26
+ }
27
+ ],
28
+ "tags": [
29
+ {
30
+ "key": "queue",
31
+ "value": "*"
32
+ },
33
+ {
34
+ "key": "job_class",
35
+ "value": "*"
36
+ },
37
+ {
38
+ "key": "status",
39
+ "value": "*"
40
+ }
41
+ ]
42
+ }
43
+ ]
44
+ },
45
+ {
46
+ "type": "timeseries",
47
+ "display": "LINE",
48
+ "title": "Job perform duration (ms)",
49
+ "description": "Distribution of how long perform_now takes per job class. P95 and P99 are the lines to watch.",
50
+ "line_label": "%job_class%",
51
+ "format": "duration",
52
+ "draw_null_as_zero": false,
53
+ "metrics": [
54
+ {
55
+ "name": "pgbus_job_duration_ms",
56
+ "fields": [
57
+ {
58
+ "field": "MEAN"
59
+ },
60
+ {
61
+ "field": "P95"
62
+ },
63
+ {
64
+ "field": "P99"
65
+ }
66
+ ],
67
+ "tags": [
68
+ {
69
+ "key": "job_class",
70
+ "value": "*"
71
+ }
72
+ ]
73
+ }
74
+ ]
75
+ },
76
+ {
77
+ "type": "timeseries",
78
+ "display": "LINE",
79
+ "title": "Events processed per minute",
80
+ "description": "Event-bus handler invocations grouped by routing key.",
81
+ "line_label": "%routing_key% - %handler% - %status%",
82
+ "format": "number",
83
+ "draw_null_as_zero": true,
84
+ "metrics": [
85
+ {
86
+ "name": "pgbus_event_count",
87
+ "fields": [
88
+ {
89
+ "field": "COUNTER"
90
+ }
91
+ ],
92
+ "tags": [
93
+ {
94
+ "key": "routing_key",
95
+ "value": "*"
96
+ },
97
+ {
98
+ "key": "handler",
99
+ "value": "*"
100
+ },
101
+ {
102
+ "key": "status",
103
+ "value": "*"
104
+ }
105
+ ]
106
+ }
107
+ ]
108
+ },
109
+ {
110
+ "type": "timeseries",
111
+ "display": "LINE",
112
+ "title": "Event handler duration (ms)",
113
+ "line_label": "%handler%",
114
+ "format": "duration",
115
+ "draw_null_as_zero": false,
116
+ "metrics": [
117
+ {
118
+ "name": "pgbus_event_duration_ms",
119
+ "fields": [
120
+ {
121
+ "field": "MEAN"
122
+ },
123
+ {
124
+ "field": "P95"
125
+ },
126
+ {
127
+ "field": "P99"
128
+ }
129
+ ],
130
+ "tags": [
131
+ {
132
+ "key": "handler",
133
+ "value": "*"
134
+ }
135
+ ]
136
+ }
137
+ ]
138
+ },
139
+ {
140
+ "type": "timeseries",
141
+ "display": "LINE",
142
+ "title": "PGMQ messages sent (per minute)",
143
+ "line_label": "%queue%",
144
+ "format": "number",
145
+ "draw_null_as_zero": true,
146
+ "metrics": [
147
+ {
148
+ "name": "pgbus_messages_sent",
149
+ "fields": [
150
+ {
151
+ "field": "COUNTER"
152
+ }
153
+ ],
154
+ "tags": [
155
+ {
156
+ "key": "queue",
157
+ "value": "*"
158
+ }
159
+ ]
160
+ }
161
+ ]
162
+ },
163
+ {
164
+ "type": "timeseries",
165
+ "display": "LINE",
166
+ "title": "Send duration (ms)",
167
+ "line_label": "%queue%",
168
+ "format": "duration",
169
+ "draw_null_as_zero": false,
170
+ "metrics": [
171
+ {
172
+ "name": "pgbus_send_duration_ms",
173
+ "fields": [
174
+ {
175
+ "field": "MEAN"
176
+ },
177
+ {
178
+ "field": "P95"
179
+ },
180
+ {
181
+ "field": "P99"
182
+ }
183
+ ],
184
+ "tags": [
185
+ {
186
+ "key": "queue",
187
+ "value": "*"
188
+ }
189
+ ]
190
+ }
191
+ ]
192
+ },
193
+ {
194
+ "type": "timeseries",
195
+ "display": "LINE",
196
+ "title": "PGMQ messages read (per minute)",
197
+ "description": "Messages fetched from queues by workers. Compare against 'sent' to spot backlog growth.",
198
+ "line_label": "%queue%",
199
+ "format": "number",
200
+ "draw_null_as_zero": true,
201
+ "metrics": [
202
+ {
203
+ "name": "pgbus_messages_read",
204
+ "fields": [
205
+ {
206
+ "field": "COUNTER"
207
+ }
208
+ ],
209
+ "tags": [
210
+ {
211
+ "key": "queue",
212
+ "value": "*"
213
+ }
214
+ ]
215
+ }
216
+ ]
217
+ }
218
+ ]
219
+ }
81
220
  }
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "socket"
4
+
3
5
  module Pgbus
4
6
  module Integrations
5
7
  module Appsignal
@@ -10,6 +12,14 @@ module Pgbus
10
12
  # method rescues StandardError and returns a safe default — but we
11
13
  # still wrap each section in our own rescue so a probe iteration
12
14
  # never raises out into the AppSignal probe runner.
15
+ #
16
+ # Tagging policy: most pgbus metrics are cluster-wide (the queue
17
+ # depth in PostgreSQL is the same regardless of which host reads it),
18
+ # so cluster-wide gauges are emitted WITHOUT a hostname tag — every
19
+ # host sends the same value and AppSignal's last-write-wins
20
+ # semantics keep the dashboard correct. The only gauge that is
21
+ # genuinely per-host is `active_processes`, which the probe filters
22
+ # to this host before tagging.
13
23
  module Probe
14
24
  METRIC_PREFIX = "pgbus_"
15
25
  private_constant :METRIC_PREFIX
@@ -43,6 +53,7 @@ module Pgbus
43
53
  class Runner
44
54
  def initialize(data_source: nil)
45
55
  @data_source = data_source
56
+ @hostname = Socket.gethostname
46
57
  end
47
58
 
48
59
  def call
@@ -68,14 +79,18 @@ module Pgbus
68
79
  gauge "queue_visible_depth", q[:queue_visible_length], tags
69
80
  gauge "queue_paused", q[:paused] ? 1 : 0, tags
70
81
  age = q[:oldest_msg_age_sec]
71
- gauge "queue_oldest_message_age_seconds", age, tags if age
82
+ if age
83
+ gauge "queue_oldest_message_age_seconds", age, tags
84
+ gauge "queue_latency", age * 1_000, tags
85
+ end
72
86
  end
73
87
  rescue StandardError => e
74
88
  log_failure("queue metrics", e)
75
89
  end
76
90
 
77
91
  def track_processes
78
- gauge "active_processes", data_source.processes.count
92
+ local_count = data_source.processes.count { |p| p[:hostname] == @hostname }
93
+ gauge "active_processes", local_count, { hostname: @hostname }
79
94
  rescue StandardError => e
80
95
  log_failure("process metrics", e)
81
96
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
3
4
  require "pgbus/integrations/appsignal/subscriber"
4
5
  require "pgbus/integrations/appsignal/probe"
5
6
 
@@ -24,6 +25,9 @@ module Pgbus
24
25
  # All metric names are prefixed `pgbus_` so they group cleanly in
25
26
  # AppSignal's custom-metrics view.
26
27
  module Appsignal
28
+ DASHBOARD_PATH = File.expand_path("appsignal/dashboard.json", __dir__).freeze
29
+ DASHBOARDS_DIR = File.expand_path("appsignal/dashboards", __dir__).freeze
30
+
27
31
  module_function
28
32
 
29
33
  def install! # rubocop:disable Naming/PredicateMethod
@@ -41,11 +45,24 @@ module Pgbus
41
45
  @installed == true
42
46
  end
43
47
 
48
+ def dashboard_definition
49
+ @dashboard_definition ||= JSON.parse(File.read(DASHBOARD_PATH))
50
+ end
51
+
52
+ def dashboard_definitions
53
+ @dashboard_definitions ||=
54
+ Dir[File.join(DASHBOARDS_DIR, "*.json")].map do |path|
55
+ JSON.parse(File.read(path))
56
+ end
57
+ end
58
+
44
59
  # Test hook: tear everything down so a fresh install! can run.
45
60
  def reset!
46
61
  Subscriber.reset! if defined?(Subscriber)
47
62
  Probe.reset! if defined?(Probe)
48
63
  @installed = false
64
+ @dashboard_definition = nil
65
+ @dashboard_definitions = nil
49
66
  end
50
67
  end
51
68
  end
data/lib/pgbus/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pgbus
4
- VERSION = "0.9.5"
4
+ VERSION = "0.9.7"
5
5
  end
@@ -41,7 +41,17 @@ module Pgbus
41
41
  # Queues — query via ActiveRecord for reliability in web processes
42
42
  # (avoids PGMQ client connection issues when the web server uses a
43
43
  # different connection lifecycle than the worker processes).
44
+ # Memoized for the lifetime of this data-source instance (one per web
45
+ # request — see ApplicationController#data_source). A single page can ask
46
+ # for queue metrics more than once (e.g. the DLQ page reads both the
47
+ # message rows and the total count); without the memo each call re-runs
48
+ # the meta query + batched metrics. Mutations redirect to a fresh request
49
+ # with a new instance, so a per-request memo never serves stale data.
44
50
  def queues_with_metrics
51
+ @queues_with_metrics ||= fetch_queues_with_metrics
52
+ end
53
+
54
+ def fetch_queues_with_metrics
45
55
  queue_names = connection.select_values("SELECT queue_name FROM pgmq.meta ORDER BY queue_name")
46
56
  return [] if queue_names.empty?
47
57
 
@@ -53,6 +63,7 @@ module Pgbus
53
63
  Pgbus.logger.error { "[Pgbus::Web] Error fetching queue metrics: #{e.class}: #{e.message}" }
54
64
  []
55
65
  end
66
+ private :fetch_queues_with_metrics
56
67
 
57
68
  # name is the full PGMQ queue name (e.g. "pgbus_default") as returned
58
69
  # by queues_with_metrics. No prefix is added.
@@ -276,6 +287,16 @@ module Pgbus
276
287
  []
277
288
  end
278
289
 
290
+ def dlq_total_count
291
+ dlq_suffix = Pgbus::DEAD_LETTER_SUFFIX
292
+ queues_with_metrics
293
+ .select { |q| q[:name].end_with?(dlq_suffix) }
294
+ .sum { |q| q[:queue_length] }
295
+ rescue StandardError => e
296
+ Pgbus.logger.debug { "[Pgbus::Web] Error fetching DLQ count: #{e.message}" }
297
+ 0
298
+ end
299
+
279
300
  def dlq_message_detail(msg_id)
280
301
  dlq_suffix = Pgbus::DEAD_LETTER_SUFFIX
281
302
  queues = queues_with_metrics.select { |q| q[:name].end_with?(dlq_suffix) }
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Pgbus
6
+ module Web
7
+ module PayloadFilter
8
+ module_function
9
+
10
+ DEFAULT_FILTER_PATTERNS = [
11
+ :password, :passphrase, :secret, :token, :authorization,
12
+ :api_key, :private_key, :access_key, :secret_key,
13
+ /(_|-)key$/
14
+ ].freeze
15
+
16
+ FILTERED = "[FILTERED]"
17
+
18
+ def filter(value)
19
+ return value unless Pgbus.configuration.dashboard_filter_sensitive
20
+
21
+ case value
22
+ when Hash
23
+ parameter_filter.filter(value)
24
+ when Array
25
+ value.map { |element| filter(element) }
26
+ else
27
+ value
28
+ end
29
+ end
30
+
31
+ def filter_json(value)
32
+ return value unless Pgbus.configuration.dashboard_filter_sensitive
33
+
34
+ case value
35
+ when nil then nil
36
+ when Hash, Array
37
+ JSON.generate(filter(value))
38
+ when String
39
+ parsed = JSON.parse(value)
40
+ JSON.generate(filter(parsed))
41
+ else
42
+ value
43
+ end
44
+ rescue JSON::ParserError
45
+ value
46
+ end
47
+
48
+ def parameter_filter
49
+ patterns = Pgbus.configuration.dashboard_filter_parameters ||
50
+ rails_filter_parameters ||
51
+ DEFAULT_FILTER_PATTERNS
52
+ ActiveSupport::ParameterFilter.new(patterns)
53
+ end
54
+
55
+ def rails_filter_parameters
56
+ return unless defined?(Rails) &&
57
+ Rails.respond_to?(:application) &&
58
+ Rails.application.respond_to?(:config) &&
59
+ Rails.application.config.respond_to?(:filter_parameters)
60
+
61
+ params = Rails.application.config.filter_parameters
62
+ params if params.is_a?(Array) && params.any?
63
+ end
64
+
65
+ private_class_method :parameter_filter, :rails_filter_parameters
66
+ end
67
+ end
68
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson
@@ -279,6 +279,7 @@ files:
279
279
  - lib/pgbus/generators/migration_detector.rb
280
280
  - lib/pgbus/instrumentation.rb
281
281
  - lib/pgbus/integrations/appsignal.rb
282
+ - lib/pgbus/integrations/appsignal/dashboard.json
282
283
  - lib/pgbus/integrations/appsignal/dashboards/pgbus_health.json
283
284
  - lib/pgbus/integrations/appsignal/dashboards/pgbus_streams.json
284
285
  - lib/pgbus/integrations/appsignal/dashboards/pgbus_throughput.json
@@ -355,6 +356,7 @@ files:
355
356
  - lib/pgbus/web/authentication.rb
356
357
  - lib/pgbus/web/data_source.rb
357
358
  - lib/pgbus/web/metrics_serializer.rb
359
+ - lib/pgbus/web/payload_filter.rb
358
360
  - lib/pgbus/web/stream_app.rb
359
361
  - lib/pgbus/web/streamer.rb
360
362
  - lib/pgbus/web/streamer/connection.rb