diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-12-17 14:59:07 +0300 |
commit | 8b573c94895dc0ac0e1d9d59cf3e8745e8b539ca (patch) | |
tree | 544930fb309b30317ae9797a9683768705d664c4 /lib/gitlab/performance_bar | |
parent | 4b1de649d0168371549608993deac953eb692019 (diff) |
Add latest changes from gitlab-org/gitlab@13-7-stable-eev13.7.0-rc42
Diffstat (limited to 'lib/gitlab/performance_bar')
-rw-r--r-- | lib/gitlab/performance_bar/logger.rb | 11 | ||||
-rw-r--r-- | lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb | 28 | ||||
-rw-r--r-- | lib/gitlab/performance_bar/stats.rb | 60 |
3 files changed, 98 insertions, 1 deletions
diff --git a/lib/gitlab/performance_bar/logger.rb b/lib/gitlab/performance_bar/logger.rb new file mode 100644 index 00000000000..a8e2f7d2d4e --- /dev/null +++ b/lib/gitlab/performance_bar/logger.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Gitlab + module PerformanceBar + class Logger < ::Gitlab::JsonLogger + def self.file_name_noext + 'performance_bar_json' + end + end + end +end diff --git a/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb b/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb index 805283b0f93..bf8d4b202b6 100644 --- a/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb +++ b/lib/gitlab/performance_bar/redis_adapter_when_peek_enabled.rb @@ -5,7 +5,33 @@ module Gitlab module PerformanceBar module RedisAdapterWhenPeekEnabled def save(request_id) - super if ::Gitlab::PerformanceBar.enabled_for_request? + return unless ::Gitlab::PerformanceBar.enabled_for_request? + return if request_id.blank? + + super + + enqueue_stats_job(request_id) + end + + # schedules a job which parses peek profile data and adds them + # to a structured log + def enqueue_stats_job(request_id) + return unless gather_stats? + + @client.sadd(GitlabPerformanceBarStatsWorker::STATS_KEY, request_id) # rubocop:disable Gitlab/ModuleWithInstanceVariables + + return unless uuid = Gitlab::ExclusiveLease.new( + GitlabPerformanceBarStatsWorker::LEASE_KEY, + timeout: GitlabPerformanceBarStatsWorker::LEASE_TIMEOUT + ).try_obtain + + GitlabPerformanceBarStatsWorker.perform_in(GitlabPerformanceBarStatsWorker::WORKER_DELAY, uuid) + end + + def gather_stats? + return unless Feature.enabled?(:performance_bar_stats) + + Gitlab.com? || !Rails.env.production? end end end diff --git a/lib/gitlab/performance_bar/stats.rb b/lib/gitlab/performance_bar/stats.rb new file mode 100644 index 00000000000..d1504d88315 --- /dev/null +++ b/lib/gitlab/performance_bar/stats.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module Gitlab + module PerformanceBar + # This class fetches Peek stats stored in redis and logs them in a + # structured log (so these can be then analyzed in Kibana) + class Stats + def initialize(redis) + @redis = redis + end + + def process(id) + data = request(id) + return unless data + + log_sql_queries(id, data) + rescue => err + logger.error(message: "failed to process request id #{id}: #{err.message}") + end + + private + + def request(id) + # Peek gem stores request data under peek:requests:request_id key + json_data = @redis.get("peek:requests:#{id}") + Gitlab::Json.parse(json_data) + end + + def log_sql_queries(id, data) + return [] unless queries = data.dig('data', 'active-record', 'details') + + queries.each do |query| + next unless location = parse_backtrace(query['backtrace']) + + log_info = location.merge( + type: :sql, + request_id: id, + duration_ms: query['duration'].to_f + ) + + logger.info(log_info) + end + end + + def parse_backtrace(backtrace) + return unless match = /(?<filename>.*):(?<filenum>\d+):in `(?<method>.*)'/.match(backtrace.first) + + { + filename: match[:filename], + filenum: match[:filenum].to_i, + method: match[:method] + } + end + + def logger + @logger ||= Gitlab::PerformanceBar::Logger.build + end + end + end +end |