railsgauge 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/CHANGELOG.md +24 -0
- data/LICENSE.txt +21 -0
- data/README.md +125 -0
- data/UPGRADE.md +33 -0
- data/lib/railsgauge/collectors/active_job.rb +84 -0
- data/lib/railsgauge/collectors/database.rb +77 -0
- data/lib/railsgauge/collectors/good_job.rb +74 -0
- data/lib/railsgauge/collectors/solid_queue.rb +77 -0
- data/lib/railsgauge/configuration.rb +93 -0
- data/lib/railsgauge/pools.rb +59 -0
- data/lib/railsgauge/puma.rb +58 -0
- data/lib/railsgauge/rails_labels.rb +25 -0
- data/lib/railsgauge/railtie.rb +17 -0
- data/lib/railsgauge/store.rb +51 -0
- data/lib/railsgauge/version.rb +5 -0
- data/lib/railsgauge.rb +111 -0
- data/test/active_job_collector_test.rb +51 -0
- data/test/configuration_test.rb +85 -0
- data/test/database_collector_test.rb +66 -0
- data/test/fork_aggregation_test.rb +54 -0
- data/test/good_job_collector_test.rb +116 -0
- data/test/pools_test.rb +81 -0
- data/test/puma_test.rb +104 -0
- data/test/rails_labels_test.rb +66 -0
- data/test/railtie_test.rb +63 -0
- data/test/solid_queue_collector_test.rb +150 -0
- data/test/store_test.rb +32 -0
- data/test/test_helper.rb +50 -0
- metadata +201 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Railsgauge
|
|
4
|
+
module Puma
|
|
5
|
+
class << self
|
|
6
|
+
def configure(dsl)
|
|
7
|
+
return false unless Railsgauge.enabled?
|
|
8
|
+
|
|
9
|
+
config = Railsgauge.configuration
|
|
10
|
+
directory = Railsgauge.prepare_store!
|
|
11
|
+
clustered = worker_count(dsl).positive?
|
|
12
|
+
Railsgauge.clustered = clustered
|
|
13
|
+
|
|
14
|
+
if clustered
|
|
15
|
+
dsl.preload_app!
|
|
16
|
+
dsl.before_worker_boot { |index| Railsgauge::Pools.start(index) }
|
|
17
|
+
dsl.before_worker_shutdown { Railsgauge::Pools.stop } if dsl.respond_to?(:before_worker_shutdown)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
dsl.activate_control_app "unix://#{directory}/control.sock"
|
|
21
|
+
dsl.plugin :yabeda
|
|
22
|
+
dsl.plugin :yabeda_prometheus
|
|
23
|
+
dsl.prometheus_exporter_url "tcp://#{config.bind_address}:#{config.port}/metrics"
|
|
24
|
+
true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def start_job_server!(bind: nil, port: nil)
|
|
28
|
+
return nil unless Railsgauge.enabled?
|
|
29
|
+
|
|
30
|
+
require "puma"
|
|
31
|
+
require "puma/server"
|
|
32
|
+
require "yabeda/prometheus"
|
|
33
|
+
|
|
34
|
+
config = Railsgauge.configuration
|
|
35
|
+
events = ::Puma::Events.respond_to?(:null) ? ::Puma::Events.null : ::Puma::Events.new
|
|
36
|
+
server = ::Puma::Server.new(
|
|
37
|
+
Yabeda::Prometheus::Exporter.rack_app,
|
|
38
|
+
events,
|
|
39
|
+
min_threads: 0,
|
|
40
|
+
max_threads: 1,
|
|
41
|
+
log_writer: ::Puma::LogWriter.stdio
|
|
42
|
+
)
|
|
43
|
+
server.add_tcp_listener(bind || config.bind_address, port || config.port)
|
|
44
|
+
server.run
|
|
45
|
+
server
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def worker_count(dsl)
|
|
51
|
+
config_options = dsl.instance_variable_get(:@config)&._options
|
|
52
|
+
raw = config_options&.[](:workers)
|
|
53
|
+
raw = dsl.instance_variable_get(:@options)&.[](:workers) if raw.nil?
|
|
54
|
+
raw.to_i
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Railsgauge
|
|
4
|
+
# Bounds client-controlled yabeda-rails labels (format, method) to prevent
|
|
5
|
+
# unbounded Prometheus label cardinality and normalizes controller naming.
|
|
6
|
+
module RailsLabels
|
|
7
|
+
def self.install!
|
|
8
|
+
return if Yabeda::Rails::Event.ancestors.include?(self)
|
|
9
|
+
|
|
10
|
+
Yabeda::Rails::Event.prepend(self)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def labels
|
|
14
|
+
values = super
|
|
15
|
+
config = Railsgauge.configuration
|
|
16
|
+
format = values[:format].to_s
|
|
17
|
+
method = values[:method].to_s
|
|
18
|
+
values.merge(
|
|
19
|
+
controller: payload[:controller].to_s,
|
|
20
|
+
format: config.allowed_formats.include?(format) ? format : "other",
|
|
21
|
+
method: config.allowed_methods.include?(method) ? method : "other"
|
|
22
|
+
)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/railtie"
|
|
4
|
+
|
|
5
|
+
module Railsgauge
|
|
6
|
+
class Railtie < ::Rails::Railtie
|
|
7
|
+
initializer "railsgauge.install", after: :load_config_initializers do
|
|
8
|
+
Railsgauge.install! if Railsgauge.enabled?
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
config.after_initialize do
|
|
12
|
+
if Railsgauge.enabled? && defined?(Yabeda) && !Yabeda.configured?
|
|
13
|
+
Yabeda.configure!
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "tmpdir"
|
|
5
|
+
|
|
6
|
+
module Railsgauge
|
|
7
|
+
module Store
|
|
8
|
+
class << self
|
|
9
|
+
attr_reader :directory
|
|
10
|
+
|
|
11
|
+
# Select Prometheus DirectFileStore before any metric is registered so
|
|
12
|
+
# forked Puma workers aggregate counters and histograms cleanly.
|
|
13
|
+
def prepare!(directory: nil)
|
|
14
|
+
return @directory if @directory
|
|
15
|
+
|
|
16
|
+
require "cgi"
|
|
17
|
+
require "cgi/core" # prometheus-client 4.2.5 uses CGI.parse on Ruby 4.
|
|
18
|
+
require "yabeda/prometheus"
|
|
19
|
+
|
|
20
|
+
target = directory || Dir.mktmpdir("railsgauge-prometheus-")
|
|
21
|
+
@owned = directory.nil?
|
|
22
|
+
@owner_pid = Process.pid
|
|
23
|
+
@directory = target
|
|
24
|
+
Prometheus::Client.config.data_store =
|
|
25
|
+
Prometheus::Client::DataStores::DirectFileStore.new(dir: @directory)
|
|
26
|
+
register_cleanup! if @owned
|
|
27
|
+
@directory
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def cleanup!
|
|
31
|
+
return unless @directory
|
|
32
|
+
|
|
33
|
+
path = @directory
|
|
34
|
+
should_remove = @owned && Process.pid == @owner_pid
|
|
35
|
+
@directory = nil
|
|
36
|
+
@owned = false
|
|
37
|
+
@owner_pid = nil
|
|
38
|
+
FileUtils.rm_rf(path) if should_remove && Dir.exist?(path)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def register_cleanup!
|
|
44
|
+
return if @cleanup_registered
|
|
45
|
+
|
|
46
|
+
@cleanup_registered = true
|
|
47
|
+
at_exit { cleanup! }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/railsgauge.rb
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "railsgauge/version"
|
|
4
|
+
require_relative "railsgauge/configuration"
|
|
5
|
+
require_relative "railsgauge/store"
|
|
6
|
+
require_relative "railsgauge/rails_labels"
|
|
7
|
+
require_relative "railsgauge/pools"
|
|
8
|
+
require_relative "railsgauge/puma"
|
|
9
|
+
require_relative "railsgauge/collectors/database"
|
|
10
|
+
require_relative "railsgauge/collectors/active_job"
|
|
11
|
+
require_relative "railsgauge/collectors/good_job"
|
|
12
|
+
require_relative "railsgauge/collectors/solid_queue"
|
|
13
|
+
require_relative "railsgauge/railtie" if defined?(Rails)
|
|
14
|
+
|
|
15
|
+
module Railsgauge
|
|
16
|
+
class << self
|
|
17
|
+
attr_writer :clustered
|
|
18
|
+
|
|
19
|
+
def configuration
|
|
20
|
+
@configuration ||= Configuration.new
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def configure
|
|
24
|
+
yield configuration
|
|
25
|
+
configuration
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def enabled?
|
|
29
|
+
@verification == true || configuration.enabled?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def enable_for_verification!
|
|
33
|
+
raise "Verification must not run in production" if configuration.production?
|
|
34
|
+
|
|
35
|
+
@verification = true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def clustered?
|
|
39
|
+
@clustered == true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def prepare_store!
|
|
43
|
+
Store.prepare!(directory: configuration.store_directory)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def install!
|
|
47
|
+
return false unless enabled?
|
|
48
|
+
return true if @installed
|
|
49
|
+
|
|
50
|
+
prepare_store!
|
|
51
|
+
require "yabeda/prometheus"
|
|
52
|
+
Yabeda.register_adapter(:prometheus, Yabeda::Prometheus::Adapter.new) unless Yabeda.adapters[:prometheus]
|
|
53
|
+
install_rails_metrics! if should_install_rails_metrics?
|
|
54
|
+
Collectors::Database.install!
|
|
55
|
+
Collectors::ActiveJob.install!
|
|
56
|
+
install_queue_gauges! if configuration.collect_queue_gauges?
|
|
57
|
+
@installed = true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def configure_puma(dsl)
|
|
61
|
+
Puma.configure(dsl)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def start_job_server!(bind: nil, port: nil)
|
|
65
|
+
Puma.start_job_server!(bind: bind, port: port)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def web_server?
|
|
69
|
+
(defined?(Rails) && Rails.const_defined?(:Server)) ||
|
|
70
|
+
Object.const_defined?("Puma::CLI") ||
|
|
71
|
+
Object.const_defined?("Unicorn::Launcher") ||
|
|
72
|
+
Object.const_defined?("PhusionPassenger")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def reset!
|
|
76
|
+
Pools.stop
|
|
77
|
+
Collectors::Database.uninstall!
|
|
78
|
+
Collectors::ActiveJob.uninstall!
|
|
79
|
+
Collectors::GoodJob.uninstall!
|
|
80
|
+
Collectors::SolidQueue.uninstall!
|
|
81
|
+
Store.cleanup!
|
|
82
|
+
@configuration = Configuration.new
|
|
83
|
+
@verification = false
|
|
84
|
+
@clustered = false
|
|
85
|
+
@installed = false
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def should_install_rails_metrics?
|
|
91
|
+
return configuration.install_rails_metrics == true unless configuration.install_rails_metrics.nil?
|
|
92
|
+
|
|
93
|
+
web_server?
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def install_rails_metrics!
|
|
97
|
+
require "yabeda/rails"
|
|
98
|
+
RailsLabels.install!
|
|
99
|
+
Yabeda::Rails.install!
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def install_queue_gauges!
|
|
103
|
+
case configuration.resolved_job_backend
|
|
104
|
+
when :good_job
|
|
105
|
+
Collectors::GoodJob.install!
|
|
106
|
+
when :solid_queue
|
|
107
|
+
Collectors::SolidQueue.install!
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class ActiveJobCollectorTest < Minitest::Test
|
|
6
|
+
FakeJob = Struct.new(:queue_name, :enqueued_at, :scheduled_at)
|
|
7
|
+
|
|
8
|
+
def setup
|
|
9
|
+
reset_observability!
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def teardown
|
|
13
|
+
reset_observability!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_records_execution_duration_result_and_queue_wait_for_allowlisted_queues
|
|
17
|
+
Dir.mktmpdir("railsgauge-aj-") do |dir|
|
|
18
|
+
setup_yabeda_with_store(dir)
|
|
19
|
+
Railsgauge.configure do |config|
|
|
20
|
+
config.queues = %w[default mailers]
|
|
21
|
+
config.job_backend = :good_job
|
|
22
|
+
end
|
|
23
|
+
Railsgauge::Collectors::ActiveJob.install!
|
|
24
|
+
Yabeda.configure!
|
|
25
|
+
|
|
26
|
+
now = Time.now.utc
|
|
27
|
+
good_job = FakeJob.new("default", now - 5, now - 2)
|
|
28
|
+
failed_job = FakeJob.new("mailers", now - 3, nil)
|
|
29
|
+
ignored_job = FakeJob.new("unlisted", now - 10, nil)
|
|
30
|
+
future_job = FakeJob.new("default", now + 60, now + 60)
|
|
31
|
+
|
|
32
|
+
ActiveSupport::Notifications.instrument("perform_start.active_job", {job: good_job}) {}
|
|
33
|
+
ActiveSupport::Notifications.instrument("perform.active_job", {job: good_job}) {}
|
|
34
|
+
|
|
35
|
+
ActiveSupport::Notifications.instrument("perform_start.active_job", {job: failed_job}) {}
|
|
36
|
+
ActiveSupport::Notifications.instrument("perform.active_job", {job: failed_job, exception: ["RuntimeError", "boom"]}) {}
|
|
37
|
+
|
|
38
|
+
ActiveSupport::Notifications.instrument("perform_start.active_job", {job: ignored_job}) {}
|
|
39
|
+
ActiveSupport::Notifications.instrument("perform.active_job", {job: ignored_job}) {}
|
|
40
|
+
|
|
41
|
+
ActiveSupport::Notifications.instrument("perform_start.active_job", {job: future_job}) {}
|
|
42
|
+
|
|
43
|
+
registry = Yabeda::Prometheus.registry
|
|
44
|
+
assert_equal 1.0, registry.get(:app_jobs_executed_total).get(labels: {queue: "default", backend: "good_job", result: "success"})
|
|
45
|
+
assert_equal 1.0, registry.get(:app_jobs_executed_total).get(labels: {queue: "mailers", backend: "good_job", result: "error"})
|
|
46
|
+
assert_equal 0.0, registry.get(:app_jobs_executed_total).get(labels: {queue: "unlisted", backend: "good_job", result: "success"})
|
|
47
|
+
assert_equal 1.0, registry.get(:app_jobs_queue_wait_seconds).get(labels: {queue: "default", backend: "good_job"})["+Inf"]
|
|
48
|
+
assert_equal 1.0, registry.get(:app_jobs_execution_duration_seconds).get(labels: {queue: "default", backend: "good_job"})["+Inf"]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class ConfigurationTest < Minitest::Test
|
|
6
|
+
def setup
|
|
7
|
+
reset_observability!
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def teardown
|
|
11
|
+
reset_observability!
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_defaults
|
|
15
|
+
config = Railsgauge.configuration
|
|
16
|
+
|
|
17
|
+
assert_equal 9394, config.port
|
|
18
|
+
assert_equal ["default"], config.queues
|
|
19
|
+
assert_equal :auto, config.job_backend
|
|
20
|
+
assert_equal 15, config.pool_sample_interval
|
|
21
|
+
assert_equal "127.0.0.1", config.bind_address
|
|
22
|
+
refute_predicate config, :enabled?
|
|
23
|
+
assert_predicate config, :collect_queue_gauges?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def test_configure_block_and_normalization
|
|
27
|
+
Railsgauge.configure do |config|
|
|
28
|
+
config.enabled = true
|
|
29
|
+
config.port = "9400"
|
|
30
|
+
config.bind = "0.0.0.0"
|
|
31
|
+
config.queues = [:default, "maintenance", "", "default"]
|
|
32
|
+
config.job_backend = "good_job"
|
|
33
|
+
config.pool_sample_interval = "5.5"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
config = Railsgauge.configuration
|
|
37
|
+
assert_predicate Railsgauge, :enabled?
|
|
38
|
+
assert_equal 9400, config.port
|
|
39
|
+
assert_equal "0.0.0.0", config.bind_address
|
|
40
|
+
assert_equal %w[default maintenance], config.queues
|
|
41
|
+
assert_equal :good_job, config.job_backend
|
|
42
|
+
assert_equal :good_job, config.resolved_job_backend
|
|
43
|
+
assert_in_delta 5.5, config.pool_sample_interval
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def test_validation_errors
|
|
47
|
+
config = Railsgauge.configuration
|
|
48
|
+
|
|
49
|
+
assert_raises(ArgumentError) { config.port = 0 }
|
|
50
|
+
assert_raises(ArgumentError) { config.port = 70_000 }
|
|
51
|
+
assert_raises(ArgumentError) { config.queues = [] }
|
|
52
|
+
assert_raises(ArgumentError) { config.job_backend = :sidekiq }
|
|
53
|
+
assert_raises(ArgumentError) { config.pool_sample_interval = 0 }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def test_callable_enabled_and_collect_queue_gauges
|
|
57
|
+
flag = false
|
|
58
|
+
Railsgauge.configure do |config|
|
|
59
|
+
config.enabled = -> { flag }
|
|
60
|
+
config.collect_queue_gauges = -> { flag }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
refute_predicate Railsgauge, :enabled?
|
|
64
|
+
refute_predicate Railsgauge.configuration, :collect_queue_gauges?
|
|
65
|
+
|
|
66
|
+
flag = true
|
|
67
|
+
assert_predicate Railsgauge, :enabled?
|
|
68
|
+
assert_predicate Railsgauge.configuration, :collect_queue_gauges?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_service_env_disables_queue_gauges_on_job_role
|
|
72
|
+
previous = ENV["SERVICE"]
|
|
73
|
+
ENV["SERVICE"] = "job"
|
|
74
|
+
|
|
75
|
+
refute_predicate Railsgauge.configuration, :collect_queue_gauges?
|
|
76
|
+
ensure
|
|
77
|
+
ENV["SERVICE"] = previous
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_enable_for_verification
|
|
81
|
+
refute_predicate Railsgauge, :enabled?
|
|
82
|
+
Railsgauge.enable_for_verification!
|
|
83
|
+
assert_predicate Railsgauge, :enabled?
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class DatabaseCollectorTest < Minitest::Test
|
|
6
|
+
FakeDbConfig = Struct.new(:name)
|
|
7
|
+
FakePool = Struct.new(:db_config)
|
|
8
|
+
FakeConnection = Struct.new(:pool)
|
|
9
|
+
|
|
10
|
+
def setup
|
|
11
|
+
reset_observability!
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def teardown
|
|
15
|
+
reset_observability!
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_records_uncached_sql_and_ignores_cached_and_schema
|
|
19
|
+
Dir.mktmpdir("railsgauge-db-") do |dir|
|
|
20
|
+
setup_yabeda_with_store(dir)
|
|
21
|
+
Railsgauge::Collectors::Database.install!
|
|
22
|
+
Yabeda.configure!
|
|
23
|
+
|
|
24
|
+
conn = FakeConnection.new(FakePool.new(FakeDbConfig.new("primary")))
|
|
25
|
+
|
|
26
|
+
ActiveSupport::Notifications.instrument("sql.active_record", {connection: conn, name: "User Load", cached: false}) {}
|
|
27
|
+
ActiveSupport::Notifications.instrument("sql.active_record", {connection: conn, name: "User Load", cached: true}) {}
|
|
28
|
+
ActiveSupport::Notifications.instrument("sql.active_record", {connection: conn, name: "SCHEMA", cached: false}) {}
|
|
29
|
+
|
|
30
|
+
registry = Yabeda::Prometheus.registry
|
|
31
|
+
assert_equal 1.0, registry.get(:app_db_queries_total).get(labels: {config: "primary"})
|
|
32
|
+
assert_equal 1.0, registry.get(:app_db_query_duration_seconds).get(labels: {config: "primary"})["+Inf"]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_records_connection_timeout_errors_for_request_and_job_contexts
|
|
37
|
+
Dir.mktmpdir("railsgauge-timeout-") do |dir|
|
|
38
|
+
setup_yabeda_with_store(dir)
|
|
39
|
+
Railsgauge::Collectors::Database.install!
|
|
40
|
+
Yabeda.configure!
|
|
41
|
+
|
|
42
|
+
with_timeout_error_class do |error_class|
|
|
43
|
+
ActiveSupport::Notifications.instrument("process_action.action_controller", {exception_object: error_class.new("busy")}) {}
|
|
44
|
+
ActiveSupport::Notifications.instrument("perform.active_job", {exception_object: error_class.new("busy")}) {}
|
|
45
|
+
ActiveSupport::Notifications.instrument("perform.active_job", {exception_object: StandardError.new("other")}) {}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
registry = Yabeda::Prometheus.registry
|
|
49
|
+
assert_equal 1.0, registry.get(:app_db_connection_timeouts_total).get(labels: {context: "request"})
|
|
50
|
+
assert_equal 1.0, registry.get(:app_db_connection_timeouts_total).get(labels: {context: "job"})
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def with_timeout_error_class
|
|
57
|
+
had_ar = Object.const_defined?(:ActiveRecord)
|
|
58
|
+
ar_mod = had_ar ? Object.const_get(:ActiveRecord) : Object.const_set(:ActiveRecord, Module.new)
|
|
59
|
+
had_err = ar_mod.const_defined?(:ConnectionTimeoutError)
|
|
60
|
+
err_class = had_err ? ar_mod.const_get(:ConnectionTimeoutError) : ar_mod.const_set(:ConnectionTimeoutError, Class.new(StandardError))
|
|
61
|
+
yield err_class
|
|
62
|
+
ensure
|
|
63
|
+
ar_mod.send(:remove_const, :ConnectionTimeoutError) unless had_err
|
|
64
|
+
Object.send(:remove_const, :ActiveRecord) unless had_ar
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
require "prometheus/client/formats/text"
|
|
5
|
+
|
|
6
|
+
class ForkAggregationTest < Minitest::Test
|
|
7
|
+
FakeJob = Struct.new(:queue_name, :enqueued_at, :scheduled_at)
|
|
8
|
+
|
|
9
|
+
def setup
|
|
10
|
+
reset_observability!
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def teardown
|
|
14
|
+
reset_observability!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_forked_workers_aggregate_counters_and_histograms_in_shared_store
|
|
18
|
+
Dir.mktmpdir("railsgauge-forks-") do |dir|
|
|
19
|
+
setup_yabeda_with_store(dir)
|
|
20
|
+
Railsgauge.configure do |config|
|
|
21
|
+
config.queues = ["default"]
|
|
22
|
+
config.job_backend = :solid_queue
|
|
23
|
+
end
|
|
24
|
+
Railsgauge::Collectors::ActiveJob.install!
|
|
25
|
+
Yabeda.configure!
|
|
26
|
+
|
|
27
|
+
children = [2, 3].map do |count|
|
|
28
|
+
fork do
|
|
29
|
+
count.times do
|
|
30
|
+
job = FakeJob.new("default", Time.now.utc - 2, Time.now.utc - 1)
|
|
31
|
+
ActiveSupport::Notifications.instrument("perform_start.active_job", {job: job}) {}
|
|
32
|
+
ActiveSupport::Notifications.instrument("perform.active_job", {job: job}) {}
|
|
33
|
+
end
|
|
34
|
+
exit! 0
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
children.each do |pid|
|
|
39
|
+
_, status = Process.wait2(pid)
|
|
40
|
+
assert_predicate status, :success?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
text = Prometheus::Client::Formats::Text.marshal(Yabeda::Prometheus.registry)
|
|
44
|
+
%w[
|
|
45
|
+
app_jobs_executed_total
|
|
46
|
+
app_jobs_execution_duration_seconds_count
|
|
47
|
+
app_jobs_queue_wait_seconds_count
|
|
48
|
+
].each do |metric|
|
|
49
|
+
total = text.lines.grep(/^#{metric}\{/o).sum { |line| Float(line.split.last) }
|
|
50
|
+
assert_in_delta 5.0, total, 1e-9, "Expected 5 aggregated observations for #{metric}"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class GoodJobCollectorTest < Minitest::Test
|
|
6
|
+
class FakeGroup
|
|
7
|
+
def initialize(counts:, minimums: {})
|
|
8
|
+
@counts = counts
|
|
9
|
+
@minimums = minimums
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def count
|
|
13
|
+
@counts
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def minimum(_column)
|
|
17
|
+
@minimums
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class FakeScope
|
|
22
|
+
def initialize(group_result)
|
|
23
|
+
@group_result = group_result
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def where(*)
|
|
27
|
+
self
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def group(_)
|
|
31
|
+
@group_result
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def setup
|
|
36
|
+
reset_observability!
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def teardown
|
|
40
|
+
reset_observability!
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_collects_good_job_queue_gauges_and_active_workers
|
|
44
|
+
Dir.mktmpdir("railsgauge-gj-") do |dir|
|
|
45
|
+
setup_yabeda_with_store(dir)
|
|
46
|
+
Railsgauge.configure do |config|
|
|
47
|
+
config.queues = %w[default maintenance]
|
|
48
|
+
config.job_backend = :good_job
|
|
49
|
+
end
|
|
50
|
+
Railsgauge::Collectors::GoodJob.install!
|
|
51
|
+
Yabeda.configure!
|
|
52
|
+
|
|
53
|
+
oldest_time = Time.now.utc - 45
|
|
54
|
+
eligible_scope = FakeScope.new(FakeGroup.new(counts: {"default" => 3}, minimums: {"default" => oldest_time}))
|
|
55
|
+
due_scope = Class.new do
|
|
56
|
+
define_method(:exclude_paused) { eligible_scope }
|
|
57
|
+
define_method(:group) { |_| FakeGroup.new(counts: {"default" => 3, "maintenance" => 2}) }
|
|
58
|
+
end.new
|
|
59
|
+
queued_scope = Struct.new(:due) do
|
|
60
|
+
def where(**)
|
|
61
|
+
due
|
|
62
|
+
end
|
|
63
|
+
end.new(due_scope)
|
|
64
|
+
running_scope = FakeScope.new(FakeGroup.new(counts: {"default" => 1}))
|
|
65
|
+
discarded_scope = FakeScope.new(FakeGroup.new(counts: {"maintenance" => 4}))
|
|
66
|
+
scheduled_scope = FakeScope.new(FakeGroup.new(counts: {"default" => 2}))
|
|
67
|
+
jobs_scope = Class.new do
|
|
68
|
+
define_method(:queued) { queued_scope }
|
|
69
|
+
define_method(:running) { running_scope }
|
|
70
|
+
define_method(:discarded) { discarded_scope }
|
|
71
|
+
define_method(:unfinished) { scheduled_scope }
|
|
72
|
+
end.new
|
|
73
|
+
fake_job = Struct.new(:scope) do
|
|
74
|
+
def where(**)
|
|
75
|
+
scope
|
|
76
|
+
end
|
|
77
|
+
end.new(jobs_scope)
|
|
78
|
+
fake_process = Class.new do
|
|
79
|
+
def self.active
|
|
80
|
+
Struct.new(:count).new(2)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
with_fake_good_job(fake_job, fake_process) do
|
|
85
|
+
Yabeda.collect!
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
registry = Yabeda::Prometheus.registry
|
|
89
|
+
default_labels = {queue: "default", backend: "good_job"}
|
|
90
|
+
maint_labels = {queue: "maintenance", backend: "good_job"}
|
|
91
|
+
|
|
92
|
+
assert_equal 3.0, registry.get(:app_jobs_ready).get(labels: default_labels)
|
|
93
|
+
assert_operator registry.get(:app_jobs_oldest_ready_age_seconds).get(labels: default_labels), :>=, 44
|
|
94
|
+
assert_equal 1.0, registry.get(:app_jobs_running).get(labels: default_labels)
|
|
95
|
+
assert_equal 2.0, registry.get(:app_jobs_scheduled).get(labels: default_labels)
|
|
96
|
+
assert_equal 0.0, registry.get(:app_jobs_paused).get(labels: default_labels)
|
|
97
|
+
|
|
98
|
+
assert_equal 0.0, registry.get(:app_jobs_ready).get(labels: maint_labels)
|
|
99
|
+
assert_equal 4.0, registry.get(:app_jobs_failed).get(labels: maint_labels)
|
|
100
|
+
assert_equal 2.0, registry.get(:app_jobs_paused).get(labels: maint_labels)
|
|
101
|
+
assert_equal 2.0, registry.get(:app_jobs_workers).get(labels: {backend: "good_job"})
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def with_fake_good_job(fake_job, fake_process)
|
|
108
|
+
mod = Module.new
|
|
109
|
+
mod.const_set(:Job, fake_job)
|
|
110
|
+
mod.const_set(:Process, fake_process)
|
|
111
|
+
Object.const_set(:GoodJob, mod)
|
|
112
|
+
yield
|
|
113
|
+
ensure
|
|
114
|
+
Object.send(:remove_const, :GoodJob) if Object.const_defined?(:GoodJob)
|
|
115
|
+
end
|
|
116
|
+
end
|