Skip to content

Commit

Permalink
Enhance Compatibility with Rack 3 and Maintain Support for Rack 2 (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Novikov <envek@envek.name>
  • Loading branch information
aroop and Envek authored Dec 11, 2023
1 parent 5edb9f1 commit 9d84af2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/yabeda/prometheus/mmap/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def call(env)
def start_metrics_server!
Thread.new do
default_port = ENV.fetch('PORT', 9394)
::Rack::Handler::WEBrick.run(

rack_handler.run(
rack_app,
Host: ENV['PROMETHEUS_EXPORTER_BIND'] || '0.0.0.0',
Port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', default_port),
Expand All @@ -32,6 +33,18 @@ def start_metrics_server!
end
end

def rack_handler
if Gem.loaded_specs['rack']&.version&.>= Gem::Version.new('3.0')
require 'rackup'
::Rackup::Handler::WEBrick
else
::Rack::Handler::WEBrick
end
rescue LoadError
warn 'Please add gems rackup and webrick to your Gemfile to expose Yabeda metrics from prometheus-mmap'
::Rack::Handler::WEBrick
end

def rack_app(exporter = self, path: '/metrics')
::Rack::Builder.new do
use ::Rack::CommonLogger if ENV['PROMETHEUS_EXPORTER_LOG_REQUESTS'] != 'false'
Expand Down

0 comments on commit 9d84af2

Please sign in to comment.