Commit 6b9a091c authored by Kevin Lyda's avatar Kevin Lyda Committed by Pawel Chojnacki

Add trailing newline to response.

Prometheus requires a trailing newline in its response.

+ cleanup
parent 5bc099c2
...@@ -23,15 +23,27 @@ class HealthController < ActionController::Base ...@@ -23,15 +23,27 @@ class HealthController < ActionController::Base
end end
def metrics def metrics
results = CHECKS.flat_map(&:metrics) response = health_metrics_text + "\n"
if Gitlab::Metrics.prometheus_metrics_enabled?
response += Prometheus::Client::Formats::Text.marshal_multiprocess(ENV['prometheus_multiproc_dir'])
end
response = results.map(&method(:metric_to_prom_line)).join("\n")
response = ::Prometheus::Client::Formats::Text.marshal_multiprocess
render text: response, content_type: 'text/plain; version=0.0.4' render text: response, content_type: 'text/plain; version=0.0.4'
end end
private private
def health_metrics_text
results = CHECKS.flat_map(&:metrics)
types = results.map(&:name)
.uniq
.map { |metric_name| "# TYPE #{metric_name} gauge" }
metrics = results.map(&method(:metric_to_prom_line))
types.concat(metrics).join("\n")
end
def metric_to_prom_line(metric) def metric_to_prom_line(metric)
labels = metric.labels&.map { |key, value| "#{key}=\"#{value}\"" }&.join(',') || '' labels = metric.labels&.map { |key, value| "#{key}=\"#{value}\"" }&.join(',') || ''
if labels.empty? if labels.empty?
......
...@@ -34,7 +34,6 @@ class SessionsController < Devise::SessionsController ...@@ -34,7 +34,6 @@ class SessionsController < Devise::SessionsController
end end
# hide the signed-in notification # hide the signed-in notification
flash[:notice] = nil flash[:notice] = nil
PromService.instance.login.increment
log_audit_event(current_user, with: authentication_method) log_audit_event(current_user, with: authentication_method)
log_user_activity(current_user) log_user_activity(current_user)
end end
...@@ -48,6 +47,10 @@ class SessionsController < Devise::SessionsController ...@@ -48,6 +47,10 @@ class SessionsController < Devise::SessionsController
private private
def self.login_counter
@login_counter ||= Gitlab::Metrics.counter(:user_session_logins, 'User logins count')
end
# Handle an "initial setup" state, where there's only one user, it's an admin, # Handle an "initial setup" state, where there's only one user, it's an admin,
# and they require a password change. # and they require a password change.
def check_initial_setup def check_initial_setup
...@@ -126,6 +129,7 @@ class SessionsController < Devise::SessionsController ...@@ -126,6 +129,7 @@ class SessionsController < Devise::SessionsController
end end
def log_user_activity(user) def log_user_activity(user)
SessionsController.login_counter.increment
Users::ActivityService.new(user, 'login').execute Users::ActivityService.new(user, 'login').execute
end end
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment