From d9ddf2ade99b4f284b695b47c7615ef8d56f590c Mon Sep 17 00:00:00 2001 From: Josh Etsenake Date: Sat, 5 Nov 2022 17:33:39 -0600 Subject: [PATCH 1/7] add support for turning on rack deflater for the rack app add support for starting the metrics server in a separate process add support for suppressing Errno::EADDRINUSE when running in clustered mode in production apps --- lib/yabeda/prometheus/exporter.rb | 41 +++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/yabeda/prometheus/exporter.rb b/lib/yabeda/prometheus/exporter.rb index 47314f1..90fc6e5 100644 --- a/lib/yabeda/prometheus/exporter.rb +++ b/lib/yabeda/prometheus/exporter.rb @@ -19,19 +19,31 @@ def call(env) @app.call(env) end - def start_metrics_server!(**rack_app_options) + def start_metrics_server!(start_in_thread: true, **rack_app_options) + if start_in_thread + start_server_in_thread!(**rack_app_options) + else + start_server_in_process!(**rack_app_options) + end + end + + def start_server_in_thread!(**rack_app_options) Thread.new do - default_port = ENV.fetch("PORT", 9394) - ::Rack::Handler::WEBrick.run( - rack_app(**rack_app_options), - Host: ENV["PROMETHEUS_EXPORTER_BIND"] || "0.0.0.0", - Port: ENV.fetch("PROMETHEUS_EXPORTER_PORT", default_port), - AccessLog: [], - ) + start_app(**rack_app_options) end end def rack_app(exporter = self, logger: Logger.new(IO::NULL), use_deflater: true, **exporter_options) + def start_server_in_process!(**rack_app_options) + pid = Process.fork do + # re-configure yabeda since we're in a new process + Yabeda.configure! + start_app(**rack_app_options) + end + Process.detach(pid) if pid + end + + def rack_app(exporter = self, path: "/metrics", logger: Logger.new(IO::NULL), use_deflater: false) ::Rack::Builder.new do use ::Rack::Deflater if use_deflater use ::Rack::CommonLogger, logger @@ -40,6 +52,19 @@ def rack_app(exporter = self, logger: Logger.new(IO::NULL), use_deflater: true, run NOT_FOUND_HANDLER end end + + def start_app(raise_start_error: true, **rack_app_options) + default_port = ENV.fetch("PORT", 9394) + ::Rack::Handler::WEBrick.run( + rack_app(**rack_app_options.remove(:raise_failed_to_start)), + Host: ENV["PROMETHEUS_EXPORTER_BIND"] || "0.0.0.0", + Port: ENV.fetch("PROMETHEUS_EXPORTER_PORT", default_port), + AccessLog: [], + ) + rescue Errno::EADDRINUSE + puts "Failed to start server might be started by another process" + raise if raise_start_error + end end def initialize(app, options = {}) From 0298ff8c0d3cf37704bc2f4770ee22236a9f775e Mon Sep 17 00:00:00 2001 From: Josh Etsenake Date: Sat, 5 Nov 2022 18:25:41 -0600 Subject: [PATCH 2/7] bump version --- lib/yabeda/prometheus/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yabeda/prometheus/version.rb b/lib/yabeda/prometheus/version.rb index a765f27..db465bd 100644 --- a/lib/yabeda/prometheus/version.rb +++ b/lib/yabeda/prometheus/version.rb @@ -2,6 +2,6 @@ module Yabeda module Prometheus - VERSION = "0.8.0" + VERSION = "0.9.0" end end From fb3b19968eeac0c33f191b0966c62ebf4dcb4356 Mon Sep 17 00:00:00 2001 From: Josh Etsenake Date: Sat, 5 Nov 2022 18:32:07 -0600 Subject: [PATCH 3/7] remove unneeded code after switching approach a bit --- lib/yabeda/prometheus/exporter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yabeda/prometheus/exporter.rb b/lib/yabeda/prometheus/exporter.rb index 90fc6e5..00fe90d 100644 --- a/lib/yabeda/prometheus/exporter.rb +++ b/lib/yabeda/prometheus/exporter.rb @@ -56,7 +56,7 @@ def rack_app(exporter = self, path: "/metrics", logger: Logger.new(IO::NULL), us def start_app(raise_start_error: true, **rack_app_options) default_port = ENV.fetch("PORT", 9394) ::Rack::Handler::WEBrick.run( - rack_app(**rack_app_options.remove(:raise_failed_to_start)), + rack_app(**rack_app_options), Host: ENV["PROMETHEUS_EXPORTER_BIND"] || "0.0.0.0", Port: ENV.fetch("PROMETHEUS_EXPORTER_PORT", default_port), AccessLog: [], From 3a9479ae2de8993ca06d7209f57bf34d22f551c8 Mon Sep 17 00:00:00 2001 From: Josh Etsenake Date: Sat, 5 Nov 2022 18:36:35 -0600 Subject: [PATCH 4/7] add better wording for error message --- lib/yabeda/prometheus/exporter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/yabeda/prometheus/exporter.rb b/lib/yabeda/prometheus/exporter.rb index 00fe90d..312ea1a 100644 --- a/lib/yabeda/prometheus/exporter.rb +++ b/lib/yabeda/prometheus/exporter.rb @@ -62,7 +62,7 @@ def start_app(raise_start_error: true, **rack_app_options) AccessLog: [], ) rescue Errno::EADDRINUSE - puts "Failed to start server might be started by another process" + puts "Yabeda prometheus exporter: Failed to start, server might be started by another process" raise if raise_start_error end end From 0bd34218534be01e6a52a4b0b5027791b2516cb5 Mon Sep 17 00:00:00 2001 From: Josh Etsenake Date: Thu, 10 Nov 2022 07:19:12 -0500 Subject: [PATCH 5/7] remove rack deflater changes --- lib/yabeda/prometheus/exporter.rb | 8 ++------ lib/yabeda/prometheus/version.rb | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/yabeda/prometheus/exporter.rb b/lib/yabeda/prometheus/exporter.rb index 312ea1a..5f2bbf0 100644 --- a/lib/yabeda/prometheus/exporter.rb +++ b/lib/yabeda/prometheus/exporter.rb @@ -43,9 +43,8 @@ def start_server_in_process!(**rack_app_options) Process.detach(pid) if pid end - def rack_app(exporter = self, path: "/metrics", logger: Logger.new(IO::NULL), use_deflater: false) + def rack_app(exporter = self, path: "/metrics", logger: Logger.new(IO::NULL)) ::Rack::Builder.new do - use ::Rack::Deflater if use_deflater use ::Rack::CommonLogger, logger use ::Rack::ShowExceptions use exporter, **exporter_options @@ -53,7 +52,7 @@ def rack_app(exporter = self, path: "/metrics", logger: Logger.new(IO::NULL), us end end - def start_app(raise_start_error: true, **rack_app_options) + def start_app(**rack_app_options) default_port = ENV.fetch("PORT", 9394) ::Rack::Handler::WEBrick.run( rack_app(**rack_app_options), @@ -61,9 +60,6 @@ def start_app(raise_start_error: true, **rack_app_options) Port: ENV.fetch("PROMETHEUS_EXPORTER_PORT", default_port), AccessLog: [], ) - rescue Errno::EADDRINUSE - puts "Yabeda prometheus exporter: Failed to start, server might be started by another process" - raise if raise_start_error end end diff --git a/lib/yabeda/prometheus/version.rb b/lib/yabeda/prometheus/version.rb index db465bd..a765f27 100644 --- a/lib/yabeda/prometheus/version.rb +++ b/lib/yabeda/prometheus/version.rb @@ -2,6 +2,6 @@ module Yabeda module Prometheus - VERSION = "0.9.0" + VERSION = "0.8.0" end end From 82d277541c5920d7cca286cfa5308bd7822d94c1 Mon Sep 17 00:00:00 2001 From: Josh Etsenake Date: Thu, 10 Nov 2022 07:31:56 -0500 Subject: [PATCH 6/7] remove unneeded change --- lib/yabeda/prometheus/exporter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/yabeda/prometheus/exporter.rb b/lib/yabeda/prometheus/exporter.rb index 5f2bbf0..90d1bce 100644 --- a/lib/yabeda/prometheus/exporter.rb +++ b/lib/yabeda/prometheus/exporter.rb @@ -33,7 +33,6 @@ def start_server_in_thread!(**rack_app_options) end end - def rack_app(exporter = self, logger: Logger.new(IO::NULL), use_deflater: true, **exporter_options) def start_server_in_process!(**rack_app_options) pid = Process.fork do # re-configure yabeda since we're in a new process @@ -43,8 +42,9 @@ def start_server_in_process!(**rack_app_options) Process.detach(pid) if pid end - def rack_app(exporter = self, path: "/metrics", logger: Logger.new(IO::NULL)) + def rack_app(exporter = self, logger: Logger.new(IO::NULL), use_deflater: true, **exporter_options) ::Rack::Builder.new do + use ::Rack::Deflater if use_deflater use ::Rack::CommonLogger, logger use ::Rack::ShowExceptions use exporter, **exporter_options From 60ffb2319e9d0a2e1374ce6f5de42050dde313c1 Mon Sep 17 00:00:00 2001 From: Josh Etsenake Date: Thu, 10 Nov 2022 08:31:34 -0500 Subject: [PATCH 7/7] call configure if not already configured in a new process --- lib/yabeda/prometheus/exporter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/yabeda/prometheus/exporter.rb b/lib/yabeda/prometheus/exporter.rb index 90d1bce..c148e67 100644 --- a/lib/yabeda/prometheus/exporter.rb +++ b/lib/yabeda/prometheus/exporter.rb @@ -35,8 +35,8 @@ def start_server_in_thread!(**rack_app_options) def start_server_in_process!(**rack_app_options) pid = Process.fork do - # re-configure yabeda since we're in a new process - Yabeda.configure! + # configure yabeda if its not already configured + Yabeda.configure! unless Yabeda.already_configured? start_app(**rack_app_options) end Process.detach(pid) if pid