Evita di saturare Puma in diretta, separa i check Rails/pubblico e traccia la latenza /up per alert più affidabili. Co-authored-by: Cursor <cursoragent@cursor.com>
100 lines
3.6 KiB
Ruby
100 lines
3.6 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe Ops::HealthChecks do
|
|
describe "#call" do
|
|
it "risolve incidenti quando il disco è sano" do
|
|
Ops::Incident.create!(
|
|
kind: "disk_space",
|
|
severity: "warning",
|
|
status: "open",
|
|
title: "Disco",
|
|
message: "alto",
|
|
metadata: {},
|
|
fingerprint: "disk_space:root",
|
|
occurrence_count: 1,
|
|
first_seen_at: Time.current,
|
|
last_seen_at: Time.current
|
|
)
|
|
|
|
allow_any_instance_of(described_class).to receive(:check_disk_root).and_return(
|
|
described_class::Finding.new(
|
|
kind: "disk_space", severity: "info", healthy: true,
|
|
title: "OK", message: "OK", metadata: {}, fingerprint: "disk_space:root"
|
|
)
|
|
)
|
|
%i[
|
|
check_recordings_size check_postgres check_redis check_mediamtx check_garage
|
|
check_sidekiq_heartbeat check_sidekiq_dead check_http_rails check_rails_latency
|
|
].each do |method|
|
|
allow_any_instance_of(described_class).to receive(method).and_return(
|
|
described_class::Finding.new(
|
|
kind: "test", severity: "info", healthy: true,
|
|
title: "OK", message: "OK", metadata: {}, fingerprint: "#{method}:ok"
|
|
)
|
|
)
|
|
end
|
|
allow_any_instance_of(described_class).to receive(:public_check_due?).and_return(false)
|
|
|
|
described_class.new.call
|
|
|
|
expect(Ops::Incident.find_by(fingerprint: "disk_space:root").status).to eq("resolved")
|
|
end
|
|
end
|
|
|
|
describe "#summary" do
|
|
it "restituisce status ok quando tutti i check sono sani" do
|
|
allow_any_instance_of(described_class).to receive(:check_disk_root).and_return(
|
|
described_class::Finding.new(
|
|
kind: "disk_space", severity: "info", healthy: true,
|
|
title: "OK", message: "OK", metadata: {}, fingerprint: "disk_space:root"
|
|
)
|
|
)
|
|
%i[
|
|
check_recordings_size check_postgres check_redis check_mediamtx check_garage
|
|
check_sidekiq_heartbeat check_sidekiq_dead check_http_rails check_rails_latency
|
|
].each do |method|
|
|
allow_any_instance_of(described_class).to receive(method).and_return(
|
|
described_class::Finding.new(
|
|
kind: "test", severity: "info", healthy: true,
|
|
title: "OK", message: "OK", metadata: {}, fingerprint: "#{method}:ok"
|
|
)
|
|
)
|
|
end
|
|
allow_any_instance_of(described_class).to receive(:public_check_due?).and_return(false)
|
|
|
|
summary = described_class.new.summary
|
|
|
|
expect(summary[:status]).to eq("ok")
|
|
expect(summary[:checks].size).to eq(10)
|
|
end
|
|
|
|
it "degraded quando la latenza p95 supera la soglia warning" do
|
|
allow_any_instance_of(described_class).to receive(:build_findings).and_return([
|
|
described_class::Finding.new(
|
|
kind: "rails_latency", severity: "warning", healthy: false,
|
|
title: "Latenza elevata", message: "p95 2500ms", metadata: {}, fingerprint: "rails_latency:p95"
|
|
)
|
|
])
|
|
|
|
expect(described_class.new.summary[:status]).to eq("degraded")
|
|
end
|
|
end
|
|
|
|
describe "#check_http_public" do
|
|
it "è warning, non critical" do
|
|
allow(Rails.env).to receive(:test?).and_return(false)
|
|
allow(MatchLiveTv).to receive(:app_public_url).and_return("https://www.matchlivetv.it")
|
|
|
|
checks = described_class.new
|
|
response = instance_double(Faraday::Response, status: 200)
|
|
allow(Faraday).to receive(:get).and_return(response)
|
|
|
|
finding = checks.send(:check_http_public)
|
|
|
|
expect(finding.severity).to eq("warning")
|
|
expect(finding.kind).to eq("http_public")
|
|
expect(finding.healthy).to be(true)
|
|
end
|
|
end
|
|
end
|