From 57af294b01e4ba0f03c6165d8eef3c4f71f2818c Mon Sep 17 00:00:00 2001 From: fredericfran-gds Date: Wed, 3 Aug 2022 15:37:52 +0100 Subject: [PATCH] Add Sidekiq Prometheus instrumentation If Sidekiq is present, add the Sidekiq Prometheus instrumentation. Ref: 1. [trello card](https://trello.com/c/K8aUtwz4/974-graphs-for-sidekiq-queues-in-grafana) --- lib/govuk_app_config.rb | 2 +- .../govuk_prometheus_exporter.rb | 24 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/govuk_app_config.rb b/lib/govuk_app_config.rb index e0ba6841..1b3c653e 100644 --- a/lib/govuk_app_config.rb +++ b/lib/govuk_app_config.rb @@ -6,9 +6,9 @@ # This require is deprecated and should be removed on next major version bump # and should be required by applications directly. require "govuk_app_config/govuk_unicorn" +require "govuk_app_config/govuk_prometheus_exporter" if defined?(Rails) - require "govuk_app_config/govuk_prometheus_exporter" require "govuk_app_config/govuk_logging" require "govuk_app_config/govuk_content_security_policy" require "govuk_app_config/railtie" diff --git a/lib/govuk_app_config/govuk_prometheus_exporter.rb b/lib/govuk_app_config/govuk_prometheus_exporter.rb index d036d964..21813407 100644 --- a/lib/govuk_app_config/govuk_prometheus_exporter.rb +++ b/lib/govuk_app_config/govuk_prometheus_exporter.rb @@ -1,14 +1,34 @@ module GovukPrometheusExporter def self.configure - unless Rails.env == "test" || (ENV["GOVUK_PROMETHEUS_EXPORTER"]) != "true" + if (ENV["GOVUK_PROMETHEUS_EXPORTER"]) == "true" + return if defined?(Rails) ? Rails.env == "test" : false + require "prometheus_exporter" require "prometheus_exporter/server" require "prometheus_exporter/middleware" + if defined?(Sidekiq) + Sidekiq.configure_server do |config| + require "prometheus_exporter/instrumentation" + config.server_middleware do |chain| + chain.add PrometheusExporter::Instrumentation::Sidekiq + end + config.death_handlers << PrometheusExporter::Instrumentation::Sidekiq.death_handler + config.on :startup do + PrometheusExporter::Instrumentation::Process.start type: "sidekiq" + PrometheusExporter::Instrumentation::SidekiqProcess.start + PrometheusExporter::Instrumentation::SidekiqQueue.start + PrometheusExporter::Instrumentation::SidekiqStats.start + end + end + end + server = PrometheusExporter::Server::WebServer.new bind: "0.0.0.0", port: 9394 server.start - Rails.application.middleware.unshift PrometheusExporter::Middleware + if defined?(Rails) + Rails.application.middleware.unshift PrometheusExporter::Middleware + end end end end