diff --git a/Gemfile b/Gemfile index aa512bb..0572317 100644 --- a/Gemfile +++ b/Gemfile @@ -11,7 +11,7 @@ gem "addressable" gem "rack-ssl-enforcer" gem "clogger" gem "encrypted_strings" -gem "prometheus-client", require: "prometheus/middleware/exporter" +gem "prometheus-client", require: "prometheus/middleware/exporter", git: "https://github.com/stefansundin/prometheus-client.git", branch: "add-most_recent-aggregation" group :production do gem "airbrake", require: false diff --git a/Gemfile.lock b/Gemfile.lock index b3fa053..e029d60 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: https://github.com/stefansundin/prometheus-client.git + revision: 5256cdfb78d5bfec1b8fbb78dcc1a2eee6c8989f + branch: add-most_recent-aggregation + specs: + prometheus-client (1.0.0) + GEM remote: https://rubygems.org/ specs: @@ -30,8 +37,6 @@ GEM nio4r (2.5.2) powder (0.4.0) thor (>= 0.11.5) - prometheus-client (0.9.0) - quantile (~> 0.2.1) pry (0.12.2) coderay (~> 1.1.0) method_source (~> 0.9.0) @@ -41,7 +46,6 @@ GEM public_suffix (4.0.1) puma (4.3.1) nio4r (~> 2.0) - quantile (0.2.1) rack (2.0.8) rack-protection (2.0.7) rack @@ -78,7 +82,7 @@ DEPENDENCIES github-release-party newrelic_rpm powder - prometheus-client + prometheus-client! pry-remote puma rack diff --git a/app/github.rb b/app/github.rb index 80e1939..b145ce9 100644 --- a/app/github.rb +++ b/app/github.rb @@ -164,7 +164,7 @@ def self.graphql(query, variables, token=nil) }.to_json, headers) response = HTTPResponse.new(raw_response, uri.to_s) if response.json && response.json["data"] && response.json["data"]["rateLimit"] && response.json["data"]["rateLimit"]["remaining"] - $metrics[:ratelimit_remaining].set({}, response.json["data"]["rateLimit"]["remaining"]) + $metrics[:ratelimit_remaining].set(response.json["data"]["rateLimit"]["remaining"]) end return response end diff --git a/config/initializers/40-prometheus.rb b/config/initializers/40-prometheus.rb index f3076c9..73fc765 100644 --- a/config/initializers/40-prometheus.rb +++ b/config/initializers/40-prometheus.rb @@ -1,7 +1,21 @@ -# The app is currently run with multiple processes, which means that the /metrics endpoint is not consistent.. That's fine for now, at least with this one metric. +# frozen_string_literal: true + +# Use DirectFileStore if we run multiple processes +store_settings = {} +if ENV["WEB_CONCURRENCY"] + require "prometheus/client/data_stores/direct_file_store" + app_path = File.expand_path("../../..", __FILE__) + Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new(dir: "#{app_path}/tmp/prometheus/") + store_settings[:aggregation] = :most_recent + + # Clean up old metric files + Dir["#{app_path}/tmp/prometheus/*.bin"].each do |file_path| + File.unlink(file_path) + end +end prometheus = Prometheus::Client.registry $metrics = { - ratelimit_remaining: prometheus.gauge(:ratelimit_remaining, "Remaining GitHub ratelimit."), + ratelimit_remaining: prometheus.gauge(:ratelimit_remaining, store_settings: store_settings, docstring: "Remaining GitHub ratelimit."), } diff --git a/config/puma.rb b/config/puma.rb index 052bc95..1ea08e2 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -23,3 +23,12 @@ if ENV["LOG_ENABLED"] stdout_redirect("#{app_path}/log/puma-stdout.log", "#{app_path}/log/puma-stderr.log", true) end + +if ENV["WEB_CONCURRENCY"] + on_worker_shutdown do |index| + # Delete stale metric files on worker shutdown + Dir["#{app_path}/tmp/prometheus/*___#{Process.pid}.bin"].each do |file_path| + File.unlink(file_path) + end + end +end