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

Yabeda integration for prom metrics #1241

Merged
merged 16 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ SECRET_KEY_BASE=8ea53ad3bc6c03923e376c8bdd85059c1885524947a7efe53d5e9c9d4e398611
EXTERNAL_PORT=8080
SCC_USERNAME=
SCC_PASSWORD=
COLLECT_PROMETHEUS_METRICS=
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ 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-sidekiq'
gem 'yabeda-prometheus'

gem 'strong_migrations'

group :development, :test do
Expand Down
37 changes: 37 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.6)
public_suffix (>= 2.0.2, < 6.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 All @@ -48,6 +50,7 @@ GEM
config (3.0.0)
deep_merge (~> 1.2, >= 1.2.1)
dry-validation (~> 1.0, >= 1.0.0)
connection_pool (2.4.1)
coveralls (0.8.23)
json (>= 1.8, < 3)
simplecov (~> 0.16.1)
Expand Down Expand Up @@ -173,6 +176,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 @@ -201,6 +206,7 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
rdiscount (2.2.0.2)
redis (4.8.1)
regexp_parser (2.6.0)
repomd_parser (1.1.0)
bzip2-ffi (~> 1.1.1)
Expand Down Expand Up @@ -267,6 +273,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 All @@ -281,6 +288,10 @@ GEM
shellany (0.0.1)
shoulda-matchers (4.5.1)
activesupport (>= 4.2.0)
sidekiq (6.5.12)
connection_pool (>= 2.2.5, < 3)
rack (~> 2.0)
redis (>= 4.5.0, < 5)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
Expand Down Expand Up @@ -321,6 +332,27 @@ 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)
yabeda-sidekiq (0.12.0)
anyway_config (>= 1.3, < 3)
sidekiq
yabeda (~> 0.6)
yard (0.9.35)
zeitwerk (2.6.15)
zstd-ruby (1.5.6.1)
Expand Down Expand Up @@ -382,6 +414,11 @@ DEPENDENCIES
vcr (~> 6.0)
versionist
webmock
yabeda
yabeda-prometheus
yabeda-puma-plugin
yabeda-rails
yabeda-sidekiq

BUNDLED WITH
1.17.3
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# require "action_cable/engine"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"
require_relative '../lib/yabeda/prometheus_patch'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand Down
82 changes: 82 additions & 0 deletions config/initializers/yabeda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

return if Settings.scc.try(:metrics) && (Settings.scc.metrics.try(:enabled) && (!!Settings.scc.metrics.enabled == false))

# require 'yabeda'
# require 'yabeda/rails'
# require 'yabeda/sidekiq'
# require 'yabeda/prometheus'

# 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 :sidekiq, &assign_labels
group :rails, &assign_labels
group :rake, &assign_labels
group :scc, &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

group :rake do
gauge :task_started_at,
comment: 'Time when the task started: unix time with decimals',
tags: %i[task_name]

gauge :task_exit_status,
comment: 'Task exit status. 1 means OK, 0 means failed, not present means no data',
tags: %i[task_name]

gauge :task_duration_ms,
comment: 'Time taken running the task',
tags: %i[task_name]

gauge :task_finished_at,
comment: 'Time when the task finished: unix time with decimals',
tags: %i[task_name]
end

group :pct do

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

ActiveSupport::Notifications.subscribe 'task_run.rake' do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
task_name = event.payload[:task_name]

success = 1
if !!(event.payload[:exception] || event.payload[:exception_class])
success = 0
end

Yabeda.rake.task_duration_ms.set({ task_name: task_name }, event.duration)
Yabeda.rake.task_started_at.set({ task_name: task_name }, event.time / 1000.0)
Yabeda.rake.task_finished_at.set({ task_name: task_name }, event.end / 1000.0)

Yabeda.rake.task_exit_status.set({ task_name: task_name }, success)
end
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.scc.try(:metrics) && (Settings.scc.metrics.try(:enabled) && (!!Settings.scc.metrics.enabled == false))
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
8 changes: 8 additions & 0 deletions config/rmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ scc:
username: <%= ENV['SCC_USERNAME'] %>
password: <%= ENV['SCC_PASSWORD'] %>
sync_systems: true
metrics:
enabled: true
interval: 60
job_name: yabeda
url: <%= ENV.fetch('PROMETHEUS_ENDPOINT') { 'http://localhost:9091' } %>
username: <%= ENV.fetch('PROMETHEUS_USERNAME') { 'admin' }%>
password: <%= ENV.fetch('PROMETHEUS_PASSWORD') { 'admin' }%>


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
25 changes: 25 additions & 0 deletions lib/yabeda/prometheus_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'yabeda'
require 'yabeda/prometheus'
require 'prometheus/client/push'

module Yabeda
module PrometheusPatch
def patched_push_gateway(grouping_key: {})
@push_gateway ||= # rubocop:disable Naming/MemoizedInstanceVariableName
::Prometheus::Client::Push.new(
job: Settings.scc.metrics.job_name,
gateway:Settings.scc.metrics.url,
grouping_key: grouping_key,
open_timeout: 5, read_timeout: 30
)
end

def self.apply!(mod)
mod.extend(self)
end
end
end

Yabeda::PrometheusPatch.apply!(Yabeda::Prometheus)
7 changes: 7 additions & 0 deletions package/obs/rmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ scc:
username:
password:
sync_systems: true
metrics:
enabled: false
interval: 60
job_name: yabeda
url: https://foo.com/metrics
username:
password:

mirroring:
mirror_src: false
Expand Down
Loading