2021-02-03 16:09:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-02 03:36:33 -04:00
|
|
|
# Keep separate directories for separate processes
|
|
|
|
def prometheus_default_multiproc_dir
|
|
|
|
return unless Rails.env.development? || Rails.env.test?
|
|
|
|
|
2019-12-22 04:07:51 -05:00
|
|
|
if Gitlab::Runtime.sidekiq?
|
2019-07-02 03:36:33 -04:00
|
|
|
Rails.root.join('tmp/prometheus_multiproc_dir/sidekiq')
|
2019-12-22 04:07:51 -05:00
|
|
|
elsif Gitlab::Runtime.puma?
|
2019-07-02 03:36:33 -04:00
|
|
|
Rails.root.join('tmp/prometheus_multiproc_dir/puma')
|
|
|
|
else
|
|
|
|
Rails.root.join('tmp/prometheus_multiproc_dir')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 11:10:16 -04:00
|
|
|
::Prometheus::Client.configure do |config|
|
2020-09-09 08:08:22 -04:00
|
|
|
config.logger = Gitlab::AppLogger
|
2017-07-19 04:54:39 -04:00
|
|
|
|
2019-07-02 03:36:33 -04:00
|
|
|
config.multiprocess_files_dir = ENV['prometheus_multiproc_dir'] || prometheus_default_multiproc_dir
|
2017-08-29 09:45:19 -04:00
|
|
|
|
2021-06-18 11:10:16 -04:00
|
|
|
config.pid_provider = ::Prometheus::PidProvider.method(:worker_id)
|
2017-07-19 04:54:39 -04:00
|
|
|
end
|
2017-08-07 13:13:02 -04:00
|
|
|
|
2017-11-22 18:26:50 -05:00
|
|
|
Gitlab::Application.configure do |config|
|
|
|
|
# 0 should be Sentry to catch errors in this middleware
|
2021-06-08 17:10:05 -04:00
|
|
|
config.middleware.insert_after(Labkit::Middleware::Rack, Gitlab::Metrics::RequestsRackMiddleware)
|
2017-11-22 18:26:50 -05:00
|
|
|
end
|
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
# Any actions beyond this check should only execute outside of tests, when running in an application
|
|
|
|
# context (i.e. not in the Rails console or rspec) and when users have enabled metrics.
|
|
|
|
return if Rails.env.test? || !Gitlab::Runtime.application? || !Gitlab::Metrics.prometheus_metrics_enabled?
|
|
|
|
|
2021-11-18 07:13:46 -05:00
|
|
|
if Gitlab::Runtime.sidekiq? && (!ENV['SIDEKIQ_WORKER_ID'] || ENV['SIDEKIQ_WORKER_ID'] == '0')
|
|
|
|
# The single worker outside of a sidekiq-cluster, or the first worker (sidekiq_0)
|
|
|
|
# in a cluster of processes, is responsible for serving health checks.
|
2021-11-23 10:11:19 -05:00
|
|
|
#
|
|
|
|
# Do not clean the metrics directory here - the supervisor script should
|
|
|
|
# have already taken care of that.
|
2021-11-18 07:13:46 -05:00
|
|
|
Sidekiq.configure_server do |config|
|
|
|
|
config.on(:startup) do
|
2021-11-23 10:11:19 -05:00
|
|
|
# In https://gitlab.com/gitlab-org/gitlab/-/issues/345804 we are looking to
|
|
|
|
# only serve health-checks from a worker process; for backwards compatibility
|
|
|
|
# we still go through the metrics exporter server, but start to configure it
|
|
|
|
# with the new settings keys.
|
|
|
|
exporter_settings = Settings.monitoring.sidekiq_health_checks
|
|
|
|
Gitlab::Metrics::Exporter::SidekiqExporter.instance(exporter_settings).start
|
2021-11-18 07:13:46 -05:00
|
|
|
end
|
2019-06-13 21:41:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
Gitlab::Cluster::LifecycleEvents.on_master_start do
|
2021-02-03 10:09:24 -05:00
|
|
|
# When running Puma in a Single mode, `on_master_start` and `on_worker_start` are the same.
|
|
|
|
# Thus, we order these events to run `reinitialize_on_pid_change` with `force: true` first.
|
2022-03-07 10:22:51 -05:00
|
|
|
::Prometheus::Client.reinitialize_on_pid_change(force: true)
|
2020-05-19 20:08:20 -04:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
Gitlab::Metrics.gauge(:deployments, 'GitLab Version', {}, :max).set({ version: Gitlab::VERSION, revision: Gitlab.revision }, 1)
|
2020-09-09 05:08:40 -04:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
if Gitlab::Runtime.puma?
|
|
|
|
Gitlab::Metrics::RequestsRackMiddleware.initialize_metrics
|
2021-02-03 10:09:24 -05:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
Gitlab::Metrics::Samplers::PumaSampler.instance.start
|
2021-01-22 16:09:10 -05:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
# Starts a metrics server to export metrics from the Puma primary.
|
|
|
|
Gitlab::Metrics::Exporter::WebExporter.instance.start
|
2017-11-02 16:23:23 -04:00
|
|
|
end
|
2019-05-29 08:27:44 -04:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
Gitlab::Ci::Parsers.instrument!
|
|
|
|
rescue IOError => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
|
|
|
Gitlab::Metrics.error_detected!
|
|
|
|
end
|
2019-10-02 17:06:22 -04:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
Gitlab::Cluster::LifecycleEvents.on_worker_start do
|
|
|
|
defined?(::Prometheus::Client.reinitialize_on_pid_change) && ::Prometheus::Client.reinitialize_on_pid_change
|
|
|
|
logger = Gitlab::AppLogger
|
|
|
|
Gitlab::Metrics::Samplers::RubySampler.initialize_instance(logger: logger).start
|
|
|
|
Gitlab::Metrics::Samplers::DatabaseSampler.initialize_instance(logger: logger).start
|
|
|
|
Gitlab::Metrics::Samplers::ThreadsSampler.initialize_instance(logger: logger).start
|
|
|
|
|
|
|
|
if Gitlab::Runtime.puma?
|
|
|
|
# Since we are running a metrics server on the Puma primary, we would inherit
|
|
|
|
# this thread after forking into workers, so we need to explicitly stop it here.
|
|
|
|
# NOTE: This will not be necessary anymore after moving to an external server
|
|
|
|
# process via https://gitlab.com/gitlab-org/gitlab/-/issues/350548
|
|
|
|
Gitlab::Metrics::Exporter::WebExporter.instance.stop
|
2020-05-15 17:08:21 -04:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
Gitlab::Metrics::Samplers::ActionCableSampler.instance(logger: logger).start
|
|
|
|
end
|
2021-01-22 16:09:10 -05:00
|
|
|
|
2022-03-07 10:22:51 -05:00
|
|
|
if Gitlab.ee? && Gitlab::Runtime.sidekiq?
|
|
|
|
Gitlab::Metrics::Samplers::GlobalSearchSampler.instance(logger: logger).start
|
2019-05-29 08:27:44 -04:00
|
|
|
end
|
2022-03-07 10:22:51 -05:00
|
|
|
|
|
|
|
Gitlab::Ci::Parsers.instrument!
|
|
|
|
rescue IOError => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
|
|
|
Gitlab::Metrics.error_detected!
|
2018-10-20 14:00:19 -04:00
|
|
|
end
|
2019-10-04 02:06:05 -04:00
|
|
|
|
2022-03-02 10:16:07 -05:00
|
|
|
if Gitlab::Runtime.puma?
|
2019-10-30 05:27:58 -04:00
|
|
|
Gitlab::Cluster::LifecycleEvents.on_before_graceful_shutdown do
|
|
|
|
# We need to ensure that before we re-exec or shutdown server
|
2019-10-16 11:06:17 -04:00
|
|
|
# we do stop the exporter
|
|
|
|
Gitlab::Metrics::Exporter::WebExporter.instance.stop
|
|
|
|
end
|
|
|
|
|
2019-10-04 02:06:05 -04:00
|
|
|
Gitlab::Cluster::LifecycleEvents.on_before_master_restart do
|
|
|
|
# We need to ensure that before we re-exec server
|
|
|
|
# we do stop the exporter
|
2019-10-16 11:06:17 -04:00
|
|
|
#
|
|
|
|
# We do it again, for being extra safe,
|
|
|
|
# but it should not be needed
|
2019-10-04 02:06:05 -04:00
|
|
|
Gitlab::Metrics::Exporter::WebExporter.instance.stop
|
|
|
|
end
|
|
|
|
end
|