From 31ad58baf55d2ecc7e9b3516a612c3c669f42293 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Guthikonda Date: Tue, 5 Dec 2023 18:42:11 -0800 Subject: [PATCH] Added support for rack v3 --- lib/yabeda/prometheus/mmap/exporter.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/yabeda/prometheus/mmap/exporter.rb b/lib/yabeda/prometheus/mmap/exporter.rb index 6337e06..ac7cda5 100644 --- a/lib/yabeda/prometheus/mmap/exporter.rb +++ b/lib/yabeda/prometheus/mmap/exporter.rb @@ -23,7 +23,9 @@ def call(env) def start_metrics_server! Thread.new do default_port = ENV.fetch('PORT', 9394) - ::Rack::Handler::WEBrick.run( + rack_handler = determine_rack_handler + + rack_handler.run( rack_app, Host: ENV['PROMETHEUS_EXPORTER_BIND'] || '0.0.0.0', Port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', default_port), @@ -32,6 +34,22 @@ def start_metrics_server! end end + def determine_rack_handler + rack_version = Gem.loaded_specs['rack'].version + + if rack_version >= Gem::Version.new('3.0') + begin + Gem::Specification.find_by_name('rackup') + require 'rackup' + ::Rackup::Handler::WEBrick + rescue Gem::MissingSpecError + ::Rack::Handler::WEBrick + end + else + ::Rack::Handler::WEBrick + end + end + def rack_app(exporter = self, path: '/metrics') ::Rack::Builder.new do use ::Rack::CommonLogger if ENV['PROMETHEUS_EXPORTER_LOG_REQUESTS'] != 'false'