diff --git a/lib/datadog/tracing/contrib/redis/instrumentation.rb b/lib/datadog/tracing/contrib/redis/instrumentation.rb index 2b7d6e2b83..7850bab4dc 100644 --- a/lib/datadog/tracing/contrib/redis/instrumentation.rb +++ b/lib/datadog/tracing/contrib/redis/instrumentation.rb @@ -21,7 +21,7 @@ module InstanceMethods def call(*args, &block) response = nil Tracing.trace(Contrib::Redis::Ext::SPAN_COMMAND) do |span| - span.service = Datadog.configuration_for(self, :service_name) || datadog_configuration[:service_name] + span.service = Datadog.configuration_for(redis_instance, :service_name) || datadog_configuration[:service_name] span.span_type = Contrib::Redis::Ext::TYPE span.resource = get_command(args) Contrib::Redis::Tags.set_common_tags(self, span) @@ -35,7 +35,7 @@ def call(*args, &block) def call_pipeline(*args, &block) response = nil Tracing.trace(Contrib::Redis::Ext::SPAN_COMMAND) do |span| - span.service = Datadog.configuration_for(self, :service_name) || datadog_configuration[:service_name] + span.service = Datadog.configuration_for(redis_instance, :service_name) || datadog_configuration[:service_name] span.span_type = Contrib::Redis::Ext::TYPE commands = get_pipeline_commands(args) span.resource = commands.any? ? commands.join("\n") : '(none)' diff --git a/lib/datadog/tracing/contrib/redis/patcher.rb b/lib/datadog/tracing/contrib/redis/patcher.rb index 921a6f00f8..437544b104 100644 --- a/lib/datadog/tracing/contrib/redis/patcher.rb +++ b/lib/datadog/tracing/contrib/redis/patcher.rb @@ -12,6 +12,38 @@ module Redis module Patcher include Contrib::Patcher + module InstancePatch + def self.included(base) + base.prepend(InstanceMethods) + end + + module InstanceMethods + def initialize(options = {}) + options[:redis_instance] = self + + super(options) + end + end + end + + module ClientPatch + def self.included(base) + base.prepend(InstanceMethods) + end + + module InstanceMethods + def initialize(options = {}) + @redis_instance = options[:redis_instance] + + super(options) + end + + private + + attr_reader :redis_instance + end + end + module_function def target_version @@ -26,6 +58,11 @@ def patch require_relative 'quantize' require_relative 'instrumentation' + # InstancePatch and ClientPatch allows the client object to access pin on redis instance + ::Redis.include(InstancePatch) + ::Redis::Client.include(ClientPatch) + + # TODO: To support redis-rb 5.x, Redis::Client -> RedisClient ::Redis::Client.include(Instrumentation) end end