class NewRelic::Agent::Transaction::RequestAttributes

Constants

HTTP_ACCEPT_HEADER_KEY
ROOT_PATH

In practice we expect req to be a Rack::Request or ActionController::AbstractRequest (for older Rails versions). But anything that responds to path can be passed to perform_action_with_newrelic_trace.

We don't expect the path to include a query string, however older test helpers for rails construct the PATH_INFO enviroment variable improperly and we're generally being defensive.

Attributes

accept[R]
content_length[R]
content_type[R]
host[R]
port[R]
referer[R]
request_method[R]
request_path[R]
user_agent[R]

Public Class Methods

new(request) click to toggle source
# File lib/new_relic/agent/transaction/request_attributes.rb, line 15
def initialize request
  @request_path = path_from_request request
  @referer = referer_from_request request
  @accept = attribute_from_env request, HTTP_ACCEPT_HEADER_KEY
  @content_length = content_length_from_request request
  @content_type = attribute_from_request request, :content_type
  @host = attribute_from_request request, :host
  @port = port_from_request request
  @user_agent = attribute_from_request request, :user_agent
  @request_method = attribute_from_request request, :request_method
end

Public Instance Methods

assign_agent_attributes(txn) click to toggle source
# File lib/new_relic/agent/transaction/request_attributes.rb, line 27
def assign_agent_attributes txn
 default_destinations = AttributeFilter::DST_TRANSACTION_TRACER|
                        AttributeFilter::DST_TRANSACTION_EVENTS|
                        AttributeFilter::DST_ERROR_COLLECTOR

  if referer
    txn.add_agent_attribute :'request.headers.referer', referer, AttributeFilter::DST_ERROR_COLLECTOR
  end

  if accept
    txn.add_agent_attribute :'request.headers.accept', accept, default_destinations
  end

  if content_length
    txn.add_agent_attribute :'request.headers.contentLength', content_length, default_destinations
  end

  if content_type
    txn.add_agent_attribute :'request.headers.contentType', content_type, default_destinations
  end

  if host
    txn.add_agent_attribute :'request.headers.host', host, default_destinations
  end

  if user_agent
    txn.add_agent_attribute :'request.headers.userAgent', user_agent, default_destinations
  end

  if request_method
    txn.add_agent_attribute :'request.method', request_method, default_destinations
  end
end

Private Instance Methods

attribute_from_env(request, key) click to toggle source
# File lib/new_relic/agent/transaction/request_attributes.rb, line 106
def attribute_from_env request, key
  if env = attribute_from_request(request, :env)
    env[key]
  end
end
attribute_from_request(request, attribute_method) click to toggle source
# File lib/new_relic/agent/transaction/request_attributes.rb, line 100
def attribute_from_request request, attribute_method
  if request.respond_to? attribute_method
    request.send(attribute_method)
  end
end
content_length_from_request(request) click to toggle source
# File lib/new_relic/agent/transaction/request_attributes.rb, line 88
def content_length_from_request request
  if content_length = attribute_from_request(request, :content_length)
    content_length.to_i
  end
end
path_from_request(request) click to toggle source
# File lib/new_relic/agent/transaction/request_attributes.rb, line 82
def path_from_request request
  path = attribute_from_request(request, :path) || ''
  path = HTTPClients::URIUtil.strip_query_string(path)
  path.empty? ? ROOT_PATH : path
end
port_from_request(request) click to toggle source
# File lib/new_relic/agent/transaction/request_attributes.rb, line 94
def port_from_request request
  if port = attribute_from_request(request, :port)
    port.to_i
  end
end
referer_from_request(request) click to toggle source

Make a safe attempt to get the referer from a request object, generally successful when it's a Rack request.

# File lib/new_relic/agent/transaction/request_attributes.rb, line 66
def referer_from_request request
  if referer = attribute_from_request(request, :referer)
    HTTPClients::URIUtil.strip_query_string referer.to_s
  end
end