2019-06-28 14:34:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'redis'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Peek
|
|
|
|
module RedisInstrumented
|
|
|
|
def call(*args, &block)
|
|
|
|
start = Time.now
|
|
|
|
super(*args, &block)
|
|
|
|
ensure
|
|
|
|
duration = (Time.now - start)
|
|
|
|
add_call_details(duration, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add_call_details(duration, args)
|
2019-08-03 02:40:32 -04:00
|
|
|
return unless peek_enabled?
|
2019-06-28 14:34:08 -04:00
|
|
|
# redis-rb passes an array (e.g. [:get, key])
|
|
|
|
return unless args.length == 1
|
|
|
|
|
|
|
|
detail_store << {
|
|
|
|
cmd: args.first,
|
|
|
|
duration: duration,
|
2019-07-24 07:37:05 -04:00
|
|
|
backtrace: ::Gitlab::Profiler.clean_backtrace(caller)
|
2019-06-28 14:34:08 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-08-03 02:40:32 -04:00
|
|
|
def peek_enabled?
|
|
|
|
Gitlab::SafeRequestStore.store[:peek_enabled]
|
|
|
|
end
|
|
|
|
|
2019-06-28 14:34:08 -04:00
|
|
|
def detail_store
|
|
|
|
::Gitlab::SafeRequestStore['redis_call_details'] ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Peek
|
|
|
|
module Views
|
2019-07-24 10:42:45 -04:00
|
|
|
class RedisDetailed < DetailedView
|
2019-07-09 10:07:41 -04:00
|
|
|
REDACTED_MARKER = "<redacted>"
|
|
|
|
|
2019-07-17 15:33:49 -04:00
|
|
|
def key
|
|
|
|
'redis'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-06-28 14:34:08 -04:00
|
|
|
def format_call_details(call)
|
2019-07-26 09:03:00 -04:00
|
|
|
super.merge(cmd: format_command(call[:cmd]))
|
2019-06-28 14:34:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_command(cmd)
|
2019-07-09 10:07:41 -04:00
|
|
|
if cmd.length >= 2 && cmd.first =~ /^auth$/i
|
|
|
|
cmd[-1] = REDACTED_MARKER
|
2019-06-28 14:34:08 -04:00
|
|
|
# Scrub out the value of the SET calls to avoid binary
|
|
|
|
# data or large data from spilling into the view
|
2019-07-09 10:07:41 -04:00
|
|
|
elsif cmd.length >= 3 && cmd.first =~ /set/i
|
|
|
|
cmd[2..-1] = REDACTED_MARKER
|
2019-06-28 14:34:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
cmd.join(' ')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Redis::Client
|
|
|
|
prepend Gitlab::Peek::RedisInstrumented
|
|
|
|
end
|