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 24, 2018
1 parent e772421 commit ef80c47
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 50 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 @@ -332,7 +332,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
19 changes: 19 additions & 0 deletions lib/ddtrace/contrib/redis/ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Datadog
module Contrib
module Redis
# Redis integration constants
module Ext
APP = 'redis'.freeze
SERVICE_NAME = 'redis'.freeze
TYPE = 'redis'.freeze

METRIC_PIPELINE_LEN = 'redis.pipeline_length'.freeze

SPAN_COMMAND = 'redis.command'.freeze

TAG_DB = 'out.redis_db'.freeze
TAG_RAW_COMMAND = 'redis.raw_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
48 changes: 20 additions & 28 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,9 +61,9 @@ 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_COMMAND) do |span|
span.service = pin.service
span.span_type = Datadog::Ext::Redis::TYPE
span.span_type = Datadog::Contrib::Redis::Ext::TYPE
span.resource = Datadog::Contrib::Redis::Quantize.format_command_args(*args)
Datadog::Contrib::Redis::Tags.set_common_tags(self, span)

Expand All @@ -83,13 +80,13 @@ 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_COMMAND) do |span|
span.service = pin.service
span.span_type = Datadog::Ext::Redis::TYPE
span.span_type = Datadog::Contrib::Redis::Ext::TYPE
commands = args[0].commands.map { |c| Datadog::Contrib::Redis::Quantize.format_command_args(c) }
span.resource = commands.join("\n")
Datadog::Contrib::Redis::Tags.set_common_tags(self, span)
span.set_metric Datadog::Ext::Redis::PIPELINE_LEN, commands.length
span.set_metric Datadog::Contrib::Redis::Ext::METRIC_PIPELINE_LEN, commands.length

response = call_pipeline_without_datadog(*args, &block)
end
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
6 changes: 3 additions & 3 deletions lib/ddtrace/contrib/redis/tags.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'ddtrace/ext/net'
require 'ddtrace/ext/redis'
require 'ddtrace/contrib/redis/ext'

module Datadog
module Contrib
Expand All @@ -11,8 +11,8 @@ module Tags
def set_common_tags(client, span)
span.set_tag Datadog::Ext::NET::TARGET_HOST, client.host
span.set_tag Datadog::Ext::NET::TARGET_PORT, client.port
span.set_tag Datadog::Ext::Redis::DB, client.db
span.set_tag Datadog::Ext::Redis::RAW_COMMAND, span.resource
span.set_tag Ext::TAG_DB, client.db
span.set_tag Ext::TAG_RAW_COMMAND, span.resource
end
end
end
Expand Down
17 changes: 0 additions & 17 deletions lib/ddtrace/ext/redis.rb

This file was deleted.

0 comments on commit ef80c47

Please sign in to comment.