From 055faac14f8482581adf3d6ef58b5e6b2984cc09 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Wed, 20 Nov 2024 12:02:31 +0100 Subject: [PATCH] Add prometheus_exporter gem and instrumentation to the project --- Gemfile | 1 + Gemfile.lock | 4 ++++ config/initializers/prometheus.rb | 15 +++++++++++++++ config/initializers/sidekiq.rb | 5 +++++ config/puma.rb | 6 ++++++ 5 files changed, 31 insertions(+) create mode 100644 config/initializers/prometheus.rb diff --git a/Gemfile b/Gemfile index 45a72399..d831ec9d 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby File.read('.ruby-version').strip gem 'bootsnap', require: false +gem 'prometheus_exporter' gem 'chartkick' gem 'data_migrate' gem 'devise' diff --git a/Gemfile.lock b/Gemfile.lock index 091c2c14..551e0ff4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -233,6 +233,8 @@ GEM patience_diff (1.2.0) optimist (~> 3.0) pg (1.5.9) + prometheus_exporter (2.1.1) + webrick pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -408,6 +410,7 @@ GEM addressable (>= 2.8.0) crack (>= 0.3.2) hashdiff (>= 0.4.0, < 2.0.0) + webrick (1.9.0) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) @@ -441,6 +444,7 @@ DEPENDENCIES lograge oj pg + prometheus_exporter pry-byebug pry-rails puma diff --git a/config/initializers/prometheus.rb b/config/initializers/prometheus.rb new file mode 100644 index 00000000..9d25d4ea --- /dev/null +++ b/config/initializers/prometheus.rb @@ -0,0 +1,15 @@ +# in config/initializers/prometheus.rb +if Rails.env != "test" + require 'prometheus_exporter/middleware' + + # This reports stats per request like HTTP status and timings + Rails.application.middleware.unshift PrometheusExporter::Middleware +end + +if Rails.env != "test" + require 'prometheus_exporter/instrumentation' + + # this reports basic process stats like RSS and GC info, type master + # means it is instrumenting the master process + PrometheusExporter::Instrumentation::Process.start(type: "master") +end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 3d2e4741..d3c9bbea 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -2,6 +2,11 @@ Sidekiq.configure_server do |config| config.redis = { url: ENV['REDIS_URL'] } + + config.server_middleware do |chain| + require 'prometheus_exporter/instrumentation' + chain.add PrometheusExporter::Instrumentation::Sidekiq + end end Sidekiq.configure_client do |config| diff --git a/config/puma.rb b/config/puma.rb index daaf0369..08673ab4 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -41,3 +41,9 @@ pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } # Allow puma to be restarted by `bin/rails restart` command. plugin :tmp_restart + +# in unicorn/puma/passenger be sure to run a new process instrumenter after fork +after_worker_boot do + require 'prometheus_exporter/instrumentation' + PrometheusExporter::Instrumentation::Process.start(type:"web") +end