Class: NewRelic::Agent::Instrumentation::MiddlewareProxy

Inherits:
Object
  • Object
show all
Includes:
MiddlewareTracing
Defined in:
lib/new_relic/agent/instrumentation/middleware_proxy.rb

Defined Under Namespace

Classes: Generator

Constant Summary collapse

ANONYMOUS_CLASS =
'AnonymousClass'.freeze
OBJECT_CLASS_NAME =
'Object'.freeze

Constants included from MiddlewareTracing

NewRelic::Agent::Instrumentation::MiddlewareTracing::CONTENT_LENGTH, NewRelic::Agent::Instrumentation::MiddlewareTracing::CONTENT_TYPE, NewRelic::Agent::Instrumentation::MiddlewareTracing::TXN_STARTED_KEY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MiddlewareTracing

#_nr_has_middleware_tracing, #build_transaction_options, #call, #capture_http_response_code, #capture_response_attributes, #capture_response_content_length, #capture_response_content_type, #events, #merge_first_middleware_options, #note_transaction_started

Constructor Details

#initialize(target, is_app = false) ⇒ MiddlewareProxy

Returns a new instance of MiddlewareProxy.



63
64
65
66
67
68
69
70
71
72
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 63

def initialize(target, is_app = false)
  @target = target
  @is_app = is_app
  @category = determine_category
  @target_class_name = determine_class_name
  @transaction_name = "#{determine_prefix}#{@target_class_name}/call"
  @transaction_options = {
    :transaction_name => @transaction_name
  }
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



61
62
63
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 61

def category
  @category
end

#targetObject (readonly)

Returns the value of attribute target.



61
62
63
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 61

def target
  @target
end

#transaction_optionsObject (readonly)

Returns the value of attribute transaction_options.



61
62
63
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 61

def transaction_options
  @transaction_options
end

Class Method Details

.for_class(target_class) ⇒ Object



42
43
44
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 42

def self.for_class(target_class)
  Generator.new(target_class)
end

.is_sinatra_app?(target) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 38

def self.is_sinatra_app?(target)
  defined?(::Sinatra::Base) && target.kind_of?(::Sinatra::Base)
end

.needs_wrapping?(target) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 46

def self.needs_wrapping?(target)
  (
    !target.respond_to?(:_nr_has_middleware_tracing) &&
    !is_sinatra_app?(target)
  )
end

.wrap(target, is_app = false) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 53

def self.wrap(target, is_app = false)
  if needs_wrapping?(target)
    self.new(target, is_app)
  else
    target
  end
end

Instance Method Details

#class_for_targetObject



100
101
102
103
104
105
106
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 100

def class_for_target
  if @target.is_a?(Class)
    @target
  else
    @target.class
  end
end

#determine_categoryObject



74
75
76
77
78
79
80
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 74

def determine_category
  if @is_app
    :rack
  else
    :middleware
  end
end

#determine_class_nameObject

In ‘normal’ usage, the target will be an application instance that responds to #call. With Rails, however, the target may be a subclass of Rails::Application that defines a method_missing that proxies #call to a singleton instance of the subclass. We need to ensure that we capture the correct name in both cases.



91
92
93
94
95
96
97
98
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 91

def determine_class_name
  clazz = class_for_target

  name = clazz.name
  name = clazz.superclass.name if name.nil? || name == ''
  name = ANONYMOUS_CLASS if name.nil? || name == OBJECT_CLASS_NAME
  name
end

#determine_prefixObject



82
83
84
# File 'lib/new_relic/agent/instrumentation/middleware_proxy.rb', line 82

def determine_prefix
  ControllerInstrumentation::TransactionNamer.prefix_for_category(nil, @category)
end