From 7b3b51d0134af048c1db05dc6a5b9cd6c33db50e Mon Sep 17 00:00:00 2001 From: Tony Hsu Date: Thu, 10 Nov 2022 11:52:10 +0100 Subject: [PATCH] Fix `Datadog.configure_onto(redis)` --- .../tracing/contrib/redis/instrumentation.rb | 4 +- lib/datadog/tracing/contrib/redis/patcher.rb | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) 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