Commit c67f5367 authored by Ahmad Sherif's avatar Ahmad Sherif

Merge branch 'sh-show-database-connection-errors' into 'master'

Log database errors

Closes #72

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