Skip to content

Commit

Permalink
Refactored: Redis to use Datadog::Contrib::Integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
delner committed Sep 21, 2018
1 parent bb6b171 commit f7e3395
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def configure(target = configuration, opts = {})
require 'ddtrace/contrib/rack/integration'
require 'ddtrace/contrib/rails/integration'
require 'ddtrace/contrib/rake/patcher'
require 'ddtrace/contrib/redis/patcher'
require 'ddtrace/contrib/redis/integration'
require 'ddtrace/contrib/resque/integration'
require 'ddtrace/contrib/rest_client/integration'
require 'ddtrace/contrib/sequel/integration'
Expand Down
2 changes: 1 addition & 1 deletion lib/ddtrace/contrib/rails/core_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def delete(*args, &block)

def self.reload_cache_store
redis = Datadog.registry[:redis]
return unless redis && redis.patched?
return unless redis && redis.patcher.patched?

return unless defined?(::ActiveSupport::Cache::RedisStore) &&
defined?(::Rails.cache) &&
Expand Down
16 changes: 16 additions & 0 deletions lib/ddtrace/contrib/redis/configuration/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'ddtrace/contrib/configuration/settings'
require 'ddtrace/contrib/redis/ext'

module Datadog
module Contrib
module Redis
module Configuration
# Custom settings for the Redis integration
class Settings < Contrib::Configuration::Settings
option :service_name, default: Ext::SERVICE_NAME
option :tracer, default: Datadog.tracer
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/ddtrace/contrib/redis/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Datadog
module Contrib
module Redis
# Redis integration constants
module Ext
APP = 'redis'.freeze
SERVICE_NAME = 'redis'.freeze

SPAN_NAME = 'redis.command'.freeze
end
end
end
end
36 changes: 36 additions & 0 deletions lib/ddtrace/contrib/redis/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'ddtrace/contrib/integration'
require 'ddtrace/contrib/redis/configuration/settings'
require 'ddtrace/contrib/redis/patcher'

module Datadog
module Contrib
module Redis
# Description of Redis integration
class Integration
include Contrib::Integration

register_as :redis, auto_patch: true

def self.version
Gem.loaded_specs['redis'] && Gem.loaded_specs['redis'].version
end

def self.present?
super && defined?(::Redis)
end

def self.compatible?
!version.nil? && version >= Gem::Version.new('3.0.0')
end

def default_configuration
Configuration::Settings.new
end

def patcher
Patcher
end
end
end
end
end
42 changes: 17 additions & 25 deletions lib/ddtrace/contrib/redis/patcher.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
# requirements should be kept minimal as Patcher is a shared requirement.
require 'ddtrace/contrib/patcher'
require 'ddtrace/contrib/redis/ext'

module Datadog
module Contrib
module Redis
SERVICE = 'redis'.freeze
DRIVER = 'redis.driver'.freeze

# Patcher enables patching of 'redis' module.
module Patcher
include Base
register_as :redis, auto_patch: true
option :service_name, default: SERVICE
option :tracer, default: Datadog.tracer

@patched = false
include Contrib::Patcher

module_function

def patched?
done?(:redis)
end

# patch applies our patch if needed
def patch
if !@patched && compatible?
do_once(:redis) do
begin
# do not require these by default, but only when actually patching
require 'redis'
require 'ddtrace/ext/app_types'
require 'ddtrace/contrib/redis/tags'
require 'ddtrace/contrib/redis/quantize'

patch_redis_client
@patched = true
RailsCachePatcher.reload_cache_store if Datadog.registry[:rails].patcher.patched?
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply Redis integration: #{e}")
end
end
@patched
end

def compatible?
defined?(::Redis::VERSION) && Gem::Version.new(::Redis::VERSION) >= Gem::Version.new('3.0.0')
end

# rubocop:disable Metrics/MethodLength
Expand All @@ -52,7 +44,12 @@ def patch_redis_client
def initialize(*args)
service = Datadog.configuration[:redis][:service_name]
tracer = Datadog.configuration[:redis][:tracer]
pin = Datadog::Pin.new(service, app: 'redis', app_type: Datadog::Ext::AppTypes::DB, tracer: tracer)
pin = Datadog::Pin.new(
service,
app: Ext::APP,
app_type: Datadog::Ext::AppTypes::DB,
tracer: tracer
)
pin.onto(self)
initialize_without_datadog(*args)
end
Expand All @@ -64,7 +61,7 @@ def call(*args, &block)
return call_without_datadog(*args, &block) unless pin && pin.tracer

response = nil
pin.tracer.trace('redis.command') do |span|
pin.tracer.trace(Datadog::Contrib::Redis::Ext::SPAN_NAME) do |span|
span.service = pin.service
span.span_type = Datadog::Ext::Redis::TYPE
span.resource = Datadog::Contrib::Redis::Quantize.format_command_args(*args)
Expand All @@ -83,7 +80,7 @@ def call_pipeline(*args, &block)
return call_pipeline_without_datadog(*args, &block) unless pin && pin.tracer

response = nil
pin.tracer.trace('redis.command') do |span|
pin.tracer.trace(Datadog::Contrib::Redis::Ext::SPAN_NAME) do |span|
span.service = pin.service
span.span_type = Datadog::Ext::Redis::TYPE
commands = args[0].commands.map { |c| Datadog::Contrib::Redis::Quantize.format_command_args(c) }
Expand All @@ -98,11 +95,6 @@ def call_pipeline(*args, &block)
end
end
end

# patched? tells wether patch has been successfully applied
def patched?
@patched
end
end
end
end
Expand Down

0 comments on commit f7e3395

Please sign in to comment.