DEV Community

Jeffrey Hicks
Jeffrey Hicks

Posted on • Edited on

Rails Performance Monitoring w/ Prometheus

Image description

gem yabeda-prometheus

Rails 7.1 Compatibility

Rails 7 excludes Webrick which yabeda uses. I was able to monkey-patch this to get it working.

# /config/initializers/yabeda_prometheus.rb

module Yabeda
  module Prometheus
    class Exporter
      class << self
        def start_metrics_server!(**rack_app_options)
          Thread.new do
            default_port = ENV.fetch("PORT", 9394)
            app = rack_app(**rack_app_options)
            server = Puma::Server.new(app)
            server.add_tcp_listener(ENV["PROMETHEUS_EXPORTER_BIND"] || "0.0.0.0", ENV.fetch("PROMETHEUS_EXPORTER_PORT", default_port))
            server.run
          end
        end
      end
    end
  end
end

Yabeda::Prometheus::Exporter.start_metrics_server!
Enter fullscreen mode Exit fullscreen mode

Alternative work around

Found this gist for running prometheus exporter as stand-alone process. Might give this a try instead especially if I got with a multi-process instead of multi-thread env.

# Example of how to launch separate process to export all metrics from Ruby processes on the same host/container.

# IMPORTANT:
# You MUST configure Direct File store in official Prometheus Ruby client for this to work.
# See https://github.com/yabeda-rb/yabeda-prometheus#multi-process-server-support

# Example of configuration that should be made for additional process to export all metrics
# Here: export Sidekiq metrics from not worker process. See https://github.com/yabeda-rb/yabeda-sidekiq#configuration
ENV["YABEDA_SIDEKIQ_COLLECT_CLUSTER_METRICS"] = 'true'
ENV["YABEDA_SIDEKIQ_DECLARE_PROCESS_METRICS"] = 'true'

# Boot Rails application
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require_relative '../config/environment'

# Run exporter
default_port = ENV.fetch('PORT', 9394)
::Rack::Handler::WEBrick.run(
  Yabeda::Prometheus::Exporter.rack_app,
  Host: ENV['PROMETHEUS_EXPORTER_BIND'] || '0.0.0.0',
  Port: ENV.fetch('PROMETHEUS_EXPORTER_PORT', default_port),
  AccessLog: []
)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)