Skip to content

Commit

Permalink
Added support for rack v3
Browse files Browse the repository at this point in the history
  • Loading branch information
aroop committed Dec 6, 2023
1 parent 5edb9f1 commit 31ad58b
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion lib/yabeda/prometheus/mmap/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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'
Expand Down

0 comments on commit 31ad58b

Please sign in to comment.