Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.

Commit

Permalink
Update prometheus-client. Use my fork with the :most_recent aggregation.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefansundin committed Dec 23, 2019
1 parent d72480c commit 936d4d6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -78,7 +82,7 @@ DEPENDENCIES
github-release-party
newrelic_rpm
powder
prometheus-client
prometheus-client!
pry-remote
puma
rack
Expand Down
2 changes: 1 addition & 1 deletion app/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions config/initializers/40-prometheus.rb
Original file line number Diff line number Diff line change
@@ -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."),
}
9 changes: 9 additions & 0 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 936d4d6

Please sign in to comment.