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
-
.apply_global_metrics_policy! ⇒ Object
Decide whether this Sidekiq worker collects the global (Redis-wide) queue metrics.
-
.apply_retries_segmentation! ⇒ Object
Mirror the gem's
retries_segmented_by_queueflag onto yabeda-sidekiq. -
.enable_global_collection! ⇒ Object
Enables global queue-metric collection in a process that is NOT a Sidekiq server — i.e.
-
.export_policy_env!(collect_cluster_metrics:, declare_process_metrics: nil) ⇒ Object
Publishes the policy through yabeda-sidekiq's own env-backed config, and it has to happen before
require 'yabeda/sidekiq'. - .install!(sidekiq_config = nil) ⇒ Object
-
.install_configure_hook(sidekiq_config) ⇒ Object
Registers the Yabeda.configure! driver on Sidekiq's :startup event.
-
.start_metrics_server! ⇒ Object
Starts the background WEBrick exporter shared with the other non-Puma entry points; guarded there so a re-entrant Sidekiq boot can't double-bind the port.
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.
59 60 61 62 63 |
# File 'lib/rails_pod_kit/sidekiq.rb', line 59 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.
68 69 70 71 |
# File 'lib/rails_pod_kit/sidekiq.rb', line 68 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).
78 79 80 81 82 83 84 85 86 |
# File 'lib/rails_pod_kit/sidekiq.rb', line 78 def enable_global_collection! export_policy_env!(collect_cluster_metrics: true, declare_process_metrics: false) require 'yabeda/sidekiq' Yabeda::Sidekiq.config.collect_cluster_metrics = true Yabeda::Sidekiq.config.declare_process_metrics = false apply_retries_segmentation! end |
.export_policy_env!(collect_cluster_metrics:, declare_process_metrics: nil) ⇒ Object
Publishes the policy through yabeda-sidekiq's own env-backed config, and it
has to happen before require 'yabeda/sidekiq'.
yabeda-sidekiq declares its cluster gauges inside its own Yabeda.configure
block, guarded by collect_cluster_metrics — and when Yabeda has already
been configured (any host where its Railtie ran first) requiring the file
evaluates that block immediately. Assigning the flag afterwards is then too
late: the gauges are never declared, while the collect block — which reads
the same flag on every scrape — starts referencing them, and /metrics 500s
with a NameError. The env values are read when the config object is built,
so setting them here makes the declaration see the policy whenever the
require lands.
100 101 102 103 104 |
# File 'lib/rails_pod_kit/sidekiq.rb', line 100 def export_policy_env!(collect_cluster_metrics:, declare_process_metrics: nil) ENV['YABEDA_SIDEKIQ_COLLECT_CLUSTER_METRICS'] = collect_cluster_metrics.to_s ENV['YABEDA_SIDEKIQ_DECLARE_PROCESS_METRICS'] = declare_process_metrics.to_s unless declare_process_metrics.nil? ENV['YABEDA_SIDEKIQ_RETRIES_SEGMENTED_BY_QUEUE'] = RailsPodKit.config.retries_segmented_by_queue.to_s end |
.install!(sidekiq_config = nil) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rails_pod_kit/sidekiq.rb', line 21 def install!(sidekiq_config = nil) return unless RailsPodKit.enabled? export_policy_env!(collect_cluster_metrics: RailsPodKit.config.sidekiq_global_metrics == :all) 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.
46 47 48 49 50 51 52 53 |
# File 'lib/rails_pod_kit/sidekiq.rb', line 46 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 shared with the other non-Puma entry points; guarded there so a re-entrant Sidekiq boot can't double-bind the port.
109 110 111 |
# File 'lib/rails_pod_kit/sidekiq.rb', line 109 def start_metrics_server! RailsPodKit::Exporter.start! end |