Module: OneApm::Agent::Instrumentation::Sinatra

Includes:
TransactionBase
Defined in:
lib/one_apm/inst/framework/sinatra.rb,
lib/one_apm/inst/framework/sinatra/ignorer.rb,
lib/one_apm/inst/framework/sinatra/transaction_namer.rb

Overview

OneApm 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

Constant Summary

Constants included from TransactionBase

TransactionBase::OA_DEFAULT_OPTIONS, TransactionBase::OA_DO_NOT_TRACE_KEY, TransactionBase::OA_IGNORE_APDEX_KEY, TransactionBase::OA_IGNORE_ENDUSER_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TransactionBase

#perform_action_with_oneapm_trace

Class Method Details

.included(clazz) ⇒ Object



79
80
81
# File 'lib/one_apm/inst/framework/sinatra.rb', line 79

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

Instance Method Details

#dispatch_and_notice_errors_with_oneapmObject



159
160
161
162
163
164
165
166
# File 'lib/one_apm/inst/framework/sinatra.rb', line 159

def dispatch_and_notice_errors_with_oneapm
  dispatch_without_oneapm
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')
  OneApm::Manager.notice_error(env['sinatra.error']) if had_error
end

#dispatch_with_oneapmObject



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/one_apm/inst/framework/sinatra.rb', line 138

def dispatch_with_oneapm
  request_params = get_request_params
  filtered_params = OneApm::Support::ParameterFiltering.apply_filters(request.env, request_params || {})

  name = TransactionNamer.initial_transaction_name(request)
  perform_action_with_oneapm_trace(:category => :sinatra,
                                     :name => name,
                                     :params => filtered_params) do
    dispatch_and_notice_errors_with_oneapm
  end
end

#do_not_trace?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/one_apm/inst/framework/sinatra.rb', line 168

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

#get_request_paramsObject



150
151
152
153
154
155
156
157
# File 'lib/one_apm/inst/framework/sinatra.rb', line 150

def get_request_params
  begin
    @request.params
  rescue => e
    OneApm::Manager.logger.debug("Failed to get params from Rack request.", e)
    nil
  end
end

#ignore_apdex?Boolean

Overrides TransactionBase implementation

Returns:

  • (Boolean)


173
174
175
# File 'lib/one_apm/inst/framework/sinatra.rb', line 173

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

#ignore_enduser?Boolean

Overrides TransactionBase implementation

Returns:

  • (Boolean)


178
179
180
# File 'lib/one_apm/inst/framework/sinatra.rb', line 178

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

#oneapm_request_headers(_) ⇒ Object

Expected method for supporting TransactionBase



75
76
77
# File 'lib/one_apm/inst/framework/sinatra.rb', line 75

def oneapm_request_headers(_)
  request.env
end

#process_route_with_oneapm(*args, &block) ⇒ Object

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



108
109
110
111
112
113
114
115
116
# File 'lib/one_apm/inst/framework/sinatra.rb', line 108

def process_route_with_oneapm(*args, &block)
  begin
    env["oneapm.last_route"] = args[0]
  rescue => e
    OneApm::Manager.logger.debug("Failed determining last route in Sinatra", e)
  end

  process_route_without_oneapm(*args, &block)
end

#route_eval_with_oneapm(*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.



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/one_apm/inst/framework/sinatra.rb', line 124

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

  route_eval_without_oneapm(*args, &block)
end