Module: GovukPrometheusExporter

Defined in:
lib/govuk_app_config/govuk_prometheus_exporter.rb

Defined Under Namespace

Classes: RailsMiddleware, SinatraMiddleware

Class Method Summary collapse

Class Method Details

.configure(collectors: [], default_aggregation: PrometheusExporter::Metric::Histogram) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/govuk_app_config/govuk_prometheus_exporter.rb', line 48

def self.configure(collectors: [], default_aggregation: PrometheusExporter::Metric::Histogram)
  return unless should_configure

  # PrometheusExporter::Metric::Histogram.DEFAULT_BUCKETS tops out at 10 but
  # we have a few controller actions which are slower than this, so we add a
  # few extra buckets for slower requests
  PrometheusExporter::Metric::Histogram.default_buckets = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 25, 50].freeze
  PrometheusExporter::Metric::Base.default_aggregation = default_aggregation

  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
    collectors.each { |c| server.collector.register_collector(c.new) }
    server.start

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

    if defined?(Sinatra)
      Sinatra.use SinatraMiddleware
    end
  rescue Errno::EADDRINUSE
    warn "not starting Prometheus metrics server: address already in use"
  end
end

.should_configureObject



44
45
46
# File 'lib/govuk_app_config/govuk_prometheus_exporter.rb', line 44

def self.should_configure
  !(defined?(Rails) && Rails.const_defined?("Console"))
end