Class: ScoutApm::FrameworkIntegrations::Rails3Or4

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/framework_integrations/rails_3_or_4.rb

Instance Method Summary collapse

Instance Method Details

#application_nameObject



23
24
25
26
27
28
29
30
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 23

def application_name
  if defined?(::Rails)
    ::Rails.application.class.to_s.
       sub(/::Application$/, '')
  end
rescue
  nil
end

#database_engineObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 36

def database_engine
  return @database_engine if @database_engine
  default = :postgres

  @database_engine = if defined?(ActiveRecord::Base)
    adapter = raw_database_adapter # can be nil

    case adapter.to_s
    when "postgres"   then :postgres
    when "postgresql" then :postgres
    when "postgis"    then :postgres
    when "sqlite3"    then :sqlite
    when "sqlite"     then :sqlite
    when "mysql"      then :mysql
    when "mysql2"     then :mysql
    else default
    end
  else
    # TODO: Figure out how to detect outside of Rails context. (sequel, ROM, etc)
    default
  end
end

#envObject



32
33
34
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 32

def env
  ::Rails.env
end

#human_nameObject



8
9
10
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 8

def human_name
  "Rails"
end

#nameObject



4
5
6
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 4

def name
  :rails3_or_4
end

#present?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 16

def present?
  defined?(::Rails) &&
    defined?(::Rails::VERSION) &&
      defined?(ActionController) &&
    Rails::VERSION::MAJOR >= 3
end

#raw_database_adapterObject

Note, this code intentionally avoids ‘.respond_to?` because of an infinite loop created by the Textacular gem (tested against 3.2.2 of that gem), which some customers have installed.

The loop was:

- Ask for database adapter
- Do .respond_to? on AR::Base to look for connection_config (which isn't present on some versions of rails)
- Textacular gem has a monkey-patch that queries the columns of the db
  This obtains a connection, and runs a query.
- Scout tries to run SQLSanitizer against the query, which needs the database adapter.
- Goes back to first step.

We avoid this issue by not calling .respond_to? here, and instead using the less optimal ‘rescue nil` approach



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 72

def raw_database_adapter
  adapter = ActiveRecord::Base.connection_config[:adapter].to_s rescue nil

  if adapter.nil?
    adapter = ActiveRecord::Base.configurations[env]["adapter"]
  end

  return adapter
rescue # this would throw an exception if ActiveRecord::Base is defined but no configuration exists.
  nil
end

#versionObject



12
13
14
# File 'lib/scout_apm/framework_integrations/rails_3_or_4.rb', line 12

def version
  Rails::VERSION::STRING
end