Module: NewRelic::Agent::Instrumentation::Sinatra

Includes:
ControllerInstrumentation
Defined in:
lib/new_relic/agent/instrumentation/sinatra.rb,
lib/new_relic/agent/instrumentation/sinatra/ignorer.rb,
lib/new_relic/agent/instrumentation/sinatra/transaction_namer.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods, Ignorer, TransactionNamer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ControllerInstrumentation

#perform_action_with_newrelic_trace, #recorded_metrics

Class Method Details

.included(clazz) ⇒ Object



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

def self.included(clazz)
  clazz.extend(ClassMethods)
end

Instance Method Details

#dispatch_and_notice_errors_with_newrelicObject



150
151
152
153
154
155
156
157
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 150

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_newrelicObject



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 136

def dispatch_with_newrelic
  if ignore_request?
    env['newrelic.ignored'] = true
    return dispatch_without_newrelic
  end

  name = TransactionNamer.initial_transaction_name(request)
  perform_action_with_newrelic_trace(:category => :sinatra,
                                     :name => name,
                                     :params => @request.params) do
    dispatch_and_notice_errors_with_newrelic
  end
end

#ignore_apdex?Boolean

Overrides ControllerInstrumentation implementation

Returns:

  • (Boolean)


164
165
166
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 164

def ignore_apdex?
  Ignorer.should_ignore?(self, :apdex)
end

#ignore_enduser?Boolean

Overrides ControllerInstrumentation implementation

Returns:

  • (Boolean)


169
170
171
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 169

def ignore_enduser?
  Ignorer.should_ignore?(self, :enduser)
end

#ignore_request?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 159

def ignore_request?
  Ignorer.should_ignore?(self, :routes)
end

#newrelic_request_headersObject

Expected method for supporting ControllerInstrumentation



78
79
80
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 78

def newrelic_request_headers
  request.env
end

#process_route_with_newrelic(*args, &block) ⇒ Object

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



109
110
111
112
113
114
115
116
117
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 109

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) ⇒ Object

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.



125
126
127
128
129
130
131
132
133
134
# File 'lib/new_relic/agent/instrumentation/sinatra.rb', line 125

def route_eval_with_newrelic(*args, &block)
  begin
    txn_name = TransactionNamer.transaction_name_for_route(env, request)
    ::NewRelic::Agent.set_transaction_name("#{self.class.name}/#{txn_name}") unless txn_name.nil?
  rescue => e
    ::NewRelic::Agent.logger.debug("Failed during route_eval to set transaction name", e)
  end

  route_eval_without_newrelic(*args, &block)
end