Class: ScoutApm::Environment

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/scout_apm/environment.rb

Constant Summary collapse

STDOUT_LOGGER =
begin
  l = Logger.new(STDOUT)
  l.level = ENV["SCOUT_LOG_LEVEL"] || Logger::INFO
  l
end
SERVER_INTEGRATIONS =

I’ve put Thin and Webrick last as they are often used in development and included in Gemfiles but less likely used in production.

[
  ScoutApm::ServerIntegrations::Passenger.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Unicorn.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Rainbows.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Puma.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Thin.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Webrick.new(STDOUT_LOGGER),
  ScoutApm::ServerIntegrations::Null.new(STDOUT_LOGGER), # must be last
]
BACKGROUND_JOB_INTEGRATIONS =
[
  ScoutApm::BackgroundJobIntegrations::Sidekiq.new,
  ScoutApm::BackgroundJobIntegrations::DelayedJob.new
]
FRAMEWORK_INTEGRATIONS =
[
  ScoutApm::FrameworkIntegrations::Rails2.new,
  ScoutApm::FrameworkIntegrations::Rails3Or4.new,
  ScoutApm::FrameworkIntegrations::Sinatra.new,
  ScoutApm::FrameworkIntegrations::Ruby.new, # Fallback if none match
]
PLATFORM_INTEGRATIONS =
[
  ScoutApm::PlatformIntegrations::Heroku.new,
  ScoutApm::PlatformIntegrations::CloudFoundry.new,
  ScoutApm::PlatformIntegrations::Server.new,
]
DEPLOY_INTEGRATIONS =
[
  ScoutApm::DeployIntegrations::Capistrano3.new(STDOUT_LOGGER),
  # ScoutApm::DeployIntegrations::Capistrano2.new(STDOUT_LOGGER),
]

Instance Method Summary collapse

Instance Method Details

#app_serverObject

App server’s name (symbol)



122
123
124
# File 'lib/scout_apm/environment.rb', line 122

def app_server
  app_server_integration.name
end

#app_server_integration(force = false) ⇒ Object

Returns the whole integration object This needs to be improved. Frequently, multiple app servers gem are present and which ever is checked first becomes the designated app server.

Next step: (1) list out all detected app servers (2) install hooks for those that need it (passenger, rainbows, unicorn).



116
117
118
119
# File 'lib/scout_apm/environment.rb', line 116

def app_server_integration(force=false)
  @app_server = nil if force
  @app_server ||= SERVER_INTEGRATIONS.detect{ |integration| integration.present? }
end

#application_nameObject



65
66
67
# File 'lib/scout_apm/environment.rb', line 65

def application_name
  Agent.instance.config.value("name") || framework_integration.application_name
end

#background_job_integrationObject



132
133
134
135
136
# File 'lib/scout_apm/environment.rb', line 132

def background_job_integration
  @background_job_integration ||= BACKGROUND_JOB_INTEGRATIONS.detect {|integration| integration.present?}
  #### Temporary Disable
  nil
end

#background_job_nameObject



138
139
140
# File 'lib/scout_apm/environment.rb', line 138

def background_job_name
  background_job_integration && background_job_integration.name
end

#database_engineObject



69
70
71
# File 'lib/scout_apm/environment.rb', line 69

def database_engine
  framework_integration.database_engine
end

#deploy_integrationObject



142
143
144
# File 'lib/scout_apm/environment.rb', line 142

def deploy_integration
  @deploy_integration ||= DEPLOY_INTEGRATIONS.detect{ |integration| integration.present? }
end

#deploy_integration?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/scout_apm/environment.rb', line 146

def deploy_integration?
  !@deploy_integration.nil?
end

#envObject



49
50
51
# File 'lib/scout_apm/environment.rb', line 49

def env
  @env ||= deploy_integration? ? deploy_integration.env : framework_integration.env
end

#forking?Boolean

If forking, don’t start worker thread in the master process. Since it’s started as a Thread, it won’t survive the fork.

Returns:

  • (Boolean)


128
129
130
# File 'lib/scout_apm/environment.rb', line 128

def forking?
  app_server_integration.forking? || (background_job_integration && background_job_integration.forking?)
end

#frameworkObject



53
54
55
# File 'lib/scout_apm/environment.rb', line 53

def framework
  framework_integration.name
end

#framework_integrationObject



57
58
59
# File 'lib/scout_apm/environment.rb', line 57

def framework_integration
  @framework ||= FRAMEWORK_INTEGRATIONS.detect{ |integration| integration.present? }
end

#framework_rootObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/scout_apm/environment.rb', line 91

def framework_root
  if override_root = Agent.instance.config.value("application_root", true)
    return override_root
  end
  if framework == :rails
    RAILS_ROOT.to_s
  elsif framework == :rails3_or_4
    Rails.root
  elsif framework == :sinatra
    Sinatra::Application.root || "."
  else
    '.'
  end
end

#hostnameObject



106
107
108
109
# File 'lib/scout_apm/environment.rb', line 106

def hostname
  config_hostname = Agent.instance.config.value("hostname", !Agent.instance.config.config_file_exists?)
  @hostname ||= config_hostname || platform_integration.hostname
end

#jruby?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/scout_apm/environment.rb', line 156

def jruby?
  defined?(JRuby)
end

#platform_integrationObject



61
62
63
# File 'lib/scout_apm/environment.rb', line 61

def platform_integration
  @platform ||= PLATFORM_INTEGRATIONS.detect{ |integration| integration.present? }
end

#processorsObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/scout_apm/environment.rb', line 73

def processors
  @processors ||= begin
                    proc_file = '/proc/cpuinfo'
                    processors = if !File.exist?(proc_file)
                                   1
                                 else
                                   lines = File.read("/proc/cpuinfo").lines.to_a
                                   lines.grep(/^processor\s*:/i).size
                                 end
                    [processors, 1].compact.max
                  end
end

#rootObject



86
87
88
89
# File 'lib/scout_apm/environment.rb', line 86

def root
  return deploy_integration.root if deploy_integration
  framework_root
end

#rubinius?Boolean

ruby checks

Returns:

  • (Boolean)


152
153
154
# File 'lib/scout_apm/environment.rb', line 152

def rubinius?
  RUBY_VERSION =~ /rubinius/i
end

#ruby_187?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/scout_apm/environment.rb', line 164

def ruby_187?
  defined?(RUBY_VERSION) && RUBY_VERSION.match(/^1\.8\.7/)
end

#ruby_19?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/scout_apm/environment.rb', line 160

def ruby_19?
  defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" && RUBY_VERSION.match(/^1\.9/)
end

#sinatra?Boolean

framework checks

Returns:

  • (Boolean)


170
171
172
# File 'lib/scout_apm/environment.rb', line 170

def sinatra?
  framework_integration.name == :sinatra
end