Commit 51742648 authored by Stan Hu's avatar Stan Hu

Log database errors

Previously all `PG::ConnectionBad` errors were quietly swallowed. We now
pass along a logger so that we can see when these errors occur.

Closes https://gitlab.com/gitlab-org/gitlab-exporter/-/issues/72
parent c1e02845
......@@ -16,8 +16,9 @@ module GitLab
end
end
def initialize(args)
def initialize(args, logger: nil)
@connection_string = args[:connection_string]
@logger = logger
end
def run
......@@ -33,10 +34,14 @@ module GitLab
begin
yield conn
rescue PG::UnableToSend => e
@logger.warn "Error sending to the database: #{e}" if @logger
conn.reset
raise e
end
end
rescue PG::Error => e
@logger.error "Error connecting to the database: #{e}" if @logger
raise e
end
end
end
......
......@@ -262,8 +262,8 @@ module GitLab
DEFAULT_UNARCHIVED_TRACES_OFFSET_MINUTES = 1440
def initialize(opts)
super(opts)
def initialize(opts, logger: nil)
super(opts, logger: logger)
@allowed_repeated_commands_count = opts[:allowed_repeated_commands_count]
@created_builds_counting_disabled = opts[:created_builds_counting_disabled]
......@@ -417,14 +417,14 @@ module GitLab
# The prober which is called when gathering metrics
class CiBuildsProber
def initialize(opts, metrics: PrometheusMetrics.new)
def initialize(opts, metrics: PrometheusMetrics.new, logger: nil)
@metrics = metrics
collector_opts = { connection_string: opts[:connection_string],
allowed_repeated_commands_count: opts[:allowed_repeated_commands_count],
created_builds_counting_disabled: opts[:created_builds_counting_disabled],
unarchived_traces_offset_minutes: opts[:unarchived_traces_offset_minutes] }
@collector = CiBuildsCollector.new(collector_opts)
@collector = CiBuildsCollector.new(collector_opts, logger: logger)
end
def probe_db
......
......@@ -35,7 +35,7 @@ module GitLab
# The prober which is called when gathering metrics
class RemoteMirrorsProber
def initialize(opts, metrics: PrometheusMetrics.new)
def initialize(opts, metrics: PrometheusMetrics.new, logger: nil)
@metrics = metrics
@collector = RemoteMirrorsCollector.new(
connection_string: opts[:connection_string],
......
......@@ -212,7 +212,7 @@ module GitLab
# The prober which is called when gathering metrics
class RowCountProber
def initialize(opts, metrics: PrometheusMetrics.new)
def initialize(opts, metrics: PrometheusMetrics.new, logger: nil)
@metrics = metrics
@collector = RowCountCollector.new(
connection_string: opts[:connection_string],
......
......@@ -25,9 +25,9 @@ module GitLab
# Probes the DB specified by opts[:connection_string] for tuple stats, then converts them to metrics
class TuplesProber
def initialize(opts, metrics: PrometheusMetrics.new)
def initialize(opts, metrics: PrometheusMetrics.new, logger: nil)
@metrics = metrics
@collector = TupleStatsCollector.new(connection_string: opts[:connection_string])
@collector = TupleStatsCollector.new(connection_string: opts[:connection_string], logger: logger)
end
def probe_db
......
......@@ -62,7 +62,7 @@ module GitLab
# Optionally takes a metrics object which by default is a PrometheusMetrics, useful to change the
# metrics writer to something else.
class GitProber
def initialize(opts, metrics: PrometheusMetrics.new)
def initialize(opts, metrics: PrometheusMetrics.new, logger: nil)
@metrics = metrics
@labels = opts[:labels] || {}
@git = Git.new(opts[:source])
......@@ -85,7 +85,7 @@ module GitLab
# A special prober for git processes
class GitProcessProber
def initialize(opts, metrics: PrometheusMetrics.new)
def initialize(opts, metrics: PrometheusMetrics.new, logger: nil)
@opts = opts
@metrics = metrics
end
......
......@@ -2,9 +2,10 @@ module GitLab
module Exporter
# A class to combine multiple probers into one
class Prober
def initialize(prober_opts, metrics: PrometheusMetrics.new)
def initialize(prober_opts, metrics: PrometheusMetrics.new, logger: nil)
@prober_opts = prober_opts
@metrics = metrics
@logger = logger
resolve_prober_classes
end
......@@ -12,7 +13,7 @@ module GitLab
def probe_all
@prober_opts.each do |_probe_name, params|
Utils.wrap_in_array(params[:opts]).each do |opts|
prober = params[:class].new(opts, metrics: @metrics)
prober = params[:class].new(opts, metrics: @metrics, logger: @logger)
params[:methods].each do |meth|
prober.send(meth)
end
......
......@@ -59,7 +59,7 @@ module GitLab
# Probes a process for info then writes metrics to a target
class ProcessProber
def initialize(options, metrics: PrometheusMetrics.new)
def initialize(options, metrics: PrometheusMetrics.new, logger: nil)
@metrics = metrics
@name = options[:name]
@pids = if options[:pid_or_pattern] =~ /^\d+$/
......
......@@ -10,9 +10,10 @@ module GitLab
QUEUE_JOB_STATS_SCRIPT = File.read(File.expand_path("#{__FILE__}/../sidekiq_queue_job_stats.lua")).freeze
QUEUE_JOB_STATS_SHA = Digest::SHA1.hexdigest(QUEUE_JOB_STATS_SCRIPT).freeze
def initialize(opts, metrics: PrometheusMetrics.new)
def initialize(opts, metrics: PrometheusMetrics.new, logger: nil)
@opts = opts
@metrics = metrics
@logger = logger
Sidekiq.configure_client do |config|
config.redis = redis_options
......
......@@ -42,6 +42,11 @@ module GitLab
memory_threshold = (config[:server] && config[:server][:memory_threshold]) || 1024
use MemoryKillerMiddleware, memory_threshold
use Rack::Logger
end
def logger
request.logger
end
def setup_server(config)
......@@ -62,7 +67,7 @@ module GitLab
get "/#{probe_name}" do
content_type "text/plain; version=0.0.4"
prober = Prober.new(opts, metrics: PrometheusMetrics.new(include_timestamp: false))
prober = Prober.new(opts, metrics: PrometheusMetrics.new(include_timestamp: false), logger: logger)
prober.probe_all
prober.write_to(response)
......
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