Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Yabeda server in a new process #22

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions lib/yabeda/prometheus/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ 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 start_server_in_process!(**rack_app_options)
pid = Process.fork do
# configure yabeda if its not already configured
Yabeda.configure! unless Yabeda.already_configured?
start_app(**rack_app_options)
end
Process.detach(pid) if pid
end

def rack_app(exporter = self, logger: Logger.new(IO::NULL), use_deflater: true, **exporter_options)
Expand All @@ -40,6 +51,16 @@ def rack_app(exporter = self, logger: Logger.new(IO::NULL), use_deflater: true,
run NOT_FOUND_HANDLER
end
end

def start_app(**rack_app_options)
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: [],
)
end
end

def initialize(app, options = {})
Expand Down