Method: NewRelic::Agent::Datastores.trace

Defined in:
lib/new_relic/agent/datastores.rb

.trace(klass, method_name, product, operation = method_name) ⇒ Object

Add Datastore tracing to a method. This properly generates the metrics for New Relic’s Datastore features. It does not capture the actual query content into Transaction Traces. Use wrap if you want to provide that functionality.

Parameters:

  • klass (Class)

    the class to instrument

  • method_name (String, Symbol)

    the name of instance method to instrument

  • product (String)

    name of your datastore for use in metric naming, e.g. “Redis”

  • operation (optional, String) (defaults to: method_name)

    the name of operation if different than the instrumented method name

[View source]

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/new_relic/agent/datastores.rb', line 35

def self.trace(klass, method_name, product, operation = method_name)
  NewRelic::Agent.record_api_supportability_metric(:trace)

  klass.class_eval do
    method_name_without_newrelic = "#{method_name}_without_newrelic"

    if NewRelic::Helper.instance_methods_include?(klass, method_name) &&
        !NewRelic::Helper.instance_methods_include?(klass, method_name_without_newrelic)

      visibility = NewRelic::Helper.instance_method_visibility(klass, method_name)

      alias_method(method_name_without_newrelic, method_name)

      define_method(method_name) do |*args, &blk|
        segment = NewRelic::Agent::Tracer.start_datastore_segment(
          product: product,
          operation: operation
        )
        begin
          send(method_name_without_newrelic, *args, &blk)
        ensure
          ::NewRelic::Agent::Transaction::Segment.finish(segment)
        end
      end

      send(visibility, method_name)
      send(visibility, method_name_without_newrelic)
    end
  end
end