2019-10-04 06:06:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
|
|
|
module Exporter
|
|
|
|
class BaseExporter < Daemon
|
|
|
|
attr_reader :server
|
|
|
|
|
2019-10-16 18:08:01 +00:00
|
|
|
attr_accessor :readiness_checks
|
2019-10-16 12:06:32 +00:00
|
|
|
|
2019-10-04 06:06:05 +00:00
|
|
|
def enabled?
|
|
|
|
settings.enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_filename
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def start_working
|
|
|
|
logger = WEBrick::Log.new(log_filename)
|
|
|
|
logger.time_format = "[%Y-%m-%dT%H:%M:%S.%L%z]"
|
|
|
|
|
|
|
|
access_log = [
|
|
|
|
[logger, WEBrick::AccessLog::COMBINED_LOG_FORMAT]
|
|
|
|
]
|
|
|
|
|
|
|
|
@server = ::WEBrick::HTTPServer.new(
|
|
|
|
Port: settings.port, BindAddress: settings.address,
|
|
|
|
Logger: logger, AccessLog: access_log)
|
2019-10-07 15:05:59 +00:00
|
|
|
server.mount_proc '/readiness' do |req, res|
|
2019-10-16 12:06:32 +00:00
|
|
|
render_probe(readiness_probe, req, res)
|
2019-10-07 15:05:59 +00:00
|
|
|
end
|
|
|
|
server.mount_proc '/liveness' do |req, res|
|
2019-10-16 12:06:32 +00:00
|
|
|
render_probe(liveness_probe, req, res)
|
2019-10-07 15:05:59 +00:00
|
|
|
end
|
|
|
|
server.mount '/', Rack::Handler::WEBrick, rack_app
|
2019-10-16 09:07:51 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_thread
|
|
|
|
server&.start
|
|
|
|
rescue IOError
|
|
|
|
# ignore forcibily closed servers
|
2019-10-04 06:06:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop_working
|
2019-10-07 12:06:18 +00:00
|
|
|
if server
|
2019-10-16 12:06:32 +00:00
|
|
|
# we close sockets if thread is not longer running
|
|
|
|
# this happens, when the process forks
|
2019-10-16 15:06:17 +00:00
|
|
|
if thread.alive?
|
|
|
|
server.shutdown
|
|
|
|
else
|
|
|
|
server.listeners.each(&:close)
|
|
|
|
end
|
2019-10-04 06:06:05 +00:00
|
|
|
end
|
2019-10-07 12:06:18 +00:00
|
|
|
|
2019-10-04 06:06:05 +00:00
|
|
|
@server = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def rack_app
|
|
|
|
Rack::Builder.app do
|
|
|
|
use Rack::Deflater
|
|
|
|
use ::Prometheus::Client::Rack::Exporter if ::Gitlab::Metrics.metrics_folder_present?
|
|
|
|
run -> (env) { [404, {}, ['']] }
|
|
|
|
end
|
|
|
|
end
|
2019-10-07 15:05:59 +00:00
|
|
|
|
2019-10-16 12:06:32 +00:00
|
|
|
def readiness_probe
|
2019-10-16 18:08:01 +00:00
|
|
|
::Gitlab::HealthChecks::Probes::Collection.new(*readiness_checks)
|
2019-10-16 12:06:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def liveness_probe
|
2019-10-16 18:08:01 +00:00
|
|
|
::Gitlab::HealthChecks::Probes::Collection.new
|
2019-10-16 12:06:32 +00:00
|
|
|
end
|
|
|
|
|
2019-10-07 15:05:59 +00:00
|
|
|
def render_probe(probe, req, res)
|
|
|
|
result = probe.execute
|
|
|
|
|
|
|
|
res.status = result.http_status
|
|
|
|
res.content_type = 'application/json; charset=utf-8'
|
|
|
|
res.body = result.json.to_json
|
|
|
|
end
|
2019-10-04 06:06:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|