2021-02-23 13:10:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Queue
|
|
|
|
class Metrics
|
|
|
|
extend Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
QUEUE_DURATION_SECONDS_BUCKETS = [1, 3, 10, 30, 60, 300, 900, 1800, 3600].freeze
|
2021-02-26 13:11:10 -05:00
|
|
|
QUEUE_ACTIVE_RUNNERS_BUCKETS = [1, 3, 10, 30, 60, 300, 900, 1800, 3600].freeze
|
2021-02-24 13:11:28 -05:00
|
|
|
QUEUE_DEPTH_TOTAL_BUCKETS = [1, 2, 3, 5, 8, 16, 32, 50, 100, 250, 500, 1000, 2000, 5000].freeze
|
2021-03-23 11:09:28 -04:00
|
|
|
QUEUE_SIZE_TOTAL_BUCKETS = [1, 5, 10, 50, 100, 500, 1000, 2000, 5000, 7500, 10000, 15000, 20000].freeze
|
2021-05-05 14:10:31 -04:00
|
|
|
QUEUE_PROCESSING_DURATION_SECONDS_BUCKETS = [0.01, 0.05, 0.1, 0.3, 0.5, 1, 5, 10, 15, 20, 30, 60].freeze
|
2021-02-24 13:11:28 -05:00
|
|
|
|
2021-02-23 13:10:40 -05:00
|
|
|
METRICS_SHARD_TAG_PREFIX = 'metrics_shard::'
|
|
|
|
DEFAULT_METRICS_SHARD = 'default'
|
2021-03-25 23:09:21 -04:00
|
|
|
JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET = 5
|
2021-02-23 13:10:40 -05:00
|
|
|
|
2021-02-24 13:11:28 -05:00
|
|
|
OPERATION_COUNTERS = [
|
|
|
|
:build_can_pick,
|
|
|
|
:build_not_pick,
|
2021-03-02 10:10:57 -05:00
|
|
|
:build_not_pending,
|
2021-05-19 11:10:40 -04:00
|
|
|
:build_queue_push,
|
|
|
|
:build_queue_pop,
|
2021-03-02 10:10:57 -05:00
|
|
|
:build_temporary_locked,
|
2021-02-24 13:11:28 -05:00
|
|
|
:build_conflict_lock,
|
|
|
|
:build_conflict_exception,
|
|
|
|
:build_conflict_transition,
|
|
|
|
:queue_attempt,
|
|
|
|
:queue_conflict,
|
|
|
|
:queue_iteration,
|
2021-03-02 13:11:20 -05:00
|
|
|
:queue_depth_limit,
|
2021-02-24 13:11:28 -05:00
|
|
|
:queue_replication_lag,
|
|
|
|
:runner_pre_assign_checks_failed,
|
2021-02-26 13:11:10 -05:00
|
|
|
:runner_pre_assign_checks_success,
|
2021-06-11 11:09:58 -04:00
|
|
|
:runner_queue_tick,
|
|
|
|
:shared_runner_build_new,
|
|
|
|
:shared_runner_build_done
|
2021-02-24 13:11:28 -05:00
|
|
|
].to_set.freeze
|
|
|
|
|
|
|
|
QUEUE_DEPTH_HISTOGRAMS = [
|
|
|
|
:found,
|
|
|
|
:not_found,
|
|
|
|
:conflict
|
|
|
|
].to_set.freeze
|
|
|
|
|
2021-02-23 13:10:40 -05:00
|
|
|
attr_reader :runner
|
|
|
|
|
|
|
|
def initialize(runner)
|
|
|
|
@runner = runner
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_failure
|
|
|
|
self.class.failed_attempt_counter.increment
|
|
|
|
self.class.attempt_counter.increment
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_success(job)
|
|
|
|
labels = { shared_runner: runner.instance_type?,
|
|
|
|
jobs_running_for_project: jobs_running_for_project(job),
|
|
|
|
shard: DEFAULT_METRICS_SHARD }
|
|
|
|
|
|
|
|
if runner.instance_type?
|
|
|
|
shard = runner.tag_list.sort.find { |name| name.starts_with?(METRICS_SHARD_TAG_PREFIX) }
|
|
|
|
labels[:shard] = shard.gsub(METRICS_SHARD_TAG_PREFIX, '') if shard
|
|
|
|
end
|
|
|
|
|
|
|
|
self.class.job_queue_duration_seconds.observe(labels, Time.current - job.queued_at) unless job.queued_at.nil?
|
|
|
|
self.class.attempt_counter.increment
|
|
|
|
end
|
|
|
|
|
2021-02-24 13:11:28 -05:00
|
|
|
def increment_queue_operation(operation)
|
2021-05-19 11:10:40 -04:00
|
|
|
self.class.increment_queue_operation(operation)
|
2021-02-24 13:11:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def observe_queue_depth(queue, size)
|
|
|
|
return unless Feature.enabled?(:gitlab_ci_builds_queuing_metrics, default_enabled: false)
|
|
|
|
|
|
|
|
if !Rails.env.production? && !QUEUE_DEPTH_HISTOGRAMS.include?(queue)
|
|
|
|
raise ArgumentError, "unknown queue depth label: #{queue}"
|
|
|
|
end
|
|
|
|
|
|
|
|
self.class.queue_depth_total.observe({ queue: queue }, size.to_f)
|
|
|
|
end
|
|
|
|
|
2021-03-23 11:09:28 -04:00
|
|
|
def observe_queue_size(size_proc, runner_type)
|
2021-02-24 13:11:28 -05:00
|
|
|
return unless Feature.enabled?(:gitlab_ci_builds_queuing_metrics, default_enabled: false)
|
|
|
|
|
2021-08-24 17:09:59 -04:00
|
|
|
size = size_proc.call.to_f
|
|
|
|
self.class.queue_size_total.observe({ runner_type: runner_type }, size)
|
|
|
|
self.class.current_queue_size.set({ runner_type: runner_type }, size)
|
2021-02-24 13:11:28 -05:00
|
|
|
end
|
|
|
|
|
2021-03-30 14:10:47 -04:00
|
|
|
def observe_queue_time(metric, runner_type)
|
2021-02-24 13:11:28 -05:00
|
|
|
start_time = ::Gitlab::Metrics::System.monotonic_time
|
|
|
|
|
|
|
|
result = yield
|
|
|
|
|
|
|
|
return result unless Feature.enabled?(:gitlab_ci_builds_queuing_metrics, default_enabled: false)
|
|
|
|
|
|
|
|
seconds = ::Gitlab::Metrics::System.monotonic_time - start_time
|
2021-03-18 11:09:04 -04:00
|
|
|
|
|
|
|
case metric
|
|
|
|
when :process
|
2021-03-30 14:10:47 -04:00
|
|
|
self.class.queue_iteration_duration_seconds.observe({ runner_type: runner_type }, seconds.to_f)
|
2021-03-18 11:09:04 -04:00
|
|
|
when :retrieve
|
2021-03-30 14:10:47 -04:00
|
|
|
self.class.queue_retrieval_duration_seconds.observe({ runner_type: runner_type }, seconds.to_f)
|
2021-03-18 11:09:04 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError unless Rails.env.production?
|
|
|
|
end
|
2021-02-24 13:11:28 -05:00
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
def self.increment_queue_operation(operation)
|
|
|
|
if !Rails.env.production? && !OPERATION_COUNTERS.include?(operation)
|
|
|
|
raise ArgumentError, "unknown queue operation: #{operation}"
|
|
|
|
end
|
|
|
|
|
|
|
|
queue_operations_total.increment(operation: operation)
|
|
|
|
end
|
|
|
|
|
2021-02-26 13:11:10 -05:00
|
|
|
def self.observe_active_runners(runners_proc)
|
|
|
|
return unless Feature.enabled?(:gitlab_ci_builds_queuing_metrics, default_enabled: false)
|
|
|
|
|
|
|
|
queue_active_runners_total.observe({}, runners_proc.call.to_f)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.increment_runner_tick(runner)
|
|
|
|
self.new(runner).increment_queue_operation(:runner_queue_tick)
|
|
|
|
end
|
|
|
|
|
2021-02-23 13:10:40 -05:00
|
|
|
def self.failed_attempt_counter
|
|
|
|
strong_memoize(:failed_attempt_counter) do
|
|
|
|
name = :job_register_attempts_failed_total
|
|
|
|
comment = 'Counts the times a runner tries to register a job'
|
|
|
|
|
|
|
|
Gitlab::Metrics.counter(name, comment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.attempt_counter
|
|
|
|
strong_memoize(:attempt_counter) do
|
|
|
|
name = :job_register_attempts_total
|
|
|
|
comment = 'Counts the times a runner tries to register a job'
|
|
|
|
|
|
|
|
Gitlab::Metrics.counter(name, comment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.job_queue_duration_seconds
|
|
|
|
strong_memoize(:job_queue_duration_seconds) do
|
|
|
|
name = :job_queue_duration_seconds
|
|
|
|
comment = 'Request handling execution time'
|
|
|
|
buckets = QUEUE_DURATION_SECONDS_BUCKETS
|
2021-02-26 13:11:10 -05:00
|
|
|
labels = {}
|
2021-02-23 13:10:40 -05:00
|
|
|
|
|
|
|
Gitlab::Metrics.histogram(name, comment, labels, buckets)
|
|
|
|
end
|
|
|
|
end
|
2021-02-24 13:11:28 -05:00
|
|
|
|
|
|
|
def self.queue_operations_total
|
|
|
|
strong_memoize(:queue_operations_total) do
|
|
|
|
name = :gitlab_ci_queue_operations_total
|
|
|
|
comment = 'Counts all the operations that are happening inside a queue'
|
|
|
|
|
|
|
|
Gitlab::Metrics.counter(name, comment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.queue_depth_total
|
|
|
|
strong_memoize(:queue_depth_total) do
|
|
|
|
name = :gitlab_ci_queue_depth_total
|
|
|
|
comment = 'Size of a CI/CD builds queue in relation to the operation result'
|
|
|
|
buckets = QUEUE_DEPTH_TOTAL_BUCKETS
|
2021-02-26 13:11:10 -05:00
|
|
|
labels = {}
|
2021-02-24 13:11:28 -05:00
|
|
|
|
|
|
|
Gitlab::Metrics.histogram(name, comment, labels, buckets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.queue_size_total
|
|
|
|
strong_memoize(:queue_size_total) do
|
|
|
|
name = :gitlab_ci_queue_size_total
|
|
|
|
comment = 'Size of initialized CI/CD builds queue'
|
|
|
|
buckets = QUEUE_SIZE_TOTAL_BUCKETS
|
2021-02-26 13:11:10 -05:00
|
|
|
labels = {}
|
2021-02-24 13:11:28 -05:00
|
|
|
|
|
|
|
Gitlab::Metrics.histogram(name, comment, labels, buckets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-24 17:09:59 -04:00
|
|
|
def self.current_queue_size
|
|
|
|
strong_memoize(:current_queue_size) do
|
|
|
|
name = :gitlab_ci_current_queue_size
|
|
|
|
comment = 'Current size of initialized CI/CD builds queue'
|
|
|
|
|
|
|
|
Gitlab::Metrics.gauge(name, comment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-24 13:11:28 -05:00
|
|
|
def self.queue_iteration_duration_seconds
|
|
|
|
strong_memoize(:queue_iteration_duration_seconds) do
|
|
|
|
name = :gitlab_ci_queue_iteration_duration_seconds
|
|
|
|
comment = 'Time it takes to find a build in CI/CD queue'
|
2021-03-18 11:09:04 -04:00
|
|
|
buckets = QUEUE_PROCESSING_DURATION_SECONDS_BUCKETS
|
|
|
|
labels = {}
|
|
|
|
|
|
|
|
Gitlab::Metrics.histogram(name, comment, labels, buckets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.queue_retrieval_duration_seconds
|
|
|
|
strong_memoize(:queue_retrieval_duration_seconds) do
|
|
|
|
name = :gitlab_ci_queue_retrieval_duration_seconds
|
|
|
|
comment = 'Time it takes to execute a SQL query to retrieve builds queue'
|
|
|
|
buckets = QUEUE_PROCESSING_DURATION_SECONDS_BUCKETS
|
2021-02-26 13:11:10 -05:00
|
|
|
labels = {}
|
|
|
|
|
|
|
|
Gitlab::Metrics.histogram(name, comment, labels, buckets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.queue_active_runners_total
|
|
|
|
strong_memoize(:queue_active_runners_total) do
|
|
|
|
name = :gitlab_ci_queue_active_runners_total
|
|
|
|
comment = 'The amount of active runners that can process queue in a project'
|
|
|
|
buckets = QUEUE_ACTIVE_RUNNERS_BUCKETS
|
|
|
|
labels = {}
|
2021-02-24 13:11:28 -05:00
|
|
|
|
|
|
|
Gitlab::Metrics.histogram(name, comment, labels, buckets)
|
|
|
|
end
|
|
|
|
end
|
2021-12-22 10:14:10 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def jobs_running_for_project(job)
|
|
|
|
return '+Inf' unless runner.instance_type?
|
|
|
|
|
|
|
|
# excluding currently started job
|
|
|
|
running_jobs_count = running_jobs_relation(job)
|
|
|
|
.limit(JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET + 1).count - 1
|
|
|
|
|
|
|
|
if running_jobs_count < JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET
|
|
|
|
running_jobs_count
|
|
|
|
else
|
|
|
|
"#{JOBS_RUNNING_FOR_PROJECT_MAX_BUCKET}+"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def running_jobs_relation(job)
|
|
|
|
if ::Feature.enabled?(:ci_pending_builds_maintain_denormalized_data, default_enabled: :yaml)
|
|
|
|
::Ci::RunningBuild.instance_type.where(project_id: job.project_id)
|
|
|
|
else
|
|
|
|
job.project.builds.running.where(runner: ::Ci::Runner.instance_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2021-02-23 13:10:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|