Module: ScoutApm::Instruments::SinatraInstruments

Defined in:
lib/scout_apm/instruments/sinatra.rb

Instance Method Summary collapse

Instance Method Details

#dispatch_with_scout_instruments!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scout_apm/instruments/sinatra.rb', line 32

def dispatch_with_scout_instruments!
  scout_controller_action = "Sinatra/#{scout_sinatra_controller_name(@request)}"

  req = ScoutApm::RequestManager.lookup
  req.annotate_request(:uri => @request.path_info)
  req.context.add_user(:ip => @request.ip)
  # req.set_headers(env) # TODO: Parse headers with name HTTP_*

  req.start_layer( ScoutApm::Layer.new("Controller", scout_controller_action) )
  begin
    dispatch_without_scout_instruments!
  rescue
    req.error!
    raise
  ensure
    req.stop_layer
  end
end

#scout_sinatra_controller_name(request) ⇒ Object

Iterates through the app’s routes, returning the matched route that the request should be grouped under for the metric name.

If not found, “unknown” is returned. This prevents a metric explosion.

Nice to have: substitute the param pattern (([^/?#]+)) w/the named key (the key param of the block).



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/scout_apm/instruments/sinatra.rb', line 57

def scout_sinatra_controller_name(request)
  name = 'unknown'
  verb = request.request_method if request && request.respond_to?(:request_method) 
  Array(self.class.routes[verb]).each do |pattern, keys, conditions, block|
    if pattern = process_route(pattern, keys, conditions) { pattern.source }
      name = pattern
    end
  end
  name.gsub!(%r{^[/^]*(.*?)[/\$\?]*$}, '\1')
  if verb
    name = [verb,name].join(' ')
  end
  name
end