Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Eliminate code duplication from redis.rb #371

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/instana/activators/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Instana
module Activators
class Redis < Activator
def can_instrument?
defined?(::Redis) && defined?(::Redis::Client) && ::Instana.config[:redis][:enabled]
defined?(::Redis) && Gem::Specification.find_by_name('redis').version < Gem::Version.new('5.0') && defined?(::Redis::Client) && ::Instana.config[:redis][:enabled]
end

def instrument
Expand Down
55 changes: 23 additions & 32 deletions lib/instana/instrumentation/redis.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,49 @@
# (c) Copyright IBM Corp. 2021
# (c) Copyright IBM Corp. 2024
# (c) Copyright Instana Inc. 2017

module Instana
module RedisInstrumentation
def call(*args, &block)
kv_payload = { redis: {} }
ORIGINAL_METHODS = {
:call => ::Redis::Client.instance_method(:call),
:call_pipeline => ::Redis::Client.instance_method(:call_pipeline)
}.freeze

def skip_instrumentation?
dnt_spans = [:redis, :'resque-client', :'sidekiq-client']
!Instana.tracer.tracing? || dnt_spans.include?(::Instana.tracer.current_span.name) || !Instana.config[:redis][:enabled]
end

if !Instana.tracer.tracing? || dnt_spans.include?(::Instana.tracer.current_span.name) || !Instana.config[:redis][:enabled]
return super(*args, &block)
def call(*args, &block)
if skip_instrumentation?
super(*args, &block)
else
call_with_instana(args[0][0].to_s.upcase, ORIGINAL_METHODS[:call], *args, &block)
end
end

begin
::Instana.tracer.log_entry(:redis)

begin
kv_payload[:redis][:connection] = "#{self.host}:#{self.port}"
kv_payload[:redis][:db] = db.to_s
kv_payload[:redis][:command] = args[0][0].to_s.upcase
rescue
nil
end

def call_pipeline(*args, &block)
if skip_instrumentation?
super(*args, &block)
rescue => e
::Instana.tracer.log_info({ redis: {error: true} })
::Instana.tracer.log_error(e)
raise
ensure
::Instana.tracer.log_exit(:redis, kv_payload)
else
call_with_instana(args.first.is_a?(::Redis::Pipeline::Multi) ? 'MULTI' : 'PIPELINE', ORIGINAL_METHODS[:call_pipeline], *args, &block)
end
end

def call_pipeline(*args, &block)
def call_with_instana(*args, &block)
command, original_super, *original_args = *args
kv_payload = { redis: {} }
dnt_spans = [:redis, :'resque-client', :'sidekiq-client']

if !Instana.tracer.tracing? || dnt_spans.include?(::Instana.tracer.current_span.name) || !Instana.config[:redis][:enabled]
return super(*args, &block)
end

begin
::Instana.tracer.log_entry(:redis)

pipeline = args.first
begin
kv_payload[:redis][:connection] = "#{self.host}:#{self.port}"
kv_payload[:redis][:db] = db.to_s
kv_payload[:redis][:command] = pipeline.is_a?(::Redis::Pipeline::Multi) ? 'MULTI' : 'PIPELINE'
kv_payload[:redis][:command] = command
rescue
nil
end

super(*args, &block)
original_super.bind(self).call(*original_args, &block)
rescue => e
::Instana.tracer.log_info({ redis: {error: true} })
::Instana.tracer.log_error(e)
Expand Down
Loading