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 = 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)



124
125
126
# File 'lib/scout_apm/environment.rb', line 124

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).



118
119
120
121
# File 'lib/scout_apm/environment.rb', line 118

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



134
135
136
137
138
139
140
# File 'lib/scout_apm/environment.rb', line 134

def background_job_integration
  if Agent.instance.config.value("enable_background_jobs")
    @background_job_integration ||= BACKGROUND_JOB_INTEGRATIONS.detect {|integration| integration.present?}
  else
    nil
  end
end

#background_job_nameObject



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

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



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

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

#deploy_integration?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/scout_apm/environment.rb', line 150

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)


130
131
132
# File 'lib/scout_apm/environment.rb', line 130

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/scout_apm/environment.rb', line 94

def framework_root
  if override_root = Agent.instance.config.value("application_root")
    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



109
110
111
# File 'lib/scout_apm/environment.rb', line 109

def hostname
  @hostname ||= Agent.instance.config.value("hostname") || platform_integration.hostname
end

#jruby?Boolean

Returns:

  • (Boolean)


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

def jruby?
  defined?(JRuby)
end

#osObject

Returns a string representation of the OS (ex: darwin, linux)



177
178
179
180
181
182
183
184
185
186
# File 'lib/scout_apm/environment.rb', line 177

def os
  return @os if @os
  raw_os = RbConfig::CONFIG['target_os']
  match = raw_os.match(/([a-z]+)/)
  if match
    @os = match[1]
  else
    @os = raw_os
  end
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



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scout_apm/environment.rb', line 77

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

#raw_database_adapterObject



73
74
75
# File 'lib/scout_apm/environment.rb', line 73

def raw_database_adapter
  framework_integration.raw_database_adapter
end

#rootObject



90
91
92
# File 'lib/scout_apm/environment.rb', line 90

def root
  @root ||= deploy_integration? ? deploy_integration.root : framework_root
end

#rubinius?Boolean

ruby checks

Returns:

  • (Boolean)


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

def rubinius?
  RUBY_VERSION =~ /rubinius/i
end

#ruby_187?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/scout_apm/environment.rb', line 168

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

#ruby_19?Boolean

Returns:

  • (Boolean)


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

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

#ruby_2?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/scout_apm/environment.rb', line 172

def ruby_2?
  @ruby_2 ||= defined?(RUBY_VERSION) && RUBY_VERSION.match(/^2/)
end

#sinatra?Boolean

framework checks

Returns:

  • (Boolean)


190
191
192
# File 'lib/scout_apm/environment.rb', line 190

def sinatra?
  framework_integration.name == :sinatra
end