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
data/test/pools_test.rb
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class PoolsTest < Minitest::Test
|
|
6
|
+
FakeDbConfig = Struct.new(:name)
|
|
7
|
+
FakePool = Struct.new(:db_config, :stat)
|
|
8
|
+
|
|
9
|
+
def setup
|
|
10
|
+
reset_observability!
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def teardown
|
|
14
|
+
reset_observability!
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def test_sample_aggregates_pools_by_config_name
|
|
18
|
+
Dir.mktmpdir("railsgauge-pools-") do |dir|
|
|
19
|
+
setup_yabeda_with_store(dir)
|
|
20
|
+
Railsgauge::Collectors::Database.install!
|
|
21
|
+
Yabeda.configure!
|
|
22
|
+
|
|
23
|
+
fake_pools = [
|
|
24
|
+
FakePool.new(FakeDbConfig.new("primary"), {size: 5, busy: 2, waiting: 1}),
|
|
25
|
+
FakePool.new(FakeDbConfig.new("primary"), {size: 5, busy: 1, waiting: 0}),
|
|
26
|
+
FakePool.new(FakeDbConfig.new("queue"), {size: 3, busy: 1, waiting: 0})
|
|
27
|
+
]
|
|
28
|
+
handler = Struct.new(:pools) do
|
|
29
|
+
def connection_pool_list(_)
|
|
30
|
+
pools
|
|
31
|
+
end
|
|
32
|
+
end.new(fake_pools)
|
|
33
|
+
|
|
34
|
+
with_fake_active_record(handler) do
|
|
35
|
+
Railsgauge::Pools.sample(0)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
registry = Yabeda::Prometheus.registry
|
|
39
|
+
assert_equal 10.0, registry.get(:app_db_pool_size).get(labels: {config: "primary", worker: "0"})
|
|
40
|
+
assert_equal 3.0, registry.get(:app_db_pool_busy).get(labels: {config: "primary", worker: "0"})
|
|
41
|
+
assert_equal 1.0, registry.get(:app_db_pool_waiting).get(labels: {config: "primary", worker: "0"})
|
|
42
|
+
assert_operator registry.get(:app_db_pool_observed_at_seconds).get(labels: {config: "primary", worker: "0"}), :>, 0
|
|
43
|
+
assert_equal 3.0, registry.get(:app_db_pool_size).get(labels: {config: "queue", worker: "0"})
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_start_survives_transient_errors_and_stops_cleanly
|
|
48
|
+
attempts = 0
|
|
49
|
+
original = Railsgauge::Pools.method(:sample)
|
|
50
|
+
Railsgauge::Pools.define_singleton_method(:sample) do |_worker|
|
|
51
|
+
attempts += 1
|
|
52
|
+
raise StandardError, "transient db hiccup" if attempts == 1
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
Railsgauge::Pools.start(0, interval: 0.02)
|
|
56
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 2
|
|
57
|
+
sleep 0.01 until attempts >= 2 || Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
|
|
58
|
+
Railsgauge::Pools.stop
|
|
59
|
+
|
|
60
|
+
assert_operator attempts, :>=, 2
|
|
61
|
+
ensure
|
|
62
|
+
Railsgauge::Pools.stop
|
|
63
|
+
Railsgauge::Pools.define_singleton_method(:sample, original) if original
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def with_fake_active_record(handler)
|
|
69
|
+
had_ar = Object.const_defined?(:ActiveRecord)
|
|
70
|
+
ar_mod = had_ar ? Object.const_get(:ActiveRecord) : Object.const_set(:ActiveRecord, Module.new)
|
|
71
|
+
had_base = ar_mod.const_defined?(:Base)
|
|
72
|
+
prev_base = had_base ? ar_mod.const_get(:Base) : nil
|
|
73
|
+
ar_mod.send(:remove_const, :Base) if had_base
|
|
74
|
+
ar_mod.const_set(:Base, Struct.new(:connection_handler).new(handler))
|
|
75
|
+
yield
|
|
76
|
+
ensure
|
|
77
|
+
ar_mod.send(:remove_const, :Base) if ar_mod.const_defined?(:Base)
|
|
78
|
+
ar_mod.const_set(:Base, prev_base) if had_base
|
|
79
|
+
Object.send(:remove_const, :ActiveRecord) unless had_ar
|
|
80
|
+
end
|
|
81
|
+
end
|
data/test/puma_test.rb
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class PumaTest < Minitest::Test
|
|
6
|
+
class FakeDsl
|
|
7
|
+
attr_reader :calls
|
|
8
|
+
|
|
9
|
+
def initialize(workers: 0)
|
|
10
|
+
@options = {workers: workers}
|
|
11
|
+
@calls = []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def preload_app!
|
|
15
|
+
@calls << [:preload_app!]
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def before_worker_boot(&block)
|
|
19
|
+
@calls << [:before_worker_boot, block]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def before_worker_shutdown(&block)
|
|
23
|
+
@calls << [:before_worker_shutdown, block]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def activate_control_app(url)
|
|
27
|
+
@calls << [:activate_control_app, url]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def plugin(name)
|
|
31
|
+
@calls << [:plugin, name]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def prometheus_exporter_url(url)
|
|
35
|
+
@calls << [:prometheus_exporter_url, url]
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def setup
|
|
40
|
+
reset_observability!
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def teardown
|
|
44
|
+
reset_observability!
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_configure_puma_returns_false_when_disabled
|
|
48
|
+
dsl = FakeDsl.new(workers: 2)
|
|
49
|
+
refute Railsgauge.configure_puma(dsl)
|
|
50
|
+
assert_empty dsl.calls
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_configure_puma_clustered_mode_registers_plugins_hooks_and_exporter
|
|
54
|
+
Railsgauge.configure do |config|
|
|
55
|
+
config.enabled = true
|
|
56
|
+
config.port = 9412
|
|
57
|
+
config.bind = "127.0.0.1"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
dsl = FakeDsl.new(workers: 3)
|
|
61
|
+
assert Railsgauge.configure_puma(dsl)
|
|
62
|
+
assert_predicate Railsgauge, :clustered?
|
|
63
|
+
|
|
64
|
+
names = dsl.calls.map(&:first)
|
|
65
|
+
assert_includes names, :preload_app!
|
|
66
|
+
assert_includes names, :before_worker_boot
|
|
67
|
+
assert_includes names, :before_worker_shutdown
|
|
68
|
+
assert_includes dsl.calls, [:plugin, :yabeda]
|
|
69
|
+
assert_includes dsl.calls, [:plugin, :yabeda_prometheus]
|
|
70
|
+
assert_includes dsl.calls, [:prometheus_exporter_url, "tcp://127.0.0.1:9412/metrics"]
|
|
71
|
+
control_call = dsl.calls.find { |call| call.first == :activate_control_app }
|
|
72
|
+
assert_match %r{\Aunix://.*/control\.sock\z}, control_call.last
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_configure_puma_single_mode_skips_preload_and_worker_hooks
|
|
76
|
+
Railsgauge.configure { |config| config.enabled = true }
|
|
77
|
+
|
|
78
|
+
dsl = FakeDsl.new(workers: 0)
|
|
79
|
+
assert Railsgauge.configure_puma(dsl)
|
|
80
|
+
refute_predicate Railsgauge, :clustered?
|
|
81
|
+
|
|
82
|
+
names = dsl.calls.map(&:first)
|
|
83
|
+
refute_includes names, :preload_app!
|
|
84
|
+
refute_includes names, :before_worker_boot
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_start_job_server_serves_prometheus_metrics_over_http_without_webrick
|
|
88
|
+
Dir.mktmpdir("railsgauge-job-http-") do |dir|
|
|
89
|
+
setup_yabeda_with_store(dir)
|
|
90
|
+
Railsgauge::Collectors::Database.install!
|
|
91
|
+
Yabeda.configure!
|
|
92
|
+
|
|
93
|
+
port = TCPServer.open("127.0.0.1", 0) { |server| server.addr[1] }
|
|
94
|
+
server = Railsgauge.start_job_server!(bind: "127.0.0.1", port: port)
|
|
95
|
+
begin
|
|
96
|
+
response = Net::HTTP.get_response(URI("http://127.0.0.1:#{port}/metrics"))
|
|
97
|
+
assert_equal "200", response.code
|
|
98
|
+
assert_includes response.body, "app_db_queries_total"
|
|
99
|
+
ensure
|
|
100
|
+
server&.stop(true)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
require "yabeda/rails"
|
|
5
|
+
|
|
6
|
+
class RailsLabelsTest < Minitest::Test
|
|
7
|
+
def setup
|
|
8
|
+
reset_observability!
|
|
9
|
+
@dir = Dir.mktmpdir("railsgauge-labels-")
|
|
10
|
+
setup_yabeda_with_store(@dir)
|
|
11
|
+
Railsgauge::RailsLabels.install!
|
|
12
|
+
Yabeda::Rails.install!
|
|
13
|
+
Yabeda.configure!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def teardown
|
|
17
|
+
reset_observability!
|
|
18
|
+
FileUtils.rm_rf(@dir) if @dir && Dir.exist?(@dir)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_bounds_unrecognized_format_and_method_and_normalizes_controller
|
|
22
|
+
event = Yabeda::Rails::Event.new(
|
|
23
|
+
"process_action.action_controller",
|
|
24
|
+
0.0,
|
|
25
|
+
0.1,
|
|
26
|
+
"id-1",
|
|
27
|
+
{
|
|
28
|
+
controller: "ReviewsController",
|
|
29
|
+
action: "index",
|
|
30
|
+
status: 200,
|
|
31
|
+
format: "weird_scanner_probe",
|
|
32
|
+
method: "PROPFIND",
|
|
33
|
+
params: {"controller" => "reviews"}
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
labels = event.labels
|
|
38
|
+
assert_equal "ReviewsController", labels[:controller]
|
|
39
|
+
assert_equal "index", labels[:action]
|
|
40
|
+
assert_equal 200, labels[:status]
|
|
41
|
+
assert_equal "other", labels[:format]
|
|
42
|
+
assert_equal "other", labels[:method]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_preserves_allowlisted_format_and_method
|
|
46
|
+
event = Yabeda::Rails::Event.new(
|
|
47
|
+
"process_action.action_controller",
|
|
48
|
+
0.0,
|
|
49
|
+
0.1,
|
|
50
|
+
"id-2",
|
|
51
|
+
{
|
|
52
|
+
controller: "ListingsController",
|
|
53
|
+
action: "show",
|
|
54
|
+
status: 200,
|
|
55
|
+
format: :turbo_stream,
|
|
56
|
+
method: "POST",
|
|
57
|
+
params: {"controller" => "listings"}
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
labels = event.labels
|
|
62
|
+
assert_equal "ListingsController", labels[:controller]
|
|
63
|
+
assert_equal "turbo_stream", labels[:format]
|
|
64
|
+
assert_equal "post", labels[:method]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
require "rails"
|
|
5
|
+
require "railsgauge/railtie"
|
|
6
|
+
|
|
7
|
+
class RailtieTest < Minitest::Test
|
|
8
|
+
def setup
|
|
9
|
+
reset_observability!
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def teardown
|
|
13
|
+
reset_observability!
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_install_skips_when_disabled
|
|
17
|
+
refute Railsgauge.install!
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_install_sets_up_store_rails_db_jobs_and_backend_collectors
|
|
21
|
+
Dir.mktmpdir("railsgauge-install-") do |dir|
|
|
22
|
+
Railsgauge.configure do |config|
|
|
23
|
+
config.enabled = true
|
|
24
|
+
config.store_directory = dir
|
|
25
|
+
config.queues = %w[default maintenance]
|
|
26
|
+
config.job_backend = :good_job
|
|
27
|
+
config.install_rails_metrics = true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
assert Railsgauge.install!
|
|
31
|
+
assert Railsgauge.install!
|
|
32
|
+
Yabeda.configure!
|
|
33
|
+
|
|
34
|
+
registry = Yabeda::Prometheus.registry
|
|
35
|
+
assert registry.exist?(:rails_requests_total)
|
|
36
|
+
assert registry.exist?(:app_db_queries_total)
|
|
37
|
+
assert registry.exist?(:app_jobs_executed_total)
|
|
38
|
+
assert registry.exist?(:app_jobs_ready)
|
|
39
|
+
assert_instance_of Prometheus::Client::DataStores::DirectFileStore, Prometheus::Client.config.data_store
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_install_skips_queue_gauges_when_collect_queue_gauges_is_false
|
|
44
|
+
Dir.mktmpdir("railsgauge-job-only-") do |dir|
|
|
45
|
+
Railsgauge.configure do |config|
|
|
46
|
+
config.enabled = true
|
|
47
|
+
config.store_directory = dir
|
|
48
|
+
config.job_backend = :solid_queue
|
|
49
|
+
config.collect_queue_gauges = false
|
|
50
|
+
config.install_rails_metrics = false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
assert Railsgauge.install!
|
|
54
|
+
Yabeda.configure!
|
|
55
|
+
|
|
56
|
+
registry = Yabeda::Prometheus.registry
|
|
57
|
+
refute registry.exist?(:rails_requests_total)
|
|
58
|
+
assert registry.exist?(:app_db_queries_total)
|
|
59
|
+
assert registry.exist?(:app_jobs_executed_total)
|
|
60
|
+
refute registry.exist?(:app_jobs_ready)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class SolidQueueCollectorTest < 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 group(_)
|
|
27
|
+
@group_result
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def setup
|
|
32
|
+
reset_observability!
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def teardown
|
|
36
|
+
reset_observability!
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_collects_solid_queue_gauges_including_blocked_paused_and_workers
|
|
40
|
+
Dir.mktmpdir("railsgauge-sq-") do |dir|
|
|
41
|
+
setup_yabeda_with_store(dir)
|
|
42
|
+
Railsgauge.configure do |config|
|
|
43
|
+
config.queues = %w[default maintenance]
|
|
44
|
+
config.job_backend = :solid_queue
|
|
45
|
+
end
|
|
46
|
+
Railsgauge::Collectors::SolidQueue.install!
|
|
47
|
+
Yabeda.configure!
|
|
48
|
+
|
|
49
|
+
oldest_time = Time.now.utc - 30
|
|
50
|
+
fake_pause = Class.new do
|
|
51
|
+
def self.where(**)
|
|
52
|
+
Struct.new(:list) do
|
|
53
|
+
def pluck(_)
|
|
54
|
+
list
|
|
55
|
+
end
|
|
56
|
+
end.new(["maintenance"])
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
fake_ready = Class.new do
|
|
60
|
+
define_singleton_method(:where) do |**|
|
|
61
|
+
eligible = FakeScope.new(FakeGroup.new(counts: {"default" => 5}, minimums: {"default" => oldest_time}))
|
|
62
|
+
paused_rel = FakeScope.new(FakeGroup.new(counts: {"maintenance" => 3}))
|
|
63
|
+
Class.new do
|
|
64
|
+
define_method(:where) do |*args, **kwargs|
|
|
65
|
+
if args.empty? && kwargs.empty?
|
|
66
|
+
Struct.new(:el) do
|
|
67
|
+
def not(**)
|
|
68
|
+
el
|
|
69
|
+
end
|
|
70
|
+
end.new(eligible)
|
|
71
|
+
else
|
|
72
|
+
paused_rel
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end.new
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
make_joined = ->(counts) do
|
|
79
|
+
Class.new do
|
|
80
|
+
define_singleton_method(:joins) { |_| self }
|
|
81
|
+
define_singleton_method(:where) { |**| self }
|
|
82
|
+
define_singleton_method(:group) { |_| FakeGroup.new(counts: counts) }
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
make_simple = ->(counts) do
|
|
86
|
+
Class.new do
|
|
87
|
+
define_singleton_method(:where) { |**| self }
|
|
88
|
+
define_singleton_method(:group) { |_| FakeGroup.new(counts: counts) }
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
fake_process = Class.new do
|
|
92
|
+
def self.where(*, **)
|
|
93
|
+
self
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def self.count
|
|
97
|
+
4
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
with_fake_solid_queue(
|
|
102
|
+
pause: fake_pause,
|
|
103
|
+
ready: fake_ready,
|
|
104
|
+
claimed: make_joined.call({"default" => 2}),
|
|
105
|
+
failed: make_joined.call({"maintenance" => 1}),
|
|
106
|
+
scheduled: make_simple.call({"default" => 6}),
|
|
107
|
+
blocked: make_simple.call({"default" => 1}),
|
|
108
|
+
process: fake_process
|
|
109
|
+
) do
|
|
110
|
+
Yabeda.collect!
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
registry = Yabeda::Prometheus.registry
|
|
114
|
+
default_labels = {queue: "default", backend: "solid_queue"}
|
|
115
|
+
maint_labels = {queue: "maintenance", backend: "solid_queue"}
|
|
116
|
+
|
|
117
|
+
assert_equal 5.0, registry.get(:app_jobs_ready).get(labels: default_labels)
|
|
118
|
+
assert_operator registry.get(:app_jobs_oldest_ready_age_seconds).get(labels: default_labels), :>=, 29
|
|
119
|
+
assert_equal 2.0, registry.get(:app_jobs_running).get(labels: default_labels)
|
|
120
|
+
assert_equal 6.0, registry.get(:app_jobs_scheduled).get(labels: default_labels)
|
|
121
|
+
assert_equal 1.0, registry.get(:app_jobs_blocked).get(labels: default_labels)
|
|
122
|
+
assert_equal 0.0, registry.get(:app_jobs_paused).get(labels: default_labels)
|
|
123
|
+
|
|
124
|
+
assert_equal 1.0, registry.get(:app_jobs_failed).get(labels: maint_labels)
|
|
125
|
+
assert_equal 3.0, registry.get(:app_jobs_paused).get(labels: maint_labels)
|
|
126
|
+
assert_equal 4.0, registry.get(:app_jobs_workers).get(labels: {backend: "solid_queue"})
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
def with_fake_solid_queue(pause:, ready:, claimed:, failed:, scheduled:, blocked:, process:)
|
|
133
|
+
mod = Module.new do
|
|
134
|
+
def self.process_alive_threshold
|
|
135
|
+
Struct.new(:ago).new(Time.now.utc - 300)
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
mod.const_set(:Pause, pause)
|
|
139
|
+
mod.const_set(:ReadyExecution, ready)
|
|
140
|
+
mod.const_set(:ClaimedExecution, claimed)
|
|
141
|
+
mod.const_set(:FailedExecution, failed)
|
|
142
|
+
mod.const_set(:ScheduledExecution, scheduled)
|
|
143
|
+
mod.const_set(:BlockedExecution, blocked)
|
|
144
|
+
mod.const_set(:Process, process)
|
|
145
|
+
Object.const_set(:SolidQueue, mod)
|
|
146
|
+
yield
|
|
147
|
+
ensure
|
|
148
|
+
Object.send(:remove_const, :SolidQueue) if Object.const_defined?(:SolidQueue)
|
|
149
|
+
end
|
|
150
|
+
end
|
data/test/store_test.rb
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
class StoreTest < Minitest::Test
|
|
6
|
+
def setup
|
|
7
|
+
reset_observability!
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def teardown
|
|
11
|
+
reset_observability!
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def test_prepare_creates_and_reuses_direct_file_store_and_cleans_up_owned_dir
|
|
15
|
+
dir = Railsgauge::Store.prepare!
|
|
16
|
+
|
|
17
|
+
assert Dir.exist?(dir)
|
|
18
|
+
assert_equal dir, Railsgauge::Store.prepare!
|
|
19
|
+
assert_instance_of Prometheus::Client::DataStores::DirectFileStore, Prometheus::Client.config.data_store
|
|
20
|
+
|
|
21
|
+
Railsgauge::Store.cleanup!
|
|
22
|
+
refute Dir.exist?(dir)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def test_cleanup_preserves_caller_supplied_directory
|
|
26
|
+
Dir.mktmpdir("railsgauge-external-") do |dir|
|
|
27
|
+
assert_equal dir, Railsgauge::Store.prepare!(directory: dir)
|
|
28
|
+
Railsgauge::Store.cleanup!
|
|
29
|
+
assert Dir.exist?(dir)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
4
|
+
|
|
5
|
+
require "simplecov"
|
|
6
|
+
|
|
7
|
+
SimpleCov.start do
|
|
8
|
+
enable_coverage :branch
|
|
9
|
+
add_filter "/test/"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
require "minitest/autorun"
|
|
13
|
+
require "net/http"
|
|
14
|
+
require "socket"
|
|
15
|
+
require "tmpdir"
|
|
16
|
+
require "railsgauge"
|
|
17
|
+
|
|
18
|
+
module RailsgaugeTestSupport
|
|
19
|
+
def reset_observability!
|
|
20
|
+
Railsgauge.reset!
|
|
21
|
+
if defined?(ActiveSupport::Notifications)
|
|
22
|
+
ActiveSupport::Notifications.notifier.listeners_for("process_action.action_controller").each do |listener|
|
|
23
|
+
ActiveSupport::Notifications.unsubscribe(listener)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
if defined?(Yabeda)
|
|
27
|
+
Yabeda.reset!
|
|
28
|
+
Yabeda.instance_variable_set(:@configured, false)
|
|
29
|
+
end
|
|
30
|
+
if defined?(Prometheus::Client::Registry)
|
|
31
|
+
Prometheus::Client.instance_variable_set(:@registry, Prometheus::Client::Registry.new)
|
|
32
|
+
if defined?(Yabeda::Prometheus::Adapter)
|
|
33
|
+
Yabeda.register_adapter(:prometheus, Yabeda::Prometheus::Adapter.new) unless Yabeda.adapters[:prometheus]
|
|
34
|
+
Yabeda.adapters[:prometheus].instance_variable_set(:@registry, nil)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def setup_yabeda_with_store(dir)
|
|
40
|
+
Railsgauge.configure do |config|
|
|
41
|
+
config.enabled = true
|
|
42
|
+
config.store_directory = dir
|
|
43
|
+
end
|
|
44
|
+
Railsgauge.prepare_store!
|
|
45
|
+
require "yabeda/prometheus"
|
|
46
|
+
Yabeda.register_adapter(:prometheus, Yabeda::Prometheus::Adapter.new) unless Yabeda.adapters[:prometheus]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
Minitest::Test.include(RailsgaugeTestSupport)
|