debian-mirror-gitlab/lib/gitlab/utils/measuring.rb

76 lines
1.8 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
require 'prometheus/pid_provider'
module Gitlab
module Utils
class Measuring
2020-05-24 23:13:21 +05:30
class << self
attr_writer :logger
def logger
@logger ||= Logger.new(STDOUT)
end
end
def initialize(base_log_data = {})
@base_log_data = base_log_data
2020-04-08 14:13:33 +05:30
end
def with_measuring
2020-05-24 23:13:21 +05:30
result = nil
2020-04-22 19:07:51 +05:30
with_gc_stats do
2020-04-08 14:13:33 +05:30
with_count_queries do
with_measure_time do
2020-05-24 23:13:21 +05:30
result = yield
2020-04-08 14:13:33 +05:30
end
end
end
2020-05-24 23:13:21 +05:30
log_info(
gc_stats: gc_stats,
time_to_finish: time_to_finish,
number_of_sql_calls: sql_calls_count,
memory_usage: "#{Gitlab::Metrics::System.memory_usage_rss.to_f / 1024 / 1024} MiB",
label: ::Prometheus::PidProvider.worker_id
)
result
2020-04-08 14:13:33 +05:30
end
private
2020-05-24 23:13:21 +05:30
attr_reader :gc_stats, :time_to_finish, :sql_calls_count, :base_log_data
2020-04-08 14:13:33 +05:30
def with_count_queries(&block)
2020-05-24 23:13:21 +05:30
@sql_calls_count = 0
2020-04-08 14:13:33 +05:30
counter_f = ->(_name, _started, _finished, _unique_id, payload) {
2020-05-24 23:13:21 +05:30
@sql_calls_count += 1 unless payload[:name].in? %w[CACHE SCHEMA]
2020-04-08 14:13:33 +05:30
}
ActiveSupport::Notifications.subscribed(counter_f, "sql.active_record", &block)
end
2020-04-22 19:07:51 +05:30
def with_gc_stats
2021-03-11 19:13:27 +05:30
stats = ::Gitlab::Memory::Instrumentation.start_thread_memory_allocations
2020-04-08 14:13:33 +05:30
yield
2021-03-11 19:13:27 +05:30
ensure
@gc_stats = ::Gitlab::Memory::Instrumentation.measure_thread_memory_allocations(stats)
2020-04-08 14:13:33 +05:30
end
def with_measure_time
2020-05-24 23:13:21 +05:30
@time_to_finish = Benchmark.realtime do
2020-04-08 14:13:33 +05:30
yield
end
end
2020-05-24 23:13:21 +05:30
def log_info(details)
details = base_log_data.merge(details)
details = details.to_yaml if ActiveSupport::Logger.logger_outputs_to?(Measuring.logger, STDOUT)
Measuring.logger.info(details)
2020-04-08 14:13:33 +05:30
end
end
end
end