module NewRelic::Agent::Instrumentation::Sinatra

NewRelic instrumentation for Sinatra applications. Sinatra actions will appear in the UI similar to controller actions, and have breakdown charts and transaction traces.

The actions in the UI will correspond to the pattern expression used to match them, not directly to full URL's.

Public Class Methods

included(clazz) click to toggle source
# File lib/new_relic/agent/instrumentation/sinatra.rb, line 82
def self.included(clazz)
  clazz.extend(ClassMethods)
end

Public Instance Methods

dispatch_and_notice_errors_with_newrelic() click to toggle source
# File lib/new_relic/agent/instrumentation/sinatra.rb, line 162
def dispatch_and_notice_errors_with_newrelic
  dispatch_without_newrelic
ensure
  # Will only see an error raised if :show_exceptions is true, but
  # will always see them in the env hash if they occur
  had_error = env.has_key?('sinatra.error')
  ::NewRelic::Agent.notice_error(env['sinatra.error']) if had_error
end
dispatch_with_newrelic() click to toggle source
# File lib/new_relic/agent/instrumentation/sinatra.rb, line 141
def dispatch_with_newrelic #THREAD_LOCAL_ACCESS
  request_params = get_request_params
  filtered_params = ParameterFiltering::apply_filters(request.env, request_params || {})

  name = TransactionNamer.initial_transaction_name(request)
  perform_action_with_newrelic_trace(:category => :sinatra,
                                     :name => name,
                                     :params => filtered_params) do
    dispatch_and_notice_errors_with_newrelic
  end
end
do_not_trace?() click to toggle source
# File lib/new_relic/agent/instrumentation/sinatra.rb, line 171
def do_not_trace?
  Ignorer.should_ignore?(self, :routes)
end
get_request_params() click to toggle source
# File lib/new_relic/agent/instrumentation/sinatra.rb, line 153
def get_request_params
  begin
    @request.params
  rescue => e
    NewRelic::Agent.logger.debug("Failed to get params from Rack request.", e)
    nil
  end
end
ignore_apdex?() click to toggle source

Overrides ControllerInstrumentation implementation

# File lib/new_relic/agent/instrumentation/sinatra.rb, line 176
def ignore_apdex?
  Ignorer.should_ignore?(self, :apdex)
end
ignore_enduser?() click to toggle source

Overrides ControllerInstrumentation implementation

# File lib/new_relic/agent/instrumentation/sinatra.rb, line 181
def ignore_enduser?
  Ignorer.should_ignore?(self, :enduser)
end
newrelic_request_headers(_) click to toggle source

Expected method for supporting ControllerInstrumentation

# File lib/new_relic/agent/instrumentation/sinatra.rb, line 78
def newrelic_request_headers(_)
  request.env
end
process_route_with_newrelic(*args, &block) click to toggle source

Capture last route we've seen. Will set for transaction on route_eval

# File lib/new_relic/agent/instrumentation/sinatra.rb, line 111
def process_route_with_newrelic(*args, &block)
  begin
    env["newrelic.last_route"] = args[0]
  rescue => e
    ::NewRelic::Agent.logger.debug("Failed determining last route in Sinatra", e)
  end

  process_route_without_newrelic(*args, &block)
end
route_eval_with_newrelic(*args, &block) click to toggle source

If a transaction name is already set, this call will tromple over it. This is intentional, as typically passing to a separate route is like an entirely separate transaction, so we pick up the new name.

If we're ignored, this remains safe, since set_transaction_name care for the gating on the transaction's existence for us.

# File lib/new_relic/agent/instrumentation/sinatra.rb, line 127
def route_eval_with_newrelic(*args, &block)
  begin
    txn_name = TransactionNamer.transaction_name_for_route(env, request)
    unless txn_name.nil?
      ::NewRelic::Agent::Transaction.set_default_transaction_name(
        "#{self.class.name}/#{txn_name}", :sinatra)
    end
  rescue => e
    ::NewRelic::Agent.logger.debug("Failed during route_eval to set transaction name", e)
  end

  route_eval_without_newrelic(*args, &block)
end