Skip to content

Commit

Permalink
9.10.0: Always initialise GovukPrometheusExporter.
Browse files Browse the repository at this point in the history
Some client apps have started to assume that the Prometheus "exporter"
process (i.e. the process that aggregates counters in multi-process
apps) is always available, including in tests.

Now that we handle the case where the `/metrics` port is already taken,
there's really no reason not to initialise GovukPrometheusExporter.

Remove the confusing heuristics in
`GovukPrometheusExporter.should_configure` and just always attempt to
initialise unless we're running under `rails console`. When the init
fails, we just print a warning and continue anyway, so this should be a
net win for robustness.

If we find edge cases where this still needs to be configurable then I
propose we address those if/when we find and understand them. I expect
we won't need to, though.
  • Loading branch information
sengi committed Mar 26, 2024
1 parent e998e34 commit 3c6dd35
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 9.10.0

* Simplify the logic for deciding whether to initialize `GovukPrometheusExporter`. `GovukPrometheusExporter` provides the `/metrics` webserver and "exporter" process for aggregating counters in multi-process apps. govuk_app_config will now always attempt to initialize GovukPrometheusExporter except when running under `rails console`. The `GOVUK_PROMETHEUS_EXPORTER` environment variable no longer has any effect.

# 9.9.2

* Add single cookie consent api URLs ([#355](https://github.com/alphagov/govuk_app_config/pull/355))
Expand Down
16 changes: 3 additions & 13 deletions lib/govuk_app_config/govuk_prometheus_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@ def default_labels(_env, _result)
end

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
!(defined?(Rails) && Rails.const_defined?("Console"))
end

def self.configure(collectors: [], default_aggregation: PrometheusExporter::Metric::Histogram)
Expand Down Expand Up @@ -81,9 +73,7 @@ def self.configure(collectors: [], default_aggregation: PrometheusExporter::Metr

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

collectors.each { |collector| server.collector.register_collector(collector.new) }

collectors.each { |c| server.collector.register_collector(c.new) }
server.start

if defined?(Rails)
Expand All @@ -94,7 +84,7 @@ def self.configure(collectors: [], default_aggregation: PrometheusExporter::Metr
Sinatra.use SinatraMiddleware
end
rescue Errno::EADDRINUSE
warn "Could not start Prometheus metrics server as address already in use."
warn "not starting Prometheus metrics server: address already in use"
end
end
end

0 comments on commit 3c6dd35

Please sign in to comment.