Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Add compatibility with Redis gem v5+ #134

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions lib/internal_metric/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,46 +66,36 @@ def collect
redis_config = GlobalSetting.redis_config
redis_primary_running[{ type: "main" }] = test_redis(
:master,
host: redis_config[:host],
port: redis_config[:port],
password: redis_config[:password],
ssl: redis_config[:ssl],
**redis_config.slice(:host, :port, :password, :ssl),
)
redis_replica_running[{ type: "main" }] = 0

if redis_config[:replica_host]
if redis_config[:custom] || redis_config[:replica_host]
redis_replica_running[{ type: "main" }] = test_redis(
:slave,
host: redis_config[:replica_host],
port: redis_config[:replica_port],
host: redis_config.dig(:custom, :replica_host) || redis_config[:replica_host],
port: redis_config.dig(:custom, :replica_port) || redis_config[:replica_port],
password: redis_config[:password],
ssl: redis_config[:ssl],
)
else
redis_replica_running[{ type: "main" }] = 0
end

if GlobalSetting.message_bus_redis_enabled
mb_redis_config = GlobalSetting.message_bus_redis_config
redis_primary_running[{ type: "message-bus" }] = test_redis(
:master,
host: mb_redis_config[:host],
port: mb_redis_config[:port],
password: mb_redis_config[:password],
ssl: mb_redis_config[:ssl],
**mb_redis_config.slice(:host, :port, :password, :ssl),
)
redis_replica_running[{ type: "message-bus" }] = 0

if mb_redis_config[:replica_host]
if mb_redis_config[:custom] || mb_redis_config[:replica_host]
redis_replica_running[{ type: "message-bus" }] = test_redis(
:slave,
host: mb_redis_config[:replica_host],
port: mb_redis_config[:replica_port],
host: mb_redis_config.dig(:custom, :replica_host) || mb_redis_config[:replica_host],
port: mb_redis_config.dig(:custom, :replica_port) || mb_redis_config[:replica_port],
password: mb_redis_config[:password],
ssl: mb_redis_config[:ssl],
)
else
redis_replica_running[{ type: "message-bus" }] = 0
end
end

Expand Down
69 changes: 69 additions & 0 deletions spec/lib/internal_metric/global_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,73 @@
expect(metric.postgres_highest_sequence).to be_a_kind_of(Hash)
expect(metric.postgres_highest_sequence[{ db: "default" }]).to be_present
end

describe "Redis" do
context "when a replica has been configured for main Redis" do
before do
allow(GlobalSetting).to receive_messages(
get_redis_replica_host: GlobalSetting.redis_host,
get_redis_replica_port: GlobalSetting.redis_port,
)
GlobalSetting.reset_redis_config!
metric.collect
end

after { GlobalSetting.reset_redis_config! }

it "collects the right metrics" do
expect(metric).to have_attributes(
redis_primary_available: {
{ type: "main" } => 1,
},
redis_master_available: {
{ type: "main" } => 1,
},
redis_slave_available: {
{ type: "main" } => 1,
},
redis_replica_available: {
{ type: "main" } => 1,
},
)
end
end

context "when a replica has been configured for MessageBus Redis" do
before do
allow(GlobalSetting).to receive_messages(
message_bus_redis_enabled: true,
message_bus_redis_host: GlobalSetting.redis_host,
message_bus_redis_port: GlobalSetting.redis_port,
get_message_bus_redis_replica_host: GlobalSetting.redis_host,
get_message_bus_redis_replica_port: GlobalSetting.redis_port,
)
GlobalSetting.reset_redis_config!
metric.collect
end

after { GlobalSetting.reset_redis_config! }

it "collects the right metrics" do
expect(metric).to have_attributes(
redis_primary_available: {
{ type: "main" } => 1,
{ type: "message-bus" } => 1,
},
redis_master_available: {
{ type: "main" } => 1,
{ type: "message-bus" } => 1,
},
redis_slave_available: {
{ type: "main" } => 0,
{ type: "message-bus" } => 1,
},
redis_replica_available: {
{ type: "main" } => 0,
{ type: "message-bus" } => 1,
},
)
end
end
end
end