From 055faac14f8482581adf3d6ef58b5e6b2984cc09 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Wed, 20 Nov 2024 12:02:31 +0100 Subject: [PATCH 1/4] 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 From da19196660b2ac1a1f29478f997802ebcc78ad02 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Wed, 20 Nov 2024 13:25:58 +0100 Subject: [PATCH 2/4] Start Prometheus exporter along with the Rails server --- .app_version | 2 +- CHANGELOG.md | 6 ++++++ Gemfile | 2 +- Procfile.dev | 1 + config/puma.rb | 2 +- 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.app_version b/.app_version index 19270385..c3f65805 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.16.5 +0.16.6 diff --git a/CHANGELOG.md b/CHANGELOG.md index e1863d71..47a89c8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.16.6 - 2024-11-20 + +### Added + +- Dawarich now exports metrics to Prometheus. You can find the metrics at `/metrics` endpoint. The metrics are being exported in the Prometheus format and can be scraped by Prometheus server. You can find an example of how to scrape the metrics in the `docker-compose.yml` file in the repository. + # 0.16.5 - 2024-11-18 ### Changed diff --git a/Gemfile b/Gemfile index d831ec9d..a1a27f8c 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,6 @@ 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' @@ -19,6 +18,7 @@ gem 'kaminari' gem 'lograge' gem 'oj' gem 'pg' +gem 'prometheus_exporter' gem 'puma' gem 'pundit' gem 'rails' diff --git a/Procfile.dev b/Procfile.dev index e6096674..2f1921fb 100644 --- a/Procfile.dev +++ b/Procfile.dev @@ -1 +1,2 @@ web: bin/rails server -p 3000 -b 0.0.0.0 +prometheus_exporter: bundle exec prometheus_exporter diff --git a/config/puma.rb b/config/puma.rb index 08673ab4..683d0ba0 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -42,7 +42,7 @@ 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 +# Promethues exporter after_worker_boot do require 'prometheus_exporter/instrumentation' PrometheusExporter::Instrumentation::Process.start(type:"web") From 6925e5598b28e26530ef7583ed67b07ce7bd5d54 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Wed, 20 Nov 2024 13:56:19 +0100 Subject: [PATCH 3/4] Allow enabling and disabling prometheus exporter in docker-compose.yml --- CHANGELOG.md | 11 ++++++++++- Procfile.dev | 1 - Procfile.prometheus.dev | 2 ++ bin/dev | 8 +++++++- docker-compose.yml | 1 + 5 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 Procfile.prometheus.dev diff --git a/CHANGELOG.md b/CHANGELOG.md index 47a89c8f..8aa52b7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added -- Dawarich now exports metrics to Prometheus. You can find the metrics at `/metrics` endpoint. The metrics are being exported in the Prometheus format and can be scraped by Prometheus server. You can find an example of how to scrape the metrics in the `docker-compose.yml` file in the repository. +- Dawarich now can export metrics to Prometheus. You can find the metrics at `your.host:9394/metrics` endpoint. The metrics are being exported in the Prometheus format and can be scraped by Prometheus server. To enable exporting, set the `PROMETHEUS_EXPORTER_ENABLED` env var in your docker-compose.yml to `true`. Example: + +```yaml + dawarich_app: + image: freikin/dawarich:latest + container_name: dawarich_app + environment: + ... + PROMETHEUS_EXPORTER_ENABLED: "true" +``` # 0.16.5 - 2024-11-18 diff --git a/Procfile.dev b/Procfile.dev index 2f1921fb..e6096674 100644 --- a/Procfile.dev +++ b/Procfile.dev @@ -1,2 +1 @@ web: bin/rails server -p 3000 -b 0.0.0.0 -prometheus_exporter: bundle exec prometheus_exporter diff --git a/Procfile.prometheus.dev b/Procfile.prometheus.dev new file mode 100644 index 00000000..2f1921fb --- /dev/null +++ b/Procfile.prometheus.dev @@ -0,0 +1,2 @@ +web: bin/rails server -p 3000 -b 0.0.0.0 +prometheus_exporter: bundle exec prometheus_exporter diff --git a/bin/dev b/bin/dev index 948d9bc6..8aa6cf2b 100755 --- a/bin/dev +++ b/bin/dev @@ -6,4 +6,10 @@ then gem install foreman fi -foreman start -f Procfile.dev +if [ "$PROMETHEUS_EXPORTER_ENABLED" = "true" ]; then + echo "Starting Foreman with Procfile.prometheus.dev..." + foreman start -f Procfile.prometheus.dev +else + echo "Starting Foreman with Procfile.dev..." + foreman start -f Procfile.dev +fi diff --git a/docker-compose.yml b/docker-compose.yml index 7324b3b8..bd3d2049 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -65,6 +65,7 @@ services: DISTANCE_UNIT: km PHOTON_API_HOST: photon.komoot.io PHOTON_API_USE_HTTPS: true + PROMETHEUS_EXPORTER_ENABLED: false logging: driver: "json-file" options: From cf5b414ae46d1b3f324518049cda2c666c63a895 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Wed, 20 Nov 2024 17:16:26 +0100 Subject: [PATCH 4/4] Use single condition in the prometheus initializer file --- config/initializers/prometheus.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/config/initializers/prometheus.rb b/config/initializers/prometheus.rb index 9d25d4ea..a8ffc08d 100644 --- a/config/initializers/prometheus.rb +++ b/config/initializers/prometheus.rb @@ -1,15 +1,12 @@ # in config/initializers/prometheus.rb if Rails.env != "test" require 'prometheus_exporter/middleware' + require 'prometheus_exporter/instrumentation' # 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 + # 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