From e4fb16218693edd4382b96482ba94eba6c8a7f1a Mon Sep 17 00:00:00 2001 From: Kevin Lyda Date: Wed, 29 Mar 2017 19:20:13 +0100 Subject: Initial pass at prometheus monitoring. This is a step for #29118. Add a single metric to count successful logins. Summary types are not supported so remove Collector. Either we need to support the summary type or we need to create a multiprocess-friendly Collector. Add config to load prometheus and set up the Collector and the Exporter. Fix `Gemfile` as current prometheus-client gemspec is missing the `mmap2` dependency. --- Gemfile.lock | 2 ++ app/controllers/sessions_controller.rb | 1 + app/services/prom_service.rb | 16 ++++++++++++++++ config.ru | 7 +++++++ 4 files changed, 26 insertions(+) create mode 100644 app/services/prom_service.rb diff --git a/Gemfile.lock b/Gemfile.lock index 3affa434e2a..7c0f405091d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -465,6 +465,7 @@ GEM mimemagic (0.3.0) mini_portile2 (2.1.0) minitest (5.7.0) + mmap2 (2.2.6) mousetrap-rails (1.4.6) multi_json (1.12.1) multi_xml (0.6.0) @@ -977,6 +978,7 @@ DEPENDENCIES mail_room (~> 0.9.1) method_source (~> 0.8) minitest (~> 5.7.0) + mmap2 (~> 2.2.6) mousetrap-rails (~> 1.4.6) mysql2 (~> 0.3.16) net-ssh (~> 3.0.1) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 8c6ba4915cd..9870d4286a6 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -34,6 +34,7 @@ class SessionsController < Devise::SessionsController end # hide the signed-in notification flash[:notice] = nil + PromService.instance.login.increment log_audit_event(current_user, with: authentication_method) log_user_activity(current_user) end diff --git a/app/services/prom_service.rb b/app/services/prom_service.rb new file mode 100644 index 00000000000..e9ccce758e9 --- /dev/null +++ b/app/services/prom_service.rb @@ -0,0 +1,16 @@ +require 'prometheus/client' +require 'singleton' + +class PromService + include Singleton + + attr_reader :login + + def initialize + @prometheus = Prometheus::Client.registry + + @login = Prometheus::Client::Counter.new(:login, 'Login counter') + @prometheus.register(@login) + + end +end diff --git a/config.ru b/config.ru index 065ce59932f..82af814341b 100644 --- a/config.ru +++ b/config.ru @@ -13,6 +13,13 @@ if defined?(Unicorn) # Max memory size (RSS) per worker use Unicorn::WorkerKiller::Oom, min, max end + + # TODO(lyda): Needs to be set externally. + ENV['prometheus_multiproc_dir'] = '/tmp' + + require 'prometheus/client/rack/exporter' + + use Prometheus::Client::Rack::Exporter, path: '/admin/metrics' end require ::File.expand_path('../config/environment', __FILE__) -- cgit v1.2.3