Sha256: 9a811610d8b8c54911e416adc8d1def1365cd07f3e1c2520dc750306f7aa308b

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

module GovukPrometheusExporter
  def self.should_configure
    # Allow us to force the Prometheus Exporter for persistent Rake tasks...
    if ENV["GOVUK_PROMETHEUS_EXPORTER"] == "force"
      true
    elsif File.basename($PROGRAM_NAME) == "rake" ||
        defined?(Rails) && (Rails.const_defined?("Console") || Rails.env == "test")
      false
    else
      ENV["GOVUK_PROMETHEUS_EXPORTER"] == "true"
    end
  end

  def self.configure
    return unless should_configure

    require "prometheus_exporter"
    require "prometheus_exporter/server"
    require "prometheus_exporter/middleware"

    if defined?(Sidekiq)
      Sidekiq.configure_server do |config|
        require "sidekiq/api"
        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

    begin
      server = PrometheusExporter::Server::WebServer.new bind: "0.0.0.0", port: 9394
      server.start

      if defined?(Rails)
        Rails.application.middleware.unshift PrometheusExporter::Middleware, instrument: :prepend
      end

      if defined?(Sinatra)
        Sinatra.use PrometheusExporter::Middleware
      end
    rescue Errno::EADDRINUSE
      warn "Warning: Could not connect to Prometheus Server"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
govuk_app_config-9.0.4 lib/govuk_app_config/govuk_prometheus_exporter.rb
govuk_app_config-9.0.3 lib/govuk_app_config/govuk_prometheus_exporter.rb
govuk_app_config-9.0.2 lib/govuk_app_config/govuk_prometheus_exporter.rb
govuk_app_config-9.0.1 lib/govuk_app_config/govuk_prometheus_exporter.rb
govuk_app_config-9.0.0 lib/govuk_app_config/govuk_prometheus_exporter.rb
govuk_app_config-8.1.1 lib/govuk_app_config/govuk_prometheus_exporter.rb