Module: RailsPodKit::Sidekiq

Defined in:
lib/rails_pod_kit/sidekiq.rb

Overview

Sidekiq integration. Called from inside Sidekiq.configure_server:

RailsPodKit::Sidekiq.install!(config)

It:

- requires yabeda-sidekiq (registers the server middleware + metrics),
- applies the global-metrics policy (see Config#sidekiq_global_metrics),
- starts the in-process Prometheus exporter (WEBrick) bound to the
configured port so the worker pod serves /metrics itself.

No-op when disabled or in the test env so specs never bind the port.

Class Method Summary collapse

Class Method Details

.apply_global_metrics_policy!Object

Decide whether this Sidekiq worker collects the global (Redis-wide) queue metrics. Only the :all policy keeps every worker collecting them; under :web they come from the web process instead, and under :off nobody does. Either way per-process job metrics are unaffected.



56
57
58
59
60
# File 'lib/rails_pod_kit/sidekiq.rb', line 56

def apply_global_metrics_policy!
  collect = RailsPodKit.config.sidekiq_global_metrics == :all
  Yabeda::Sidekiq.config.collect_cluster_metrics = collect
  apply_retries_segmentation!
end

.apply_retries_segmentation!Object

Mirror the gem's retries_segmented_by_queue flag onto yabeda-sidekiq. Must run before Yabeda.configure! so the retry gauge is declared with (or without) the queue tag. Off by default — see RailsPodKit::Config.



65
66
67
68
# File 'lib/rails_pod_kit/sidekiq.rb', line 65

def apply_retries_segmentation!
  Yabeda::Sidekiq.config.retries_segmented_by_queue =
    RailsPodKit.config.retries_segmented_by_queue
end

.enable_global_collection!Object

Enables global queue-metric collection in a process that is NOT a Sidekiq server — i.e. the web (Puma) process under the :web policy. The web has the Sidekiq client configured (Redis access), so yabeda-sidekiq can read the cluster stats there. We force collect_cluster_metrics on and keep declare_process_metrics off (the web runs no jobs). Must run before Yabeda.configure! so the gauges are declared.



76
77
78
79
80
81
82
# File 'lib/rails_pod_kit/sidekiq.rb', line 76

def enable_global_collection!
  require 'yabeda/sidekiq'

  Yabeda::Sidekiq.config.collect_cluster_metrics = true
  Yabeda::Sidekiq.config.declare_process_metrics = false
  apply_retries_segmentation!
end

.install!(sidekiq_config = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails_pod_kit/sidekiq.rb', line 20

def install!(sidekiq_config = nil)
  return unless RailsPodKit.enabled?

  require 'yabeda/sidekiq'
  require 'yabeda/prometheus/mmap'

  # Must run before Yabeda.configure! so the cluster-metric gauges are gated
  # correctly (they are only declared when the policy leaves them enabled).
  apply_global_metrics_policy!

  # We can't rely on yabeda's Railtie to call Yabeda.configure!: depending on
  # boot order it may never register. Drive it from Sidekiq's :startup
  # lifecycle event, which fires after every initializer (so the sidekiq
  # collector is registered) but before any job runs.
  install_configure_hook(sidekiq_config)

  start_metrics_server!
end

.install_configure_hook(sidekiq_config) ⇒ Object

Registers the Yabeda.configure! driver on Sidekiq's :startup event. The real app always passes a Sidekiq config that responds to :on; when it doesn't (e.g. unit specs that pass nil) we skip, so the global Yabeda singleton isn't configured as a test side effect.



43
44
45
46
47
48
49
50
# File 'lib/rails_pod_kit/sidekiq.rb', line 43

def install_configure_hook(sidekiq_config)
  return unless sidekiq_config.respond_to?(:on)

  sidekiq_config.on(:startup) do
    require 'yabeda'
    Yabeda.configure! unless Yabeda.already_configured?
  end
end

.start_metrics_server!Object

Starts the background WEBrick exporter. start_metrics_server! reads the bind port from PROMETHEUS_EXPORTER_PORT, so we set it from config first. Guarded so a re-entrant Sidekiq boot doesn't try to double-bind the port.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rails_pod_kit/sidekiq.rb', line 87

def start_metrics_server!
  return if @server_started

  ENV['PROMETHEUS_EXPORTER_PORT'] ||= RailsPodKit.config.port.to_s
  # Drop the WEBrick exporter's per-scrape access log (Rack::CommonLogger,
  # which the mmap exporter mounts unless this is exactly 'false'). See
  # Config#silence_exporter_access_log.
  ENV['PROMETHEUS_EXPORTER_LOG_REQUESTS'] = 'false' if RailsPodKit.config.silence_exporter_access_log
  Yabeda::Prometheus::Exporter.start_metrics_server!
  @server_started = true
end