-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the Redis client patch to use prepend (#1743)
- Loading branch information
1 parent
6180c0e
commit e84cdfa
Showing
4 changed files
with
106 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# typed: false | ||
require 'ddtrace/contrib/patcher' | ||
require 'ddtrace/contrib/redis/ext' | ||
require 'ddtrace/contrib/redis/configuration/resolver' | ||
|
||
module Datadog | ||
module Contrib | ||
module Redis | ||
# Instrumentation for Redis | ||
module Instrumentation | ||
def self.included(base) | ||
base.prepend(InstanceMethods) | ||
end | ||
|
||
# InstanceMethods - implementing instrumentation | ||
module InstanceMethods | ||
def call(*args, &block) | ||
pin = Datadog::Pin.get_from(self) | ||
return super unless pin && pin.tracer | ||
|
||
response = nil | ||
pin.tracer.trace(Datadog::Contrib::Redis::Ext::SPAN_COMMAND) do |span| | ||
span.service = pin.service | ||
span.span_type = Datadog::Contrib::Redis::Ext::TYPE | ||
span.resource = get_command(args) | ||
Datadog::Contrib::Redis::Tags.set_common_tags(self, span) | ||
|
||
response = super | ||
end | ||
|
||
response | ||
end | ||
|
||
def call_pipeline(*args, &block) | ||
pin = Datadog::Pin.get_from(self) | ||
return super unless pin && pin.tracer | ||
|
||
response = nil | ||
pin.tracer.trace(Datadog::Contrib::Redis::Ext::SPAN_COMMAND) do |span| | ||
span.service = pin.service | ||
span.span_type = Datadog::Contrib::Redis::Ext::TYPE | ||
commands = get_pipeline_commands(args) | ||
span.resource = commands.join("\n") | ||
span.set_metric Datadog::Contrib::Redis::Ext::METRIC_PIPELINE_LEN, commands.length | ||
Datadog::Contrib::Redis::Tags.set_common_tags(self, span) | ||
|
||
response = super | ||
end | ||
|
||
response | ||
end | ||
|
||
def datadog_pin | ||
@datadog_pin ||= begin | ||
pin = Datadog::Pin.new( | ||
datadog_configuration[:service_name], | ||
app: Ext::APP, | ||
app_type: Datadog::Ext::AppTypes::DB, | ||
tracer: -> { datadog_configuration[:tracer] } | ||
) | ||
pin.onto(self) | ||
end | ||
end | ||
|
||
private | ||
|
||
def get_command(args) | ||
if datadog_configuration[:command_args] | ||
Datadog::Contrib::Redis::Quantize.format_command_args(*args) | ||
else | ||
Datadog::Contrib::Redis::Quantize.get_verb(*args) | ||
end | ||
end | ||
|
||
def get_pipeline_commands(args) | ||
if datadog_configuration[:command_args] | ||
args[0].commands.map { |c| Datadog::Contrib::Redis::Quantize.format_command_args(c) } | ||
else | ||
args[0].commands.map { |c| Datadog::Contrib::Redis::Quantize.get_verb(c) } | ||
end | ||
end | ||
|
||
def datadog_configuration | ||
Datadog.configuration[:redis, options] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# typed: false | ||
require 'ddtrace/contrib/support/spec_helper' | ||
require 'ddtrace' | ||
|
||
RSpec.describe Datadog::Contrib::Redis::Patcher do | ||
describe '.patch' do | ||
it 'adds Instrumentation methods to ancestors of Redis class' do | ||
described_class.patch | ||
|
||
expect(Redis::Client.ancestors).to include(Datadog::Contrib::Redis::Instrumentation) | ||
expect(Redis::Client.ancestors).to include(Datadog::Contrib::Redis::Instrumentation) | ||
end | ||
end | ||
end |