Module: ScoutApm::AutoInstrument::Rails

Defined in:
lib/scout_apm/auto_instrument/rails.rb

Defined Under Namespace

Classes: Rewriter

Constant Summary collapse

CONTROLLER_FILE =

A general pattern to match Rails controller files:

/\/app\/controllers\/*\/.*_controller.rb$/.freeze
GEM_FILE =

Some gems (Devise) provide controllers that match CONTROLLER_FILE pattern. Try a simple match to see if it’s a Gemfile

/\/gems?\//.freeze

Class Method Summary collapse

Class Method Details

.controller_path?(path) ⇒ Boolean

Whether the given path is likely to be a Rails controller and not provided by a Gem.

Returns:

  • (Boolean)


16
17
18
# File 'lib/scout_apm/auto_instrument/rails.rb', line 16

def self.controller_path? path
  CONTROLLER_FILE.match(path) && !GEM_FILE.match(path)
end

.ignore?(path) ⇒ Boolean

Autoinstruments increases overhead when applied to many code expressions that perform little work. You can exclude files from autoinstruments via the ‘auto_instruments_ignore` option.

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
# File 'lib/scout_apm/auto_instrument/rails.rb', line 22

def self.ignore?(path)
  res = false
  ScoutApm::Agent.instance.context.config.value('auto_instruments_ignore').each do |ignored_file_name|
    if path.include?(ignored_file_name)
      res = true
      break
    end
  end
  res
end

.rewrite(path, code = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/scout_apm/auto_instrument/rails.rb', line 33

def self.rewrite(path, code = nil)
  code ||= File.read(path)

  ast = ::Parser::CurrentRuby.parse(code)

  # pp ast

  buffer = ::Parser::Source::Buffer.new(path)
  buffer.source = code

  rewriter = Rewriter.new

  # Rewrite the AST, returns a String with the new form.
  rewriter.rewrite(buffer, ast)
end