Skip to content

Commit

Permalink
Merge pull request #1241 from SUSE/test_yabeda
Browse files Browse the repository at this point in the history
Yabeda integration for prom metrics
  • Loading branch information
ngetahun authored Nov 7, 2024
2 parents 88c81c2 + ddbb38f commit ac5c206
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ SECRET_KEY_BASE=8ea53ad3bc6c03923e376c8bdd85059c1885524947a7efe53d5e9c9d4e398611
EXTERNAL_PORT=8080
SCC_USERNAME=
SCC_PASSWORD=
RMT_METRICS_ENABLED=
PROMETHEUS_JOB_NAME=
6 changes: 3 additions & 3 deletions .github/workflows/lint-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ jobs:
- name: Run core tests
run: |
bundle exec rspec --format documentation
bundle exec rake test:core
- name: Run PubCloud engines tests
run: |
bundle exec rake test:engines
Expand All @@ -91,4 +91,4 @@ jobs:
run: |
echo "::group::Version verification checks"
ruby ci/check-version-matches.rb
echo "::endgroup::"
echo "::endgroup::"
4 changes: 4 additions & 0 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ unless ENV['NO_COVERAGE']
# omit registration sharing (removing systems using rmt-cli)
add_filter('engines/registration_sharing/lib/registration_sharing.rb')

add_filter('lib/rmt/db.rb')
add_filter('lib/rmt.rb')
add_filter('config')

track_files('app/**/*.rb')
track_files('lib/**/*.rb')
end
Expand Down
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ gem 'repomd_parser', '~> 1.1.0'
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

# Prometheus Exporter:
gem 'yabeda'
gem 'yabeda-rails'
gem 'yabeda-puma-plugin'
gem 'yabeda-prometheus'

gem 'strong_migrations'

group :development, :test do
Expand Down
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ GEM
zeitwerk (~> 2.3)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
anyway_config (2.6.4)
ruby-next-core (~> 1.0)
ast (2.4.2)
awesome_print (1.9.2)
base32 (0.3.4)
Expand Down Expand Up @@ -173,6 +175,8 @@ GEM
prime (0.1.2)
forwardable
singleton
prometheus-client (4.2.3)
base64
pry (0.14.0)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -266,6 +270,7 @@ GEM
rubocop (~> 1.19)
rubocop-thread_safety (0.4.4)
rubocop (>= 0.53.0)
ruby-next-core (1.0.3)
ruby-progressbar (1.11.0)
ruby_parser (3.19.2)
sexp_processor (~> 4.16)
Expand Down Expand Up @@ -319,6 +324,23 @@ GEM
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
yabeda (0.13.1)
anyway_config (>= 1.0, < 3)
concurrent-ruby
dry-initializer
yabeda-prometheus (0.9.1)
prometheus-client (>= 3.0, < 5.0)
rack
yabeda (~> 0.10)
yabeda-puma-plugin (0.7.1)
json
puma
yabeda (~> 0.5)
yabeda-rails (0.9.0)
activesupport
anyway_config (>= 1.3, < 3)
railties
yabeda (~> 0.8)
yard (0.9.35)
zeitwerk (2.6.15)
zstd-ruby (1.5.6.1)
Expand Down Expand Up @@ -380,6 +402,10 @@ DEPENDENCIES
vcr (~> 6.0)
versionist
webmock
yabeda
yabeda-prometheus
yabeda-puma-plugin
yabeda-rails

BUNDLED WITH
1.17.3
38 changes: 38 additions & 0 deletions config/initializers/yabeda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# :nocov:
# frozen_string_literal: true

return unless Settings.dig(:scc, :metrics, :enabled)

# Configure prometheus client
Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new(
dir: './tmp/prometheus/'
)

# Configure yabeda
Yabeda.configure do
assign_labels = lambda {
default_tag :environment, Rails.env
default_tag :application, 'rmt'
}

group :rails, &assign_labels

group :rails do
counter :started_requests_total,
comment: 'A counter of the total number of HTTP requests rails has started to process.',
tags: %i[controller action format method]
end
end

# Instrument the request from the start
ActiveSupport::Notifications.subscribe 'start_processing.action_controller' do |*args|
# Match the same event as Yabeda
event = Yabeda::Rails::Event.new(*args)

Yabeda.rails.started_requests_total.tap do |metric|
labels = event.labels.slice(*metric.tags)

metric.increment(labels, by: 1)
end
end
# :nocov:
5 changes: 5 additions & 0 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rmt/config'
require_relative 'puma/prometheus'

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
Expand Down Expand Up @@ -36,3 +37,7 @@

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

if Settings.dig(:scc, :metrics, :enabled)
configure_prometheus!(self)
end
7 changes: 7 additions & 0 deletions config/puma/prometheus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def configure_prometheus!(puma)
ENV['STARTED_FROM_PUMA'] = '1'

puma.activate_control_app
puma.plugin :yabeda
puma.plugin :yabeda_prometheus
end
3 changes: 3 additions & 0 deletions config/rmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ scc:
username: <%= ENV['SCC_USERNAME'] %>
password: <%= ENV['SCC_PASSWORD'] %>
sync_systems: true
metrics:
enabled: <%= ENV.fetch('RMT_METRICS_ENABLED') { false } %>
job_name: <%= ENV.fetch('PROMETHEUS_JOB_NAME') { 'rmt-webserver' } %>

mirroring:
mirror_src: false
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
get '/v2/_catalog', to: 'registry/registry#catalog'
end

# For bin/rails s users: expose the metrics endpoint via rails instead of puma
if Rails.env.development? && ENV['STARTED_FROM_PUMA'].blank?
Rails.logger.info('Mounting Yabeda in Rails.routes')
mount Yabeda::Prometheus::Exporter, at: '/metrics'
end

if defined?(SccSumaApi::Engine)
mount SccSumaApi::Engine, at: '/api/scc'

Expand Down
2 changes: 2 additions & 0 deletions lib/rmt/config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# :nocov:
require 'config'
require_relative '../rmt'

Expand Down Expand Up @@ -77,3 +78,4 @@ def validate_int(value)
end
end
end
# :nocov:
5 changes: 4 additions & 1 deletion package/obs/rmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ scc:
username:
password:
sync_systems: true

metrics:
enabled: false
job_name: rmt

mirroring:
mirror_src: false
dedup_method: hardlink
Expand Down

0 comments on commit ac5c206

Please sign in to comment.