DEV: minor cleanup of specs, improve error reporting

DEV: minor cleanup of specs, improve error reporting

biggest change here is that if prometheus_trusted_ip_whitelist_regex stuff is going to pop up in /logs

diff --git a/lib/middleware/metrics.rb b/lib/middleware/metrics.rb
index 681b1b4..e10bc8b 100644
--- a/lib/middleware/metrics.rb
+++ b/lib/middleware/metrics.rb
@@ -24,7 +24,7 @@ module DiscoursePrometheus
 
     def is_private_ip?(env)
       request = Rack::Request.new(env)
-      ip = IPAddr.new(request.ip) rescue false
+      ip = IPAddr.new(request.ip) rescue nil
       !!(ip && ip.to_s =~ PRIVATE_IP)
     end
 
@@ -34,8 +34,9 @@ module DiscoursePrometheus
         trusted_ip_regex = Regexp.new GlobalSetting.prometheus_trusted_ip_whitelist_regex
         request = Rack::Request.new(env)
         ip = IPAddr.new(request.ip)
-      rescue
-        false
+      rescue => e
+        # failed to parse regex
+        Discourse.warn_exception(e, message: "Error parsing prometheus trusted ip whitelist", env: env)
       end
       !!(trusted_ip_regex && ip && ip.to_s =~ trusted_ip_regex)
     end
diff --git a/spec/middleware/metrics_spec.rb b/spec/middleware/metrics_spec.rb
index ece6bd7..f30168a 100644
--- a/spec/middleware/metrics_spec.rb
+++ b/spec/middleware/metrics_spec.rb
@@ -20,7 +20,7 @@ describe ::DiscoursePrometheus::Middleware::Metrics do
   end
 
   it "will 404 for unauthed and invalid regex" do
-    GlobalSetting.stubs(:prometheus_trusted_ip_whitelist_regex).returns("unbalanced bracket[")
+    global_setting :prometheus_trusted_ip_whitelist_regex, "unbalanced bracket["
     status, = middleware.call("PATH_INFO" => '/metrics', "REMOTE_ADDR" => '200.0.1.1', "rack.input" => StringIO.new)
     expect(status).to eq(404)
   end
@@ -43,7 +43,7 @@ describe ::DiscoursePrometheus::Middleware::Metrics do
   end
 
   it "can proxy the dedicated port even with invalid regex" do
-    GlobalSetting.stubs(:prometheus_trusted_ip_whitelist_regex).returns("unbalanced bracket[")
+    global_setting :prometheus_trusted_ip_whitelist_regex, "unbalanced bracket["
     stub_request(:get, "http://localhost:#{GlobalSetting.prometheus_collector_port}/metrics").
       to_return(status: 200, body: "hello world", headers: {})
 
@@ -56,7 +56,7 @@ describe ::DiscoursePrometheus::Middleware::Metrics do
   end
 
   it "can proxy the dedicated port on trusted IP" do
-    GlobalSetting.stubs(:prometheus_trusted_ip_whitelist_regex).returns("(200\.0)")
+    global_setting :prometheus_trusted_ip_whitelist_regex, "(200\.0)"
     stub_request(:get, "http://localhost:#{GlobalSetting.prometheus_collector_port}/metrics").
       to_return(status: 200, body: "hello world", headers: {})
 

GitHub sha: 22aa6dfc