lib/ddtrace/contrib/dalli/instrumentation.rb in ddtrace-0.19.1 vs lib/ddtrace/contrib/dalli/instrumentation.rb in ddtrace-0.20.0

- old
+ new

@@ -1,33 +1,58 @@ +require 'ddtrace/ext/app_types' require 'ddtrace/ext/net' require 'ddtrace/contrib/dalli/quantize' module Datadog module Contrib module Dalli # Instruments every interaction with the memcached server module Instrumentation - module_function + def self.included(base) + if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0') + base.class_eval do + alias_method :request_without_datadog, :request + remove_method :request + include InstanceMethods + end + else + base.send(:prepend, InstanceMethods) + end + end - def patch! - ::Dalli::Server.class_eval do - alias_method :__request, :request + # Compatibility shim for Rubies not supporting `.prepend` + module InstanceMethodsCompatibility + def request(*args, &block) + request_without_datadog(*args, &block) + end + end - def request(op, *args) - pin = Datadog::Pin.get_from(::Dalli) + # InstanceMethods - implementing instrumentation + module InstanceMethods + include InstanceMethodsCompatibility unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.0.0') - pin.tracer.trace(Datadog::Contrib::Dalli::Ext::SPAN_COMMAND) do |span| - span.resource = op.to_s.upcase - span.service = pin.service - span.span_type = pin.app_type - span.set_tag(Datadog::Ext::NET::TARGET_HOST, hostname) - span.set_tag(Datadog::Ext::NET::TARGET_PORT, port) - cmd = Datadog::Contrib::Dalli::Quantize.format_command(op, args) - span.set_tag(Datadog::Contrib::Dalli::Ext::TAG_COMMAND, cmd) + def request(op, *args) + tracer.trace(Datadog::Contrib::Dalli::Ext::SPAN_COMMAND) do |span| + span.resource = op.to_s.upcase + span.service = datadog_configuration[:service_name] + span.span_type = Datadog::Ext::AppTypes::CACHE + span.set_tag(Datadog::Ext::NET::TARGET_HOST, hostname) + span.set_tag(Datadog::Ext::NET::TARGET_PORT, port) + cmd = Datadog::Contrib::Dalli::Quantize.format_command(op, args) + span.set_tag(Datadog::Contrib::Dalli::Ext::TAG_COMMAND, cmd) - __request(op, *args) - end + super end + end + + private + + def tracer + datadog_configuration[:tracer] + end + + def datadog_configuration + Datadog.configuration[:dalli] end end end end end